From d606669a5849f40f5a1e1e1fe695b3d97ede09f3 Mon Sep 17 00:00:00 2001 From: thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> Date: Mon, 10 Jun 2024 11:34:27 -0700 Subject: [PATCH] Fix sieve restocking and sieving with >64 sieves --- build.gradle | 3 +- .../blockentity/AbstractSieveBlockEntity.java | 55 ++++++++++++------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/build.gradle b/build.gradle index b6ebcdb4..660c1664 100644 --- a/build.gradle +++ b/build.gradle @@ -153,7 +153,8 @@ dependencies { compileOnly fg.deobf('maven.modrinth:embeddium:0.2.12+mc1.20.1') // testing - implementation fg.deobf("curse.maven:allthecompressed-514045:4938351") + //implementation fg.deobf("curse.maven:allthecompressed-514045:4938351") + implementation fg.deobf("curse.maven:inventory-tweaks-refoxed-976858:5315928") //implementation fg.deobf("curse.maven:inventorysorter-240633:4655091") implementation fg.deobf("curse.maven:cyclic-239286:4994392") implementation fg.deobf("curse.maven:flib-661261:4724762") diff --git a/src/main/java/thedarkcolour/exdeorum/blockentity/AbstractSieveBlockEntity.java b/src/main/java/thedarkcolour/exdeorum/blockentity/AbstractSieveBlockEntity.java index 9743a598..f43d4942 100644 --- a/src/main/java/thedarkcolour/exdeorum/blockentity/AbstractSieveBlockEntity.java +++ b/src/main/java/thedarkcolour/exdeorum/blockentity/AbstractSieveBlockEntity.java @@ -22,11 +22,13 @@ import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; +import net.minecraft.stats.Stats; import net.minecraft.util.RandomSource; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntityType; @@ -102,8 +104,6 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S this.logic.setContents(buffer.readItem()); } - - public InteractionResult use(Level level, Player player, InteractionHand hand) { ItemStack playerItem = player.getItemInHand(hand); boolean isClientSide = level.isClientSide; @@ -134,8 +134,9 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S if (this.logic.getContents().isEmpty()) { // If the input has any sieve drops, insert it into the mesh if (this.logic.isValidInput(playerItem)) { - playerItem = insertContents(player, hand, this.logic); + var usedItem = playerItem.getItem(); var realPlayer = !(player instanceof FakePlayer); + insertContents(player, playerItem, this.logic); // prevent machines placing in multiple sieves if nerf is off to avoid confusion if ((realPlayer || !EConfig.SERVER.nerfAutomatedSieves.get()) && canUseSimultaneously()) { @@ -148,7 +149,11 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S for (int x = -range; x <= range; x++) { for (int z = -range; z <= range; z++) { if (playerItem.isEmpty()) { - break otherSieves; + playerItem = restockSieveMaterial(player, hand, usedItem); + + if (playerItem.isEmpty()) { + break otherSieves; + } } if ((x | z) != 0) { @@ -157,7 +162,7 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S if (otherLogic.getContents().isEmpty()) { if (this.logic.getMesh().getItem() == otherLogic.getMesh().getItem()) { - playerItem = insertContents(player, hand, otherLogic); + insertContents(player, playerItem, otherLogic); } } } @@ -202,25 +207,35 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S return InteractionResult.sidedSuccess(isClientSide); } - // Fills the sieve (assumes contents is EMPTY) and returns the remaining item, putting it in the player's hand - public static ItemStack insertContents(Player player, InteractionHand hand, SieveLogic logic) { - var consume = !player.getAbilities().instabuild; - var playerItem = player.getItemInHand(hand); + // search for another stack in inventory and restock held item + private static ItemStack restockSieveMaterial(Player player, InteractionHand hand, Item usedItem) { + var inventory = player.getInventory(); + var inventorySize = inventory.getContainerSize(); - if (consume) { - if (playerItem.getCount() == 1) { - logic.startSifting(playerItem); - player.setItemInHand(hand, ItemStack.EMPTY); - playerItem = ItemStack.EMPTY; - } else { - logic.startSifting(singleCopy(playerItem)); - playerItem.shrink(1); + for (int i = 0; i < inventorySize; i++) { + var stack = inventory.getItem(i); + + if (stack.is(usedItem)) { + stack = stack.copy(); + player.setItemInHand(hand, stack); + inventory.setItem(i, ItemStack.EMPTY); + return stack; } - } else { - logic.startSifting(singleCopy(playerItem)); } - return playerItem; + return ItemStack.EMPTY; + } + + // Fills the sieve (assumes contents is EMPTY) and returns the remaining item, putting it in the player's hand + public static void insertContents(Player player, ItemStack playerItem, SieveLogic logic) { + var consume = !player.getAbilities().instabuild; + + logic.startSifting(singleCopy(playerItem)); + player.awardStat(Stats.ITEM_USED.get(playerItem.getItem())); + + if (consume) { + playerItem.shrink(1); + } } // Do not call on client side