mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-23 13:29:40 +00:00
317 lines
7.2 KiB
Java
317 lines
7.2 KiB
Java
package mekanism.common.tile;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.EnumSet;
|
|
|
|
import mekanism.api.Coord4D;
|
|
import mekanism.api.EnumColor;
|
|
import mekanism.api.Range4D;
|
|
import mekanism.api.transmitters.TransmissionType;
|
|
import mekanism.common.Mekanism;
|
|
import mekanism.common.SideData;
|
|
import mekanism.common.Tier.EnergyCubeTier;
|
|
import mekanism.common.base.IRedstoneControl;
|
|
import mekanism.common.base.ISideConfiguration;
|
|
import mekanism.common.integration.IComputerIntegration;
|
|
import mekanism.common.network.PacketTileEntity.TileEntityMessage;
|
|
import mekanism.common.security.ISecurityTile;
|
|
import mekanism.common.tile.component.TileComponentConfig;
|
|
import mekanism.common.tile.component.TileComponentEjector;
|
|
import mekanism.common.tile.component.TileComponentSecurity;
|
|
import mekanism.common.util.CableUtils;
|
|
import mekanism.common.util.ChargeUtils;
|
|
import mekanism.common.util.InventoryUtils;
|
|
import mekanism.common.util.LangUtils;
|
|
import mekanism.common.util.MekanismUtils;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.util.MathHelper;
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
|
|
public class TileEntityEnergyCube extends TileEntityElectricBlock implements IComputerIntegration, IRedstoneControl, ISideConfiguration, ISecurityTile
|
|
{
|
|
/** This Energy Cube's tier. */
|
|
public EnergyCubeTier tier = EnergyCubeTier.BASIC;
|
|
|
|
/** The redstone level this Energy Cube is outputting at. */
|
|
public int currentRedstoneLevel;
|
|
|
|
/** This machine's current RedstoneControl type. */
|
|
public RedstoneControl controlType;
|
|
|
|
public int prevScale;
|
|
|
|
public TileComponentEjector ejectorComponent;
|
|
public TileComponentConfig configComponent;
|
|
public TileComponentSecurity securityComponent;
|
|
|
|
/**
|
|
* A block used to store and transfer electricity.
|
|
*/
|
|
public TileEntityEnergyCube()
|
|
{
|
|
super("EnergyCube", 0);
|
|
|
|
configComponent = new TileComponentConfig(this, TransmissionType.ENERGY, TransmissionType.ITEM);
|
|
|
|
configComponent.addOutput(TransmissionType.ITEM, new SideData("None", EnumColor.GREY, InventoryUtils.EMPTY));
|
|
configComponent.addOutput(TransmissionType.ITEM, new SideData("Charge", EnumColor.DARK_BLUE, new int[] {0}));
|
|
configComponent.addOutput(TransmissionType.ITEM, new SideData("Discharge", EnumColor.DARK_RED, new int[] {1}));
|
|
|
|
configComponent.setConfig(TransmissionType.ITEM, new byte[] {0, 0, 0, 0, 2, 1});
|
|
configComponent.setCanEject(TransmissionType.ITEM, false);
|
|
configComponent.setIOConfig(TransmissionType.ENERGY);
|
|
configComponent.setEjecting(TransmissionType.ENERGY, true);
|
|
|
|
inventory = new ItemStack[2];
|
|
controlType = RedstoneControl.DISABLED;
|
|
|
|
ejectorComponent = new TileComponentEjector(this);
|
|
|
|
securityComponent = new TileComponentSecurity(this);
|
|
}
|
|
|
|
@Override
|
|
public void onUpdate()
|
|
{
|
|
super.onUpdate();
|
|
|
|
if(!worldObj.isRemote)
|
|
{
|
|
ChargeUtils.charge(0, this);
|
|
ChargeUtils.discharge(1, this);
|
|
|
|
if(MekanismUtils.canFunction(this) && configComponent.isEjecting(TransmissionType.ENERGY))
|
|
{
|
|
CableUtils.emit(this);
|
|
}
|
|
|
|
int newScale = getScaledEnergyLevel(20);
|
|
|
|
if(newScale != prevScale)
|
|
{
|
|
Mekanism.packetHandler.sendToReceivers(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new ArrayList())), new Range4D(Coord4D.get(this)));
|
|
}
|
|
|
|
prevScale = newScale;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public String getInventoryName()
|
|
{
|
|
return LangUtils.localize("tile.EnergyCube" + tier.getBaseTier().getName() + ".name");
|
|
}
|
|
|
|
@Override
|
|
public double getMaxOutput()
|
|
{
|
|
if(tier == EnergyCubeTier.CREATIVE)
|
|
{
|
|
return Integer.MAX_VALUE;
|
|
}
|
|
|
|
return tier.output;
|
|
}
|
|
|
|
@Override
|
|
public boolean isItemValidForSlot(int slotID, ItemStack itemstack)
|
|
{
|
|
if(slotID == 0)
|
|
{
|
|
return ChargeUtils.canBeCharged(itemstack);
|
|
}
|
|
else if(slotID == 1)
|
|
{
|
|
return ChargeUtils.canBeDischarged(itemstack);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public EnumSet<ForgeDirection> getConsumingSides()
|
|
{
|
|
return configComponent.getSidesForData(TransmissionType.ENERGY, facing, 1);
|
|
}
|
|
|
|
@Override
|
|
public EnumSet<ForgeDirection> getOutputtingSides()
|
|
{
|
|
return configComponent.getSidesForData(TransmissionType.ENERGY, facing, 2);
|
|
}
|
|
|
|
@Override
|
|
public boolean canSetFacing(int side)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public double getMaxEnergy()
|
|
{
|
|
return tier.maxEnergy;
|
|
}
|
|
|
|
@Override
|
|
public int[] getAccessibleSlotsFromSide(int side)
|
|
{
|
|
return configComponent.getOutput(TransmissionType.ITEM, side, facing).availableSlots;
|
|
}
|
|
|
|
@Override
|
|
public boolean canExtractItem(int slotID, ItemStack itemstack, int side)
|
|
{
|
|
if(slotID == 1)
|
|
{
|
|
return ChargeUtils.canBeOutputted(itemstack, false);
|
|
}
|
|
else if(slotID == 0)
|
|
{
|
|
return ChargeUtils.canBeOutputted(itemstack, true);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
private static final String[] methods = new String[] {"getEnergy", "getOutput", "getMaxEnergy", "getEnergyNeeded"};
|
|
|
|
@Override
|
|
public String[] getMethods()
|
|
{
|
|
return methods;
|
|
}
|
|
|
|
@Override
|
|
public Object[] invoke(int method, Object[] arguments) throws Exception
|
|
{
|
|
switch(method)
|
|
{
|
|
case 0:
|
|
return new Object[] {getEnergy()};
|
|
case 1:
|
|
return new Object[] {tier.output};
|
|
case 2:
|
|
return new Object[] {getMaxEnergy()};
|
|
case 3:
|
|
return new Object[] {(getMaxEnergy()-getEnergy())};
|
|
default:
|
|
throw new NoSuchMethodException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void handlePacketData(ByteBuf dataStream)
|
|
{
|
|
if(worldObj.isRemote)
|
|
{
|
|
tier = EnergyCubeTier.values()[dataStream.readInt()];
|
|
|
|
super.handlePacketData(dataStream);
|
|
|
|
controlType = RedstoneControl.values()[dataStream.readInt()];
|
|
|
|
MekanismUtils.updateBlock(worldObj, xCoord, yCoord, zCoord);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ArrayList getNetworkedData(ArrayList data)
|
|
{
|
|
data.add(tier.ordinal());
|
|
|
|
super.getNetworkedData(data);
|
|
|
|
data.add(controlType.ordinal());
|
|
|
|
return data;
|
|
}
|
|
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound nbtTags)
|
|
{
|
|
super.readFromNBT(nbtTags);
|
|
|
|
tier = EnergyCubeTier.getFromName(nbtTags.getString("tier"));
|
|
controlType = RedstoneControl.values()[nbtTags.getInteger("controlType")];
|
|
}
|
|
|
|
@Override
|
|
public void writeToNBT(NBTTagCompound nbtTags)
|
|
{
|
|
super.writeToNBT(nbtTags);
|
|
|
|
nbtTags.setString("tier", tier.getBaseTier().getName());
|
|
nbtTags.setInteger("controlType", controlType.ordinal());
|
|
}
|
|
|
|
@Override
|
|
public void setEnergy(double energy)
|
|
{
|
|
if(tier == EnergyCubeTier.CREATIVE && energy != Double.MAX_VALUE)
|
|
{
|
|
return;
|
|
}
|
|
|
|
super.setEnergy(energy);
|
|
|
|
int newRedstoneLevel = getRedstoneLevel();
|
|
|
|
if(newRedstoneLevel != currentRedstoneLevel)
|
|
{
|
|
markDirty();
|
|
currentRedstoneLevel = newRedstoneLevel;
|
|
}
|
|
}
|
|
|
|
public int getRedstoneLevel()
|
|
{
|
|
double fractionFull = getEnergy()/getMaxEnergy();
|
|
return MathHelper.floor_float((float)(fractionFull * 14.0F)) + (fractionFull > 0 ? 1 : 0);
|
|
}
|
|
|
|
@Override
|
|
public RedstoneControl getControlType()
|
|
{
|
|
return controlType;
|
|
}
|
|
|
|
@Override
|
|
public void setControlType(RedstoneControl type)
|
|
{
|
|
controlType = type;
|
|
}
|
|
|
|
@Override
|
|
public boolean canPulse()
|
|
{
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public TileComponentEjector getEjector()
|
|
{
|
|
return ejectorComponent;
|
|
}
|
|
|
|
@Override
|
|
public TileComponentConfig getConfig()
|
|
{
|
|
return configComponent;
|
|
}
|
|
|
|
@Override
|
|
public int getOrientation()
|
|
{
|
|
return facing;
|
|
}
|
|
|
|
@Override
|
|
public TileComponentSecurity getSecurity()
|
|
{
|
|
return securityComponent;
|
|
}
|
|
}
|