Mekanism-tilera-Edition/common/mekanism/common/item/ItemBlockGasTank.java

267 lines
6.4 KiB
Java

package mekanism.common.item;
import java.util.List;
import mekanism.api.EnumColor;
import mekanism.api.gas.Gas;
import mekanism.api.gas.GasRegistry;
import mekanism.api.gas.GasStack;
import mekanism.api.gas.IGasItem;
import mekanism.common.ISustainedInventory;
import mekanism.common.Mekanism;
import mekanism.common.tileentity.TileEntityGasTank;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.Icon;
import net.minecraft.world.World;
import org.lwjgl.input.Keyboard;
public class ItemBlockGasTank extends ItemBlock implements IGasItem, ISustainedInventory
{
public Block metaBlock;
/** The maximum amount of gas this tank can hold. */
public int MAX_GAS = 96000;
/** How fast this tank can transfer gas. */
public static final int TRANSFER_RATE = 16;
public ItemBlockGasTank(int id, Block block)
{
super(id);
metaBlock = block;
setHasSubtypes(true);
setMaxStackSize(1);
setMaxDamage(100);
setNoRepair();
setCreativeTab(Mekanism.tabMekanism);
}
@Override
public int getMetadata(int i)
{
return i;
}
@Override
public Icon getIconFromDamage(int i)
{
return metaBlock.getIcon(2, i);
}
@Override
public String getUnlocalizedName(ItemStack itemstack)
{
return getUnlocalizedName() + "." + "GasTank";
}
@Override
public boolean placeBlockAt(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ, int metadata)
{
boolean place = super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata);
if(place)
{
TileEntityGasTank tileEntity = (TileEntityGasTank)world.getBlockTileEntity(x, y, z);
tileEntity.gasTank.setGas(getGas(stack));
((ISustainedInventory)tileEntity).setInventory(getInventory(stack));
}
return place;
}
@Override
public void addInformation(ItemStack itemstack, EntityPlayer entityplayer, List list, boolean flag)
{
GasStack gasStack = getGas(itemstack);
if(gasStack == null)
{
list.add("No gas stored.");
}
else {
list.add("Stored " + gasStack.getGas().getLocalizedName() + ": " + gasStack.amount);
}
if(!Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
{
list.add("Hold " + EnumColor.AQUA + "shift" + EnumColor.GREY + " for more details.");
}
else {
list.add(EnumColor.AQUA + "Inventory: " + EnumColor.GREY + (getInventory(itemstack) != null && getInventory(itemstack).tagCount() != 0));
}
}
@Override
public void onCreated(ItemStack itemstack, World world, EntityPlayer entityplayer)
{
itemstack = getEmptyItem();
}
@Override
public GasStack getGas(ItemStack itemstack)
{
if(itemstack.stackTagCompound == null)
{
return null;
}
GasStack stored = GasStack.readFromNBT(itemstack.stackTagCompound.getCompoundTag("stored"));
if(stored == null)
{
itemstack.setItemDamage(100);
}
else {
itemstack.setItemDamage((int)Math.max(1, (Math.abs((((float)stored.amount/getMaxGas(itemstack))*100)-100))));
}
return stored;
}
@Override
public void setGas(ItemStack itemstack, GasStack stack)
{
if(itemstack.stackTagCompound == null)
{
itemstack.setTagCompound(new NBTTagCompound());
}
if(stack == null || stack.amount == 0)
{
itemstack.setItemDamage(100);
itemstack.stackTagCompound.removeTag("stored");
}
else {
int amount = Math.max(0, Math.min(stack.amount, getMaxGas(itemstack)));
GasStack gasStack = new GasStack(stack.getGas(), amount);
itemstack.setItemDamage((int)Math.max(1, (Math.abs((((float)amount/getMaxGas(itemstack))*100)-100))));
itemstack.stackTagCompound.setCompoundTag("stored", gasStack.write(new NBTTagCompound()));
}
}
public ItemStack getEmptyItem()
{
ItemStack empty = new ItemStack(this);
setGas(empty, null);
empty.setItemDamage(100);
return empty;
}
@Override
public void getSubItems(int i, CreativeTabs tabs, List list)
{
ItemStack empty = new ItemStack(this);
setGas(empty, null);
empty.setItemDamage(100);
list.add(empty);
for(Gas type : GasRegistry.getRegisteredGasses())
{
ItemStack filled = new ItemStack(this);
setGas(filled, new GasStack(type, ((IGasItem)filled.getItem()).getMaxGas(filled)));
list.add(filled);
}
}
@Override
public int getMaxGas(ItemStack itemstack)
{
return MAX_GAS;
}
@Override
public int getRate(ItemStack itemstack)
{
return TRANSFER_RATE;
}
@Override
public int addGas(ItemStack itemstack, GasStack stack)
{
if(getGas(itemstack) != null && getGas(itemstack).getGas() != stack.getGas())
{
return 0;
}
int toUse = Math.min(getMaxGas(itemstack)-getStored(itemstack), Math.min(getRate(itemstack), stack.amount));
setGas(itemstack, new GasStack(stack.getGas(), getStored(itemstack)+toUse));
return toUse;
}
@Override
public GasStack removeGas(ItemStack itemstack, int amount)
{
if(getGas(itemstack) == null)
{
return null;
}
Gas type = getGas(itemstack).getGas();
int gasToUse = Math.min(getStored(itemstack), Math.min(getRate(itemstack), amount));
setGas(itemstack, new GasStack(type, getStored(itemstack)-gasToUse));
return new GasStack(type, gasToUse);
}
private int getStored(ItemStack itemstack)
{
return getGas(itemstack) != null ? getGas(itemstack).amount : 0;
}
@Override
public boolean canReceiveGas(ItemStack itemstack, Gas type)
{
return getGas(itemstack) == null || getGas(itemstack).getGas() == type;
}
@Override
public boolean canProvideGas(ItemStack itemstack, Gas type)
{
return getGas(itemstack) != null && (type == null || getGas(itemstack).getGas() == type);
}
@Override
public void setInventory(NBTTagList nbtTags, Object... data)
{
if(data[0] instanceof ItemStack)
{
ItemStack itemStack = (ItemStack)data[0];
if(itemStack.stackTagCompound == null)
{
itemStack.setTagCompound(new NBTTagCompound());
}
itemStack.stackTagCompound.setTag("Items", nbtTags);
}
}
@Override
public NBTTagList getInventory(Object... data)
{
if(data[0] instanceof ItemStack)
{
ItemStack itemStack = (ItemStack)data[0];
if(itemStack.stackTagCompound == null)
{
return null;
}
return itemStack.stackTagCompound.getTagList("Items");
}
return null;
}
}