From 1360187f1662f351c761cf80b547b87908f60dbf Mon Sep 17 00:00:00 2001 From: drcrazy Date: Thu, 10 Sep 2020 16:19:39 +0300 Subject: [PATCH] Creative tank will void excessive fluid. Closes #2205 --- .../BaseFluidGeneratorBlockEntity.java | 21 +++++++------------ .../FluidReplicatorBlockEntity.java | 6 ++++-- .../IndustrialGrinderBlockEntity.java | 5 ++++- .../IndustrialSawmillBlockEntity.java | 5 ++++- .../fluid/TankUnitBaseBlockEntity.java | 15 +++++++++++-- .../java/techreborn/utils/FluidUtils.java | 19 +++++++++-------- 6 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/main/java/techreborn/blockentity/generator/BaseFluidGeneratorBlockEntity.java b/src/main/java/techreborn/blockentity/generator/BaseFluidGeneratorBlockEntity.java index 9e58ae4b7..eb19a1868 100644 --- a/src/main/java/techreborn/blockentity/generator/BaseFluidGeneratorBlockEntity.java +++ b/src/main/java/techreborn/blockentity/generator/BaseFluidGeneratorBlockEntity.java @@ -30,11 +30,11 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.util.math.Direction; import org.apache.commons.lang3.Validate; +import org.jetbrains.annotations.Nullable; import reborncore.api.IToolDrop; import reborncore.api.blockentity.InventoryProvider; import reborncore.common.blocks.BlockMachineBase; import reborncore.common.fluid.FluidValue; -import reborncore.common.fluid.container.FluidInstance; import reborncore.common.fluid.container.ItemFluidInfo; import reborncore.common.powerSystem.PowerAcceptorBlockEntity; import reborncore.common.util.RebornInventory; @@ -45,8 +45,6 @@ import techreborn.api.generator.FluidGeneratorRecipeList; import techreborn.api.generator.GeneratorRecipeHelper; import techreborn.utils.FluidUtils; -import org.jetbrains.annotations.Nullable; - public abstract class BaseFluidGeneratorBlockEntity extends PowerAcceptorBlockEntity implements IToolDrop, InventoryProvider { private final FluidGeneratorRecipeList recipes; @@ -74,9 +72,15 @@ public abstract class BaseFluidGeneratorBlockEntity extends PowerAcceptorBlockEn this.ticksSinceLastChange = 0; } + @SuppressWarnings("deprecation") @Override public void tick() { super.tick(); + + if (world == null) { + return; + } + ticksSinceLastChange++; if (world.isClient) { @@ -88,7 +92,7 @@ public abstract class BaseFluidGeneratorBlockEntity extends PowerAcceptorBlockEn ItemStack inputStack = inventory.getStack(0); if (!inputStack.isEmpty()) { if (FluidUtils.isContainerEmpty(inputStack) && !tank.getFluidAmount().isEmpty()) { - FluidUtils.fillContainers(tank, inventory, 0, 1, tank.getFluid()); + FluidUtils.fillContainers(tank, inventory, 0, 1); } else if (inputStack.getItem() instanceof ItemFluidInfo && getRecipes().getRecipeForFluid(((ItemFluidInfo) inputStack.getItem()).getFluid(inputStack)).isPresent()) { FluidUtils.drainContainers(tank, inventory, 0, 1); } @@ -141,15 +145,6 @@ public abstract class BaseFluidGeneratorBlockEntity extends PowerAcceptorBlockEn return false; } - protected boolean acceptFluid() { - if (!inventory.getStack(0).isEmpty()) { - FluidInstance stack = FluidUtils.getFluidStackInContainer(inventory.getStack(0)); - if (stack != null) - return recipes.getRecipeForFluid(stack.getFluid()).isPresent(); - } - return false; - } - public FluidGeneratorRecipeList getRecipes() { return recipes; } diff --git a/src/main/java/techreborn/blockentity/machine/multiblock/FluidReplicatorBlockEntity.java b/src/main/java/techreborn/blockentity/machine/multiblock/FluidReplicatorBlockEntity.java index 7bfe6995d..ac2d08691 100644 --- a/src/main/java/techreborn/blockentity/machine/multiblock/FluidReplicatorBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/multiblock/FluidReplicatorBlockEntity.java @@ -72,17 +72,19 @@ public class FluidReplicatorBlockEntity extends GenericMachineBlockEntity implem // TileGenericMachine @Override public void tick() { + if (world == null){ + return; + } ticksSinceLastChange++; // Check cells input slot 2 time per second if (!world.isClient && ticksSinceLastChange >= 10) { if (!inventory.getStack(1).isEmpty()) { - FluidUtils.fillContainers(tank, inventory, 1, 2, tank.getFluid()); + FluidUtils.fillContainers(tank, inventory, 1, 2); } ticksSinceLastChange = 0; } super.tick(); - } @Override diff --git a/src/main/java/techreborn/blockentity/machine/multiblock/IndustrialGrinderBlockEntity.java b/src/main/java/techreborn/blockentity/machine/multiblock/IndustrialGrinderBlockEntity.java index 70925547c..4fded940f 100644 --- a/src/main/java/techreborn/blockentity/machine/multiblock/IndustrialGrinderBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/multiblock/IndustrialGrinderBlockEntity.java @@ -76,12 +76,15 @@ public class IndustrialGrinderBlockEntity extends GenericMachineBlockEntity impl // TilePowerAcceptor @Override public void tick() { + if (world == null){ + return; + } ticksSinceLastChange++; // Check cells input slot 2 time per second if (!world.isClient && ticksSinceLastChange >= 10) { if (!inventory.getStack(1).isEmpty()) { FluidUtils.drainContainers(tank, inventory, 1, 6); - FluidUtils.fillContainers(tank, inventory, 1, 6, tank.getFluid()); + FluidUtils.fillContainers(tank, inventory, 1, 6); } ticksSinceLastChange = 0; } diff --git a/src/main/java/techreborn/blockentity/machine/multiblock/IndustrialSawmillBlockEntity.java b/src/main/java/techreborn/blockentity/machine/multiblock/IndustrialSawmillBlockEntity.java index 4b61c263d..5ca3a052d 100644 --- a/src/main/java/techreborn/blockentity/machine/multiblock/IndustrialSawmillBlockEntity.java +++ b/src/main/java/techreborn/blockentity/machine/multiblock/IndustrialSawmillBlockEntity.java @@ -76,12 +76,15 @@ public class IndustrialSawmillBlockEntity extends GenericMachineBlockEntity impl // TileGenericMachine @Override public void tick() { + if (world == null) { + return; + } ticksSinceLastChange++; // Check cells input slot 2 time per second if (!world.isClient && ticksSinceLastChange >= 10) { if (!inventory.getStack(1).isEmpty()) { FluidUtils.drainContainers(tank, inventory, 1, 5); - FluidUtils.fillContainers(tank, inventory, 1, 5, tank.getFluidInstance().getFluid()); + FluidUtils.fillContainers(tank, inventory, 1, 5); } ticksSinceLastChange = 0; } diff --git a/src/main/java/techreborn/blockentity/storage/fluid/TankUnitBaseBlockEntity.java b/src/main/java/techreborn/blockentity/storage/fluid/TankUnitBaseBlockEntity.java index d71eec98c..b37cce099 100644 --- a/src/main/java/techreborn/blockentity/storage/fluid/TankUnitBaseBlockEntity.java +++ b/src/main/java/techreborn/blockentity/storage/fluid/TankUnitBaseBlockEntity.java @@ -33,6 +33,7 @@ import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; import org.apache.commons.lang3.text.WordUtils; +import org.jetbrains.annotations.NotNull; import reborncore.api.IListInfoProvider; import reborncore.api.IToolDrop; import reborncore.api.blockentity.InventoryProvider; @@ -48,7 +49,6 @@ import techreborn.init.TRBlockEntities; import techreborn.init.TRContent; import techreborn.utils.FluidUtils; -import org.jetbrains.annotations.NotNull; import java.util.List; public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider { @@ -75,12 +75,16 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I public void tick() { super.tick(); + if (world == null){ + return; + } + if (world.isClient()) { return; } if (FluidUtils.drainContainers(tank, inventory, 0, 1) - || FluidUtils.fillContainers(tank, inventory, 0, 1, tank.getFluid())) { + || FluidUtils.fillContainers(tank, inventory, 0, 1)) { if (type == TRContent.TankUnit.CREATIVE) { if (!tank.isEmpty() && !tank.isFull()) { @@ -89,6 +93,11 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I } syncWithAll(); } + + // Void excessive fluid in creative tank (#2205) + if (type == TRContent.TankUnit.CREATIVE && tank.isFull()) { + FluidUtils.drainContainers(tank, inventory, 0, 1, true); + } } @Override @@ -129,7 +138,9 @@ public class TankUnitBaseBlockEntity extends MachineBaseBlockEntity implements I return this.inventory; } + // IListInfoProvider + @SuppressWarnings("deprecation") @Override public void addInfo(final List info, final boolean isReal, boolean hasData) { if (isReal || hasData) { diff --git a/src/main/java/techreborn/utils/FluidUtils.java b/src/main/java/techreborn/utils/FluidUtils.java index 8acb029b6..7b0847b53 100644 --- a/src/main/java/techreborn/utils/FluidUtils.java +++ b/src/main/java/techreborn/utils/FluidUtils.java @@ -56,6 +56,10 @@ public class FluidUtils { } public static boolean drainContainers(GenericFluidContainer target, Inventory inventory, int inputSlot, int outputSlot) { + return drainContainers(target, inventory, inputSlot, outputSlot, false); + } + + public static boolean drainContainers(GenericFluidContainer target, Inventory inventory, int inputSlot, int outputSlot, boolean voidFluid) { ItemStack inputStack = inventory.getStack(inputSlot); ItemStack outputStack = inventory.getStack(outputSlot); @@ -76,11 +80,12 @@ public class FluidUtils { } } - if (freeSpace.equalOrMoreThan(FluidValue.BUCKET)) { + if (freeSpace.equalOrMoreThan(FluidValue.BUCKET) || voidFluid) { inputStack.decrement(1); - targetFluidInstance.setFluid(itemFluidInfo.getFluid(inputStack)); - targetFluidInstance.addAmount(FluidValue.BUCKET); - + if (!voidFluid) { + targetFluidInstance.setFluid(itemFluidInfo.getFluid(inputStack)); + targetFluidInstance.addAmount(FluidValue.BUCKET); + } if (outputStack.isEmpty()) { inventory.setStack(outputSlot, itemFluidInfo.getEmpty()); } else { @@ -92,7 +97,7 @@ public class FluidUtils { return true; } - public static boolean fillContainers(GenericFluidContainer source, Inventory inventory, int inputSlot, int outputSlot, Fluid fluidToFill) { + public static boolean fillContainers(GenericFluidContainer source, Inventory inventory, int inputSlot, int outputSlot) { ItemStack inputStack = inventory.getStack(inputSlot); ItemStack outputStack = inventory.getStack(outputSlot); @@ -137,10 +142,6 @@ public class FluidUtils { return fluid == fluid1; } - public static FluidInstance getFluidStackInContainer(@NotNull ItemStack invStack) { - return null; - } - public static boolean isContainerEmpty(ItemStack inputStack) { if (inputStack.isEmpty()) return false;