mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-22 04:52:25 +00:00
318 lines
8.2 KiB
Java
318 lines
8.2 KiB
Java
package mekanism.common.item;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import mekanism.api.Coord4D;
|
|
import mekanism.api.EnumColor;
|
|
import mekanism.api.Range4D;
|
|
import mekanism.api.gas.Gas;
|
|
import mekanism.api.gas.GasRegistry;
|
|
import mekanism.api.gas.GasStack;
|
|
import mekanism.api.gas.IGasItem;
|
|
import mekanism.client.MekKeyHandler;
|
|
import mekanism.client.MekanismKeyHandler;
|
|
import mekanism.common.Mekanism;
|
|
import mekanism.common.Tier.BaseTier;
|
|
import mekanism.common.Tier.GasTankTier;
|
|
import mekanism.common.base.ISustainedInventory;
|
|
import mekanism.common.base.ITierItem;
|
|
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
|
|
import mekanism.common.tile.TileEntityGasTank;
|
|
import mekanism.common.util.LangUtils;
|
|
import net.minecraft.block.Block;
|
|
import net.minecraft.client.settings.GameSettings;
|
|
import net.minecraft.creativetab.CreativeTabs;
|
|
import net.minecraft.entity.player.EntityPlayer;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemBlock;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.nbt.NBTTagList;
|
|
import net.minecraft.util.IIcon;
|
|
import net.minecraft.world.World;
|
|
import net.minecraftforge.common.util.Constants.NBT;
|
|
|
|
public class ItemBlockGasTank extends ItemBlock implements IGasItem, ISustainedInventory, ITierItem
|
|
{
|
|
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 = 256;
|
|
|
|
public ItemBlockGasTank(Block block)
|
|
{
|
|
super(block);
|
|
metaBlock = block;
|
|
setHasSubtypes(true);
|
|
setMaxStackSize(1);
|
|
setMaxDamage(100);
|
|
setNoRepair();
|
|
setCreativeTab(Mekanism.tabMekanism);
|
|
}
|
|
|
|
@Override
|
|
public int getMetadata(int i)
|
|
{
|
|
return i;
|
|
}
|
|
|
|
@Override
|
|
public IIcon getIconFromDamage(int i)
|
|
{
|
|
return metaBlock.getIcon(2, i);
|
|
}
|
|
|
|
@Override
|
|
public String getItemStackDisplayName(ItemStack itemstack)
|
|
{
|
|
return LangUtils.localize("tile.GasTank" + getBaseTier(itemstack).getName() + ".name");
|
|
}
|
|
|
|
@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.getTileEntity(x, y, z);
|
|
tileEntity.tier = GasTankTier.values()[getBaseTier(stack).ordinal()];
|
|
tileEntity.gasTank.setMaxGas(tileEntity.tier.storage);
|
|
tileEntity.gasTank.setGas(getGas(stack));
|
|
|
|
((ISustainedInventory)tileEntity).setInventory(getInventory(stack));
|
|
|
|
if(!world.isRemote)
|
|
{
|
|
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(tileEntity), tileEntity.getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(tileEntity)));
|
|
}
|
|
}
|
|
|
|
return place;
|
|
}
|
|
|
|
@Override
|
|
public void addInformation(ItemStack itemstack, EntityPlayer entityplayer, List list, boolean flag)
|
|
{
|
|
GasStack gasStack = getGas(itemstack);
|
|
|
|
if(gasStack == null)
|
|
{
|
|
list.add(LangUtils.localize("tooltip.noGas") + ".");
|
|
}
|
|
else {
|
|
list.add(LangUtils.localize("tooltip.stored") + " " + gasStack.getGas().getLocalizedName() + ": " + gasStack.amount);
|
|
}
|
|
|
|
if(!MekKeyHandler.getIsKeyPressed(MekanismKeyHandler.sneakKey))
|
|
{
|
|
list.add(LangUtils.localize("tooltip.hold") + " " + EnumColor.AQUA + GameSettings.getKeyDisplayString(MekanismKeyHandler.sneakKey.getKeyCode()) + EnumColor.GREY + " " + LangUtils.localize("tooltip.forDetails") + ".");
|
|
}
|
|
else {
|
|
list.add(EnumColor.AQUA + LangUtils.localize("tooltip.inventory") + ": " + EnumColor.GREY + LangUtils.transYesNo(getInventory(itemstack) != null && getInventory(itemstack).tagCount() != 0));
|
|
}
|
|
}
|
|
|
|
@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.setTag("stored", gasStack.write(new NBTTagCompound()));
|
|
}
|
|
}
|
|
|
|
public ItemStack getEmptyItem(GasTankTier tier)
|
|
{
|
|
ItemStack empty = new ItemStack(this);
|
|
setBaseTier(empty, tier.getBaseTier());
|
|
setGas(empty, null);
|
|
empty.setItemDamage(100);
|
|
|
|
return empty;
|
|
}
|
|
|
|
@Override
|
|
public void getSubItems(Item item, CreativeTabs tabs, List list)
|
|
{
|
|
for(GasTankTier tier : GasTankTier.values())
|
|
{
|
|
ItemStack empty = new ItemStack(this);
|
|
setBaseTier(empty, tier.getBaseTier());
|
|
setGas(empty, null);
|
|
empty.setItemDamage(100);
|
|
list.add(empty);
|
|
}
|
|
|
|
for(Gas type : GasRegistry.getRegisteredGasses())
|
|
{
|
|
if(type.isVisible())
|
|
{
|
|
ItemStack filled = new ItemStack(this);
|
|
setBaseTier(filled, BaseTier.ULTIMATE);
|
|
setGas(filled, new GasStack(type, ((IGasItem)filled.getItem()).getMaxGas(filled)));
|
|
list.add(filled);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public BaseTier getBaseTier(ItemStack itemstack)
|
|
{
|
|
if(itemstack.stackTagCompound == null)
|
|
{
|
|
return BaseTier.BASIC;
|
|
}
|
|
|
|
return BaseTier.values()[itemstack.stackTagCompound.getInteger("tier")];
|
|
}
|
|
|
|
@Override
|
|
public void setBaseTier(ItemStack itemstack, BaseTier tier)
|
|
{
|
|
if(itemstack.stackTagCompound == null)
|
|
{
|
|
itemstack.setTagCompound(new NBTTagCompound());
|
|
}
|
|
|
|
itemstack.stackTagCompound.setInteger("tier", tier.ordinal());
|
|
}
|
|
|
|
@Override
|
|
public int getMaxGas(ItemStack itemstack)
|
|
{
|
|
return GasTankTier.values()[getBaseTier(itemstack).ordinal()].storage;
|
|
}
|
|
|
|
@Override
|
|
public int getRate(ItemStack itemstack)
|
|
{
|
|
return GasTankTier.values()[getBaseTier(itemstack).ordinal()].output;
|
|
}
|
|
|
|
@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", NBT.TAG_COMPOUND);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
@Override
|
|
public boolean isMetadataSpecific(ItemStack itemStack)
|
|
{
|
|
return false;
|
|
}
|
|
}
|