Skip to content

Commit

Permalink
Add various helpers to FluidStack similar to those in ItemStack and B…
Browse files Browse the repository at this point in the history
…lockState (#585)
  • Loading branch information
pupnewfster authored Jan 30, 2024
1 parent e21619f commit 33dcab5
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 31 deletions.
54 changes: 51 additions & 3 deletions src/main/java/net/neoforged/neoforge/fluids/FluidStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +52,7 @@ public class FluidStack {

private boolean isEmpty;
private int amount;
@Nullable
private CompoundTag tag;
private final Fluid fluid;

Expand Down Expand Up @@ -143,6 +147,38 @@ public final Fluid getRawFluid() {
return fluid;
}

public final FluidType getFluidType() {
return getFluid().getFluidType();
}

public final Holder<Fluid> getFluidHolder() {
return getFluid().builtInRegistryHolder();
}

public final boolean is(TagKey<Fluid> 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<Fluid> holder) {
return is(holder.value());
}

public final boolean is(HolderSet<Fluid> holderSet) {
return holderSet.contains(getFluidHolder());
}

public final Stream<TagKey<Fluid>> getTags() {
return getFluidHolder().tags();
}

public boolean isEmpty() {
return isEmpty;
}
Expand Down Expand Up @@ -173,6 +209,7 @@ public boolean hasTag() {
return tag != null;
}

@Nullable
public CompoundTag getTag() {
return tag;
}
Expand Down Expand Up @@ -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);
}

Expand All @@ -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) {
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/net/neoforged/neoforge/fluids/FluidUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 33dcab5

Please sign in to comment.