From c8e04537a04b09d0b279c7d4f6fa9d82d1761c14 Mon Sep 17 00:00:00 2001 From: drcrazy Date: Sun, 7 Aug 2022 16:15:08 +0300 Subject: [PATCH] Do not store player heads in storages. Closes #2888 --- .../blockentity/MachineBaseBlockEntity.java | 25 +++---- .../item/StorageUnitBaseBlockEntity.java | 67 ++++++++++--------- .../blocks/storage/item/StorageUnitBlock.java | 3 +- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/RebornCore/src/main/java/reborncore/common/blockentity/MachineBaseBlockEntity.java b/RebornCore/src/main/java/reborncore/common/blockentity/MachineBaseBlockEntity.java index 621e2d228..40ca017d7 100644 --- a/RebornCore/src/main/java/reborncore/common/blockentity/MachineBaseBlockEntity.java +++ b/RebornCore/src/main/java/reborncore/common/blockentity/MachineBaseBlockEntity.java @@ -294,19 +294,6 @@ public class MachineBaseBlockEntity extends BlockEntity implements BlockEntityTi tagCompound.put("redstoneConfig", redstoneConfiguration.write()); } - private boolean isItemValidForSlot(int index, ItemStack stack) { - if (slotConfiguration == null) { - return false; - } - SlotConfiguration.SlotConfigHolder slotConfigHolder = slotConfiguration.getSlotDetails(index); - if (slotConfigHolder.filter() && getOptionalCrafter().isPresent()) { - RecipeCrafter crafter = getOptionalCrafter().get(); - if (!crafter.isStackValidInput(stack)) { - return false; - } - } - return true; - } // Inventory end @Override @@ -473,7 +460,17 @@ public class MachineBaseBlockEntity extends BlockEntity implements BlockEntityTi @Override public boolean isValid(int slot, ItemStack stack) { - return isItemValidForSlot(slot, stack); + if (slotConfiguration == null) { + return false; + } + SlotConfiguration.SlotConfigHolder slotConfigHolder = slotConfiguration.getSlotDetails(slot); + if (slotConfigHolder.filter() && getOptionalCrafter().isPresent()) { + RecipeCrafter crafter = getOptionalCrafter().get(); + if (!crafter.isStackValidInput(stack)) { + return false; + } + } + return true; } @Override diff --git a/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java b/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java index 6bc8a15ac..284452a3a 100644 --- a/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java +++ b/src/main/java/techreborn/blockentity/storage/item/StorageUnitBaseBlockEntity.java @@ -32,6 +32,7 @@ import net.fabricmc.fabric.api.transfer.v1.storage.base.CombinedStorage; import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.item.SkullItem; import net.minecraft.nbt.NbtCompound; import net.minecraft.text.Text; import net.minecraft.util.Formatting; @@ -70,6 +71,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement private ItemStack storeItemStack; // Fabric transfer API support for the internal stack (one per direction); + @SuppressWarnings("UnstableApiUsage") private final SingleStackStorage[] internalStoreStorage = new SingleStackStorage[6]; private TRContent.StorageUnit type; @@ -183,10 +185,12 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement public ItemStack processInput(ItemStack inputStack) { - boolean isSameStack = canAcceptStack(inputStack); + if (!isValid(INPUT_SLOT, inputStack)){ + return inputStack; + } - if (storeItemStack == ItemStack.EMPTY && (isSameStack || (getCurrentCapacity() == 0 && !isLocked()))) { - // Check if storage is empty, NOT including the output slot + if (getDisplayedStack().isEmpty()) { + // Check if storage is empty, including the output slot and locked stack storeItemStack = inputStack.copy(); if (inputStack.getCount() <= maxCapacity) { @@ -196,16 +200,22 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement storeItemStack.setCount(maxCapacity); inputStack.decrement(maxCapacity); } - } else if (isSameStack) { + } else { // Not empty but same type // Amount of items that can be added before reaching capacity int reminder = maxCapacity - getCurrentCapacity(); - if (inputStack.getCount() <= reminder) { // Add full stack - addStoredItemCount(inputStack.getCount()); + if (storeItemStack == ItemStack.EMPTY){ + // copy input stack into stored if everything is in OUTPUT_SLOT + storeItemStack = inputStack.copy(); + } + else { + addStoredItemCount(inputStack.getCount()); + } + inputStack = ItemStack.EMPTY; } else { // Add only what is needed to reach max capacity @@ -218,21 +228,6 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement return inputStack; } - public boolean canAcceptStack(ItemStack inputStack) { - if (inputStack == ItemStack.EMPTY){ - return false; - } - if (isLocked()) { - return ItemUtils.isItemEqual(lockedItemStack, inputStack, true, true); - } - - if (isEmpty()){ - return true; - } - - return ItemUtils.isItemEqual(getStoredStack(), inputStack, true, true); - } - // Creative function private void fillToCapacity() { storeItemStack = getStoredStack(); @@ -288,7 +283,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement @Override public boolean canInsert(int index, ItemStack stack, @Nullable Direction direction) { - return super.canInsert(index, stack, direction) && (this.isEmpty() && !isLocked() || canAcceptStack(stack)); + return super.canInsert(index, stack, direction) && isValid(INPUT_SLOT, stack); } @Override @@ -383,16 +378,26 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement } @Override - public boolean isValid(int slot, ItemStack stack) { - if (slot == INPUT_SLOT && isLocked()) { - return ItemUtils.isItemEqual(lockedItemStack, stack, true, true); - } - - - if (slot == INPUT_SLOT && !(isEmpty() || canAcceptStack(stack))) { + public boolean isValid(int slot, ItemStack inputStack) { + if (slot != INPUT_SLOT) { return false; } - return super.isValid(slot, stack); + if (inputStack == ItemStack.EMPTY){ + return false; + } + // Do not allow player heads into storage due to lag. Fix #2888 + if (inputStack.getItem() instanceof SkullItem){ + return false; + } + if (isLocked()) { + return ItemUtils.isItemEqual(lockedItemStack, inputStack, true, true); + } + + if (isEmpty()){ + return true; + } + + return ItemUtils.isItemEqual(getStoredStack(), inputStack, true, true); } @Override @@ -504,6 +509,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement storeItemStack = ItemStack.fromNbt(tag); } + @SuppressWarnings("UnstableApiUsage") private Storage getInternalStoreStorage(Direction side) { Objects.requireNonNull(side); if (internalStoreStorage[side.getId()] == null) { @@ -550,6 +556,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement return internalStoreStorage[side.getId()]; } + @SuppressWarnings("UnstableApiUsage") public Storage getExposedStorage(Direction side) { return new CombinedStorage<>(List.of( getInternalStoreStorage(side), diff --git a/src/main/java/techreborn/blocks/storage/item/StorageUnitBlock.java b/src/main/java/techreborn/blocks/storage/item/StorageUnitBlock.java index af804b8b2..eca137f88 100644 --- a/src/main/java/techreborn/blocks/storage/item/StorageUnitBlock.java +++ b/src/main/java/techreborn/blocks/storage/item/StorageUnitBlock.java @@ -74,7 +74,7 @@ public class StorageUnitBlock extends BlockMachineBase { } ItemStack stackInHand = playerIn.getStackInHand(Hand.MAIN_HAND); - if (!storageEntity.canAcceptStack(stackInHand)) { + if (!storageEntity.isValid(StorageUnitBaseBlockEntity.INPUT_SLOT, stackInHand)) { return super.onUse(state, worldIn, pos, playerIn, hand, hitResult); } @@ -94,6 +94,7 @@ public class StorageUnitBlock extends BlockMachineBase { return ActionResult.SUCCESS; } + @SuppressWarnings("deprecation") @Override public void onBlockBreakStart(BlockState state, World world, BlockPos pos, PlayerEntity player) { super.onBlockBreakStart(state, world, pos, player);