TechReborn/src/main/java/techreborn/tiles/TileQuantumTank.java

240 lines
6.9 KiB
Java
Raw Normal View History

2015-04-11 11:37:47 +02:00
package techreborn.tiles;
2015-04-11 18:32:45 +02:00
import ic2.api.tile.IWrenchable;
2015-04-11 11:37:47 +02:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
2015-11-23 15:45:16 +01:00
import net.minecraft.util.EnumFacing;
2015-04-11 12:30:16 +02:00
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
2015-11-08 13:15:45 +01:00
import reborncore.api.IListInfoProvider;
import reborncore.common.util.FluidUtils;
import reborncore.common.util.Inventory;
import reborncore.common.util.Tank;
2015-04-11 19:14:57 +02:00
import techreborn.init.ModBlocks;
2015-04-11 11:37:47 +02:00
import java.util.List;
2015-04-24 15:20:09 +02:00
public class TileQuantumTank extends TileMachineBase implements IFluidHandler,
2015-10-22 19:19:05 +02:00
IInventory, IWrenchable, IListInfoProvider {
public Tank tank = new Tank("TileQuantumTank", Integer.MAX_VALUE, this);
public Inventory inventory = new Inventory(3, "TileQuantumTank", 64);
@Override
public void readFromNBT(NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
readFromNBTWithoutCoords(tagCompound);
}
public void readFromNBTWithoutCoords(NBTTagCompound tagCompound) {
tank.readFromNBT(tagCompound);
inventory.readFromNBT(tagCompound);
}
@Override
public void writeToNBT(NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
writeToNBTWithoutCoords(tagCompound);
}
public void writeToNBTWithoutCoords(NBTTagCompound tagCompound) {
tank.writeToNBT(tagCompound);
inventory.writeToNBT(tagCompound);
}
public Packet getDescriptionPacket() {
NBTTagCompound nbtTag = new NBTTagCompound();
writeToNBT(nbtTag);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord,
this.zCoord, 1, nbtTag);
}
@Override
public void onDataPacket(NetworkManager net,
S35PacketUpdateTileEntity packet) {
worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord,
yCoord, zCoord);
readFromNBT(packet.func_148857_g());
}
2015-04-24 15:20:09 +02:00
@Override
public void updateEntity() {
super.updateEntity();
if (!worldObj.isRemote) {
FluidUtils.drainContainers(this, inventory, 0, 1);
FluidUtils.fillContainers(this, inventory, 0, 1, tank.getFluidType());
if (tank.getFluidType() != null && getStackInSlot(2) == null) {
inventory.setInventorySlotContents(2, new ItemStack(tank
.getFluidType().getBlock()));
} else if (tank.getFluidType() == null && getStackInSlot(2) != null) {
setInventorySlotContents(2, null);
}
2015-11-12 22:38:11 +01:00
tank.compareAndUpdate();
}
}
// IFluidHandler
@Override
2015-11-23 15:45:16 +01:00
public int fill(EnumFacing from, FluidStack resource, boolean doFill) {
2015-11-12 22:38:11 +01:00
int fill = tank.fill(resource, doFill);
tank.compareAndUpdate();
return fill;
}
@Override
2015-11-23 15:45:16 +01:00
public FluidStack drain(EnumFacing from, FluidStack resource,
boolean doDrain) {
2015-11-12 22:38:11 +01:00
FluidStack drain = tank.drain(resource.amount, doDrain);
tank.compareAndUpdate();
return drain;
}
@Override
2015-11-23 15:45:16 +01:00
public FluidStack drain(EnumFacing from, int maxDrain, boolean doDrain) {
2015-11-12 22:38:11 +01:00
FluidStack drain = tank.drain(maxDrain, doDrain);
tank.compareAndUpdate();
return drain;
}
@Override
2015-11-23 15:45:16 +01:00
public boolean canFill(EnumFacing from, Fluid fluid) {
return tank.getFluid() == null || tank.getFluid().getFluid() == fluid;
}
@Override
2015-11-23 15:45:16 +01:00
public boolean canDrain(EnumFacing from, Fluid fluid) {
return tank.getFluid() == null || tank.getFluid().getFluid() == fluid;
}
@Override
2015-11-23 15:45:16 +01:00
public FluidTankInfo[] getTankInfo(EnumFacing from) {
2015-09-10 18:41:59 +02:00
return new FluidTankInfo[]{tank.getInfo()};
}
// IInventory
@Override
public int getSizeInventory() {
return inventory.getSizeInventory();
}
@Override
public ItemStack getStackInSlot(int slot) {
return inventory.getStackInSlot(slot);
}
@Override
public ItemStack decrStackSize(int slotId, int count) {
ItemStack stack = inventory.decrStackSize(slotId, count);
syncWithAll();
return stack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return inventory.getStackInSlotOnClosing(slot);
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack) {
inventory.setInventorySlotContents(slot, stack);
syncWithAll();
}
@Override
public String getInventoryName() {
return inventory.getInventoryName();
}
@Override
public boolean hasCustomInventoryName() {
return inventory.hasCustomInventoryName();
}
@Override
public int getInventoryStackLimit() {
return inventory.getInventoryStackLimit();
}
@Override
public boolean isUseableByPlayer(EntityPlayer player) {
return inventory.isUseableByPlayer(player);
}
@Override
public void openInventory() {
inventory.openInventory();
}
@Override
public void closeInventory() {
inventory.closeInventory();
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
return inventory.isItemValidForSlot(slot, stack);
}
@Override
public boolean wrenchCanSetFacing(EntityPlayer entityPlayer, int side) {
return false;
}
@Override
public short getFacing() {
return 0;
}
@Override
public void setFacing(short facing) {
}
@Override
public boolean wrenchCanRemove(EntityPlayer entityPlayer) {
if (entityPlayer.isSneaking()) {
return true;
}
return false;
}
@Override
public float getWrenchDropRate() {
return 1F;
}
@Override
public ItemStack getWrenchDrop(EntityPlayer entityPlayer) {
return getDropWithNBT();
}
public ItemStack getDropWithNBT() {
NBTTagCompound tileEntity = new NBTTagCompound();
ItemStack dropStack = new ItemStack(ModBlocks.quantumTank, 1);
writeToNBTWithoutCoords(tileEntity);
dropStack.setTagCompound(new NBTTagCompound());
dropStack.stackTagCompound.setTag("tileEntity", tileEntity);
return dropStack;
}
@Override
2015-10-22 19:19:05 +02:00
public void addInfo(List<String> info, boolean isRealTile) {
2015-11-08 13:15:45 +01:00
if (isRealTile) {
2015-10-22 19:19:05 +02:00
if (tank.getFluid() != null) {
info.add(tank.getFluidAmount() + " of "
+ tank.getFluidType().getName());
} else {
info.add("Empty");
}
}
2015-10-22 19:19:05 +02:00
info.add("Capacity " + tank.getCapacity() + " mb");
}
2015-04-11 11:37:47 +02:00
}