diff --git a/src/dev/kotlin/badasintended/slotlink/dev/SlotlinkDev.kt b/src/dev/kotlin/badasintended/slotlink/dev/SlotlinkDev.kt index 9df2b82..5f4e4bb 100644 --- a/src/dev/kotlin/badasintended/slotlink/dev/SlotlinkDev.kt +++ b/src/dev/kotlin/badasintended/slotlink/dev/SlotlinkDev.kt @@ -6,7 +6,7 @@ import net.minecraft.util.registry.Registry object SlotlinkDev { fun main() { - Registry.register(Registry.ITEM, modId("inventory_filler"), InventoryFillerItem) + Registry.register(Registry.ITEM, modId("storage_filler"), StorageFillerItem) } } \ No newline at end of file diff --git a/src/dev/kotlin/badasintended/slotlink/dev/InventoryFillerItem.kt b/src/dev/kotlin/badasintended/slotlink/dev/StorageFillerItem.kt similarity index 60% rename from src/dev/kotlin/badasintended/slotlink/dev/InventoryFillerItem.kt rename to src/dev/kotlin/badasintended/slotlink/dev/StorageFillerItem.kt index e356a7b..0dade5d 100644 --- a/src/dev/kotlin/badasintended/slotlink/dev/InventoryFillerItem.kt +++ b/src/dev/kotlin/badasintended/slotlink/dev/StorageFillerItem.kt @@ -1,18 +1,20 @@ package badasintended.slotlink.dev import badasintended.slotlink.item.ModItem -import net.minecraft.block.InventoryProvider -import net.minecraft.inventory.Inventory +import kotlin.random.Random +import kotlin.random.asJavaRandom +import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage +import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant +import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction import net.minecraft.item.Item import net.minecraft.item.ItemStack import net.minecraft.item.ItemUsageContext import net.minecraft.text.LiteralText import net.minecraft.util.ActionResult +import net.minecraft.util.math.Direction import net.minecraft.util.registry.Registry -import kotlin.random.Random -import kotlin.random.asJavaRandom -object InventoryFillerItem : Item(ModItem.SETTINGS) { +object StorageFillerItem : Item(ModItem.SETTINGS) { private val random = Random.asJavaRandom() @@ -20,6 +22,7 @@ object InventoryFillerItem : Item(ModItem.SETTINGS) { return true } + @Suppress("DEPRECATION", "UnstableApiUsage") override fun useOnBlock(context: ItemUsageContext): ActionResult { val world = context.world val player = context.player ?: return ActionResult.FAIL @@ -27,17 +30,14 @@ object InventoryFillerItem : Item(ModItem.SETTINGS) { if (world.isClient) return ActionResult.SUCCESS val pos = context.blockPos - val state = world.getBlockState(pos) - val block = state.block - - val inv = if (block is InventoryProvider) block.getInventory(state, world, pos) else world.getBlockEntity(pos) as? Inventory + val storage = ItemStorage.SIDED.find(world, pos, Direction.UP) - inv?.apply { - for (i in 0 until size()) { + if (storage != null) Transaction.openOuter().use { transaction -> + while (true) { val item = Registry.ITEM.getRandom(random) - inv.setStack(i, ItemStack(item, item.maxCount)) + if (storage.insert(ItemVariant.of(item), item.maxCount.toLong(), transaction) == 0L) break } - + transaction.commit() player.sendMessage(LiteralText("Filled (${pos.x}, ${pos.y}, ${pos.z})"), true) } diff --git a/src/dev/resources/assets/slotlink/models/item/inventory_filler.json b/src/dev/resources/assets/slotlink/models/item/storage_filler.json similarity index 100% rename from src/dev/resources/assets/slotlink/models/item/inventory_filler.json rename to src/dev/resources/assets/slotlink/models/item/storage_filler.json