TheTorchMod/com/bernard/torch/blocks/tileentities/TorchRechargerTileEntity.java

367 lines
12 KiB
Java

package com.bernard.torch.blocks.tileentities;
import com.bernard.torch.api.ITorchRechargable;
import com.bernard.torch.init.CoalStickAndTorchesRegister;
import java.io.PrintStream;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
public class TorchRechargerTileEntity extends TileEntity implements IInventory
{
int torchValue = 0; int stickValue = 0; int coalValue = 0;
public static final int[] maxCoalValueFromMeta = { 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 };
public static final int[] maxStickValueFromMeta = { 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768 };
public static final int[] maxTorchValueFromMeta = { 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536 };
ItemStack stackStored;
ItemStack stackCharging;
public void readFromNBT(NBTTagCompound compound) { super.readFromNBT(compound);
this.torchValue = compound.getInteger("TorchValue");
this.stickValue = compound.getInteger("StickValue");
this.coalValue = compound.getInteger("CoalValue");
if (compound.hasKey("StoredItem", 10)) {
NBTTagCompound itemCompound = compound.getCompoundTag("StoredItem");
this.stackStored = ItemStack.loadItemStackFromNBT(itemCompound);
}
if (compound.hasKey("ChargingItem", 10)) {
NBTTagCompound itemCompound = compound.getCompoundTag("ChargingItem");
this.stackCharging = ItemStack.loadItemStackFromNBT(itemCompound);
}
}
public void writeToNBT(NBTTagCompound compound)
{
super.writeToNBT(compound);
compound.setInteger("TorchValue", this.torchValue);
compound.setInteger("StickValue", this.stickValue);
compound.setInteger("CoalValue", this.coalValue);
if (this.stackStored != null) {
NBTTagCompound itemCompound = new NBTTagCompound();
this.stackStored.writeToNBT(itemCompound);
compound.setTag("StoredItem", itemCompound);
}
if (this.stackCharging != null) {
NBTTagCompound itemCompound = new NBTTagCompound();
this.stackCharging.writeToNBT(itemCompound);
compound.setTag("ChargingItem", itemCompound);
}
}
public Packet getDescriptionPacket()
{
NBTTagCompound compound = new NBTTagCompound();
writeToNBT(compound);
return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 0, compound);
}
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
{
readFromNBT(pkt.getNbtCompound());
}
public boolean canUpdate()
{
return true;
}
public void updateEntity()
{
if (this.blockMetadata == -1)
this.blockMetadata = 0;
if (this.stackStored != null)
updateStackStored();
updateTransfers();
updateCharging();
markDirty();
}
public void updateStackStored() {
try {
if (isItemValidForSlot(0, this.stackStored)) {
if ((this.stickValue < maxStickValueFromMeta[this.blockMetadata]) &&
(CoalStickAndTorchesRegister.isStick(this.stackStored.getItem()))) {
int singleValue = CoalStickAndTorchesRegister.getStickValue(this.stackStored.getItem());
if (singleValue * this.stackStored.stackSize + this.stickValue <= maxStickValueFromMeta[this.blockMetadata]) {
this.stickValue += singleValue * this.stackStored.stackSize;
this.stackStored = null;
} else {
for (int i = 0;
this.stickValue + singleValue <= maxStickValueFromMeta[this.blockMetadata]; i++)
this.stickValue += singleValue;
this.stackStored.stackSize -= i;
if (this.stackStored.stackSize <= 0) {
this.stackStored = null;
}
}
}
if ((this.coalValue < maxCoalValueFromMeta[this.blockMetadata]) &&
(CoalStickAndTorchesRegister.isCoal(this.stackStored.getItem()))) {
int singleValue = CoalStickAndTorchesRegister.getCoalValue(this.stackStored.getItem());
if (singleValue * this.stackStored.stackSize + this.coalValue <= maxCoalValueFromMeta[this.blockMetadata]) {
this.coalValue += singleValue * this.stackStored.stackSize;
this.stackStored = null;
} else {
for (int i = 0;
this.coalValue + singleValue <= maxCoalValueFromMeta[this.blockMetadata]; i++)
this.coalValue += singleValue;
this.stackStored.stackSize -= i;
if (this.stackStored.stackSize <= 0)
this.stackStored = null;
}
}
if ((this.torchValue < maxTorchValueFromMeta[this.blockMetadata]) &&
(CoalStickAndTorchesRegister.isTorch(this.stackStored.getItem()))) {
int singleValue = CoalStickAndTorchesRegister.getTorchValue(this.stackStored.getItem());
if (singleValue * this.stackStored.stackSize + this.torchValue <= maxTorchValueFromMeta[this.blockMetadata]) {
this.torchValue += singleValue * this.stackStored.stackSize;
this.stackStored = null;
} else {
for (int i = 0;
this.torchValue + singleValue <= maxTorchValueFromMeta[this.blockMetadata]; i++)
this.torchValue += singleValue;
this.stackStored.stackSize -= i;
if (this.stackStored.stackSize <= 0)
this.stackStored = null;
}
}
}
} catch (NullPointerException e) {
System.err.println("NullPointer");
}
}
public void updateTransfers() {
if (this.torchValue < maxTorchValueFromMeta[this.blockMetadata]) {
int torchNedded = maxTorchValueFromMeta[this.blockMetadata] - this.torchValue;
if (torchNedded <= Math.min(this.coalValue, this.stickValue)) {
this.torchValue = maxTorchValueFromMeta[this.blockMetadata];
this.coalValue -= torchNedded;
this.stickValue -= torchNedded;
} else {
int torchRecharged = Math.min(this.coalValue, this.stickValue);
this.coalValue -= torchRecharged;
this.stickValue -= torchRecharged;
this.torchValue += torchRecharged;
}
}
}
public void updateCharging() {
if (isItemValidForSlot(1, this.stackCharging)) {
ITorchRechargable torchRechargable = (ITorchRechargable)this.stackCharging.getItem();
int nedded = torchRechargable.getLuxNedded(this.stackCharging);
if (this.torchValue <= nedded) {
torchRechargable.recharge(this.stackCharging, this.torchValue);
this.torchValue = 0;
} else {
this.torchValue -= nedded;
torchRechargable.recharge(this.stackCharging, nedded);
}
}
}
public int getSizeInventory()
{
return 2;
}
public ItemStack getStackInSlot(int slot)
{
switch (slot) {
case 0:
return this.stackStored;
case 1:
return this.stackCharging;
}
return null;
}
public ItemStack decrStackSize(int slot, int amount)
{
switch (slot) {
case 0:
if (this.stackStored == null)
return null;
ItemStack stack; if (amount >= this.stackStored.stackSize) {
ItemStack stack = this.stackStored;
this.stackStored = null;
markDirty();
} else {
stack = this.stackStored.splitStack(amount);
if (this.stackStored.stackSize <= 0)
this.stackStored = null;
markDirty();
}
return stack;
case 1:
if (this.stackCharging == null)
return null;
ItemStack stack; if (amount >= this.stackCharging.stackSize) {
ItemStack stack = this.stackCharging;
this.stackCharging = null;
markDirty();
} else {
stack = this.stackCharging.splitStack(amount);
if (this.stackCharging.stackSize <= 0)
this.stackCharging = null;
markDirty();
}
return stack;
}
return null;
}
public ItemStack getStackInSlotOnClosing(int slot)
{
switch (slot) {
case 0:
ItemStack stack = this.stackStored;
this.stackStored = null;
return stack;
case 1:
ItemStack stack = this.stackCharging;
this.stackCharging = null;
return stack;
}
return null;
}
public void setInventorySlotContents(int slot, ItemStack stack)
{
switch (slot) {
case 0:
this.stackStored = stack;
return;
case 1:
this.stackCharging = stack;
return;
}
}
public String getInventoryName()
{
return "tile.torch_recharger";
}
public boolean isCustomInventoryName()
{
return false;
}
public int getInventoryStackLimit()
{
return 64;
}
public boolean isUseableByPlayer(EntityPlayer player)
{
return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) == this;
}
public void openChest() {}
public void closeChest() {}
public boolean isItemValidForSlot(int slot, ItemStack stack)
{
if (stack == null)
return false;
switch (slot) {
case 0:
return CoalStickAndTorchesRegister.isRegistered(stack.getItem());
case 1:
return stack.getItem() instanceof ITorchRechargable;
}
return false;
}
public int getTorchValue()
{
return this.torchValue;
}
public int getStickValue() {
return this.stickValue;
}
public int getCoalValue() {
return this.coalValue;
}
public int getMaxTorchValue() {
if (this.blockMetadata == -1) {
this.blockMetadata = 0;
}
return maxTorchValueFromMeta[this.blockMetadata];
}
public int getMaxStickValue() {
if (this.blockMetadata == -1) {
this.blockMetadata = 0;
}
return maxStickValueFromMeta[this.blockMetadata];
}
public int getMaxCoalValue() {
if (this.blockMetadata == -1) {
this.blockMetadata = 0;
}
return maxCoalValueFromMeta[this.blockMetadata];
}
public ItemStack getStackStored() {
return this.stackStored;
}
public ItemStack getStackCharging() {
return this.stackCharging;
}
public String toString()
{
return this.torchValue + ";" + this.stickValue + ";" + this.coalValue + "p=" + this.xCoord + ";" + this.yCoord + ";" + this.zCoord;
}
}