Fix sieve restocking and sieving with >64 sieves

This commit is contained in:
thedarkcolour 2024-06-12 12:22:09 -07:00
parent 77e2e55067
commit a02e7c4bc6
No known key found for this signature in database
GPG Key ID: 6599A8E0516C8F38

View File

@ -22,11 +22,13 @@ import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
import net.minecraft.stats.Stats;
import net.minecraft.util.RandomSource; import net.minecraft.util.RandomSource;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
@ -104,7 +106,6 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S
this.logic.copyVisualData(fromIntegratedServer); this.logic.copyVisualData(fromIntegratedServer);
} }
@Override @Override
public InteractionResult use(Level level, Player player, InteractionHand hand) { public InteractionResult use(Level level, Player player, InteractionHand hand) {
ItemStack playerItem = player.getItemInHand(hand); ItemStack playerItem = player.getItemInHand(hand);
@ -136,9 +137,11 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S
if (this.logic.getContents().isEmpty()) { if (this.logic.getContents().isEmpty()) {
// If the input has any sieve drops, insert it into the mesh // If the input has any sieve drops, insert it into the mesh
if (this.logic.isValidInput(playerItem)) { 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);
if (canUseSimultaneously()) { if ((realPlayer || !EConfig.SERVER.nerfAutomatedSieves.get()) && canUseSimultaneously()) {
int range = EConfig.SERVER.simultaneousSieveUsageRange.get(); int range = EConfig.SERVER.simultaneousSieveUsageRange.get();
var cursor = this.worldPosition.mutable().move(-range, 0, -range); var cursor = this.worldPosition.mutable().move(-range, 0, -range);
@ -147,14 +150,20 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S
for (int x = -range; x <= range; x++) { for (int x = -range; x <= range; x++) {
for (int z = -range; z <= range; z++) { for (int z = -range; z <= range; z++) {
if (playerItem.isEmpty()) { if (playerItem.isEmpty()) {
break otherSieves; playerItem = restockSieveMaterial(player, hand, usedItem);
if (playerItem.isEmpty()) {
break otherSieves;
}
} }
if ((x | z) != 0) { if ((x | z) != 0) {
if (level.getBlockEntity(cursor) instanceof SieveBlockEntity other) { if (level.getBlockEntity(cursor) instanceof SieveBlockEntity other) {
if (other.logic.getContents().isEmpty()) { var otherLogic = other.logic;
if (this.logic.getMesh().getItem() == other.logic.getMesh().getItem()) {
playerItem = insertContents(player, hand, other.logic); if (otherLogic.getContents().isEmpty()) {
if (this.logic.getMesh().getItem() == otherLogic.getMesh().getItem()) {
insertContents(player, playerItem, otherLogic);
} }
} }
} }
@ -199,25 +208,35 @@ public abstract class AbstractSieveBlockEntity extends EBlockEntity implements S
return InteractionResult.sidedSuccess(isClientSide); return InteractionResult.sidedSuccess(isClientSide);
} }
// Fills the sieve (assumes contents is EMPTY) and returns the remaining item, putting it in the player's hand // search for another stack in inventory and restock held item
public static ItemStack insertContents(Player player, InteractionHand hand, SieveLogic logic) { private static ItemStack restockSieveMaterial(Player player, InteractionHand hand, Item usedItem) {
var consume = !player.getAbilities().instabuild; var inventory = player.getInventory();
var playerItem = player.getItemInHand(hand); var inventorySize = inventory.getContainerSize();
if (consume) { for (int i = 0; i < inventorySize; i++) {
if (playerItem.getCount() == 1) { var stack = inventory.getItem(i);
logic.startSifting(playerItem);
player.setItemInHand(hand, ItemStack.EMPTY); if (stack.is(usedItem)) {
playerItem = ItemStack.EMPTY; stack = stack.copy();
} else { player.setItemInHand(hand, stack);
logic.startSifting(singleCopy(playerItem)); inventory.setItem(i, ItemStack.EMPTY);
playerItem.shrink(1); 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 // Do not call on client side