From 2b8884da3e62fcf1b418a4710efc5bbb9caaee3a Mon Sep 17 00:00:00 2001 From: Flanks255 <32142731+Flanks255@users.noreply.github.com> Date: Thu, 7 Nov 2024 15:20:30 -0600 Subject: [PATCH] Fluid Component safety. --- .../mod/components/ActuallyComponents.java | 29 +++++++++++++++---- .../mod/tile/TileEntityCanolaPress.java | 2 +- .../mod/tile/TileEntityCoffeeMachine.java | 2 +- .../mod/tile/TileEntityFermentingBarrel.java | 4 +-- .../mod/tile/TileEntityFluidCollector.java | 2 +- .../mod/tile/TileEntityOilGenerator.java | 2 +- 6 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/components/ActuallyComponents.java b/src/main/java/de/ellpeck/actuallyadditions/mod/components/ActuallyComponents.java index 0ec625c40..d4ba55e15 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/components/ActuallyComponents.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/components/ActuallyComponents.java @@ -161,24 +161,43 @@ public class ActuallyComponents { .build()); - public record FluidContents(FluidStack inner) { - public static final Codec CODEC = FluidStack.OPTIONAL_CODEC.xmap(FluidContents::new, FluidContents::inner); - public static final StreamCodec STREAM_CODEC = FluidStack.OPTIONAL_STREAM_CODEC.map(FluidContents::new, FluidContents::inner); + public static class FluidContents { + private final FluidStack inner; + public static final Codec CODEC = FluidStack.OPTIONAL_CODEC.xmap(FluidContents::of, FluidContents::inner); + public static final StreamCodec STREAM_CODEC = FluidStack.OPTIONAL_STREAM_CODEC.map(FluidContents::of, FluidContents::inner); - public static final FluidContents EMPTY = new FluidContents(FluidStack.EMPTY); + public static final FluidContents EMPTY = FluidContents.of(FluidStack.EMPTY); public static FluidContents of(FluidStack stack) { return new FluidContents(stack); } + private FluidStack inner() { + return inner; + } + + private FluidContents(FluidStack inner) { + this.inner = inner; + } + + public FluidStack get() { + return inner.copy(); + } + @Override - public boolean equals(Object obj) { + public boolean equals(Object obj) { if (obj instanceof FluidStack other) { return FluidStack.matches(inner, other); } return false; } + + @Override + public int hashCode() { + return inner.hashCode(); } + } + /* * This is a supplier for an attachment type that can be used to attach an energy storage to an item. * Implementation is based on EnderIO's https://github.com/Team-EnderIO/EnderIO/blob/e1f022df745131ed5fea718bd860880a5785d4c7/src/core/java/com/enderio/core/common/attachment/AttachmentUtil.java#L47-L60 diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java index f6926763d..e3087eeec 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCanolaPress.java @@ -194,7 +194,7 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements Me super.applyImplicitComponents(componentInput); storage.setEnergyStored(componentInput.getOrDefault(ActuallyComponents.ENERGY_STORAGE, 0)); - tank.setFluid(componentInput.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).inner()); + tank.setFluid(componentInput.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).get()); } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java index b5d93c27b..3040e40ee 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoffeeMachine.java @@ -264,7 +264,7 @@ public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements super.applyImplicitComponents(componentInput); storage.setEnergyStored(componentInput.getOrDefault(ActuallyComponents.ENERGY_STORAGE, 0)); - tank.setFluid(componentInput.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).inner()); + tank.setFluid(componentInput.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).get()); this.coffeeCacheAmount = componentInput.getOrDefault(ActuallyComponents.MISC_INT, 0); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFermentingBarrel.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFermentingBarrel.java index b49a8a06d..7b0976164 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFermentingBarrel.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFermentingBarrel.java @@ -125,8 +125,8 @@ public class TileEntityFermentingBarrel extends TileEntityBase implements IShari protected void applyImplicitComponents(DataComponentInput componentInput) { super.applyImplicitComponents(componentInput); - tanks.inputTank.setFluid(componentInput.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).inner()); - tanks.outputTank.setFluid(componentInput.getOrDefault(ActuallyComponents.FLUID_B, ActuallyComponents.FluidContents.EMPTY).inner()); + tanks.inputTank.setFluid(componentInput.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).get()); + tanks.outputTank.setFluid(componentInput.getOrDefault(ActuallyComponents.FLUID_B, ActuallyComponents.FluidContents.EMPTY).get()); } @Override diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java index 26ea8227b..1d0b1acf7 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityFluidCollector.java @@ -235,7 +235,7 @@ public class TileEntityFluidCollector extends TileEntityBase implements ISharing protected void applyImplicitComponents(@Nonnull DataComponentInput input) { super.applyImplicitComponents(input); - tank.setFluid(input.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).inner()); + tank.setFluid(input.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).get()); this.isPulseMode = input.getOrDefault(ActuallyComponents.PULSE_MODE, false); } diff --git a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java index 4c620c3b2..a81778bdd 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java +++ b/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityOilGenerator.java @@ -237,7 +237,7 @@ public class TileEntityOilGenerator extends TileEntityBase implements ISharingEn super.applyImplicitComponents(input); storage.setEnergyStored(input.getOrDefault(ActuallyComponents.ENERGY_STORAGE, 0)); - tank.setFluid(input.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).inner()); + tank.setFluid(input.getOrDefault(ActuallyComponents.FLUID_A, ActuallyComponents.FluidContents.EMPTY).get()); } @Override