mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-17 19:15:32 +00:00
Fluid rework derp fixes. (#6098)
* Fix nulls returning from non-null methods, and null-check of non-nullable values. * Fix missed computation of the isEmpty flag in FluidStack * Cleanup getAmount/isEmpty calls. * Remove redundant suppliers. That info is readily available from the vanilla Fluid / FluidState.
This commit is contained in:
1 parent
bae2ee0420
commit
d9f6659f2c
12 files changed
+55
-80
No files matched your search
@@ -53,7 +53,7 @@ public class DispenseFluidContainer extends DefaultDispenseItemBehavior
|
||||
@Nonnull
|
||||
public ItemStack dispenseStack(@Nonnull IBlockSource source, @Nonnull ItemStack stack)
|
||||
{
|
||||
if (FluidUtil.getFluidContained(stack) != null)
|
||||
if (FluidUtil.getFluidContained(stack).isPresent())
|
||||
{
|
||||
return dumpContainer(source, stack);
|
||||
}
|
||||
|
||||
@@ -131,9 +131,6 @@ public class FluidAttributes
|
||||
*/
|
||||
private final Rarity rarity;
|
||||
|
||||
private final Supplier<Block> blockSupplier;
|
||||
private final Supplier<Item> bucketSupplier;
|
||||
|
||||
/**
|
||||
* Color used by universal bucket and the ModelFluid baked model.
|
||||
* Note that this int includes the alpha so converting this to RGB with alpha would be
|
||||
@@ -151,7 +148,6 @@ public class FluidAttributes
|
||||
this.stillTexture = builder.stillTexture;
|
||||
this.flowingTexture = builder.flowingTexture;
|
||||
this.overlayTexture = builder.overlayTexture;
|
||||
this.blockSupplier = builder.blockSupplier;
|
||||
this.color = builder.color;
|
||||
this.fillSound = builder.fillSound;
|
||||
this.emptySound = builder.emptySound;
|
||||
@@ -161,7 +157,6 @@ public class FluidAttributes
|
||||
this.density = builder.density;
|
||||
this.isGaseous = builder.isGaseous;
|
||||
this.rarity = builder.rarity;
|
||||
this.bucketSupplier = builder.bucketSupplier;
|
||||
}
|
||||
|
||||
public final String getName()
|
||||
@@ -171,12 +166,12 @@ public class FluidAttributes
|
||||
|
||||
public ItemStack getBucket(FluidStack stack)
|
||||
{
|
||||
return bucketSupplier != null ? new ItemStack(bucketSupplier.get()) : ItemStack.EMPTY;
|
||||
return new ItemStack(stack.getFluid().getFilledBucket());
|
||||
}
|
||||
|
||||
public BlockState getBlock(IEnviromentBlockReader reader, BlockPos pos, IFluidState state)
|
||||
{
|
||||
return (blockSupplier != null ? blockSupplier.get() : Blocks.AIR).getDefaultState();
|
||||
return state.getBlockState();
|
||||
}
|
||||
|
||||
public IFluidState getStateForPlacement(IEnviromentBlockReader reader, BlockPos pos, FluidStack state)
|
||||
@@ -186,12 +181,12 @@ public class FluidAttributes
|
||||
|
||||
public final boolean canBePlacedInWorld(IEnviromentBlockReader reader, BlockPos pos, IFluidState state)
|
||||
{
|
||||
return getBlock(reader, pos, state) != null;
|
||||
return !getBlock(reader, pos, state).isAir(reader, pos);
|
||||
}
|
||||
|
||||
public final boolean canBePlacedInWorld(IEnviromentBlockReader reader, BlockPos pos, FluidStack state)
|
||||
{
|
||||
return getBlock(reader, pos, getStateForPlacement(reader, pos, state)) != null;
|
||||
return !getBlock(reader, pos, getStateForPlacement(reader, pos, state)).isAir(reader, pos);
|
||||
}
|
||||
|
||||
public final boolean isLighterThanAir()
|
||||
@@ -362,8 +357,6 @@ public class FluidAttributes
|
||||
private String translationKey;
|
||||
private SoundEvent fillSound;
|
||||
private SoundEvent emptySound;
|
||||
private Supplier<Block> blockSupplier;
|
||||
private Supplier<Item> bucketSupplier;
|
||||
private int luminosity = 0;
|
||||
private int density = 1000;
|
||||
private int temperature = 300;
|
||||
@@ -402,18 +395,6 @@ public class FluidAttributes
|
||||
return this;
|
||||
}
|
||||
|
||||
public final Builder block(Supplier<Block> supplier)
|
||||
{
|
||||
blockSupplier = supplier;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final Builder bucket(Supplier<Item> supplier)
|
||||
{
|
||||
bucketSupplier = supplier;
|
||||
return this;
|
||||
}
|
||||
|
||||
public final Builder luminosity(int luminosity)
|
||||
{
|
||||
this.luminosity = luminosity;
|
||||
|
||||
@@ -69,6 +69,8 @@ public class FluidStack
|
||||
}
|
||||
this.fluidDelegate = fluid.delegate;
|
||||
this.amount = amount;
|
||||
|
||||
updateEmpty();
|
||||
}
|
||||
|
||||
public FluidStack(Fluid fluid, int amount, CompoundNBT nbt)
|
||||
@@ -170,7 +172,6 @@ public class FluidStack
|
||||
public void setAmount(int amount)
|
||||
{
|
||||
if (getRawFluid() == Fluids.EMPTY) throw new IllegalStateException("Can't modify the empty stack.");
|
||||
if (amount < 0) throw new IllegalStateException("The amount can't be negative.");
|
||||
this.amount = amount;
|
||||
updateEmpty();
|
||||
}
|
||||
@@ -217,8 +218,10 @@ public class FluidStack
|
||||
{
|
||||
getOrCreateTag();
|
||||
CompoundNBT child = tag.getCompound(childName);
|
||||
if (child == null)
|
||||
tag.put(childName, child = new CompoundNBT());
|
||||
if (!tag.contains(childName, Constants.NBT.TAG_COMPOUND))
|
||||
{
|
||||
tag.put(childName, child);
|
||||
}
|
||||
return child;
|
||||
}
|
||||
|
||||
@@ -302,13 +305,8 @@ public class FluidStack
|
||||
* The ItemStack for comparison
|
||||
* @return true if the Fluids (IDs and NBT Tags) are the same
|
||||
*/
|
||||
public boolean isFluidEqual(ItemStack other)
|
||||
public boolean isFluidEqual(@Nonnull ItemStack other)
|
||||
{
|
||||
if (other == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return FluidUtil.getFluidContained(other).map(this::isFluidEqual).orElse(false);
|
||||
}
|
||||
|
||||
|
||||
@@ -554,7 +554,7 @@ public class FluidUtil
|
||||
return false;
|
||||
}
|
||||
|
||||
if (fluidSource.drain(resource, IFluidHandler.FluidAction.SIMULATE) == null)
|
||||
if (fluidSource.drain(resource, IFluidHandler.FluidAction.SIMULATE).isEmpty())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -574,7 +574,7 @@ public class FluidUtil
|
||||
if (world.dimension.doesWaterVaporize() && fluid.getAttributes().doesVaporize(world, pos, resource))
|
||||
{
|
||||
FluidStack result = fluidSource.drain(resource, IFluidHandler.FluidAction.EXECUTE);
|
||||
if (result != null)
|
||||
if (!result.isEmpty())
|
||||
{
|
||||
result.getFluid().getAttributes().vaporize(player, world, pos, result);
|
||||
return true;
|
||||
@@ -585,7 +585,7 @@ public class FluidUtil
|
||||
// This fluid handler places the fluid block when filled
|
||||
IFluidHandler handler = getFluidBlockHandler(fluid, world, pos);
|
||||
FluidStack result = tryFluidTransfer(handler, fluidSource, resource, true);
|
||||
if (result != null)
|
||||
if (!result.isEmpty())
|
||||
{
|
||||
SoundEvent soundevent = resource.getFluid().getAttributes().getEmptySound(resource);
|
||||
world.playSound(player, pos, soundevent, SoundCategory.BLOCKS, 1.0F, 1.0F);
|
||||
|
||||
@@ -36,7 +36,7 @@ public class EmptyFluidHandler implements IFluidHandler
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidStack getFluidInTank(int tank) { return null; }
|
||||
public FluidStack getFluidInTank(int tank) { return FluidStack.EMPTY; }
|
||||
|
||||
@Override
|
||||
public int getTankCapacity(int tank) { return 0; }
|
||||
@@ -54,13 +54,13 @@ public class EmptyFluidHandler implements IFluidHandler
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, FluidAction action)
|
||||
{
|
||||
return null;
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, FluidAction action)
|
||||
{
|
||||
return null;
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -119,7 +119,7 @@ public class FluidHandlerItemStack implements IFluidHandlerItem, ICapabilityProv
|
||||
@Override
|
||||
public int fill(FluidStack resource, FluidAction doFill)
|
||||
{
|
||||
if (container.getCount() != 1 || resource == null || resource.getAmount() <= 0 || !canFillFluidType(resource))
|
||||
if (container.getCount() != 1 || resource.isEmpty() || !canFillFluidType(resource))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -190,7 +190,7 @@ public class FluidHandlerItemStack implements IFluidHandlerItem, ICapabilityProv
|
||||
if (action.execute())
|
||||
{
|
||||
contained.shrink(drainAmount);
|
||||
if (contained.getAmount() == 0)
|
||||
if (contained.isEmpty())
|
||||
{
|
||||
setContainerToEmpty();
|
||||
}
|
||||
|
||||
+1
-1
@@ -143,7 +143,7 @@ public class FluidHandlerItemStackSimple implements IFluidHandlerItem, ICapabili
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, FluidAction action)
|
||||
{
|
||||
if (container.getCount() != 1 || resource == null || resource.getAmount() <= 0 || !resource.isFluidEqual(getFluid()))
|
||||
if (container.getCount() != 1 || resource.isEmpty() || !resource.isFluidEqual(getFluid()))
|
||||
{
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
|
||||
@@ -89,25 +89,15 @@ public class FluidTank implements IFluidHandler, IFluidTank {
|
||||
|
||||
public FluidTank readFromNBT(CompoundNBT nbt) {
|
||||
|
||||
FluidStack fluid = FluidStack.EMPTY;
|
||||
if (!nbt.contains("Empty"))
|
||||
{
|
||||
fluid = FluidStack.loadFluidStackFromNBT(nbt);
|
||||
}
|
||||
FluidStack fluid = FluidStack.loadFluidStackFromNBT(nbt);
|
||||
setFluid(fluid);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CompoundNBT writeToNBT(CompoundNBT nbt) {
|
||||
|
||||
if (fluid != null)
|
||||
{
|
||||
fluid.writeToNBT(nbt);
|
||||
}
|
||||
else
|
||||
{
|
||||
nbt.putString("Empty", "");
|
||||
}
|
||||
fluid.writeToNBT(nbt);
|
||||
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@@ -139,13 +129,13 @@ public class FluidTank implements IFluidHandler, IFluidTank {
|
||||
@Override
|
||||
public int fill(FluidStack resource, FluidAction action)
|
||||
{
|
||||
if (resource == null || !isFluidValid(resource))
|
||||
if (resource.isEmpty() || !isFluidValid(resource))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (action.simulate())
|
||||
{
|
||||
if (fluid == null)
|
||||
if (fluid.isEmpty())
|
||||
{
|
||||
return Math.min(capacity, resource.getAmount());
|
||||
}
|
||||
@@ -155,7 +145,7 @@ public class FluidTank implements IFluidHandler, IFluidTank {
|
||||
}
|
||||
return Math.min(capacity - fluid.getAmount(), resource.getAmount());
|
||||
}
|
||||
if (fluid == null)
|
||||
if (fluid.isEmpty())
|
||||
{
|
||||
onContentsChanged();
|
||||
fluid = new FluidStack(resource, Math.min(capacity, resource.getAmount()));
|
||||
@@ -183,7 +173,7 @@ public class FluidTank implements IFluidHandler, IFluidTank {
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, FluidAction action)
|
||||
{
|
||||
if (resource == null || !resource.isFluidEqual(fluid))
|
||||
if (resource.isEmpty() || !resource.isFluidEqual(fluid))
|
||||
{
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
@@ -203,10 +193,6 @@ public class FluidTank implements IFluidHandler, IFluidTank {
|
||||
if (action.execute())
|
||||
{
|
||||
fluid.shrink(drained);
|
||||
if (fluid.getAmount() <= 0)
|
||||
{
|
||||
fluid = FluidStack.EMPTY;
|
||||
}
|
||||
}
|
||||
return stack;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class VoidFluidHandler implements IFluidHandler
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidStack getFluidInTank(int tank) { return null; }
|
||||
public FluidStack getFluidInTank(int tank) { return FluidStack.EMPTY; }
|
||||
|
||||
@Override
|
||||
public int getTankCapacity(int tank) { return Integer.MAX_VALUE; }
|
||||
@@ -58,13 +58,13 @@ public class VoidFluidHandler implements IFluidHandler
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, FluidAction action)
|
||||
{
|
||||
return null;
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, FluidAction action)
|
||||
{
|
||||
return null;
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
}
|
||||
@@ -77,13 +77,13 @@ public class FluidBucketWrapper implements IFluidHandlerItem, ICapabilityProvide
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
}
|
||||
|
||||
protected void setFluid(@Nonnull FluidStack fluidStack)
|
||||
{
|
||||
if (fluidStack.getAmount() <= 0)
|
||||
if (fluidStack.isEmpty())
|
||||
container = new ItemStack(Items.BUCKET);
|
||||
else
|
||||
container = FluidUtil.getFilledBucket(fluidStack);
|
||||
@@ -117,7 +117,7 @@ public class FluidBucketWrapper implements IFluidHandlerItem, ICapabilityProvide
|
||||
@Override
|
||||
public int fill(FluidStack resource, FluidAction action)
|
||||
{
|
||||
if (container.getCount() != 1 || resource == null || resource.getAmount() < FluidAttributes.BUCKET_VOLUME || container.getItem() instanceof MilkBucketItem || getFluid() != null || !canFillFluidType(resource))
|
||||
if (container.getCount() != 1 || resource.getAmount() < FluidAttributes.BUCKET_VOLUME || container.getItem() instanceof MilkBucketItem || !getFluid().isEmpty() || !canFillFluidType(resource))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -134,13 +134,13 @@ public class FluidBucketWrapper implements IFluidHandlerItem, ICapabilityProvide
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, FluidAction action)
|
||||
{
|
||||
if (container.getCount() != 1 || resource == null || resource.getAmount() < FluidAttributes.BUCKET_VOLUME)
|
||||
if (container.getCount() != 1 || resource.getAmount() < FluidAttributes.BUCKET_VOLUME)
|
||||
{
|
||||
return FluidStack.EMPTY;
|
||||
}
|
||||
|
||||
FluidStack fluidStack = getFluid();
|
||||
if (fluidStack != null && fluidStack.isFluidEqual(resource))
|
||||
if (!fluidStack.isEmpty() && fluidStack.isFluidEqual(resource))
|
||||
{
|
||||
if (action.execute())
|
||||
{
|
||||
@@ -162,7 +162,7 @@ public class FluidBucketWrapper implements IFluidHandlerItem, ICapabilityProvide
|
||||
}
|
||||
|
||||
FluidStack fluidStack = getFluid();
|
||||
if (fluidStack != null)
|
||||
if (!fluidStack.isEmpty())
|
||||
{
|
||||
if (action.execute())
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user