Fix Universal bucket handling for Fluids with NBT (Fix #3757)

This commit is contained in:
mezz committed 2017-06-22 23:40:49 -07:00
1 parent 0e9820c51c
commit 423f9fc04b
4 files changed
+68 -83

No files matched your search

@@ -29,7 +29,9 @@ import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBucket;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
@@ -37,6 +39,7 @@ import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeModContainer;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandler;
import net.minecraftforge.fluids.capability.IFluidHandlerItem;
@@ -46,7 +49,6 @@ import net.minecraftforge.fluids.capability.wrappers.FluidBlockWrapper;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.items.wrapper.InvWrapper;
public class FluidUtil
{
@@ -100,41 +102,6 @@ public class FluidUtil
return false;
}
/**
* Used to handle the common case of a player holding a fluid item and right-clicking on a fluid handler.
* First it tries to fill the container item from the fluid handler,
* if that action fails then it tries to drain the container item into the fluid handler.
*
* @param stack The filled or empty fluid container.
* Will not be modified directly, if modifications are necessary a modified copy is returned in the result.
* @param fluidHandler The fluid handler to interact with.
* @param player The player doing the interaction between the item and fluid handler.
* @return a {@link FluidActionResult} holding the result and resulting container.
*
* @deprecated Use {@link #interactWithFluidHandler(EntityPlayer, EnumHand, World, BlockPos, EnumFacing)} which is easier to use.
*/
@Deprecated
@Nonnull
public static FluidActionResult interactWithFluidHandler(@Nonnull ItemStack stack, IFluidHandler fluidHandler, EntityPlayer player)
{
if (stack.isEmpty() || fluidHandler == null || player == null)
{
return FluidActionResult.FAILURE;
}
IItemHandler playerInventory = new InvWrapper(player.inventory);
FluidActionResult fillResult = tryFillContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
if (fillResult.isSuccess())
{
return fillResult;
}
else
{
return tryEmptyContainerAndStow(stack, fluidHandler, playerInventory, Integer.MAX_VALUE, player);
}
}
/**
* Fill a container from the given fluidSource.
*
@@ -681,4 +648,47 @@ public class FluidUtil
}
}
}
/**
* @param fluidStack contents used to fill the bucket.
* FluidStack is used instead of Fluid to preserve fluid NBT, the amount is ignored.
* @return a filled vanilla bucket or filled universal bucket.
* Returns empty itemStack if none of the enabled buckets can hold the fluid.
*/
@Nonnull
public static ItemStack getFilledBucket(@Nonnull FluidStack fluidStack)
{
Fluid fluid = fluidStack.getFluid();
if (fluidStack.tag == null || fluidStack.tag.hasNoTags())
{
if (fluid == FluidRegistry.WATER)
{
return new ItemStack(Items.WATER_BUCKET);
}
else if (fluid == FluidRegistry.LAVA)
{
return new ItemStack(Items.LAVA_BUCKET);
}
else if (fluid.getName().equals("milk"))
{
return new ItemStack(Items.MILK_BUCKET);
}
}
if (FluidRegistry.isUniversalBucketEnabled() && FluidRegistry.getBucketFluids().contains(fluid))
{
UniversalBucket bucket = ForgeModContainer.getInstance().universalBucket;
ItemStack filledBucket = new ItemStack(bucket);
FluidStack fluidContents = new FluidStack(fluidStack, bucket.getCapacity());
NBTTagCompound tag = new NBTTagCompound();
fluidContents.writeToNBT(tag);
filledBucket.setTagCompound(tag);
return filledBucket;
}
return ItemStack.EMPTY;
}
}