mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-10-26 14:23:57 +00:00
Port Railcraft's Fluid utilities to Forestry.
This commit is contained in:
1 parent
ce1d24952f
commit
2f7ed2386c
7 files changed
+1009
No files matched your search
@@ -0,0 +1,286 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.fluids;
|
||||
|
||||
import forestry.core.fluids.tanks.StandardTank;
|
||||
import forestry.core.inventory.InvTools;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockLiquid;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.fluids.*;
|
||||
import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public final class FluidHelper {
|
||||
|
||||
public static final int BUCKET_FILL_TIME = 8;
|
||||
public static final int NETWORK_UPDATE_INTERVAL = 128;
|
||||
public static final int BUCKET_VOLUME = 1000;
|
||||
|
||||
private FluidHelper() {
|
||||
}
|
||||
|
||||
public static boolean handleRightClick(IFluidHandler tank, ForgeDirection side, EntityPlayer player, boolean fill, boolean drain) {
|
||||
if (player == null)
|
||||
return false;
|
||||
ItemStack current = player.inventory.getCurrentItem();
|
||||
if (current != null) {
|
||||
|
||||
FluidStack liquid = getFluidStackInContainer(current);
|
||||
|
||||
if (fill && liquid != null) {
|
||||
int used = tank.fill(side, liquid, true);
|
||||
|
||||
if (used > 0) {
|
||||
if (!player.capabilities.isCreativeMode) {
|
||||
player.inventory.setInventorySlotContents(player.inventory.currentItem, InvTools.depleteItem(current));
|
||||
player.inventory.markDirty();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} else if (drain) {
|
||||
|
||||
FluidStack available = tank.drain(side, Integer.MAX_VALUE, false);
|
||||
if (available != null) {
|
||||
ItemStack filled = fillContainer(available, current);
|
||||
|
||||
liquid = getFluidStackInContainer(filled);
|
||||
if (liquid != null) {
|
||||
|
||||
if (current.stackSize > 1) {
|
||||
if (!player.inventory.addItemStackToInventory(filled))
|
||||
return false;
|
||||
player.inventory.setInventorySlotContents(player.inventory.currentItem, InvTools.depleteItem(current));
|
||||
player.inventory.markDirty();
|
||||
} else {
|
||||
player.inventory.setInventorySlotContents(player.inventory.currentItem, InvTools.depleteItem(current));
|
||||
player.inventory.setInventorySlotContents(player.inventory.currentItem, filled);
|
||||
player.inventory.markDirty();
|
||||
}
|
||||
|
||||
tank.drain(side, liquid.amount, true);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void processContainers(StandardTank tank, IInventory inv, int inputSlot, int outputSlot) {
|
||||
processContainers(tank, inv, inputSlot, outputSlot, tank.getFluidType(), true, true);
|
||||
}
|
||||
|
||||
public static void processContainers(StandardTank tank, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill, boolean processFilled, boolean processEmpty) {
|
||||
TankManager tankManger = new TankManager();
|
||||
tankManger.add(tank);
|
||||
processContainers(tankManger, inv, inputSlot, outputSlot, fluidToFill, processFilled, processEmpty);
|
||||
}
|
||||
|
||||
public static void processContainers(TankManager tank, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill) {
|
||||
processContainers(tank, inv, inputSlot, outputSlot, fluidToFill, true, true);
|
||||
}
|
||||
|
||||
public static void processContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill, boolean processFilled, boolean processEmpty) {
|
||||
ItemStack input = inv.getStackInSlot(inputSlot);
|
||||
|
||||
if (input == null)
|
||||
return;
|
||||
|
||||
if (processFilled && drainContainers(fluidHandler, inv, inputSlot, outputSlot))
|
||||
return;
|
||||
|
||||
if (processEmpty && fluidToFill != null)
|
||||
fillContainers(fluidHandler, inv, inputSlot, outputSlot, fluidToFill);
|
||||
}
|
||||
|
||||
public static boolean fillContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot, Fluid fluidToFill) {
|
||||
ItemStack input = inv.getStackInSlot(inputSlot);
|
||||
ItemStack output = inv.getStackInSlot(outputSlot);
|
||||
ItemStack filled = getFilledContainer(fluidToFill, input);
|
||||
if (filled != null && (output == null || (output.stackSize < output.getMaxStackSize() && InvTools.isItemEqual(filled, output)))) {
|
||||
FluidStack fluidInContainer = getFluidStackInContainer(filled);
|
||||
FluidStack drain = fluidHandler.drain(ForgeDirection.UNKNOWN, fluidInContainer, false);
|
||||
if (drain != null && drain.amount == fluidInContainer.amount) {
|
||||
fluidHandler.drain(ForgeDirection.UNKNOWN, fluidInContainer, true);
|
||||
if (output == null)
|
||||
inv.setInventorySlotContents(outputSlot, filled);
|
||||
else
|
||||
output.stackSize++;
|
||||
inv.decrStackSize(inputSlot, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean drainContainers(IFluidHandler fluidHandler, IInventory inv, int inputSlot, int outputSlot) {
|
||||
ItemStack input = inv.getStackInSlot(inputSlot);
|
||||
ItemStack output = inv.getStackInSlot(outputSlot);
|
||||
|
||||
if (input != null) {
|
||||
FluidStack fluidInContainer = getFluidStackInContainer(input);
|
||||
ItemStack emptyItem = input.getItem().getContainerItem(input);
|
||||
if (fluidInContainer != null && (emptyItem == null || output == null || (output.stackSize < output.getMaxStackSize() && InvTools.isItemEqual(output, emptyItem)))) {
|
||||
int used = fluidHandler.fill(ForgeDirection.UNKNOWN, fluidInContainer, false);
|
||||
if (used >= fluidInContainer.amount) {
|
||||
fluidHandler.fill(ForgeDirection.UNKNOWN, fluidInContainer, true);
|
||||
if (emptyItem != null)
|
||||
if (output == null)
|
||||
inv.setInventorySlotContents(outputSlot, emptyItem);
|
||||
else
|
||||
output.stackSize++;
|
||||
inv.decrStackSize(inputSlot, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean isBucket(ItemStack stack) {
|
||||
return FluidContainerRegistry.isBucket(stack);
|
||||
}
|
||||
|
||||
public static boolean isContainer(ItemStack stack) {
|
||||
if (stack != null && stack.getItem() instanceof IFluidContainerItem)
|
||||
return true;
|
||||
return FluidContainerRegistry.isContainer(stack);
|
||||
}
|
||||
|
||||
public static boolean isFilledContainer(ItemStack stack) {
|
||||
return FluidContainerRegistry.isFilledContainer(stack);
|
||||
}
|
||||
|
||||
public static boolean isEmptyContainer(ItemStack stack) {
|
||||
return FluidContainerRegistry.isEmptyContainer(stack);
|
||||
}
|
||||
|
||||
public static ItemStack getFilledContainer(Fluid fluid, ItemStack empty) {
|
||||
if (fluid == null || empty == null) return null;
|
||||
return FluidContainerRegistry.fillFluidContainer(new FluidStack(fluid, Integer.MAX_VALUE), empty);
|
||||
}
|
||||
|
||||
public static ItemStack getFilledContainer(FluidStack fluid, ItemStack empty) {
|
||||
if (fluid == null || empty == null) return null;
|
||||
fluid = fluid.copy();
|
||||
fluid.amount = Integer.MAX_VALUE;
|
||||
return FluidContainerRegistry.fillFluidContainer(fluid, empty);
|
||||
}
|
||||
|
||||
public static ItemStack fillContainer(FluidStack liquid, ItemStack empty) {
|
||||
if (liquid == null || empty == null) return null;
|
||||
return FluidContainerRegistry.fillFluidContainer(liquid, empty);
|
||||
}
|
||||
|
||||
public static FluidStack getFluidStackInContainer(ItemStack stack) {
|
||||
return FluidContainerRegistry.getFluidForFilledItem(stack);
|
||||
}
|
||||
|
||||
public static Fluid getFluidInContianer(ItemStack stack) {
|
||||
FluidStack fluidStack = FluidContainerRegistry.getFluidForFilledItem(stack);
|
||||
return fluidStack != null ? fluidStack.getFluid() : null;
|
||||
}
|
||||
|
||||
public static boolean containsFluidStack(ItemStack stack, FluidStack fluidStack) {
|
||||
return FluidContainerRegistry.containsFluid(stack, fluidStack);
|
||||
}
|
||||
|
||||
public static boolean containsFluid(ItemStack stack, Fluid fluid) {
|
||||
return FluidContainerRegistry.containsFluid(stack, new FluidStack(fluid, 1));
|
||||
}
|
||||
|
||||
public static boolean isFluidEqual(FluidStack L1, FluidStack L2) {
|
||||
if (L1 == null || L2 == null)
|
||||
return false;
|
||||
return L1.isFluidEqual(L2);
|
||||
}
|
||||
|
||||
public static Collection<ItemStack> getContainersFilledWith(FluidStack fluidStack) {
|
||||
List<ItemStack> containers = new ArrayList<ItemStack>();
|
||||
for (FluidContainerData data : FluidContainerRegistry.getRegisteredFluidContainerData()) {
|
||||
FluidStack inContainer = getFluidStackInContainer(data.filledContainer);
|
||||
if (inContainer != null && inContainer.containsFluid(fluidStack))
|
||||
containers.add(data.filledContainer.copy());
|
||||
}
|
||||
return containers;
|
||||
}
|
||||
|
||||
public static void nerfWaterBottle() {
|
||||
for (FluidContainerData data : FluidContainerRegistry.getRegisteredFluidContainerData()) {
|
||||
if (data.filledContainer.getItem() == Items.potionitem && data.emptyContainer.getItem() == Items.glass_bottle && Fluids.WATER.is(data.fluid)) {
|
||||
data.fluid.amount = 333;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static FluidStack drainBlock(World world, int x, int y, int z, boolean doDrain) {
|
||||
return drainBlock(world.getBlock(x, y, z), world, x, y, z, doDrain);
|
||||
}
|
||||
|
||||
public static FluidStack drainBlock(Block block, World world, int x, int y, int z, boolean doDrain) {
|
||||
if (block instanceof IFluidBlock) {
|
||||
IFluidBlock fluidBlock = (IFluidBlock)block;
|
||||
if (fluidBlock.canDrain(world, x, y, z))
|
||||
return fluidBlock.drain(world, x, y, z, doDrain);
|
||||
} else if (block == Blocks.water || block == Blocks.flowing_water) {
|
||||
int meta = world.getBlockMetadata(x, y, z);
|
||||
if (meta != 0)
|
||||
return null;
|
||||
if (doDrain)
|
||||
world.setBlockToAir(x, y, z);
|
||||
return new FluidStack(FluidRegistry.WATER, FluidContainerRegistry.BUCKET_VOLUME);
|
||||
} else if (block == Blocks.lava || block == Blocks.flowing_lava) {
|
||||
int meta = world.getBlockMetadata(x, y, z);
|
||||
if (meta != 0)
|
||||
return null;
|
||||
if (doDrain)
|
||||
world.setBlockToAir(x, y, z);
|
||||
return new FluidStack(FluidRegistry.LAVA, FluidContainerRegistry.BUCKET_VOLUME);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isFullFluidBlock(World world, int x, int y, int z) {
|
||||
return isFullFluidBlock(world.getBlock(x, y, z), world, x, y, z);
|
||||
}
|
||||
|
||||
public static boolean isFullFluidBlock(Block block, World world, int x, int y, int z) {
|
||||
if (block instanceof BlockLiquid || block instanceof IFluidBlock)
|
||||
return world.getBlockMetadata(x, y, z) == 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Fluid getFluid(Block block) {
|
||||
if (block instanceof IFluidBlock)
|
||||
return ((IFluidBlock) block).getFluid();
|
||||
else if (block == Blocks.water || block == Blocks.flowing_water)
|
||||
return FluidRegistry.WATER;
|
||||
else if (block == Blocks.lava || block == Blocks.flowing_lava)
|
||||
return FluidRegistry.LAVA;
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.fluids;
|
||||
|
||||
import java.util.Locale;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidContainerRegistry;
|
||||
import net.minecraftforge.fluids.FluidRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public enum Fluids {
|
||||
|
||||
WATER, LAVA, FUEL, BIOFUEL, CREOSOTE, STEAM, BIOETHANOL, COAL, PYROTHEUM;
|
||||
private final String tag;
|
||||
|
||||
private Fluids() {
|
||||
tag = name().toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
|
||||
public String getTag() {
|
||||
return tag;
|
||||
}
|
||||
|
||||
public Fluid get() {
|
||||
return FluidRegistry.getFluid(tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a FluidStack filled with qty milliBuckets worth of Fluid.
|
||||
*
|
||||
* @param qty
|
||||
* @return
|
||||
*/
|
||||
public FluidStack get(int qty) {
|
||||
return FluidRegistry.getFluidStack(tag, qty);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a FluidStack filled with n buckets worth of Fluid.
|
||||
*
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
public FluidStack getB(int n) {
|
||||
return FluidRegistry.getFluidStack(tag, n * FluidContainerRegistry.BUCKET_VOLUME);
|
||||
}
|
||||
|
||||
public boolean is(Fluid fluid) {
|
||||
return get() == fluid;
|
||||
}
|
||||
|
||||
public boolean is(FluidStack fluidStack) {
|
||||
return fluidStack != null && get() == fluidStack.getFluid();
|
||||
}
|
||||
|
||||
public boolean isContained(ItemStack containerStack) {
|
||||
return containerStack != null && FluidHelper.containsFluid(containerStack, get());
|
||||
}
|
||||
|
||||
public static boolean areEqual(Fluid fluid, FluidStack fluidStack) {
|
||||
if (fluidStack != null && fluid == fluidStack.getFluid())
|
||||
return true;
|
||||
return fluid == null && fluidStack == null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.fluids;
|
||||
|
||||
import com.google.common.collect.ForwardingList;
|
||||
import forestry.core.fluids.tanks.StandardTank;
|
||||
import forestry.core.inventory.ITileFilter;
|
||||
import forestry.core.utils.NBTUtil;
|
||||
import forestry.core.utils.NBTUtil.NBTList;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import net.minecraft.inventory.Container;
|
||||
import net.minecraft.inventory.ICrafting;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
import net.minecraftforge.fluids.IFluidHandler;
|
||||
import net.minecraftforge.fluids.IFluidTank;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class TankManager extends ForwardingList<StandardTank> implements IFluidHandler, List<StandardTank> {
|
||||
|
||||
public static final ITileFilter TANK_FILTER = new ITileFilter() {
|
||||
@Override
|
||||
public boolean matches(TileEntity tile) {
|
||||
return tile instanceof IFluidHandler;
|
||||
}
|
||||
|
||||
};
|
||||
private static final byte NETWORK_DATA = 3;
|
||||
private final List<StandardTank> tanks = new ArrayList<StandardTank>();
|
||||
private final List<FluidStack> prevFluidStacks = new ArrayList<FluidStack>();
|
||||
private final List<Integer> prevColor = new ArrayList<Integer>();
|
||||
|
||||
public TankManager() {
|
||||
}
|
||||
|
||||
public TankManager(StandardTank... tanks) {
|
||||
addAll(Arrays.asList(tanks));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<StandardTank> delegate() {
|
||||
return tanks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(StandardTank tank) {
|
||||
boolean added = tanks.add(tank);
|
||||
int index = tanks.indexOf(tank);
|
||||
tank.setTankIndex(index);
|
||||
prevFluidStacks.add(tank.getFluid() == null ? null : tank.getFluid().copy());
|
||||
prevColor.add(tank.getColor());
|
||||
return added;
|
||||
}
|
||||
|
||||
public void writeTanksToNBT(NBTTagCompound data) {
|
||||
NBTTagList tagList = new NBTTagList();
|
||||
for (byte slot = 0; slot < tanks.size(); slot++) {
|
||||
StandardTank tank = tanks.get(slot);
|
||||
if (tank.getFluid() != null) {
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
tag.setByte("tank", slot);
|
||||
tank.writeToNBT(tag);
|
||||
tagList.appendTag(tag);
|
||||
}
|
||||
}
|
||||
data.setTag("tanks", tagList);
|
||||
}
|
||||
|
||||
public void readTanksFromNBT(NBTTagCompound data) {
|
||||
NBTList<NBTTagCompound> tagList = NBTUtil.getNBTList(data, "tanks", NBTUtil.EnumNBTType.COMPOUND);
|
||||
for (NBTTagCompound tag : tagList) {
|
||||
int slot = tag.getByte("tank");
|
||||
if (slot >= 0 && slot < tanks.size())
|
||||
tanks.get(slot).readFromNBT(tag);
|
||||
}
|
||||
}
|
||||
|
||||
public void writePacketData(DataOutputStream data) throws IOException {
|
||||
for (int i = 0; i < tanks.size(); i++)
|
||||
writePacketData(data, i);
|
||||
}
|
||||
|
||||
public void writePacketData(DataOutputStream data, int tankIndex) throws IOException {
|
||||
if (tankIndex >= tanks.size())
|
||||
return;
|
||||
StandardTank tank = tanks.get(tankIndex);
|
||||
FluidStack fluidStack = tank.getFluid();
|
||||
if (fluidStack != null) {
|
||||
data.writeShort(fluidStack.fluidID);
|
||||
data.writeInt(fluidStack.amount);
|
||||
data.writeInt(fluidStack.getFluid().getColor(fluidStack));
|
||||
} else
|
||||
data.writeShort(-1);
|
||||
}
|
||||
|
||||
public void readPacketData(DataInputStream data) throws IOException {
|
||||
for (int i = 0; i < tanks.size(); i++)
|
||||
readPacketData(data, i);
|
||||
}
|
||||
|
||||
public void readPacketData(DataInputStream data, int tankIndex) throws IOException {
|
||||
if (tankIndex >= tanks.size())
|
||||
return;
|
||||
StandardTank tank = tanks.get(tankIndex);
|
||||
int fluidId = data.readShort();
|
||||
if (fluidId != -1) {
|
||||
tank.setFluid(new FluidStack(fluidId, data.readInt()));
|
||||
tank.colorCache = data.readInt();
|
||||
} else
|
||||
tank.setFluid(null);
|
||||
}
|
||||
|
||||
public void initGuiData(Container container, ICrafting player, int tankIndex) {
|
||||
if (tankIndex >= tanks.size())
|
||||
return;
|
||||
FluidStack fluidStack = tanks.get(tankIndex).getFluid();
|
||||
int fluidId = -1;
|
||||
int fluidAmount = 0;
|
||||
if (fluidStack != null && fluidStack.amount > 0) {
|
||||
fluidId = fluidStack.getFluid().getID();
|
||||
fluidAmount = fluidStack.amount;
|
||||
}
|
||||
|
||||
player.sendProgressBarUpdate(container, tankIndex * NETWORK_DATA + 0, fluidId);
|
||||
PacketBuilder.instance().sendGuiIntegerPacket((EntityPlayerMP) player, container.windowId, tankIndex * NETWORK_DATA + 1, fluidAmount);
|
||||
}
|
||||
|
||||
public void updateGuiData(Container container, List crafters, int tankIndex) {
|
||||
StandardTank tank = tanks.get(tankIndex);
|
||||
FluidStack fluidStack = tank.getFluid();
|
||||
FluidStack prev = prevFluidStacks.get(tankIndex);
|
||||
int color = tank.getColor();
|
||||
int pColor = prevColor.get(tankIndex);
|
||||
|
||||
for (Object crafter1 : crafters) {
|
||||
ICrafting crafter = (ICrafting) crafter1;
|
||||
EntityPlayerMP player = (EntityPlayerMP) crafter1;
|
||||
if (fluidStack == null ^ prev == null) {
|
||||
int fluidId = -1;
|
||||
int fluidAmount = 0;
|
||||
if (fluidStack != null) {
|
||||
fluidId = fluidStack.fluidID;
|
||||
fluidAmount = fluidStack.amount;
|
||||
}
|
||||
crafter.sendProgressBarUpdate(container, tankIndex * NETWORK_DATA + 0, fluidId);
|
||||
PacketBuilder.instance().sendGuiIntegerPacket(player, container.windowId, tankIndex * NETWORK_DATA + 1, fluidAmount);
|
||||
} else if (fluidStack != null && prev != null) {
|
||||
if (fluidStack.getFluid() != prev.getFluid())
|
||||
crafter.sendProgressBarUpdate(container, tankIndex * NETWORK_DATA + 0, fluidStack.fluidID);
|
||||
if (fluidStack.amount != prev.amount)
|
||||
PacketBuilder.instance().sendGuiIntegerPacket(player, container.windowId, tankIndex * NETWORK_DATA + 1, fluidStack.amount);
|
||||
if (color != pColor)
|
||||
PacketBuilder.instance().sendGuiIntegerPacket(player, container.windowId, tankIndex * NETWORK_DATA + 2, color);
|
||||
}
|
||||
}
|
||||
|
||||
prevFluidStacks.set(tankIndex, tank.getFluid() == null ? null : tank.getFluid().copy());
|
||||
prevColor.set(tankIndex, color);
|
||||
}
|
||||
|
||||
public void processGuiUpdate(int messageId, int data) {
|
||||
int tankIndex = messageId / NETWORK_DATA;
|
||||
|
||||
if (tankIndex >= tanks.size())
|
||||
return;
|
||||
StandardTank tank = tanks.get(tankIndex);
|
||||
FluidStack fluidStack = tank.getFluid();
|
||||
if (fluidStack == null) {
|
||||
fluidStack = new FluidStack(-1, 0);
|
||||
tank.setFluid(fluidStack);
|
||||
}
|
||||
int fluidId = fluidStack.fluidID;
|
||||
int amount = fluidStack.amount;
|
||||
int color = tank.colorCache;
|
||||
boolean newLiquid = false;
|
||||
switch (messageId % NETWORK_DATA) {
|
||||
case 0:
|
||||
fluidId = data;
|
||||
newLiquid = true;
|
||||
break;
|
||||
case 1:
|
||||
amount = data;
|
||||
break;
|
||||
case 2:
|
||||
color = data;
|
||||
break;
|
||||
}
|
||||
if (newLiquid) {
|
||||
fluidStack = new FluidStack(fluidId, 0);
|
||||
tank.setFluid(fluidStack);
|
||||
}
|
||||
fluidStack.amount = amount;
|
||||
tank.colorCache = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
|
||||
return fill(0, resource, doFill);
|
||||
}
|
||||
|
||||
public int fill(int tankIndex, FluidStack resource, boolean doFill) {
|
||||
if (tankIndex < 0 || tankIndex >= tanks.size() || resource == null)
|
||||
return 0;
|
||||
|
||||
return tanks.get(tankIndex).fill(resource, doFill);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
|
||||
return drain(0, maxDrain, doDrain);
|
||||
}
|
||||
|
||||
public FluidStack drain(int tankIndex, int maxDrain, boolean doDrain) {
|
||||
if (tankIndex < 0 || tankIndex >= tanks.size())
|
||||
return null;
|
||||
|
||||
return tanks.get(tankIndex).drain(maxDrain, doDrain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
|
||||
for (StandardTank tank : tanks)
|
||||
if (tankCanDrainFluid(tank, resource))
|
||||
return tank.drain(resource.amount, doDrain);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canFill(ForgeDirection from, Fluid fluid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canDrain(ForgeDirection from, Fluid fluid) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidTankInfo[] getTankInfo(ForgeDirection direction) {
|
||||
FluidTankInfo[] info = new FluidTankInfo[size()];
|
||||
for (int i = 0; i < size(); i++)
|
||||
info[i] = get(i).getInfo();
|
||||
return info;
|
||||
}
|
||||
|
||||
public FluidTankInfo[] getTankInfo() {
|
||||
return getTankInfo(ForgeDirection.UNKNOWN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StandardTank get(int tankIndex) {
|
||||
if (tankIndex < 0 || tankIndex >= tanks.size())
|
||||
return null;
|
||||
return tanks.get(tankIndex);
|
||||
}
|
||||
|
||||
public void setCapacity(int tankIndex, int capacity) {
|
||||
StandardTank tank = get(tankIndex);
|
||||
tank.setCapacity(capacity);
|
||||
FluidStack fluidStack = tank.getFluid();
|
||||
if (fluidStack != null && fluidStack.amount > capacity)
|
||||
fluidStack.amount = capacity;
|
||||
}
|
||||
|
||||
// Used to push fluids to surrounding blocks, missing code from Railcraft, if needed contact CovertJaguar
|
||||
// public void outputLiquid(AdjacentTileCache cache, ITileFilter filter, ForgeDirection[] sides, int tankIndex, int amount) {
|
||||
// for (ForgeDirection side : sides) {
|
||||
// TileEntity tile = cache.getTileOnSide(side);
|
||||
// if (!filter.matches(tile)) continue;
|
||||
// IFluidHandler tank = getTankFromTile(tile);
|
||||
// if (tank == null) continue;
|
||||
// outputLiquid((IFluidHandler) tile, side, tankIndex, amount);
|
||||
// }
|
||||
// }
|
||||
public void outputLiquid(IFluidHandler[] outputs, int tankIndex, int amount) {
|
||||
for (int side = 0; side < 6; side++) {
|
||||
IFluidHandler nearbyTank = outputs[side];
|
||||
if (nearbyTank != null)
|
||||
outputLiquid(nearbyTank, ForgeDirection.getOrientation(side), tankIndex, amount);
|
||||
}
|
||||
}
|
||||
|
||||
public static IFluidHandler getTankFromTile(TileEntity tile) {
|
||||
IFluidHandler tank = null;
|
||||
if (tile instanceof IFluidHandler)
|
||||
tank = (IFluidHandler) tile;
|
||||
return tank;
|
||||
}
|
||||
|
||||
private void outputLiquid(IFluidHandler tank, ForgeDirection side, int tankIndex, int amount) {
|
||||
FluidStack available = drain(tankIndex, amount, false);
|
||||
if (available != null) {
|
||||
int used = tank.fill(side.getOpposite(), available, true);
|
||||
if (used > 0)
|
||||
drain(tankIndex, used, true);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean tankAcceptsFluid(IFluidTank tank, FluidStack fluidStack) {
|
||||
if (fluidStack == null)
|
||||
return false;
|
||||
if (tank.fill(fluidStack, false) > 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean tankCanDrainFluid(IFluidTank tank, FluidStack fluidStack) {
|
||||
if (fluidStack == null)
|
||||
return false;
|
||||
FluidStack drained = tank.drain(1, false);
|
||||
if (drained != null && drained.amount > 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* This code is the property of CovertJaguar
|
||||
* and may only be used with explicit written
|
||||
* permission unless otherwise specified on the
|
||||
* license page at railcraft.wikispaces.com.
|
||||
*/
|
||||
package mods.railcraft.common.fluids.tanks;
|
||||
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
import net.minecraftforge.fluids.IFluidTank;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public final class FakeTank extends FluidTank {
|
||||
|
||||
public static final IFluidTank INSTANCE = new FakeTank();
|
||||
public static final IFluidTank[] ARRAY = new IFluidTank[]{INSTANCE};
|
||||
public static final FluidTankInfo[] INFO = new FluidTankInfo[]{INSTANCE.getInfo()};
|
||||
|
||||
private FakeTank() {
|
||||
super(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.fluids.tanks;
|
||||
|
||||
import forestry.core.gui.tooltips.ToolTipLine;
|
||||
import net.minecraft.item.EnumRarity;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class FilteredTank extends StandardTank {
|
||||
|
||||
private final Fluid filter;
|
||||
|
||||
public FilteredTank(int capacity, Fluid filter) {
|
||||
this(capacity, filter, null);
|
||||
}
|
||||
|
||||
public FilteredTank(int capacity, Fluid filter, TileEntity tile) {
|
||||
super(capacity, tile);
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(FluidStack resource, boolean doFill) {
|
||||
if (liquidMatchesFilter(resource))
|
||||
return super.fill(resource, doFill);
|
||||
return 0;
|
||||
}
|
||||
|
||||
public Fluid getFilter() {
|
||||
return filter;
|
||||
}
|
||||
|
||||
public boolean liquidMatchesFilter(FluidStack resource) {
|
||||
if (resource == null || filter == null)
|
||||
return false;
|
||||
return resource.getFluid() == filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refreshTooltip() {
|
||||
toolTip.clear();
|
||||
int amount = 0;
|
||||
if (filter != null) {
|
||||
EnumRarity rarity = filter.getRarity();
|
||||
if (rarity == null)
|
||||
rarity = EnumRarity.common;
|
||||
ToolTipLine name = new ToolTipLine(filter.getLocalizedName(), rarity.rarityColor);
|
||||
name.setSpacing(2);
|
||||
toolTip.add(name);
|
||||
if (getFluid() != null)
|
||||
amount = getFluid().amount;
|
||||
}
|
||||
toolTip.add(new ToolTipLine(String.format("%,d", amount) + " / " + String.format("%,d", getCapacity())));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.fluids.tanks;
|
||||
|
||||
import forestry.core.gui.tooltips.ToolTip;
|
||||
import forestry.core.gui.tooltips.ToolTipLine;
|
||||
import java.util.Locale;
|
||||
import net.minecraft.item.EnumRarity;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.fluids.Fluid;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTank;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info>
|
||||
*/
|
||||
public class StandardTank extends FluidTank {
|
||||
|
||||
public static final int DEFAULT_COLOR = 0xFFFFFF;
|
||||
public int colorCache = DEFAULT_COLOR;
|
||||
private int tankIndex;
|
||||
|
||||
public StandardTank(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
public StandardTank(int capacity, TileEntity tile) {
|
||||
this(capacity);
|
||||
this.tile = tile;
|
||||
}
|
||||
|
||||
public void setTankIndex(int index) {
|
||||
this.tankIndex = index;
|
||||
}
|
||||
|
||||
public int getTankIndex() {
|
||||
return tankIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFluid(FluidStack fluid) {
|
||||
super.setFluid(fluid);
|
||||
colorCache = StandardTank.DEFAULT_COLOR;
|
||||
}
|
||||
|
||||
public int getColor() {
|
||||
Fluid f = getFluidType();
|
||||
if (f == null)
|
||||
return DEFAULT_COLOR;
|
||||
return f.getColor(getFluid());
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return getFluid() == null || getFluid().amount <= 0;
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
return getFluid() != null && getFluid().amount == getCapacity();
|
||||
}
|
||||
|
||||
public int getRemainingSpace() {
|
||||
return capacity - getFluidAmount();
|
||||
}
|
||||
|
||||
public Fluid getFluidType() {
|
||||
return getFluid() != null ? getFluid().getFluid() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int fill(final FluidStack resource, final boolean doFill) {
|
||||
if (resource == null)
|
||||
return 0;
|
||||
if (resource.amount <= 0)
|
||||
return 0;
|
||||
return super.fill(resource, doFill);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FluidStack drain(int maxDrain, boolean doDrain) {
|
||||
if (maxDrain <= 0)
|
||||
return null;
|
||||
return super.drain(maxDrain, doDrain);
|
||||
}
|
||||
|
||||
public ToolTip getToolTip() {
|
||||
return toolTip;
|
||||
}
|
||||
|
||||
protected void refreshTooltip() {
|
||||
toolTip.clear();
|
||||
int amount = 0;
|
||||
if (getFluid() != null && getFluid().amount > 0 && getFluid().getFluid() != null) {
|
||||
Fluid fluidType = getFluidType();
|
||||
EnumRarity rarity = fluidType.getRarity();
|
||||
if (rarity == null)
|
||||
rarity = EnumRarity.common;
|
||||
ToolTipLine fluidName = new ToolTipLine(fluidType.getLocalizedName(), rarity.rarityColor);
|
||||
fluidName.setSpacing(2);
|
||||
toolTip.add(fluidName);
|
||||
amount = getFluid().amount;
|
||||
}
|
||||
toolTip.add(new ToolTipLine(String.format(Locale.ENGLISH, "%,d / %,d", amount, getCapacity())));
|
||||
}
|
||||
|
||||
protected final ToolTip toolTip = new ToolTip() {
|
||||
@Override
|
||||
public void refresh() {
|
||||
refreshTooltip();
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2011-2014 SirSengir.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the GNU Lesser Public License v3
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
*
|
||||
* Various Contributors including, but not limited to:
|
||||
* SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges
|
||||
******************************************************************************/
|
||||
package forestry.core.utils;
|
||||
|
||||
import com.google.common.collect.ForwardingList;
|
||||
import cpw.mods.fml.common.ObfuscationReflectionHelper;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import net.minecraft.nbt.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author CovertJaguar <http://www.railcraft.info/>
|
||||
*/
|
||||
public class NBTUtil {
|
||||
|
||||
public enum EnumNBTType {
|
||||
|
||||
END(NBTTagEnd.class),
|
||||
BYTE(NBTTagByte.class),
|
||||
SHORT(NBTTagShort.class),
|
||||
INT(NBTTagInt.class),
|
||||
LONG(NBTTagLong.class),
|
||||
FLOAT(NBTTagFloat.class),
|
||||
DOUBLE(NBTTagDouble.class),
|
||||
BYTE_ARRAY(NBTTagByteArray.class),
|
||||
STRING(NBTTagString.class),
|
||||
LIST(NBTTagList.class),
|
||||
COMPOUND(NBTTagCompound.class),
|
||||
INT_ARRAY(NBTTagIntArray.class);
|
||||
public static final EnumNBTType[] VALUES = values();
|
||||
public final Class<? extends NBTBase> classObject;
|
||||
|
||||
private EnumNBTType(Class<? extends NBTBase> c) {
|
||||
this.classObject = c;
|
||||
}
|
||||
|
||||
public static EnumNBTType fromClass(Class<? extends NBTBase> c) {
|
||||
for (EnumNBTType type : VALUES) {
|
||||
if (type.classObject == c)
|
||||
return type;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static <T extends NBTBase> NBTList<T> getNBTList(NBTTagCompound nbt, String tag, EnumNBTType type) {
|
||||
NBTTagList nbtList = nbt.getTagList(tag, type.ordinal());
|
||||
return new NBTList<T>(nbtList);
|
||||
}
|
||||
|
||||
public static class NBTList<T extends NBTBase> extends ForwardingList<T> {
|
||||
|
||||
private final ArrayList<T> backingList;
|
||||
private final NBTTagList nbtList;
|
||||
|
||||
public NBTList(NBTTagList nbtList) {
|
||||
this.nbtList = nbtList;
|
||||
backingList = ObfuscationReflectionHelper.getPrivateValue(NBTTagList.class, nbtList, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<T> delegate() {
|
||||
return backingList;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user