Added ability for item battery to store charge
This commit is contained in:
parent
344e1458fe
commit
c244ee7c17
3 changed files with 140 additions and 12 deletions
|
@ -1,19 +1,52 @@
|
|||
package resonantinduction.battery;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.ForgeDirection;
|
||||
import resonantinduction.levitator.TileEMLevitator;
|
||||
import universalelectricity.api.CompatibilityModule;
|
||||
import universalelectricity.api.UniversalClass;
|
||||
import universalelectricity.api.UniversalElectricity;
|
||||
import universalelectricity.api.energy.UnitDisplay;
|
||||
import universalelectricity.api.energy.UnitDisplay.Unit;
|
||||
import universalelectricity.api.item.IEnergyItem;
|
||||
import universalelectricity.api.item.IVoltageItem;
|
||||
|
||||
public class ItemBlockBattery extends ItemBlock
|
||||
@UniversalClass
|
||||
public class ItemBlockBattery extends ItemBlock implements IEnergyItem, IVoltageItem
|
||||
{
|
||||
public ItemBlockBattery(int id)
|
||||
{
|
||||
super(id);
|
||||
this.setMaxStackSize(1);
|
||||
this.setMaxDamage(100);
|
||||
this.setNoRepair();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInformation(ItemStack itemStack, EntityPlayer entityPlayer, List list, boolean par4)
|
||||
{
|
||||
String color = "";
|
||||
long joules = this.getEnergy(itemStack);
|
||||
|
||||
if (joules <= this.getEnergyCapacity(itemStack) / 3)
|
||||
{
|
||||
color = "\u00a74";
|
||||
}
|
||||
else if (joules > this.getEnergyCapacity(itemStack) * 2 / 3)
|
||||
{
|
||||
color = "\u00a72";
|
||||
}
|
||||
else
|
||||
{
|
||||
color = "\u00a76";
|
||||
}
|
||||
|
||||
list.add("Energy: " + color + UnitDisplay.getDisplayShort(joules, Unit.JOULES) + "/" + UnitDisplay.getDisplayShort(this.getEnergyCapacity(itemStack), Unit.JOULES));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,6 +54,106 @@ public class ItemBlockBattery extends ItemBlock
|
|||
{
|
||||
boolean place = super.placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, metadata);
|
||||
|
||||
if (place)
|
||||
{
|
||||
TileBattery tileEntity = (TileBattery) world.getBlockTileEntity(x, y, z);
|
||||
tileEntity.setEnergy(null, this.getEnergy(stack));
|
||||
}
|
||||
|
||||
return place;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes sure the item is uncharged when it is crafted and not charged. Change this if you do
|
||||
* not want this to happen!
|
||||
*/
|
||||
@Override
|
||||
public void onCreated(ItemStack itemStack, World par2World, EntityPlayer par3EntityPlayer)
|
||||
{
|
||||
this.setEnergy(itemStack, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long recharge(ItemStack itemStack, long energy, boolean doReceive)
|
||||
{
|
||||
long rejectedElectricity = Math.max((this.getEnergy(itemStack) + energy) - this.getEnergyCapacity(itemStack), 0);
|
||||
long energyToReceive = Math.min(energy - rejectedElectricity, getTransferRate());
|
||||
|
||||
if (doReceive)
|
||||
{
|
||||
this.setEnergy(itemStack, this.getEnergy(itemStack) + energyToReceive);
|
||||
}
|
||||
|
||||
return energyToReceive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long discharge(ItemStack itemStack, long energy, boolean doTransfer)
|
||||
{
|
||||
long energyToExtract = Math.min(Math.min(this.getEnergy(itemStack), energy), getTransferRate());
|
||||
|
||||
if (doTransfer)
|
||||
{
|
||||
this.setEnergy(itemStack, this.getEnergy(itemStack) - energyToExtract);
|
||||
}
|
||||
|
||||
return energyToExtract;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getVoltage(ItemStack itemStack)
|
||||
{
|
||||
return UniversalElectricity.DEFAULT_VOLTAGE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEnergy(ItemStack itemStack, long joules)
|
||||
{
|
||||
if (itemStack.getTagCompound() == null)
|
||||
{
|
||||
itemStack.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
|
||||
long electricityStored = Math.max(Math.min(joules, this.getEnergyCapacity(itemStack)), 0);
|
||||
itemStack.getTagCompound().setLong("electricity", electricityStored);
|
||||
itemStack.setItemDamage((int) (100 - ((double) electricityStored / (double) getEnergyCapacity(itemStack)) * 100));
|
||||
}
|
||||
|
||||
public long getTransfer(ItemStack itemStack)
|
||||
{
|
||||
return this.getEnergyCapacity(itemStack) - this.getEnergy(itemStack);
|
||||
}
|
||||
|
||||
/** Gets the energy stored in the item. Energy is stored using item NBT */
|
||||
@Override
|
||||
public long getEnergy(ItemStack itemStack)
|
||||
{
|
||||
if (itemStack.getTagCompound() == null)
|
||||
{
|
||||
itemStack.setTagCompound(new NBTTagCompound());
|
||||
}
|
||||
|
||||
long energyStored = itemStack.getTagCompound().getLong("electricity");
|
||||
itemStack.setItemDamage((int) (100 - ((double) energyStored / (double) getEnergyCapacity(itemStack)) * 100));
|
||||
return energyStored;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getEnergyCapacity(ItemStack theItem)
|
||||
{
|
||||
return TileBattery.STORAGE;
|
||||
}
|
||||
|
||||
public long getTransferRate()
|
||||
{
|
||||
return TileBattery.STORAGE / 200;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
|
||||
{
|
||||
par3List.add(CompatibilityModule.getItemWithCharge(new ItemStack(this), 0));
|
||||
par3List.add(CompatibilityModule.getItemWithCharge(new ItemStack(this), this.getEnergyCapacity(new ItemStack(this))));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,6 +30,8 @@ import cpw.mods.fml.common.network.Player;
|
|||
*/
|
||||
public class TileBattery extends TileEntityElectrical implements IPacketSender, IPacketReceiver
|
||||
{
|
||||
public static final long STORAGE = 10000000;
|
||||
|
||||
public Set<EntityPlayer> playersUsing = new HashSet<EntityPlayer>();
|
||||
|
||||
public SynchronizedBatteryData structure = SynchronizedBatteryData.getBase(this);
|
||||
|
|
|
@ -371,14 +371,7 @@ public class PartMultimeter extends PartAdvanced implements IConnectable, TFaceP
|
|||
{
|
||||
if (pass == 0)
|
||||
{
|
||||
GL11.glDisable(GL11.GL_LIGHTING);
|
||||
TextureUtils.bindAtlas(0);
|
||||
CCRenderState.useModelColours(true);
|
||||
CCRenderState.startDrawing(7);
|
||||
RenderMultimeter.render(this, pos.x, pos.y, pos.z);
|
||||
CCRenderState.draw();
|
||||
CCRenderState.setColour(-1);
|
||||
GL11.glEnable(GL11.GL_LIGHTING);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue