mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-21 04:32:15 +00:00
Add new IFluidHandler for capabilities. (#2942)
Old fluid system is deprecated and slated for removal in the next cleanup cycle.
This commit is contained in:
1 parent
13ed510fcf
commit
92914f82ae
39 files changed
+2612
-298
No files matched your search
@@ -0,0 +1,65 @@
|
||||
package net.minecraftforge.fluids.capability;
|
||||
|
||||
import net.minecraft.nbt.NBTBase;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.CapabilityInject;
|
||||
import net.minecraftforge.common.capabilities.CapabilityManager;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
import net.minecraftforge.fluids.IFluidTank;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class CapabilityFluidHandler
|
||||
{
|
||||
@CapabilityInject(IFluidHandler.class)
|
||||
public static Capability<IFluidHandler> FLUID_HANDLER_CAPABILITY = null;
|
||||
|
||||
public static void register()
|
||||
{
|
||||
CapabilityManager.INSTANCE.register(IFluidHandler.class, new Capability.IStorage<IFluidHandler>()
|
||||
{
|
||||
@Override
|
||||
public NBTBase writeNBT(Capability<IFluidHandler> capability, IFluidHandler instance, EnumFacing side)
|
||||
{
|
||||
if (!(instance instanceof IFluidTank))
|
||||
throw new RuntimeException("IFluidHandler instance does not implement IFluidTank");
|
||||
NBTTagCompound nbt = new NBTTagCompound();
|
||||
IFluidTank tank = (IFluidTank) instance;
|
||||
FluidStack fluid = tank.getFluid();
|
||||
if (fluid != null)
|
||||
{
|
||||
fluid.writeToNBT(nbt);
|
||||
}
|
||||
else
|
||||
{
|
||||
nbt.setString("Empty", "");
|
||||
}
|
||||
nbt.setInteger("Capacity", tank.getCapacity());
|
||||
return nbt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readNBT(Capability<IFluidHandler> capability, IFluidHandler instance, EnumFacing side, NBTBase nbt)
|
||||
{
|
||||
if (!(instance instanceof IFluidTank))
|
||||
throw new RuntimeException("IFluidHandler instance is not instance of FluidTank");
|
||||
NBTTagCompound tags = (NBTTagCompound) nbt;
|
||||
FluidTank tank = (FluidTank) instance;
|
||||
tank.setCapacity(tags.getInteger("Capacity"));
|
||||
tank.readFromNBT(tags);
|
||||
}
|
||||
}, new Callable<IFluidHandler>()
|
||||
{
|
||||
@Override
|
||||
public IFluidHandler call() throws Exception
|
||||
{
|
||||
return new FluidTank(Fluid.BUCKET_VOLUME);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package net.minecraftforge.fluids.capability;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
|
||||
/**
|
||||
* Basic implementation of {@link IFluidTankProperties}.
|
||||
*/
|
||||
public class FluidTankProperties implements IFluidTankProperties
|
||||
{
|
||||
public static FluidTankProperties[] convert(FluidTankInfo[] fluidTankInfos)
|
||||
{
|
||||
FluidTankProperties[] properties = new FluidTankProperties[fluidTankInfos.length];
|
||||
for (int i = 0; i < fluidTankInfos.length; i++)
|
||||
{
|
||||
FluidTankInfo info = fluidTankInfos[i];
|
||||
properties[i] = new FluidTankProperties(info.fluid, info.capacity);
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private final FluidStack contents;
|
||||
private final int capacity;
|
||||
private final boolean canFill;
|
||||
private final boolean canDrain;
|
||||
|
||||
public FluidTankProperties(@Nullable FluidStack contents, int capacity)
|
||||
{
|
||||
this(contents, capacity, true, true);
|
||||
}
|
||||
|
||||
public FluidTankProperties(@Nullable FluidStack contents, int capacity, boolean canFill, boolean canDrain)
|
||||
{
|
||||
this.contents = contents;
|
||||
this.capacity = capacity;
|
||||
this.canFill = canFill;
|
||||
this.canDrain = canDrain;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack getContents()
|
||||
{
|
||||
return contents == null ? null : contents.copy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCapacity()
|
||||
{
|
||||
return capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFill()
|
||||
{
|
||||
return canFill;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrain()
|
||||
{
|
||||
return canDrain;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFillFluidType(FluidStack fluidStack)
|
||||
{
|
||||
return canFill;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrainFluidType(FluidStack fluidStack)
|
||||
{
|
||||
return canDrain;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package net.minecraftforge.fluids.capability;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
|
||||
/**
|
||||
* Basic {@link IFluidTankProperties} wrapper for {@link FluidTank}.
|
||||
*/
|
||||
public class FluidTankPropertiesWrapper implements IFluidTankProperties
|
||||
{
|
||||
protected final FluidTank tank;
|
||||
|
||||
public FluidTankPropertiesWrapper(FluidTank tank)
|
||||
{
|
||||
this.tank = tank;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack getContents()
|
||||
{
|
||||
FluidStack contents = tank.getFluid();
|
||||
return contents == null ? null : contents.copy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCapacity()
|
||||
{
|
||||
return tank.getCapacity();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFill()
|
||||
{
|
||||
return tank.canFill();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrain()
|
||||
{
|
||||
return tank.canDrain();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFillFluidType(FluidStack fluidStack)
|
||||
{
|
||||
return tank.canFillFluidType(fluidStack);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrainFluidType(FluidStack fluidStack)
|
||||
{
|
||||
return tank.canDrainFluidType(fluidStack);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package net.minecraftforge.fluids.capability;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraftforge.fluids.*;
|
||||
|
||||
/**
|
||||
* Implement this interface as a capability which should handle fluids, generally storing them in
|
||||
* one or more internal {@link IFluidTank} objects.
|
||||
* <p/>
|
||||
* A reference implementation is provided {@link TileFluidHandler}.
|
||||
*/
|
||||
public interface IFluidHandler
|
||||
{
|
||||
/**
|
||||
* Returns an array of objects which represent the internal tanks.
|
||||
* These objects cannot be used to manipulate the internal tanks.
|
||||
*
|
||||
* @return Properties for the relevant internal tanks.
|
||||
*/
|
||||
IFluidTankProperties[] getTankProperties();
|
||||
|
||||
/**
|
||||
* Fills fluid into internal tanks, distribution is left entirely to the IFluidHandler.
|
||||
*
|
||||
* @param resource FluidStack representing the Fluid and maximum amount of fluid to be filled.
|
||||
* @param doFill If false, fill will only be simulated.
|
||||
* @return Amount of resource that was (or would have been, if simulated) filled.
|
||||
*/
|
||||
int fill(FluidStack resource, boolean doFill);
|
||||
|
||||
/**
|
||||
* Drains fluid out of internal tanks, distribution is left entirely to the IFluidHandler.
|
||||
*
|
||||
* @param resource FluidStack representing the Fluid and maximum amount of fluid to be drained.
|
||||
* @param doDrain If false, drain will only be simulated.
|
||||
* @return FluidStack representing the Fluid and amount that was (or would have been, if
|
||||
* simulated) drained.
|
||||
*/
|
||||
@Nullable
|
||||
FluidStack drain(FluidStack resource, boolean doDrain);
|
||||
|
||||
/**
|
||||
* Drains fluid out of internal tanks, distribution is left entirely to the IFluidHandler.
|
||||
* <p/>
|
||||
* This method is not Fluid-sensitive.
|
||||
*
|
||||
* @param maxDrain Maximum amount of fluid to drain.
|
||||
* @param doDrain If false, drain will only be simulated.
|
||||
* @return FluidStack representing the Fluid and amount that was (or would have been, if
|
||||
* simulated) drained.
|
||||
*/
|
||||
@Nullable
|
||||
FluidStack drain(int maxDrain, boolean doDrain);
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package net.minecraftforge.fluids.capability;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
/**
|
||||
* Simplified Read-only Information about the internals of an {@link IFluidHandler}.
|
||||
* This is useful for displaying information, and as hints for interacting with it.
|
||||
* These properties are constant and do not depend on the fluid contents (except the contents themselves, of course).
|
||||
*
|
||||
* The information here may not tell the full story of how the tank actually works,
|
||||
* for real fluid transactions you must use {@link IFluidHandler} to simulate, check, and then interact.
|
||||
* None of the information in these properties is required to successfully interact using a {@link IFluidHandler}.
|
||||
*/
|
||||
public interface IFluidTankProperties
|
||||
{
|
||||
/**
|
||||
* @return A copy of the fluid contents of this tank. May be null.
|
||||
* To modify the contents, use {@link IFluidHandler}.
|
||||
*/
|
||||
@Nullable
|
||||
FluidStack getContents();
|
||||
|
||||
/**
|
||||
* @return The maximum amount of fluid this tank can hold, in millibuckets.
|
||||
*/
|
||||
int getCapacity();
|
||||
|
||||
/**
|
||||
* Returns true if the tank can be filled at any time (even if it is currently full).
|
||||
* It does not consider the contents or capacity of the tank.
|
||||
*
|
||||
* This value is constant. If the tank behavior is more complicated, returns true.
|
||||
*/
|
||||
boolean canFill();
|
||||
|
||||
/**
|
||||
* Returns true if the tank can be drained at any time (even if it is currently empty).
|
||||
* It does not consider the contents or capacity of the tank.
|
||||
*
|
||||
* This value is constant. If the tank behavior is more complicated, returns true.
|
||||
*/
|
||||
boolean canDrain();
|
||||
|
||||
/**
|
||||
* Returns true if the tank can be filled with a specific type of fluid.
|
||||
* Used as a filter for fluid types.
|
||||
*
|
||||
* Does not consider the current contents or capacity of the tank,
|
||||
* only whether it could ever fill with this type of fluid.
|
||||
* {@link FluidStack} is used here because fluid properties can depend on NBT, the amount is ignored.
|
||||
*/
|
||||
boolean canFillFluidType(FluidStack fluidStack);
|
||||
|
||||
/**
|
||||
* Returns true if the tank can drain out this a specific of fluid.
|
||||
* Used as a filter for fluid types.
|
||||
*
|
||||
* Does not consider the current contents or capacity of the tank,
|
||||
* only whether it could ever drain out this type of fluid.
|
||||
* {@link FluidStack} is used here because fluid properties can depend on NBT, the amount is ignored.
|
||||
*/
|
||||
boolean canDrainFluidType(FluidStack fluidStack);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.minecraftforge.fluids.capability;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.fluids.FluidContainerRegistry;
|
||||
import net.minecraftforge.fluids.IFluidContainerItem;
|
||||
import net.minecraftforge.fluids.capability.templates.FluidHandlerItemStack;
|
||||
|
||||
/**
|
||||
* A simple fluid container, to replace the functionality of {@link FluidContainerRegistry) and {@link IFluidContainerItem}.
|
||||
* This fluid container may be set so that is can only completely filled or empty. (binary)
|
||||
* It may also be set so that it gets consumed when it is drained. (consumable)
|
||||
*/
|
||||
public class ItemFluidContainer extends Item
|
||||
{
|
||||
protected final int capacity;
|
||||
|
||||
/**
|
||||
* @param capacity The maximum capacity of this fluid container.
|
||||
*/
|
||||
public ItemFluidContainer(int capacity)
|
||||
{
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ICapabilityProvider initCapabilities(ItemStack stack, NBTTagCompound nbt)
|
||||
{
|
||||
return new FluidHandlerItemStack(stack, capacity);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package net.minecraftforge.fluids.capability;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
|
||||
public class TileFluidHandler extends TileEntity
|
||||
{
|
||||
protected FluidTank tank = new FluidTank(Fluid.BUCKET_VOLUME);
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound tag)
|
||||
{
|
||||
super.readFromNBT(tag);
|
||||
tank.readFromNBT(tag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NBTTagCompound writeToNBT(NBTTagCompound tag)
|
||||
{
|
||||
tag = super.writeToNBT(tag);
|
||||
tank.writeToNBT(tag);
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
|
||||
{
|
||||
return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
||||
{
|
||||
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
|
||||
return (T) tank;
|
||||
return super.getCapability(capability, facing);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package net.minecraftforge.fluids.capability.templates;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.IFluidTank;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
public class EmptyFluidHandler implements IFluidHandler, IFluidTank
|
||||
{
|
||||
public static final EmptyFluidHandler INSTANCE = new EmptyFluidHandler();
|
||||
public static final FluidTankInfo EMPTY_TANK_INFO = new FluidTankInfo(null, 0);
|
||||
public static final IFluidTankProperties EMPTY_TANK_PROPERTIES = new FluidTankProperties(null, 0, false, false);
|
||||
public static final IFluidTankProperties[] EMPTY_TANK_PROPERTIES_ARRAY = new IFluidTankProperties[] { EMPTY_TANK_PROPERTIES };
|
||||
|
||||
protected EmptyFluidHandler() {}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
return EMPTY_TANK_PROPERTIES_ARRAY;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public FluidStack getFluid()
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getFluidAmount()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCapacity()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidTankInfo getInfo()
|
||||
{
|
||||
return EMPTY_TANK_INFO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package net.minecraftforge.fluids.capability.templates;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* FluidHandlerConcatenate is a template class for concatenating multiple handlers into one.
|
||||
* If each tank is restricted to exactly one type of fluid, then use {@link FluidHandlerFluidMap} as it is more efficient.
|
||||
*/
|
||||
public class FluidHandlerConcatenate implements IFluidHandler
|
||||
{
|
||||
protected final IFluidHandler[] subHandlers;
|
||||
|
||||
public FluidHandlerConcatenate(IFluidHandler... subHandlers)
|
||||
{
|
||||
this.subHandlers = subHandlers;
|
||||
}
|
||||
|
||||
public FluidHandlerConcatenate(Collection<IFluidHandler> subHandlers)
|
||||
{
|
||||
this.subHandlers = subHandlers.toArray(new IFluidHandler[subHandlers.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
List<IFluidTankProperties> tanks = Lists.newArrayList();
|
||||
for (IFluidHandler handler : subHandlers)
|
||||
{
|
||||
Collections.addAll(tanks, handler.getTankProperties());
|
||||
}
|
||||
return tanks.toArray(new IFluidTankProperties[tanks.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
if (resource == null || resource.amount <= 0)
|
||||
return 0;
|
||||
|
||||
resource = resource.copy();
|
||||
|
||||
int totalFillAmount = 0;
|
||||
for (IFluidHandler handler : subHandlers)
|
||||
{
|
||||
int fillAmount = handler.fill(resource, doFill);
|
||||
totalFillAmount += fillAmount;
|
||||
resource.amount -= fillAmount;
|
||||
if (resource.amount <= 0)
|
||||
break;
|
||||
}
|
||||
return totalFillAmount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (resource == null || resource.amount <= 0)
|
||||
return null;
|
||||
|
||||
resource = resource.copy();
|
||||
|
||||
FluidStack totalDrained = null;
|
||||
for (IFluidHandler handler : subHandlers)
|
||||
{
|
||||
FluidStack drain = handler.drain(resource, doDrain);
|
||||
if (drain != null)
|
||||
{
|
||||
if (totalDrained == null)
|
||||
totalDrained = drain;
|
||||
else
|
||||
totalDrained.amount += drain.amount;
|
||||
|
||||
resource.amount -= drain.amount;
|
||||
if (resource.amount <= 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
return totalDrained;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (maxDrain == 0)
|
||||
return null;
|
||||
FluidStack totalDrained = null;
|
||||
for (IFluidHandler handler : subHandlers)
|
||||
{
|
||||
if (totalDrained == null)
|
||||
{
|
||||
totalDrained = handler.drain(maxDrain, doDrain);
|
||||
if (totalDrained != null)
|
||||
{
|
||||
maxDrain -= totalDrained.amount;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FluidStack copy = totalDrained.copy();
|
||||
copy.amount = maxDrain;
|
||||
FluidStack drain = handler.drain(copy, doDrain);
|
||||
if (drain != null)
|
||||
{
|
||||
totalDrained.amount += drain.amount;
|
||||
maxDrain -= drain.amount;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxDrain <= 0)
|
||||
break;
|
||||
}
|
||||
return totalDrained;
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package net.minecraftforge.fluids.capability.templates;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* FluidHandlerFluidMap is a template class for concatenating multiple handlers into one,
|
||||
* where each handler is associated with a different fluid.
|
||||
*/
|
||||
public class FluidHandlerFluidMap implements IFluidHandler
|
||||
{
|
||||
protected final Map<Fluid, IFluidHandler> handlers;
|
||||
|
||||
public FluidHandlerFluidMap()
|
||||
{
|
||||
// LinkedHashMap to ensure iteration order is consistent.
|
||||
this(new LinkedHashMap<Fluid, IFluidHandler>());
|
||||
}
|
||||
|
||||
public FluidHandlerFluidMap(Map<Fluid, IFluidHandler> handlers)
|
||||
{
|
||||
this.handlers = handlers;
|
||||
}
|
||||
|
||||
public FluidHandlerFluidMap addHandler(Fluid fluid, IFluidHandler handler)
|
||||
{
|
||||
handlers.put(fluid, handler);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
List<IFluidTankProperties> tanks = Lists.newArrayList();
|
||||
for (IFluidHandler iFluidHandler : handlers.values())
|
||||
{
|
||||
Collections.addAll(tanks, iFluidHandler.getTankProperties());
|
||||
}
|
||||
return tanks.toArray(new IFluidTankProperties[tanks.size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
if (resource == null)
|
||||
return 0;
|
||||
IFluidHandler handler = handlers.get(resource.getFluid());
|
||||
if (handler == null)
|
||||
return 0;
|
||||
return handler.fill(resource, doFill);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (resource == null)
|
||||
return null;
|
||||
IFluidHandler handler = handlers.get(resource.getFluid());
|
||||
if (handler == null)
|
||||
return null;
|
||||
return handler.drain(resource, doDrain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
for (IFluidHandler handler : handlers.values())
|
||||
{
|
||||
FluidStack drain = handler.drain(maxDrain, doDrain);
|
||||
if (drain != null)
|
||||
return drain;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
package net.minecraftforge.fluids.capability.templates;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.fluids.*;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
|
||||
/**
|
||||
* FluidHandlerItemStack is a template capability provider for ItemStacks.
|
||||
* Data is stored directly in the vanilla NBT, in the same way as the old deprecated {@link ItemFluidContainer}.
|
||||
*
|
||||
* This class allows an itemStack to contain any partial level of fluid up to its capacity, unlike {@link FluidHandlerItemStackSimple}
|
||||
*
|
||||
* Additional examples are provided to enable consumable fluid containers (see {@link Consumable}),
|
||||
* fluid containers with different empty and full items (see {@link SwapEmpty},
|
||||
*/
|
||||
public class FluidHandlerItemStack implements IFluidHandler, ICapabilityProvider
|
||||
{
|
||||
public static final String FLUID_NBT_KEY = "Fluid";
|
||||
|
||||
protected final ItemStack container;
|
||||
protected final int capacity;
|
||||
|
||||
/**
|
||||
* @param container The container itemStack, data is stored on it directly as NBT.
|
||||
* @param capacity The maximum capacity of this fluid tank.
|
||||
*/
|
||||
public FluidHandlerItemStack(ItemStack container, int capacity)
|
||||
{
|
||||
this.container = container;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public FluidStack getFluid()
|
||||
{
|
||||
NBTTagCompound tagCompound = container.getTagCompound();
|
||||
if (tagCompound == null || !tagCompound.hasKey(FLUID_NBT_KEY))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return FluidStack.loadFluidStackFromNBT(tagCompound.getCompoundTag(FLUID_NBT_KEY));
|
||||
}
|
||||
|
||||
protected void setFluid(FluidStack fluid)
|
||||
{
|
||||
if (!container.hasTagCompound())
|
||||
{
|
||||
container.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
|
||||
NBTTagCompound fluidTag = new NBTTagCompound();
|
||||
fluid.writeToNBT(fluidTag);
|
||||
container.getTagCompound().setTag(FLUID_NBT_KEY, fluidTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
return new FluidTankProperties[] { new FluidTankProperties(getFluid(), capacity) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null || resource.amount <= 0 || !canFillFluidType(resource))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
FluidStack contained = getFluid();
|
||||
if (contained == null)
|
||||
{
|
||||
int fillAmount = Math.min(capacity, resource.amount);
|
||||
|
||||
if (doFill)
|
||||
{
|
||||
FluidStack filled = resource.copy();
|
||||
filled.amount = fillAmount;
|
||||
setFluid(filled);
|
||||
}
|
||||
|
||||
return fillAmount;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (contained.isFluidEqual(resource))
|
||||
{
|
||||
int fillAmount = Math.min(capacity - contained.amount, resource.amount);
|
||||
|
||||
if (doFill && fillAmount > 0) {
|
||||
contained.amount += fillAmount;
|
||||
setFluid(contained);
|
||||
}
|
||||
|
||||
return fillAmount;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null || resource.amount <= 0 || !resource.isFluidEqual(getFluid()))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return drain(resource.amount, doDrain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1 || maxDrain <= 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack contained = getFluid();
|
||||
if (contained == null || contained.amount <= 0 || !canDrainFluidType(contained))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
final int drainAmount = Math.min(contained.amount, maxDrain);
|
||||
|
||||
FluidStack drained = contained.copy();
|
||||
drained.amount = drainAmount;
|
||||
|
||||
if (doDrain)
|
||||
{
|
||||
contained.amount -= drainAmount;
|
||||
if (contained.amount == 0)
|
||||
{
|
||||
setContainerToEmpty();
|
||||
}
|
||||
else
|
||||
{
|
||||
setFluid(contained);
|
||||
}
|
||||
}
|
||||
|
||||
return drained;
|
||||
}
|
||||
|
||||
public boolean canFillFluidType(FluidStack fluid)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean canDrainFluidType(FluidStack fluid)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method for special handling.
|
||||
* Can be used to swap out the container's item for a different one with "container.setItem".
|
||||
* Can be used to destroy the container with "container.stackSize--"
|
||||
*/
|
||||
protected void setContainerToEmpty()
|
||||
{
|
||||
container.getTagCompound().removeTag(FLUID_NBT_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
|
||||
{
|
||||
return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
||||
{
|
||||
return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY ? (T) this : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the container item when it's emptied.
|
||||
*/
|
||||
public static class Consumable extends FluidHandlerItemStack
|
||||
{
|
||||
public Consumable(ItemStack container, int capacity)
|
||||
{
|
||||
super(container, capacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setContainerToEmpty()
|
||||
{
|
||||
super.setContainerToEmpty();
|
||||
container.stackSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps the container item for a different one when it's emptied.
|
||||
*/
|
||||
public static class SwapEmpty extends FluidHandlerItemStack
|
||||
{
|
||||
protected final ItemStack emptyContainer;
|
||||
|
||||
public SwapEmpty(ItemStack container, ItemStack emptyContainer, int capacity)
|
||||
{
|
||||
super(container, capacity);
|
||||
this.emptyContainer = emptyContainer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setContainerToEmpty()
|
||||
{
|
||||
super.setContainerToEmpty();
|
||||
container.setItem(emptyContainer.getItem());
|
||||
container.setTagCompound(emptyContainer.getTagCompound());
|
||||
container.setItemDamage(emptyContainer.getItemDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
package net.minecraftforge.fluids.capability.templates;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.ItemFluidContainer;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
/**
|
||||
* FluidHandlerItemStackSimple is a template capability provider for ItemStacks.
|
||||
* Data is stored directly in the vanilla NBT, in the same way as the old deprecated {@link ItemFluidContainer}.
|
||||
*
|
||||
* This implementation only allows item containers to be fully filled or emptied, similar to vanilla buckets.
|
||||
*/
|
||||
public class FluidHandlerItemStackSimple implements IFluidHandler, ICapabilityProvider
|
||||
{
|
||||
public static final String FLUID_NBT_KEY = "Fluid";
|
||||
|
||||
protected final ItemStack container;
|
||||
protected final int capacity;
|
||||
|
||||
/**
|
||||
* @param container The container itemStack, data is stored on it directly as NBT.
|
||||
* @param capacity The maximum capacity of this fluid tank.
|
||||
*/
|
||||
public FluidHandlerItemStackSimple(ItemStack container, int capacity)
|
||||
{
|
||||
this.container = container;
|
||||
this.capacity = capacity;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public FluidStack getFluid()
|
||||
{
|
||||
NBTTagCompound tagCompound = container.getTagCompound();
|
||||
if (tagCompound == null || !tagCompound.hasKey(FLUID_NBT_KEY))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return FluidStack.loadFluidStackFromNBT(tagCompound.getCompoundTag(FLUID_NBT_KEY));
|
||||
}
|
||||
|
||||
protected void setFluid(FluidStack fluid)
|
||||
{
|
||||
if (!container.hasTagCompound())
|
||||
{
|
||||
container.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
|
||||
NBTTagCompound fluidTag = new NBTTagCompound();
|
||||
fluid.writeToNBT(fluidTag);
|
||||
container.getTagCompound().setTag(FLUID_NBT_KEY, fluidTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
return new IFluidTankProperties[] { new FluidTankProperties(getFluid(), capacity) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null || resource.amount <= 0 || !canFillFluidType(resource))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
FluidStack contained = getFluid();
|
||||
if (contained == null)
|
||||
{
|
||||
int fillAmount = Math.min(capacity, resource.amount);
|
||||
if (fillAmount == capacity) {
|
||||
if (doFill) {
|
||||
FluidStack filled = resource.copy();
|
||||
filled.amount = fillAmount;
|
||||
setFluid(filled);
|
||||
}
|
||||
|
||||
return fillAmount;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null || resource.amount <= 0 || !resource.isFluidEqual(getFluid()))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return drain(resource.amount, doDrain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1 || maxDrain <= 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack contained = getFluid();
|
||||
if (contained == null || contained.amount <= 0 || !canDrainFluidType(contained))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
final int drainAmount = Math.min(contained.amount, maxDrain);
|
||||
if (drainAmount == capacity) {
|
||||
FluidStack drained = contained.copy();
|
||||
|
||||
if (doDrain) {
|
||||
setContainerToEmpty();
|
||||
}
|
||||
|
||||
return drained;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean canFillFluidType(FluidStack fluid)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean canDrainFluidType(FluidStack fluid)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method for special handling.
|
||||
* Can be used to swap out the container's item for a different one with "container.setItem".
|
||||
* Can be used to destroy the container with "container.stackSize--"
|
||||
*/
|
||||
protected void setContainerToEmpty()
|
||||
{
|
||||
container.getTagCompound().removeTag(FLUID_NBT_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
|
||||
{
|
||||
return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
||||
{
|
||||
return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY ? (T) this : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the container item when it's emptied.
|
||||
*/
|
||||
public static class Consumable extends FluidHandlerItemStackSimple
|
||||
{
|
||||
public Consumable(ItemStack container, int capacity)
|
||||
{
|
||||
super(container, capacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setContainerToEmpty()
|
||||
{
|
||||
super.setContainerToEmpty();
|
||||
container.stackSize--;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Swaps the container item for a different one when it's emptied.
|
||||
*/
|
||||
public static class SwapEmpty extends FluidHandlerItemStackSimple
|
||||
{
|
||||
protected final ItemStack emptyContainer;
|
||||
|
||||
public SwapEmpty(ItemStack container, ItemStack emptyContainer, int capacity)
|
||||
{
|
||||
super(container, capacity);
|
||||
this.emptyContainer = emptyContainer;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setContainerToEmpty()
|
||||
{
|
||||
super.setContainerToEmpty();
|
||||
container.setItem(emptyContainer.getItem());
|
||||
container.setTagCompound(emptyContainer.getTagCompound());
|
||||
container.setItemDamage(emptyContainer.getItemDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package net.minecraftforge.fluids.capability.wrappers;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.block.BlockLiquid;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumHand;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
/**
|
||||
* Wrapper to handle vanilla Water or Lava as an IFluidHandler.
|
||||
* Methods are modeled after {@link net.minecraft.item.ItemBucket#onItemRightClick(ItemStack, World, EntityPlayer, EnumHand)}
|
||||
*/
|
||||
public class BlockLiquidWrapper implements IFluidHandler
|
||||
{
|
||||
protected final BlockLiquid blockLiquid;
|
||||
protected final World world;
|
||||
protected final BlockPos blockPos;
|
||||
|
||||
public BlockLiquidWrapper(BlockLiquid blockLiquid, World world, BlockPos blockPos)
|
||||
{
|
||||
this.blockLiquid = blockLiquid;
|
||||
this.world = world;
|
||||
this.blockPos = blockPos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
FluidStack containedStack = null;
|
||||
IBlockState blockState = world.getBlockState(blockPos);
|
||||
if (blockState.getBlock() == blockLiquid)
|
||||
{
|
||||
containedStack = getStack(blockState);
|
||||
}
|
||||
return new FluidTankProperties[]{new FluidTankProperties(containedStack, Fluid.BUCKET_VOLUME, false, true)};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (resource == null || resource.amount < Fluid.BUCKET_VOLUME)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
IBlockState blockState = world.getBlockState(blockPos);
|
||||
if (blockState.getBlock() == blockLiquid && blockState.getValue(BlockLiquid.LEVEL) == 0)
|
||||
{
|
||||
FluidStack containedStack = getStack(blockState);
|
||||
if (containedStack != null && resource.containsFluid(containedStack))
|
||||
{
|
||||
if (doDrain)
|
||||
{
|
||||
world.setBlockState(blockPos, Blocks.AIR.getDefaultState(), 11);
|
||||
}
|
||||
return containedStack;
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (maxDrain < Fluid.BUCKET_VOLUME)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
IBlockState blockState = world.getBlockState(blockPos);
|
||||
if (blockState.getBlock() == blockLiquid)
|
||||
{
|
||||
FluidStack containedStack = getStack(blockState);
|
||||
if (containedStack != null && containedStack.amount <= maxDrain)
|
||||
{
|
||||
if (doDrain)
|
||||
{
|
||||
world.setBlockState(blockPos, Blocks.AIR.getDefaultState(), 11);
|
||||
}
|
||||
return containedStack;
|
||||
}
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private FluidStack getStack(IBlockState blockState)
|
||||
{
|
||||
Material material = blockState.getMaterial();
|
||||
if (material == Material.WATER && blockState.getValue(BlockLiquid.LEVEL) == 0)
|
||||
{
|
||||
return new FluidStack(FluidRegistry.WATER, Fluid.BUCKET_VOLUME);
|
||||
}
|
||||
else if (material == Material.LAVA && blockState.getValue(BlockLiquid.LEVEL) == 0)
|
||||
{
|
||||
return new FluidStack(FluidRegistry.LAVA, Fluid.BUCKET_VOLUME);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package net.minecraftforge.fluids.capability.wrappers;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.IFluidBlock;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
/**
|
||||
* Wrapper to handle {@link IFluidBlock} as an IFluidHandler
|
||||
*/
|
||||
public class FluidBlockWrapper implements IFluidHandler
|
||||
{
|
||||
protected final IFluidBlock fluidBlock;
|
||||
protected final World world;
|
||||
protected final BlockPos blockPos;
|
||||
|
||||
public FluidBlockWrapper(IFluidBlock fluidBlock, World world, BlockPos blockPos)
|
||||
{
|
||||
this.fluidBlock = fluidBlock;
|
||||
this.world = world;
|
||||
this.blockPos = blockPos;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
float percentFilled = fluidBlock.getFilledPercentage(world, blockPos);
|
||||
if (percentFilled < 0)
|
||||
{
|
||||
percentFilled *= -1;
|
||||
}
|
||||
int amountFilled = Math.round(Fluid.BUCKET_VOLUME * percentFilled);
|
||||
FluidStack fluid = amountFilled > 0 ? new FluidStack(fluidBlock.getFluid(), amountFilled) : null;
|
||||
return new FluidTankProperties[]{ new FluidTankProperties(fluid, Fluid.BUCKET_VOLUME, false, true)};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (resource == null || !fluidBlock.canDrain(world, blockPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack simulatedDrain = fluidBlock.drain(world, blockPos, false);
|
||||
if (resource.containsFluid(simulatedDrain))
|
||||
{
|
||||
if (doDrain)
|
||||
{
|
||||
return fluidBlock.drain(world, blockPos, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return simulatedDrain;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (maxDrain <= 0 || !fluidBlock.canDrain(world, blockPos))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack simulatedDrain = fluidBlock.drain(world, blockPos, false);
|
||||
if (simulatedDrain != null && simulatedDrain.amount <= maxDrain)
|
||||
{
|
||||
if (doDrain)
|
||||
{
|
||||
return fluidBlock.drain(world, blockPos, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
return simulatedDrain;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
package net.minecraftforge.fluids.capability.wrappers;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.ForgeModContainer;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.UniversalBucket;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
/**
|
||||
* Wrapper for vanilla and forge buckets.
|
||||
* Swaps between empty bucket and filled bucket of the correct type.
|
||||
*/
|
||||
public class FluidBucketWrapper implements IFluidHandler, ICapabilityProvider
|
||||
{
|
||||
protected final ItemStack container;
|
||||
|
||||
public FluidBucketWrapper(ItemStack container)
|
||||
{
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
public boolean canFillFluidType(FluidStack fluid)
|
||||
{
|
||||
if (fluid.getFluid() == FluidRegistry.WATER || fluid.getFluid() == FluidRegistry.LAVA || fluid.getFluid().getName().equals("milk"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return FluidRegistry.isUniversalBucketEnabled() && FluidRegistry.getBucketFluids().contains(fluid.getFluid());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public FluidStack getFluid()
|
||||
{
|
||||
Item item = container.getItem();
|
||||
if (item == Items.WATER_BUCKET)
|
||||
{
|
||||
return new FluidStack(FluidRegistry.WATER, Fluid.BUCKET_VOLUME);
|
||||
}
|
||||
else if (item == Items.LAVA_BUCKET)
|
||||
{
|
||||
return new FluidStack(FluidRegistry.LAVA, Fluid.BUCKET_VOLUME);
|
||||
}
|
||||
else if (item == Items.MILK_BUCKET)
|
||||
{
|
||||
return FluidRegistry.getFluidStack("milk", Fluid.BUCKET_VOLUME);
|
||||
}
|
||||
else if (item == ForgeModContainer.getInstance().universalBucket)
|
||||
{
|
||||
return ForgeModContainer.getInstance().universalBucket.getFluid(container);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
protected void setFluid(Fluid fluid) {
|
||||
if (fluid == null)
|
||||
{
|
||||
container.setItem(Items.BUCKET);
|
||||
container.setTagCompound(null);
|
||||
container.setItemDamage(0);
|
||||
}
|
||||
else if (fluid == FluidRegistry.WATER)
|
||||
{
|
||||
container.setItem(Items.WATER_BUCKET);
|
||||
container.setTagCompound(null);
|
||||
container.setItemDamage(0);
|
||||
}
|
||||
else if (fluid == FluidRegistry.LAVA)
|
||||
{
|
||||
container.setItem(Items.LAVA_BUCKET);
|
||||
container.setTagCompound(null);
|
||||
container.setItemDamage(0);
|
||||
}
|
||||
else if (fluid.getName().equals("milk"))
|
||||
{
|
||||
container.setItem(Items.MILK_BUCKET);
|
||||
container.setTagCompound(null);
|
||||
container.setItemDamage(0);
|
||||
}
|
||||
else if (FluidRegistry.isUniversalBucketEnabled() && FluidRegistry.getBucketFluids().contains(fluid))
|
||||
{
|
||||
ItemStack filledBucket = UniversalBucket.getFilledBucket(ForgeModContainer.getInstance().universalBucket, fluid);
|
||||
container.setItem(filledBucket.getItem());
|
||||
container.setTagCompound(filledBucket.getTagCompound());
|
||||
container.setItemDamage(filledBucket.getItemDamage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
return new FluidTankProperties[] { new FluidTankProperties(getFluid(), Fluid.BUCKET_VOLUME) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null || resource.amount < Fluid.BUCKET_VOLUME || getFluid() != null || !canFillFluidType(resource))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (doFill)
|
||||
{
|
||||
setFluid(resource.getFluid());
|
||||
}
|
||||
|
||||
return Fluid.BUCKET_VOLUME;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null || resource.amount < Fluid.BUCKET_VOLUME)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack fluidStack = getFluid();
|
||||
if (fluidStack != null && fluidStack.isFluidEqual(resource))
|
||||
{
|
||||
if (doDrain)
|
||||
{
|
||||
setFluid(null);
|
||||
}
|
||||
return fluidStack;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1 || maxDrain < Fluid.BUCKET_VOLUME)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack fluidStack = getFluid();
|
||||
if (fluidStack != null)
|
||||
{
|
||||
if (doDrain)
|
||||
{
|
||||
setFluid(null);
|
||||
}
|
||||
return fluidStack;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
|
||||
{
|
||||
return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
||||
{
|
||||
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
|
||||
{
|
||||
return CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.cast(this);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
package net.minecraftforge.fluids.capability.wrappers;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.IFluidContainerItem;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
/**
|
||||
* FluidContainerItemWrapper converts an old {@link IFluidContainerItem} to IFluidHandler.
|
||||
* Note that successful operations WILL modify the container itemStack.
|
||||
* @deprecated will be removed along with {@link IFluidContainerItem}
|
||||
*/
|
||||
@Deprecated
|
||||
public class FluidContainerItemWrapper implements IFluidHandler, ICapabilityProvider
|
||||
{
|
||||
protected final IFluidContainerItem handler;
|
||||
protected final ItemStack container;
|
||||
|
||||
public FluidContainerItemWrapper(IFluidContainerItem handler, ItemStack container)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidTankProperties[] getTankProperties()
|
||||
{
|
||||
return new FluidTankProperties[] { new FluidTankProperties(handler.getFluid(container), handler.getCapacity(container)) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
if (container.stackSize != 1)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return handler.fill(container, resource, doFill);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack canDrain = drain(resource.amount, false);
|
||||
if (canDrain != null)
|
||||
{
|
||||
if (canDrain.isFluidEqual(resource))
|
||||
{
|
||||
return drain(resource.amount, doDrain);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return handler.drain(container, maxDrain, doDrain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCapability(Capability<?> capability, EnumFacing facing)
|
||||
{
|
||||
return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, EnumFacing facing)
|
||||
{
|
||||
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
|
||||
{
|
||||
return CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.cast(this);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package net.minecraftforge.fluids.capability.wrappers;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.capabilities.ICapabilityProvider;
|
||||
import net.minecraftforge.fluids.FluidContainerRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
/**
|
||||
* Wraps a liquid container that uses the {@link FluidContainerRegistry}
|
||||
* Note that successful operations WILL modify the container itemStack.
|
||||
* @deprecated will be removed along with {@link FluidContainerRegistry}
|
||||
*/
|
||||
@Deprecated
|
||||
public class FluidContainerRegistryWrapper implements IFluidHandler, ICapabilityProvider
|
||||
{
|
||||
protected final ItemStack container;
|
||||
|
||||
public FluidContainerRegistryWrapper(ItemStack container)
|
||||
{
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
private void updateContainer(ItemStack newContainerData)
|
||||
{
|
||||
container.setItem(newContainerData.getItem());
|
||||
container.setTagCompound(newContainerData.getTagCompound());
|
||||
container.setItemDamage(newContainerData.getItemDamage());
|
||||
container.stackSize = newContainerData.stackSize;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(container);
|
||||
int capacity = FluidContainerRegistry.getContainerCapacity(fluid, container);
|
||||
return new FluidTankProperties[] { new FluidTankProperties(fluid, capacity) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
FluidStack originalContained = FluidContainerRegistry.getFluidForFilledItem(container);
|
||||
|
||||
ItemStack result = FluidContainerRegistry.fillFluidContainer(resource, container);
|
||||
if (result == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (doFill)
|
||||
{
|
||||
updateContainer(result);
|
||||
}
|
||||
|
||||
FluidStack newContained = FluidContainerRegistry.getFluidForFilledItem(result);
|
||||
|
||||
int originalAmount = originalContained == null ? 0 : originalContained.amount;
|
||||
int newAmount = newContained == null ? 0 : newContained.amount;
|
||||
return newAmount - originalAmount;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1 || resource == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack contained = FluidContainerRegistry.getFluidForFilledItem(container);
|
||||
if (contained != null && contained.isFluidEqual(resource))
|
||||
{
|
||||
return drain(resource.amount, doDrain);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
if (container.stackSize != 1)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
FluidStack contained = FluidContainerRegistry.getFluidForFilledItem(container);
|
||||
if (contained != null)
|
||||
{
|
||||
if (contained.amount <= maxDrain)
|
||||
{
|
||||
ItemStack emptyContainer = FluidContainerRegistry.drainFluidContainer(container);
|
||||
if (emptyContainer != null)
|
||||
{
|
||||
if (doDrain)
|
||||
{
|
||||
if (FluidContainerRegistry.hasNullEmptyContainer(container))
|
||||
{
|
||||
emptyContainer.stackSize = 0;
|
||||
}
|
||||
updateContainer(emptyContainer);
|
||||
}
|
||||
return contained;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing)
|
||||
{
|
||||
return capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing)
|
||||
{
|
||||
if (capability == CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY)
|
||||
{
|
||||
return CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.cast(this);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package net.minecraftforge.fluids.capability.wrappers;
|
||||
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.capability.FluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidTankProperties;
|
||||
import net.minecraftforge.fluids.capability.IFluidHandler;
|
||||
|
||||
/**
|
||||
* FluidHandlerWrapper automatically converts the old {@link net.minecraftforge.fluids.IFluidHandler} to the new version.
|
||||
* @deprecated will be removed along with {@link net.minecraftforge.fluids.IFluidHandler}
|
||||
*/
|
||||
@Deprecated
|
||||
public class FluidHandlerWrapper implements IFluidHandler
|
||||
{
|
||||
protected final net.minecraftforge.fluids.IFluidHandler handler;
|
||||
protected final EnumFacing side;
|
||||
|
||||
public FluidHandlerWrapper(net.minecraftforge.fluids.IFluidHandler handler, EnumFacing side)
|
||||
{
|
||||
this.handler = handler;
|
||||
this.side = side;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IFluidTankProperties[] getTankProperties()
|
||||
{
|
||||
return FluidTankProperties.convert(handler.getTankInfo(side));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill)
|
||||
{
|
||||
if (resource == null || !handler.canFill(side, resource.getFluid()))
|
||||
return 0;
|
||||
return handler.fill(side, resource, doFill);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(FluidStack resource, boolean doDrain)
|
||||
{
|
||||
if (resource == null || !handler.canDrain(side, resource.getFluid()))
|
||||
return null;
|
||||
return handler.drain(side, resource, doDrain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain)
|
||||
{
|
||||
return handler.drain(side, maxDrain, doDrain);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user