mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-25 14:24:35 +00:00
Port Config system, Remove some unused code, Refactored various Tier enums out into their own package
325 lines
9.9 KiB
Java
325 lines
9.9 KiB
Java
package mekanism.common.tile;
|
|
|
|
import io.netty.buffer.ByteBuf;
|
|
import javax.annotation.Nonnull;
|
|
import mekanism.api.Coord4D;
|
|
import mekanism.api.EnumColor;
|
|
import mekanism.api.IConfigCardAccess;
|
|
import mekanism.api.Range4D;
|
|
import mekanism.api.TileNetworkList;
|
|
import mekanism.api.transmitters.TransmissionType;
|
|
import mekanism.common.Mekanism;
|
|
import mekanism.common.SideData;
|
|
import mekanism.common.tier.BaseTier;
|
|
import mekanism.common.tier.EnergyCubeTier;
|
|
import mekanism.common.base.IRedstoneControl;
|
|
import mekanism.common.base.ISideConfiguration;
|
|
import mekanism.common.base.ITierUpgradeable;
|
|
import mekanism.common.capabilities.Capabilities;
|
|
import mekanism.common.integration.computer.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.tile.prefab.TileEntityElectricBlock;
|
|
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.EnumFacing;
|
|
import net.minecraft.util.NonNullList;
|
|
import net.minecraft.util.math.MathHelper;
|
|
import net.minecraftforge.common.capabilities.Capability;
|
|
import net.minecraftforge.fml.common.FMLCommonHandler;
|
|
|
|
public class TileEntityEnergyCube extends TileEntityElectricBlock implements IComputerIntegration, IRedstoneControl,
|
|
ISideConfiguration, ISecurityTile, ITierUpgradeable, IConfigCardAccess {
|
|
|
|
private static final String[] methods = new String[]{"getEnergy", "getOutput", "getMaxEnergy", "getEnergyNeeded"};
|
|
/**
|
|
* 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 = NonNullList.withSize(2, ItemStack.EMPTY);
|
|
controlType = RedstoneControl.DISABLED;
|
|
|
|
ejectorComponent = new TileComponentEjector(this);
|
|
|
|
securityComponent = new TileComponentSecurity(this);
|
|
}
|
|
|
|
@Override
|
|
public void onUpdate() {
|
|
super.onUpdate();
|
|
|
|
if (!world.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 TileNetworkList())),
|
|
new Range4D(Coord4D.get(this)));
|
|
}
|
|
|
|
prevScale = newScale;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean upgrade(BaseTier upgradeTier) {
|
|
if (upgradeTier.ordinal() != tier.ordinal() + 1) {
|
|
return false;
|
|
}
|
|
|
|
tier = EnergyCubeTier.values()[upgradeTier.ordinal()];
|
|
|
|
Mekanism.packetHandler
|
|
.sendToReceivers(new TileEntityMessage(Coord4D.get(this), getNetworkedData(new TileNetworkList())),
|
|
new Range4D(Coord4D.get(this)));
|
|
markDirty();
|
|
|
|
return true;
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public String getName() {
|
|
return LangUtils.localize("tile.EnergyCube" + tier.getBaseTier().getSimpleName() + ".name");
|
|
}
|
|
|
|
@Override
|
|
public double getMaxOutput() {
|
|
if (tier == EnergyCubeTier.CREATIVE) {
|
|
return Integer.MAX_VALUE;
|
|
}
|
|
|
|
return tier.getOutput();
|
|
}
|
|
|
|
@Override
|
|
public boolean isItemValidForSlot(int slotID, @Nonnull ItemStack itemstack) {
|
|
if (slotID == 0) {
|
|
return ChargeUtils.canBeCharged(itemstack);
|
|
} else if (slotID == 1) {
|
|
return ChargeUtils.canBeDischarged(itemstack);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean sideIsConsumer(EnumFacing side) {
|
|
return configComponent.hasSideForData(TransmissionType.ENERGY, facing, 1, side);
|
|
}
|
|
|
|
@Override
|
|
public boolean sideIsOutput(EnumFacing side) {
|
|
return configComponent.hasSideForData(TransmissionType.ENERGY, facing, 2, side);
|
|
}
|
|
|
|
@Override
|
|
public boolean canSetFacing(int side) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public double getMaxEnergy() {
|
|
return tier.getMaxEnergy();
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public int[] getSlotsForFace(@Nonnull EnumFacing side) {
|
|
return configComponent.getOutput(TransmissionType.ITEM, side, facing).availableSlots;
|
|
}
|
|
|
|
@Override
|
|
public boolean canExtractItem(int slotID, @Nonnull ItemStack itemstack, @Nonnull EnumFacing side) {
|
|
if (slotID == 1) {
|
|
return ChargeUtils.canBeOutputted(itemstack, false);
|
|
} else if (slotID == 0) {
|
|
return ChargeUtils.canBeOutputted(itemstack, true);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
@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.getOutput()};
|
|
case 2:
|
|
return new Object[]{getMaxEnergy()};
|
|
case 3:
|
|
return new Object[]{(getMaxEnergy() - getEnergy())};
|
|
default:
|
|
throw new NoSuchMethodException();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void handlePacketData(ByteBuf dataStream) {
|
|
super.handlePacketData(dataStream);
|
|
|
|
if (FMLCommonHandler.instance().getEffectiveSide().isClient()) {
|
|
EnergyCubeTier prevTier = tier;
|
|
|
|
tier = EnergyCubeTier.values()[dataStream.readInt()];
|
|
controlType = RedstoneControl.values()[dataStream.readInt()];
|
|
|
|
if (prevTier != tier) {
|
|
MekanismUtils.updateBlock(world, getPos());
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public TileNetworkList getNetworkedData(TileNetworkList data) {
|
|
super.getNetworkedData(data);
|
|
|
|
data.add(tier.ordinal());
|
|
data.add(controlType.ordinal());
|
|
|
|
return data;
|
|
}
|
|
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound nbtTags) {
|
|
super.readFromNBT(nbtTags);
|
|
|
|
tier = EnergyCubeTier.values()[nbtTags.getInteger("tier")];
|
|
controlType = RedstoneControl.values()[nbtTags.getInteger("controlType")];
|
|
}
|
|
|
|
@Nonnull
|
|
@Override
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbtTags) {
|
|
super.writeToNBT(nbtTags);
|
|
|
|
nbtTags.setInteger("tier", tier.ordinal());
|
|
nbtTags.setInteger("controlType", controlType.ordinal());
|
|
|
|
return nbtTags;
|
|
}
|
|
|
|
@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) (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 EnumFacing getOrientation() {
|
|
return facing;
|
|
}
|
|
|
|
@Override
|
|
public TileComponentSecurity getSecurity() {
|
|
return securityComponent;
|
|
}
|
|
|
|
@Override
|
|
public boolean hasCapability(@Nonnull Capability<?> capability, EnumFacing side) {
|
|
return capability == Capabilities.CONFIG_CARD_CAPABILITY || super.hasCapability(capability, side);
|
|
}
|
|
|
|
@Override
|
|
public <T> T getCapability(@Nonnull Capability<T> capability, EnumFacing side) {
|
|
//Special isCapabilityDisabled override not needed here as it already gets handled in TileEntityElectricBlock
|
|
if (capability == Capabilities.CONFIG_CARD_CAPABILITY) {
|
|
return Capabilities.CONFIG_CARD_CAPABILITY.cast(this);
|
|
}
|
|
|
|
return super.getCapability(capability, side);
|
|
}
|
|
}
|