Skip to content

Commit

Permalink
update filler to use fabric transfer api
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Sep 14, 2021
1 parent 91c7299 commit c414d6e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/dev/kotlin/badasintended/slotlink/dev/SlotlinkDev.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
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()

override fun hasGlint(stack: ItemStack?): Boolean {
return true
}

@Suppress("DEPRECATION", "UnstableApiUsage")
override fun useOnBlock(context: ItemUsageContext): ActionResult {
val world = context.world
val player = context.player ?: return ActionResult.FAIL

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)
}

Expand Down

0 comments on commit c414d6e

Please sign in to comment.