From c78a059145d9d1bdb7bf6ea24dac735ae63b81be Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Tue, 2 Jul 2024 10:49:24 +0200 Subject: [PATCH] fix: Add an internal buffer to hold overflow items instead of dropping them in world #314 --- .../forge/compat/exdeorum/ExDeorumAddon.java | 2 +- .../entity/AbstractAutoSieveBlockEntity.java | 27 +++++++++++++---- .../entity/AutoCompressorBlockEntity.java | 30 +++++++++++++------ .../block/entity/AutoHammerBlockEntity.java | 28 +++++++++++------ 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/forge/src/main/java/net/blay09/mods/excompressum/forge/compat/exdeorum/ExDeorumAddon.java b/forge/src/main/java/net/blay09/mods/excompressum/forge/compat/exdeorum/ExDeorumAddon.java index e4bf8db1..9260fee7 100644 --- a/forge/src/main/java/net/blay09/mods/excompressum/forge/compat/exdeorum/ExDeorumAddon.java +++ b/forge/src/main/java/net/blay09/mods/excompressum/forge/compat/exdeorum/ExDeorumAddon.java @@ -322,7 +322,7 @@ private LootPoolSingletonContainer.Builder buildLootEntry(ItemStack outputIte return LootTableUtils.buildLootEntry(outputItem, -1f); } - /* TODO private LootPoolSingletonContainer.Builder buildLootEntry(ItemStackWithChance outputItem) { + /* private LootPoolSingletonContainer.Builder buildLootEntry(ItemStackWithChance outputItem) { return LootTableUtils.buildLootEntry(outputItem.getStack(), outputItem.getChance()); }*/ diff --git a/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AbstractAutoSieveBlockEntity.java b/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AbstractAutoSieveBlockEntity.java index 1d5c9488..a09a37f0 100644 --- a/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AbstractAutoSieveBlockEntity.java +++ b/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AbstractAutoSieveBlockEntity.java @@ -21,9 +21,7 @@ import net.blay09.mods.excompressum.utils.StupidUtils; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.NbtUtils; -import net.minecraft.nbt.Tag; +import net.minecraft.nbt.*; import net.minecraft.network.chat.Component; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; @@ -42,7 +40,10 @@ import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; import java.util.Collection; +import java.util.List; public abstract class AbstractAutoSieveBlockEntity extends AbstractBaseBlockEntity implements BalmMenuProvider, BalmContainerProvider { @@ -100,6 +101,7 @@ public int getCount() { private final SubContainer inputSlots = new SubContainer(backingContainer, 0, 1); private final SubContainer outputSlots = new SubContainer(backingContainer, 1, 21); private final SubContainer meshSlots = new SubContainer(backingContainer, 21, 22); + private final List overflowBuffer = new ArrayList<>(); private final DelegateContainer container = new DelegateContainer(backingContainer) { @Override @@ -195,7 +197,7 @@ public void serverTick() { } int effectiveEnergy = getEffectiveEnergy(); - if (!isDisabledByRedstone() && getEnergyStored() >= effectiveEnergy) { + if (!isDisabledByRedstone() && overflowBuffer.isEmpty() && getEnergyStored() >= effectiveEnergy) { if (currentStack.isEmpty()) { ItemStack inputStack = inputSlots.getItem(0); SieveMeshRegistryEntry sieveMesh = getSieveMesh(); @@ -233,7 +235,7 @@ public void serverTick() { Collection rewards = rollSieveRewards(level, currentStack, sieveMesh, getEffectiveLuck(), level.random); for (ItemStack itemStack : rewards) { if (!addItemToOutput(itemStack)) { - level.addFreshEntity(new ItemEntity(level, worldPosition.getX() + 0.5, worldPosition.getY() + 1.5, worldPosition.getZ() + 0.5, itemStack)); + overflowBuffer.add(itemStack); } } if (ExNihilo.getInstance().doMeshesHaveDurability()) { @@ -248,7 +250,7 @@ public void serverTick() { } } else { if (!addItemToOutput(currentStack)) { - level.addFreshEntity(new ItemEntity(level, worldPosition.getX() + 0.5, worldPosition.getY() + 1.5, worldPosition.getZ() + 0.5, currentStack)); + overflowBuffer.add(currentStack); } } } @@ -256,6 +258,10 @@ public void serverTick() { currentStack = ItemStack.EMPTY; } } + } else if (!overflowBuffer.isEmpty()) { + if (addItemToOutput(overflowBuffer.get(0))) { + overflowBuffer.remove(0); + } } } @@ -328,6 +334,10 @@ public void load(CompoundTag tag) { particleCount = tag.getInt("ParticleCount"); backingContainer.deserialize(tag.getCompound("ItemHandler")); isDisabledByRedstone = tag.getBoolean("IsDisabledByRedstone"); + overflowBuffer.clear(); + for (final var overflowItem : tag.getList("OverflowBuffer", Tag.TAG_COMPOUND)) { + overflowBuffer.add(ItemStack.of(((CompoundTag) overflowItem))); + } if (tag.contains("MeshStack", Tag.TAG_COMPOUND)) { meshSlots.setItem(0, ItemStack.of(tag.getCompound("MeshStack"))); @@ -350,6 +360,11 @@ public void saveAdditional(CompoundTag tag) { tag.putInt("ParticleCount", particleCount); tag.put("ItemHandler", backingContainer.serialize()); tag.putBoolean("IsDisabledByRedstone", isDisabledByRedstone()); + final var overflowList = new ListTag(); + for (ItemStack itemStack : overflowBuffer) { + overflowList.add(itemStack.save(new CompoundTag())); + } + tag.put("OverflowBuffer", overflowList); } @Override diff --git a/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AutoCompressorBlockEntity.java b/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AutoCompressorBlockEntity.java index e14ccdb0..a77d2ccc 100644 --- a/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AutoCompressorBlockEntity.java +++ b/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AutoCompressorBlockEntity.java @@ -17,6 +17,8 @@ import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.Tag; import net.minecraft.network.chat.Component; import net.minecraft.world.Container; import net.minecraft.world.entity.item.ItemEntity; @@ -30,6 +32,9 @@ import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import java.util.ArrayList; +import java.util.List; + public class AutoCompressorBlockEntity extends AbstractBaseBlockEntity implements BalmMenuProvider, BalmEnergyStorageProvider, BalmContainerProvider { private final EnergyStorage energyStorage = new EnergyStorage(32000) { @@ -52,6 +57,7 @@ public boolean canPlaceItem(int slot, ItemStack itemStack) { }; private final SubContainer inputSlots = new SubContainer(backingContainer, 0, 12); private final SubContainer outputSlots = new SubContainer(backingContainer, 12, 24); + private final List overflowBuffer = new ArrayList<>(); private final DelegateContainer container = new DelegateContainer(backingContainer) { @Override public ItemStack removeItem(int slot, int count) { @@ -127,7 +133,7 @@ public static void serverTick(Level level, BlockPos pos, BlockState state, AutoC public void serverTick() { int effectiveEnergy = getEffectiveEnergy(); - if (!isDisabledByRedstone() && energyStorage.getEnergy() > effectiveEnergy) { + if (!isDisabledByRedstone() && overflowBuffer.isEmpty() && energyStorage.getEnergy() > effectiveEnergy) { if (currentRecipe == null) { inputItems.clear(); for (int i = 0; i < inputSlots.getContainerSize(); i++) { @@ -191,14 +197,7 @@ public void serverTick() { if (compressedRecipe != null) { ItemStack resultStack = compressedRecipe.getResultStack().copy(); if (!addItemToOutput(resultStack)) { - ItemEntity entityItem = new ItemEntity(level, - worldPosition.getX() + 0.5, - worldPosition.getY() + 1.5, - worldPosition.getZ() + 0.5, - resultStack); - double motion = 0.05; - entityItem.setDeltaMovement(level.random.nextGaussian() * motion, 0.2, level.random.nextGaussian() * motion); - level.addFreshEntity(entityItem); + overflowBuffer.add(resultStack); } } } @@ -207,6 +206,10 @@ public void serverTick() { progress = 0f; } } + } else if (!overflowBuffer.isEmpty()) { + if (addItemToOutput(overflowBuffer.get(0))) { + overflowBuffer.remove(0); + } } } @@ -258,6 +261,10 @@ public void load(CompoundTag tagCompound) { progress = tagCompound.getFloat("Progress"); backingContainer.deserialize(tagCompound.getCompound("ItemHandler")); energyStorage.deserialize(tagCompound.get("EnergyStorage")); + overflowBuffer.clear(); + for (final var overflowItem : tagCompound.getList("OverflowBuffer", Tag.TAG_COMPOUND)) { + overflowBuffer.add(ItemStack.of(((CompoundTag) overflowItem))); + } } @Override @@ -270,6 +277,11 @@ public void saveAdditional(CompoundTag tag) { tag.putFloat("Progress", progress); tag.put("ItemHandler", backingContainer.serialize()); tag.put("EnergyStorage", energyStorage.serialize()); + final var overflowList = new ListTag(); + for (ItemStack itemStack : overflowBuffer) { + overflowList.add(itemStack.save(new CompoundTag())); + } + tag.put("OverflowBuffer", overflowList); } @Override diff --git a/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AutoHammerBlockEntity.java b/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AutoHammerBlockEntity.java index 64b66220..d351603f 100644 --- a/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AutoHammerBlockEntity.java +++ b/shared/src/main/java/net/blay09/mods/excompressum/block/entity/AutoHammerBlockEntity.java @@ -44,8 +44,10 @@ import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.List; public class AutoHammerBlockEntity extends AbstractBaseBlockEntity implements BalmMenuProvider, BalmContainerProvider, BalmEnergyStorageProvider { @@ -111,6 +113,7 @@ public void slotChanged(int slot) { private final SubContainer inputSlots = new SubContainer(backingContainer, 0, 1); private final SubContainer outputSlots = new SubContainer(backingContainer, 1, 21); private final SubContainer hammerSlots = new SubContainer(backingContainer, 21, 23); + private final List overflowBuffer = new ArrayList<>(); private final DelegateContainer container = new DelegateContainer(backingContainer) { @Override public ItemStack removeItem(int slot, int count) { @@ -186,7 +189,7 @@ public void serverTick() { } int effectiveEnergy = getEffectiveEnergy(); - if (!isDisabledByRedstone() && getEnergyStored() >= effectiveEnergy) { + if (!isDisabledByRedstone() && overflowBuffer.isEmpty() && getEnergyStored() >= effectiveEnergy) { if (currentStack.isEmpty() && cooldown <= 0) { ItemStack inputStack = inputSlots.getItem(0); if (!inputStack.isEmpty() && isRegistered(inputStack)) { @@ -230,14 +233,7 @@ public void serverTick() { Collection rewards = rollHammerRewards(currentStack, getEffectiveTool(), level.random); for (ItemStack itemStack : rewards) { if (!addItemToOutput(itemStack)) { - ItemEntity entityItem = new ItemEntity(level, - worldPosition.getX() + 0.5, - worldPosition.getY() + 1.5, - worldPosition.getZ() + 0.5, - itemStack); - double motion = 0.05; - entityItem.setDeltaMovement(level.random.nextGaussian() * motion, 0.2, level.random.nextGaussian() * motion); - level.addFreshEntity(entityItem); + overflowBuffer.add(itemStack); } } } @@ -248,6 +244,10 @@ public void serverTick() { currentStack = ItemStack.EMPTY; } } + } else if (!overflowBuffer.isEmpty()) { + if (addItemToOutput(overflowBuffer.get(0))) { + overflowBuffer.remove(0); + } } // Sync to clients @@ -357,6 +357,10 @@ public void load(CompoundTag tag) { if (tag.contains("SecondHammer", Tag.TAG_COMPOUND)) { hammerSlots.setItem(1, ItemStack.of(tag.getCompound("SecondHammer"))); } + overflowBuffer.clear(); + for (final var overflowItem : tag.getList("OverflowBuffer", Tag.TAG_COMPOUND)) { + overflowBuffer.add(ItemStack.of(((CompoundTag) overflowItem))); + } } @Override @@ -374,6 +378,12 @@ public void saveAdditional(CompoundTag tag) { if (!finishedStack.isEmpty()) { tag.put("FinishedStack", finishedStack.save(new CompoundTag())); } + + final var overflowList = new ListTag(); + for (ItemStack itemStack : overflowBuffer) { + overflowList.add(itemStack.save(new CompoundTag())); + } + tag.put("OverflowBuffer", overflowList); } @Override