package mekanism.common.tile; import ic2.api.energy.EnergyNet; import ic2.api.energy.event.EnergyTileLoadEvent; import ic2.api.energy.event.EnergyTileUnloadEvent; import ic2.api.energy.tile.IEnergyConductor; import ic2.api.energy.tile.IEnergySink; import ic2.api.energy.tile.IEnergySource; import ic2.api.energy.tile.IEnergyTile; import ic2.api.tile.IEnergyStorage; import io.netty.buffer.ByteBuf; import java.util.ArrayList; import mekanism.api.Coord4D; import mekanism.api.EnumColor; import mekanism.api.IConfigurable; import mekanism.api.MekanismConfig.general; import mekanism.api.Range4D; import mekanism.api.energy.ICableOutputter; import mekanism.api.energy.IStrictEnergyAcceptor; import mekanism.api.energy.IStrictEnergyStorage; import mekanism.api.transmitters.IGridTransmitter; import mekanism.common.Mekanism; import mekanism.common.network.PacketTileEntity.TileEntityMessage; import mekanism.common.util.LangUtils; import mekanism.common.util.MekanismUtils; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.ChatComponentText; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.ForgeDirection; import cofh.api.energy.IEnergyHandler; import cpw.mods.fml.common.Optional.Interface; import cpw.mods.fml.common.Optional.InterfaceList; import cpw.mods.fml.common.Optional.Method; @InterfaceList({ @Interface(iface = "ic2.api.energy.tile.IEnergySink", modid = "IC2"), @Interface(iface = "ic2.api.energy.tile.IEnergySource", modid = "IC2"), @Interface(iface = "ic2.api.tile.IEnergyStorage", modid = "IC2"), @Interface(iface = "cofh.api.energy.IEnergyHandler", modid = "CoFHAPI|energy"), }) public class TileEntityInductionPort extends TileEntityInductionCasing implements IStrictEnergyStorage, IEnergyHandler, IEnergySink, IEnergySource, IEnergyStorage, IStrictEnergyAcceptor, ICableOutputter, IConfigurable { public boolean ic2Registered = false; /** false = input, true = output */ public boolean mode; public TileEntityInductionPort() { super("InductionPort"); } @Override public void onUpdate() { if(!ic2Registered && MekanismUtils.useIC2()) { register(); } } @Override public boolean canUpdate() { return true; } @Method(modid = "IC2") public void register() { if(!worldObj.isRemote) { TileEntity registered = EnergyNet.instance.getTileEntity(worldObj, xCoord, yCoord, zCoord); if(registered != this) { if(registered instanceof IEnergyTile) { MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)registered)); } else if(registered == null) { MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(this)); ic2Registered = true; } } } } @Method(modid = "IC2") public void deregister() { if(!worldObj.isRemote) { TileEntity registered = EnergyNet.instance.getTileEntity(worldObj, xCoord, yCoord, zCoord); if(registered instanceof IEnergyTile) { MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent((IEnergyTile)registered)); } } } public double getMaxOutput() { return structure != null ? structure.outputCap : 0; } @Override public double getEnergy() { return structure != null ? structure.electricityStored : 0; } @Override public void setEnergy(double energy) { if(structure != null) { structure.electricityStored = Math.max(Math.min(energy, getMaxEnergy()), 0); MekanismUtils.saveChunk(this); } } @Override public double getMaxEnergy() { return structure != null ? structure.storageCap : 0; } @Override public void handlePacketData(ByteBuf dataStream) { super.handlePacketData(dataStream); mode = dataStream.readBoolean(); MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord); } @Override public ArrayList getNetworkedData(ArrayList data) { super.getNetworkedData(data); data.add(mode); return data; } @Override public void onAdded() { super.onAdded(); if(MekanismUtils.useIC2()) { register(); } } @Override public void onChunkUnload() { if(MekanismUtils.useIC2()) { deregister(); } super.onChunkUnload(); } @Override public void invalidate() { super.invalidate(); if(MekanismUtils.useIC2()) { deregister(); } } @Override public void readFromNBT(NBTTagCompound nbtTags) { super.readFromNBT(nbtTags); mode = nbtTags.getBoolean("mode"); } @Override public void writeToNBT(NBTTagCompound nbtTags) { super.writeToNBT(nbtTags); nbtTags.setBoolean("mode", mode); } /** * Gets the scaled energy level for the GUI. * @param i - multiplier * @return scaled energy */ public int getScaledEnergyLevel(int i) { return (int)(getEnergy()*i / getMaxEnergy()); } @Override @Method(modid = "CoFHAPI|energy") public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) { if(structure != null && mode == false) { double toAdd = (int)Math.min(getMaxEnergy()-getEnergy(), maxReceive* general.FROM_TE); if(!simulate) { setEnergy(getEnergy() + toAdd); } return (int)Math.round(toAdd* general.TO_TE); } return 0; } @Override @Method(modid = "CoFHAPI|energy") public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) { if(structure != null && mode == true) { double toSend = Math.min(getEnergy(), Math.min(getMaxOutput(), maxExtract* general.FROM_TE)); if(!simulate) { setEnergy(getEnergy() - toSend); } return (int)Math.round(toSend* general.TO_TE); } return 0; } @Override @Method(modid = "CoFHAPI|energy") public boolean canConnectEnergy(ForgeDirection from) { return structure != null; } @Override @Method(modid = "CoFHAPI|energy") public int getEnergyStored(ForgeDirection from) { return (int)Math.round(getEnergy()* general.TO_TE); } @Override @Method(modid = "CoFHAPI|energy") public int getMaxEnergyStored(ForgeDirection from) { return (int)Math.round(getMaxEnergy()* general.TO_TE); } @Override @Method(modid = "IC2") public int getSinkTier() { return 4; } @Override @Method(modid = "IC2") public int getSourceTier() { return 4; } @Override @Method(modid = "IC2") public void setStored(int energy) { setEnergy(energy*general.FROM_IC2); } @Override @Method(modid = "IC2") public int addEnergy(int amount) { setEnergy(getEnergy() + amount*general.FROM_IC2); return (int)Math.round(getEnergy()*general.TO_IC2); } @Override @Method(modid = "IC2") public boolean isTeleporterCompatible(ForgeDirection side) { return structure != null && mode == true; } @Override public boolean canOutputTo(ForgeDirection side) { return structure != null && mode == true; } @Override @Method(modid = "IC2") public boolean acceptsEnergyFrom(TileEntity emitter, ForgeDirection direction) { return structure != null && mode == false; } @Override @Method(modid = "IC2") public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction) { return structure != null && mode == true && receiver instanceof IEnergyConductor; } @Override @Method(modid = "IC2") public int getStored() { return (int)Math.round(getEnergy()*general.TO_IC2); } @Override @Method(modid = "IC2") public int getCapacity() { return (int)Math.round(getMaxEnergy()*general.TO_IC2); } @Override @Method(modid = "IC2") public int getOutput() { return (int)Math.round(getMaxOutput()*general.TO_IC2); } @Override @Method(modid = "IC2") public double getDemandedEnergy() { return (getMaxEnergy() - getEnergy())*general.TO_IC2; } @Override @Method(modid = "IC2") public double getOfferedEnergy() { return Math.min(getEnergy(), getMaxOutput())*general.TO_IC2; } @Override public boolean canReceiveEnergy(ForgeDirection side) { return structure != null && mode == false; } @Override @Method(modid = "IC2") public double getOutputEnergyUnitsPerTick() { return getMaxOutput()*general.TO_IC2; } @Override @Method(modid = "IC2") public double injectEnergy(ForgeDirection direction, double amount, double voltage) { if(Coord4D.get(this).getFromSide(direction).getTileEntity(worldObj) instanceof IGridTransmitter) { return amount; } return amount-transferEnergyToAcceptor(direction, amount*general.FROM_IC2)*general.TO_IC2; } @Override @Method(modid = "IC2") public void drawEnergy(double amount) { setEnergy(Math.max(getEnergy() - (amount*general.FROM_IC2), 0)); } @Override public double transferEnergyToAcceptor(ForgeDirection side, double amount) { if(structure == null || mode == true) { return 0; } double toUse = Math.min(getMaxEnergy()-getEnergy(), amount); setEnergy(getEnergy() + toUse); return toUse; } @Override public boolean onSneakRightClick(EntityPlayer player, int side) { if(!worldObj.isRemote) { mode = !mode; String modeText = " " + (mode ? EnumColor.DARK_RED : EnumColor.DARK_GREEN) + LangUtils.transOutputInput(mode) + "."; player.addChatMessage(new ChatComponentText(EnumColor.DARK_BLUE + "[Mekanism] " + EnumColor.GREY + MekanismUtils.localize("tooltip.configurator.inductionPortMode") + modeText)); Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(this))); markDirty(); } return true; } @Override public boolean onRightClick(EntityPlayer player, int side) { return false; } }