From 50ae6db1cf45ef593726f77993d78ee718bf0e69 Mon Sep 17 00:00:00 2001 From: DarkGuardsman Date: Sun, 2 Feb 2014 02:51:55 -0500 Subject: [PATCH] Started on item charging panel --- .../electrical/charger/BlockCharger.java | 23 +++ .../electrical/charger/TileCharger.java | 135 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 src/main/java/resonantinduction/electrical/charger/BlockCharger.java create mode 100644 src/main/java/resonantinduction/electrical/charger/TileCharger.java diff --git a/src/main/java/resonantinduction/electrical/charger/BlockCharger.java b/src/main/java/resonantinduction/electrical/charger/BlockCharger.java new file mode 100644 index 000000000..c0be2a3f3 --- /dev/null +++ b/src/main/java/resonantinduction/electrical/charger/BlockCharger.java @@ -0,0 +1,23 @@ +package resonantinduction.electrical.charger; + +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; +import resonantinduction.core.prefab.block.BlockRI; + +/** Block that is used to charge an item on its surface + * + * @author Darkguardsman */ +public class BlockCharger extends BlockRI +{ + public BlockCharger() + { + super("BlockItemCharger"); + } + + @Override + public TileEntity createNewTileEntity(World world) + { + return new TileCharger(); + } + +} diff --git a/src/main/java/resonantinduction/electrical/charger/TileCharger.java b/src/main/java/resonantinduction/electrical/charger/TileCharger.java new file mode 100644 index 000000000..2647122a0 --- /dev/null +++ b/src/main/java/resonantinduction/electrical/charger/TileCharger.java @@ -0,0 +1,135 @@ +package resonantinduction.electrical.charger; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.ForgeDirection; +import universalelectricity.api.CompatibilityModule; +import universalelectricity.api.energy.IEnergyContainer; +import universalelectricity.api.energy.IEnergyInterface; +import calclavia.lib.prefab.tile.IRotatable; +import calclavia.lib.prefab.tile.TileExternalInventory; +import calclavia.lib.utility.inventory.ExternalInventory; + +/** @author Darkguardsman */ +public class TileCharger extends TileExternalInventory implements IRotatable, IEnergyInterface, IEnergyContainer +{ + private long energyCap = 0; + private long energyStored = 0; + private ChargerMode currentMode = ChargerMode.SINGLE; + + private static enum ChargerMode + { + SINGLE(1), + DUAL(2), + MULTI(4); + public final int limit; + + private ChargerMode(int limit) + { + this.limit = limit; + } + } + + @Override + public void initiate() + { + super.initiate(); + } + + @Override + public void updateEntity() + { + super.updateEntity(); + if (this.ticks % 5 == 0) + { + this.energyCap = 0; + this.energyStored = 0; + for (int i = 0; i < this.getSizeInventory(); i++) + { + this.energyCap += CompatibilityModule.getMaxEnergyItem(this.getStackInSlot(i)); + this.energyStored += CompatibilityModule.getEnergyItem(this.getStackInSlot(i)); + } + } + } + + @Override + public ForgeDirection getDirection() + { + return ForgeDirection.getOrientation(this.worldObj.getBlockMetadata(xCoord, yCoord, zCoord)); + } + + @Override + public void setDirection(ForgeDirection direction) + { + // TODO Auto-generated method stub + + } + + @Override + public boolean canConnect(ForgeDirection direction) + { + return direction == this.getDirection().getOpposite(); + } + + @Override + public long onReceiveEnergy(ForgeDirection from, long receive, boolean doReceive) + { + long energyUsed = 0; + long energyLeft = receive; + for (int i = 0; i < this.getSizeInventory(); i++) + { + long input = CompatibilityModule.chargeItem(this.getStackInSlot(i), energyLeft, true); + energyUsed += input; + energyLeft -= input; + if (energyLeft <= 0) + break; + } + return energyUsed; + } + + @Override + public long onExtractEnergy(ForgeDirection from, long extract, boolean doExtract) + { + return 0; + } + + @Override + public void setEnergy(ForgeDirection from, long energy) + { + } + + @Override + public long getEnergy(ForgeDirection from) + { + if (this.canConnect(from)) + { + return this.energyStored; + } + return 0; + } + + @Override + public long getEnergyCapacity(ForgeDirection from) + { + if (this.canConnect(from)) + { + return this.energyCap; + } + return 0; + } + + @Override + public void readFromNBT(NBTTagCompound nbt) + { + this.currentMode = ChargerMode.values()[nbt.getInteger("chargerMode")]; + this.inventory = new ExternalInventory(this, this.currentMode.limit); + super.readFromNBT(nbt); + } + + @Override + public void writeToNBT(NBTTagCompound nbt) + { + super.writeToNBT(nbt); + nbt.setInteger("chargerMode", this.currentMode.ordinal()); + } + +}