Skip to content

Commit

Permalink
fix: Add an internal buffer to hold overflow items instead of droppin…
Browse files Browse the repository at this point in the history
…g them in world #314
  • Loading branch information
BlayTheNinth committed Jul 2, 2024
1 parent 41576c8 commit c78a059
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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<ItemStack> overflowBuffer = new ArrayList<>();

private final DelegateContainer container = new DelegateContainer(backingContainer) {
@Override
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -233,7 +235,7 @@ public void serverTick() {
Collection<ItemStack> 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()) {
Expand All @@ -248,14 +250,18 @@ 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);
}
}
}
progress = 0f;
currentStack = ItemStack.EMPTY;
}
}
} else if (!overflowBuffer.isEmpty()) {
if (addItemToOutput(overflowBuffer.get(0))) {
overflowBuffer.remove(0);
}
}
}

Expand Down Expand Up @@ -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")));
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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<ItemStack> overflowBuffer = new ArrayList<>();
private final DelegateContainer container = new DelegateContainer(backingContainer) {
@Override
public ItemStack removeItem(int slot, int count) {
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -207,6 +206,10 @@ public void serverTick() {
progress = 0f;
}
}
} else if (!overflowBuffer.isEmpty()) {
if (addItemToOutput(overflowBuffer.get(0))) {
overflowBuffer.remove(0);
}
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<ItemStack> overflowBuffer = new ArrayList<>();
private final DelegateContainer container = new DelegateContainer(backingContainer) {
@Override
public ItemStack removeItem(int slot, int count) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -230,14 +233,7 @@ public void serverTick() {
Collection<ItemStack> 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);
}
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c78a059

Please sign in to comment.