diff --git a/src/main/java/net/neoforged/neoforge/fluids/FluidStack.java b/src/main/java/net/neoforged/neoforge/fluids/FluidStack.java index bf69f6e4ab..7a2909b51f 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/FluidStack.java +++ b/src/main/java/net/neoforged/neoforge/fluids/FluidStack.java @@ -8,13 +8,16 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import java.util.Optional; +import java.util.stream.Stream; import net.minecraft.core.Holder; +import net.minecraft.core.HolderSet; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; +import net.minecraft.tags.TagKey; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.Fluids; @@ -49,6 +52,7 @@ public class FluidStack { private boolean isEmpty; private int amount; + @Nullable private CompoundTag tag; private final Fluid fluid; @@ -143,6 +147,38 @@ public final Fluid getRawFluid() { return fluid; } + public final FluidType getFluidType() { + return getFluid().getFluidType(); + } + + public final Holder getFluidHolder() { + return getFluid().builtInRegistryHolder(); + } + + public final boolean is(TagKey tag) { + return getFluidHolder().is(tag); + } + + public final boolean is(Fluid fluid) { + return getFluid() == fluid; + } + + public final boolean is(FluidType fluidType) { + return getFluidType() == fluidType; + } + + public final boolean is(Holder holder) { + return is(holder.value()); + } + + public final boolean is(HolderSet holderSet) { + return holderSet.contains(getFluidHolder()); + } + + public final Stream> getTags() { + return getFluidHolder().tags(); + } + public boolean isEmpty() { return isEmpty; } @@ -173,6 +209,7 @@ public boolean hasTag() { return tag != null; } + @Nullable public CompoundTag getTag() { return tag; } @@ -209,17 +246,28 @@ public void removeChildTag(String childName) { } public Component getDisplayName() { - return this.getFluid().getFluidType().getDescription(this); + return getFluidType().getDescription(this); } public String getTranslationKey() { - return this.getFluid().getFluidType().getDescriptionId(this); + return getFluidType().getDescriptionId(this); } /** * @return A copy of this FluidStack */ public FluidStack copy() { + //TODO - 1.20.5: Mirror vanilla's ItemStack#copy method and return the empty instance if this is empty + return new FluidStack(getFluid(), amount, tag); + } + + /** + * @return A copy of this FluidStack + */ + public FluidStack copyWithAmount(int amount) { + if (isEmpty() || amount <= 0) { + return EMPTY; + } return new FluidStack(getFluid(), amount, tag); } @@ -231,7 +279,7 @@ public FluidStack copy() { * @return true if the Fluids (IDs and NBT Tags) are the same */ public boolean isFluidEqual(FluidStack other) { - return getFluid() == other.getFluid() && isFluidStackTagEqual(other); + return is(other.getFluid()) && isFluidStackTagEqual(other); } private boolean isFluidStackTagEqual(FluidStack other) { diff --git a/src/main/java/net/neoforged/neoforge/fluids/FluidUtil.java b/src/main/java/net/neoforged/neoforge/fluids/FluidUtil.java index f5dfdbbb0c..a55efb1c47 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/FluidUtil.java +++ b/src/main/java/net/neoforged/neoforge/fluids/FluidUtil.java @@ -119,7 +119,7 @@ public static FluidActionResult tryFillContainer(ItemStack container, IFluidHand if (doFill) { tryFluidTransfer(containerFluidHandler, fluidSource, maxAmount, true); if (player != null) { - SoundEvent soundevent = simulatedTransfer.getFluid().getFluidType().getSound(simulatedTransfer, SoundActions.BUCKET_FILL); + SoundEvent soundevent = simulatedTransfer.getFluidType().getSound(simulatedTransfer, SoundActions.BUCKET_FILL); if (soundevent != null) { player.level().playSound(null, player.getX(), player.getY() + 0.5, player.getZ(), soundevent, SoundSource.BLOCKS, 1.0F, 1.0F); @@ -166,7 +166,7 @@ public static FluidActionResult tryEmptyContainer(ItemStack container, IFluidHan } if (doDrain && player != null) { - SoundEvent soundevent = transfer.getFluid().getFluidType().getSound(transfer, SoundActions.BUCKET_EMPTY); + SoundEvent soundevent = transfer.getFluidType().getSound(transfer, SoundActions.BUCKET_EMPTY); if (soundevent != null) { player.level().playSound(null, player.getX(), player.getY() + 0.5, player.getZ(), soundevent, SoundSource.BLOCKS, 1.0F, 1.0F); @@ -490,7 +490,7 @@ public static boolean tryPlaceFluid(@Nullable Player player, Level level, Intera if (fluid.getFluidType().isVaporizedOnPlacement(level, pos, resource)) { FluidStack result = fluidSource.drain(resource, IFluidHandler.FluidAction.EXECUTE); if (!result.isEmpty()) { - result.getFluid().getFluidType().onVaporize(player, level, pos, result); + result.getFluidType().onVaporize(player, level, pos, result); return true; } } else { @@ -503,7 +503,7 @@ public static boolean tryPlaceFluid(@Nullable Player player, Level level, Intera } FluidStack result = tryFluidTransfer(handler, fluidSource, resource, true); if (!result.isEmpty()) { - SoundEvent soundevent = resource.getFluid().getFluidType().getSound(resource, SoundActions.BUCKET_EMPTY); + SoundEvent soundevent = resource.getFluidType().getSound(resource, SoundActions.BUCKET_EMPTY); if (soundevent != null) { level.playSound(player, pos, soundevent, SoundSource.BLOCKS, 1.0F, 1.0F); @@ -553,16 +553,13 @@ public static void destroyBlockOnFluidPlacement(Level level, BlockPos pos) { * Returns empty itemStack if none of the enabled buckets can hold the fluid. */ public static ItemStack getFilledBucket(FluidStack fluidStack) { - Fluid fluid = fluidStack.getFluid(); - if (!fluidStack.hasTag() || fluidStack.getTag().isEmpty()) { - if (fluid == Fluids.WATER) { + if (fluidStack.is(Fluids.WATER)) { return new ItemStack(Items.WATER_BUCKET); - } else if (fluid == Fluids.LAVA) { + } else if (fluidStack.is(Fluids.LAVA)) { return new ItemStack(Items.LAVA_BUCKET); } } - - return fluid.getFluidType().getBucket(fluidStack); + return fluidStack.getFluidType().getBucket(fluidStack); } } diff --git a/src/main/java/net/neoforged/neoforge/fluids/capability/templates/FluidHandlerItemStack.java b/src/main/java/net/neoforged/neoforge/fluids/capability/templates/FluidHandlerItemStack.java index d6a8754c2c..4310e86cc9 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/capability/templates/FluidHandlerItemStack.java +++ b/src/main/java/net/neoforged/neoforge/fluids/capability/templates/FluidHandlerItemStack.java @@ -88,9 +88,7 @@ public int fill(FluidStack resource, FluidAction doFill) { int fillAmount = Math.min(capacity, resource.getAmount()); if (doFill.execute()) { - FluidStack filled = resource.copy(); - filled.setAmount(fillAmount); - setFluid(filled); + setFluid(resource.copyWithAmount(fillAmount)); } return fillAmount; @@ -131,8 +129,7 @@ public FluidStack drain(int maxDrain, FluidAction action) { final int drainAmount = Math.min(contained.getAmount(), maxDrain); - FluidStack drained = contained.copy(); - drained.setAmount(drainAmount); + FluidStack drained = contained.copyWithAmount(drainAmount); if (action.execute()) { contained.shrink(drainAmount); diff --git a/src/main/java/net/neoforged/neoforge/fluids/capability/templates/FluidHandlerItemStackSimple.java b/src/main/java/net/neoforged/neoforge/fluids/capability/templates/FluidHandlerItemStackSimple.java index 3470531a92..11a3d60dbc 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/capability/templates/FluidHandlerItemStackSimple.java +++ b/src/main/java/net/neoforged/neoforge/fluids/capability/templates/FluidHandlerItemStackSimple.java @@ -85,9 +85,7 @@ public int fill(FluidStack resource, FluidAction action) { int fillAmount = Math.min(capacity, resource.getAmount()); if (fillAmount == capacity) { if (action.execute()) { - FluidStack filled = resource.copy(); - filled.setAmount(fillAmount); - setFluid(filled); + setFluid(resource.copyWithAmount(fillAmount)); } return fillAmount; diff --git a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/BlockWrapper.java b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/BlockWrapper.java index 98f1fdfa17..8e43667f58 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/BlockWrapper.java +++ b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/BlockWrapper.java @@ -62,7 +62,7 @@ public int fill(FluidStack resource, FluidAction action) { BlockState state = world.getBlockState(blockPos); if (liquidContainer.canPlaceLiquid(null, world, blockPos, state, resource.getFluid())) { if (action.execute()) { - liquidContainer.placeLiquid(world, blockPos, state, resource.getFluid().getFluidType().getStateForPlacement(world, blockPos, resource)); + liquidContainer.placeLiquid(world, blockPos, state, resource.getFluidType().getStateForPlacement(world, blockPos, resource)); } return FluidType.BUCKET_VOLUME; } diff --git a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/BucketPickupHandlerWrapper.java b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/BucketPickupHandlerWrapper.java index e827b89008..d010c0b67e 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/BucketPickupHandlerWrapper.java +++ b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/BucketPickupHandlerWrapper.java @@ -70,7 +70,7 @@ public int fill(FluidStack resource, FluidAction action) { public FluidStack drain(FluidStack resource, FluidAction action) { if (!resource.isEmpty() && FluidType.BUCKET_VOLUME <= resource.getAmount()) { FluidState fluidState = world.getFluidState(blockPos); - if (!fluidState.isEmpty() && resource.getFluid() == fluidState.getType()) { + if (!fluidState.isEmpty() && resource.is(fluidState.getType())) { if (action.execute()) { ItemStack itemStack = bucketPickupHandler.pickupBlock(player, world, blockPos, world.getBlockState(blockPos)); if (itemStack != ItemStack.EMPTY && itemStack.getItem() instanceof BucketItem bucket) { diff --git a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/CauldronWrapper.java b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/CauldronWrapper.java index d5df96a3f4..c806c91f3f 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/CauldronWrapper.java +++ b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/CauldronWrapper.java @@ -84,7 +84,7 @@ public int fill(FluidStack resource, FluidAction action) { BlockState state = level.getBlockState(pos); CauldronFluidContent currentContent = getContent(state); - if (currentContent.fluid != Fluids.EMPTY && currentContent.fluid != resource.getFluid()) { + if (currentContent.fluid != Fluids.EMPTY && !resource.is(currentContent.fluid)) { // Fluid mismatch return 0; } @@ -110,7 +110,7 @@ public FluidStack drain(FluidStack resource, FluidAction action) { } BlockState state = level.getBlockState(pos); - if (getContent(state).fluid == resource.getFluid() && !resource.hasTag()) { + if (resource.is(getContent(state).fluid) && !resource.hasTag()) { return drain(state, resource.getAmount(), action); } else { return FluidStack.EMPTY; diff --git a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/FluidBlockWrapper.java b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/FluidBlockWrapper.java index 664a95bd78..8c56e87a40 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/FluidBlockWrapper.java +++ b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/FluidBlockWrapper.java @@ -47,7 +47,7 @@ public int getTankCapacity(int tank) { @Override public boolean isFluidValid(int tank, FluidStack stack) { - return stack.getFluid() == fluidBlock.getFluid(); + return stack.is(fluidBlock.getFluid()); } @Override @@ -57,7 +57,7 @@ public int fill(FluidStack resource, FluidAction action) { @Override public FluidStack drain(FluidStack resource, FluidAction action) { - if (!resource.isEmpty() && fluidBlock.canDrain(world, blockPos) && resource.getFluid() == fluidBlock.getFluid()) { + if (!resource.isEmpty() && fluidBlock.canDrain(world, blockPos) && resource.is(fluidBlock.getFluid())) { FluidStack simulatedDrained = fluidBlock.drain(world, blockPos, FluidAction.SIMULATE); if (simulatedDrained.getAmount() <= resource.getAmount() && resource.isFluidEqual(simulatedDrained)) { if (action.execute()) { diff --git a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/FluidBucketWrapper.java b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/FluidBucketWrapper.java index a7fd1f9d9b..8f4a1b82ac 100644 --- a/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/FluidBucketWrapper.java +++ b/src/main/java/net/neoforged/neoforge/fluids/capability/wrappers/FluidBucketWrapper.java @@ -34,10 +34,10 @@ public ItemStack getContainer() { } public boolean canFillFluidType(FluidStack fluid) { - if (fluid.getFluid() == Fluids.WATER || fluid.getFluid() == Fluids.LAVA) { + if (fluid.is(Fluids.WATER) || fluid.is(Fluids.LAVA)) { return true; } - return !fluid.getFluid().getFluidType().getBucket(fluid).isEmpty(); + return !fluid.getFluidType().getBucket(fluid).isEmpty(); } public FluidStack getFluid() { diff --git a/tests/src/main/java/net/neoforged/neoforge/oldtest/FluidUtilTest.java b/tests/src/main/java/net/neoforged/neoforge/oldtest/FluidUtilTest.java index 6a66944cd9..7fe183d9c5 100644 --- a/tests/src/main/java/net/neoforged/neoforge/oldtest/FluidUtilTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/oldtest/FluidUtilTest.java @@ -131,7 +131,7 @@ private static void checkItemStack(ItemStack stack, Item item, int count) { } private static void checkFluidStack(FluidStack stack, Fluid fluid, int amount) { - if (stack.getFluid() != fluid) + if (!stack.is(fluid)) throw new AssertionError("Expected fluid " + BuiltInRegistries.FLUID.getKey(fluid) + ", got: " + BuiltInRegistries.FLUID.getKey(stack.getFluid())); if (stack.getAmount() != amount) throw new AssertionError("Expected amount " + amount + ", got: " + stack.getAmount()); diff --git a/tests/src/main/java/net/neoforged/neoforge/oldtest/item/CustomFluidContainerTest.java b/tests/src/main/java/net/neoforged/neoforge/oldtest/item/CustomFluidContainerTest.java index cd9de26c7f..a0932646a5 100644 --- a/tests/src/main/java/net/neoforged/neoforge/oldtest/item/CustomFluidContainerTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/oldtest/item/CustomFluidContainerTest.java @@ -72,7 +72,7 @@ public Component getName(ItemStack itemStack) { if (fluidStack.isEmpty()) { name.set(name.get() + " (empty)"); } else { - name.set(name.get() + " (" + fluidStack.getFluid().getFluidType().getDescription().getString() + ")"); + name.set(name.get() + " (" + fluidStack.getFluidType().getDescription().getString() + ")"); } }); return Component.literal(name.get());