BloodMagic/1.7.10/main/java/WayofTime/alchemicalWizardry/common/tileEntity/TEAlchemicCalcinator.java

377 lines
9.8 KiB
Java
Raw Normal View History

2014-08-25 11:58:39 +00:00
package WayofTime.alchemicalWizardry.common.tileEntity;
2014-10-13 20:33:20 +00:00
import WayofTime.alchemicalWizardry.api.alchemy.energy.Reagent;
import WayofTime.alchemicalWizardry.api.alchemy.energy.ReagentContainer;
import WayofTime.alchemicalWizardry.api.alchemy.energy.ReagentRegistry;
import WayofTime.alchemicalWizardry.api.alchemy.energy.ReagentStack;
import WayofTime.alchemicalWizardry.api.items.interfaces.IBloodOrb;
import WayofTime.alchemicalWizardry.api.soulNetwork.SoulNetworkHandler;
import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper;
2014-08-25 11:58:39 +00:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection;
public class TEAlchemicCalcinator extends TEReagentConduit implements IInventory
{
2014-10-13 20:33:20 +00:00
protected ItemStack[] inv;
protected ReagentContainer bufferTank = new ReagentContainer(Reagent.REAGENT_SIZE * 2);
protected int bufferTransferRate = 20;
private int lpPerTick = 10;
private int ticksPerReagent = 200;
public int progress;
public TEAlchemicCalcinator()
{
super(1, Reagent.REAGENT_SIZE * 4);
this.inv = new ItemStack[2];
this.tickRate = 20;
this.maxConnextions = 1;
this.progress = 0;
}
@Override
public void readFromNBT(NBTTagCompound tag)
{
super.readFromNBT(tag);
bufferTransferRate = tag.getInteger("bufferTransferRate");
progress = tag.getInteger("progress");
NBTTagCompound bufferTankTag = tag.getCompoundTag("bufferTank");
this.bufferTank = ReagentContainer.readFromNBT(bufferTankTag);
NBTTagList tagList = tag.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
2014-08-25 11:58:39 +00:00
for (int i = 0; i < tagList.tagCount(); i++)
{
NBTTagCompound savedTag = (NBTTagCompound) tagList.getCompoundTagAt(i);
2014-10-13 20:33:20 +00:00
if (savedTag.getBoolean("Empty"))
2014-08-25 11:58:39 +00:00
{
2014-10-13 20:33:20 +00:00
inv[i] = null;
} else
2014-08-25 11:58:39 +00:00
{
inv[i] = ItemStack.loadItemStackFromNBT(savedTag);
}
}
2014-10-13 20:33:20 +00:00
}
@Override
public void writeToNBT(NBTTagCompound tag)
{
super.writeToNBT(tag);
tag.setInteger("bufferTransferRate", bufferTransferRate);
tag.setInteger("progress", progress);
NBTTagCompound bufferTankTag = new NBTTagCompound();
this.bufferTank.writeToNBT(bufferTankTag);
tag.setTag("bufferTank", bufferTankTag);
NBTTagList itemList = new NBTTagList();
2014-08-25 11:58:39 +00:00
for (int i = 0; i < inv.length; i++)
{
ItemStack stack = inv[i];
NBTTagCompound savedTag = new NBTTagCompound();
2014-10-13 20:33:20 +00:00
2014-08-25 11:58:39 +00:00
if (inv[i] != null)
{
inv[i].writeToNBT(savedTag);
2014-10-13 20:33:20 +00:00
} else
2014-08-25 11:58:39 +00:00
{
2014-10-13 20:33:20 +00:00
savedTag.setBoolean("Empty", true);
2014-08-25 11:58:39 +00:00
}
2014-10-13 20:33:20 +00:00
2014-08-25 11:58:39 +00:00
itemList.appendTag(savedTag);
}
tag.setTag("Inventory", itemList);
2014-10-13 20:33:20 +00:00
}
@Override
public void updateEntity()
{
super.updateEntity();
if (!worldObj.isRemote)
{
moveBufferToMain();
tickProgress();
}
}
public void moveBufferToMain()
{
ReagentStack amountStack = this.bufferTank.drain(bufferTransferRate, false);
int drainAmount = this.fill(ForgeDirection.UNKNOWN, amountStack, false);
if (drainAmount > 0)
{
ReagentStack drainedStack = this.bufferTank.drain(drainAmount, true);
this.fill(ForgeDirection.UNKNOWN, drainedStack, true);
}
}
public void tickProgress()
{
ItemStack reagentItemStack = this.getStackInSlot(1);
if (reagentItemStack == null)
{
progress = 0;
return;
}
ReagentStack possibleReagent = ReagentRegistry.getReagentStackForItem(reagentItemStack);
if (possibleReagent == null || !this.canReagentFitBuffer(possibleReagent))
{
return;
}
ItemStack orbStack = this.getStackInSlot(0);
if (orbStack == null || !(orbStack.getItem() instanceof IBloodOrb))
{
return;
}
if (!SoulNetworkHandler.canSyphonFromOnlyNetwork(orbStack, lpPerTick))
{
SoulNetworkHandler.causeNauseaToPlayer(orbStack);
return;
}
SoulNetworkHandler.syphonFromNetwork(orbStack, lpPerTick);
progress++;
if (worldObj.getWorldTime() % 4 == 0)
2014-08-25 11:58:39 +00:00
{
SpellHelper.sendIndexedParticleToAllAround(worldObj, xCoord, yCoord, zCoord, 20, worldObj.provider.dimensionId, 1, xCoord, yCoord, zCoord);
}
2014-10-13 20:33:20 +00:00
if (progress >= this.ticksPerReagent)
{
progress = 0;
this.bufferTank.fill(possibleReagent, true);
this.decrStackSize(1, 1);
}
}
public boolean canReagentFitBuffer(ReagentStack stack)
{
int amount = this.bufferTank.fill(stack, false);
return amount >= stack.amount;
}
@Override
public void readClientNBT(NBTTagCompound tag)
{
super.readClientNBT(tag);
NBTTagList tagList = tag.getTagList("reagentTanks", Constants.NBT.TAG_COMPOUND);
2014-08-25 11:58:39 +00:00
int size = tagList.tagCount();
this.tanks = new ReagentContainer[size];
2014-10-13 20:33:20 +00:00
for (int i = 0; i < size; i++)
2014-08-25 11:58:39 +00:00
{
2014-10-13 20:33:20 +00:00
NBTTagCompound savedTag = tagList.getCompoundTagAt(i);
this.tanks[i] = ReagentContainer.readFromNBT(savedTag);
2014-08-25 11:58:39 +00:00
}
2014-10-13 20:33:20 +00:00
NBTTagList invTagList = tag.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
2014-08-25 11:58:39 +00:00
for (int i = 0; i < invTagList.tagCount(); i++)
{
NBTTagCompound savedTag = (NBTTagCompound) invTagList.getCompoundTagAt(i);
2014-10-13 20:33:20 +00:00
if (savedTag.getBoolean("Empty"))
2014-08-25 11:58:39 +00:00
{
2014-10-13 20:33:20 +00:00
inv[i] = null;
} else
2014-08-25 11:58:39 +00:00
{
inv[i] = ItemStack.loadItemStackFromNBT(savedTag);
}
}
2014-10-13 20:33:20 +00:00
}
@Override
public void writeClientNBT(NBTTagCompound tag)
{
super.writeClientNBT(tag);
NBTTagList tagList = new NBTTagList();
for (int i = 0; i < this.tanks.length; i++)
2014-08-25 11:58:39 +00:00
{
2014-10-13 20:33:20 +00:00
NBTTagCompound savedTag = new NBTTagCompound();
if (this.tanks[i] != null)
{
this.tanks[i].writeToNBT(savedTag);
}
tagList.appendTag(savedTag);
2014-08-25 11:58:39 +00:00
}
2014-10-13 20:33:20 +00:00
2014-08-25 11:58:39 +00:00
tag.setTag("reagentTanks", tagList);
2014-10-13 20:33:20 +00:00
2014-08-25 11:58:39 +00:00
NBTTagList itemList = new NBTTagList();
for (int i = 0; i < inv.length; i++)
{
ItemStack stack = inv[i];
NBTTagCompound savedTag = new NBTTagCompound();
2014-10-13 20:33:20 +00:00
2014-08-25 11:58:39 +00:00
if (inv[i] != null)
{
inv[i].writeToNBT(savedTag);
2014-10-13 20:33:20 +00:00
} else
2014-08-25 11:58:39 +00:00
{
2014-10-13 20:33:20 +00:00
savedTag.setBoolean("Empty", true);
2014-08-25 11:58:39 +00:00
}
2014-10-13 20:33:20 +00:00
2014-08-25 11:58:39 +00:00
itemList.appendTag(savedTag);
}
tag.setTag("Inventory", itemList);
2014-10-13 20:33:20 +00:00
}
@Override
public Packet getDescriptionPacket()
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
writeClientNBT(nbttagcompound);
return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, -999, nbttagcompound);
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet)
{
super.onDataPacket(net, packet);
readClientNBT(packet.func_148857_g());
}
@Override
2014-08-25 11:58:39 +00:00
public int getSizeInventory()
{
return inv.length;
}
@Override
public ItemStack getStackInSlot(int slot)
{
return inv[slot];
}
@Override
public void setInventorySlotContents(int slot, ItemStack stack)
{
inv[slot] = stack;
2014-10-13 20:33:20 +00:00
2014-08-25 11:58:39 +00:00
if (stack != null && stack.stackSize > getInventoryStackLimit())
{
stack.stackSize = getInventoryStackLimit();
}
2014-10-13 20:33:20 +00:00
2014-08-25 11:58:39 +00:00
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
@Override
public ItemStack decrStackSize(int slot, int amt)
{
ItemStack stack = getStackInSlot(slot);
if (stack != null)
{
if (stack.stackSize <= amt)
{
setInventorySlotContents(slot, null);
} else
{
stack = stack.splitStack(amt);
if (stack.stackSize == 0)
{
setInventorySlotContents(slot, null);
}
}
}
return stack;
}
@Override
public ItemStack getStackInSlotOnClosing(int slot)
{
ItemStack stack = getStackInSlot(slot);
if (stack != null)
{
setInventorySlotContents(slot, null);
}
return stack;
}
@Override
public int getInventoryStackLimit()
{
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer player)
{
return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this && player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
}
@Override
public void openInventory()
{
}
@Override
public void closeInventory()
{
}
2014-10-13 20:33:20 +00:00
@Override
public String getInventoryName()
{
return "AlchemicCalcinator";
}
@Override
public boolean hasCustomInventoryName()
{
return false;
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack itemStack)
{
return true;
}
@Override
2014-08-25 11:58:39 +00:00
public int fill(ForgeDirection from, ReagentStack resource, boolean doFill)
2014-10-13 20:33:20 +00:00
{
if (doFill && !worldObj.isRemote)
{
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
return super.fill(from, resource, doFill);
}
2014-08-25 11:58:39 +00:00
}