Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.limplungs.limpcraft.tileentities;
- import com.limplungs.limpcraft.Construct;
- import com.limplungs.limpcraft.blocks.machines.CarbonCondenser;
- import com.limplungs.limpcraft.recipes.RecipesCarbonCondenser;
- import cpw.mods.fml.relauncher.Side;
- import cpw.mods.fml.relauncher.SideOnly;
- import net.minecraft.block.Block;
- import net.minecraft.client.Minecraft;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.inventory.ISidedInventory;
- 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.minecraft.tileentity.TileEntity;
- import net.minecraft.world.World;
- public class TileEntityCarbonCondenser extends TileEntity implements ISidedInventory
- {
- private static final int[] slotsAutomation = new int[] { 0, 1, 2, 3 };
- private static String invname = "Carbon Condenser";
- private ItemStack[] itemslots = new ItemStack[9];
- public long speed = 0;
- public int smeltcount = 0;
- public int smeltrequire = 0;
- public long fuelcount = 0;
- public long fuelrequire = 0;
- public long condenserCookTime;
- public long currentItemBurnTime;
- public long condenserBurnTime;
- public int sTop = 0;
- public int sBot = 0;
- public int sLeft = 0;
- public int sRight = 0;
- public int sBack = 0;
- public int bot = 0;
- public int top = 1;
- public int left = 5;
- public int right = 4;
- public int back = 3;
- public int meta = 0;
- @Override
- public int getSizeInventory()
- {
- return this.itemslots.length;
- }
- @Override
- public ItemStack getStackInSlot(int i)
- {
- return itemslots[i];
- }
- @Override
- public ItemStack decrStackSize(int i, int count)
- {
- ItemStack itemstack = getStackInSlot(i);
- if (itemstack != null)
- {
- if (itemstack.stackSize <= count)
- {
- setInventorySlotContents(i, null);
- }
- else
- {
- itemstack = itemstack.splitStack(count);
- markDirty();
- }
- }
- return itemstack;
- }
- @Override
- public ItemStack getStackInSlotOnClosing(int i)
- {
- ItemStack item = getStackInSlot(i);
- setInventorySlotContents(i, null);
- return item;
- }
- @Override
- public void setInventorySlotContents(int i, ItemStack itemstack)
- {
- itemslots[i] = itemstack;
- if (itemstack != null && itemstack.stackSize > getInventoryStackLimit())
- {
- itemstack.stackSize = getInventoryStackLimit();
- }
- markDirty();
- }
- @Override
- public String getInventoryName()
- {
- return invname;
- }
- @Override
- public boolean hasCustomInventoryName()
- {
- return true;
- }
- @Override
- public int getInventoryStackLimit()
- {
- return 64;
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer player)
- {
- return true;
- }
- @Override
- public void openInventory()
- {
- }
- @Override
- public void closeInventory()
- {
- }
- @Override
- public boolean isItemValidForSlot(int par1, ItemStack itemstack)
- {
- if (par1 == 0 && isItemInput(itemstack) == true)
- {
- return true;
- }
- else if (par1 == 1 && isItemFuel(itemstack) == true)
- {
- return true;
- }
- else if (par1 == 2 && itemstack.getItem() == new ItemStack(Blocks.air).getItem())
- {
- return true;
- }
- else if (par1 == 3 && isItemUpgrade(itemstack) == true)
- {
- return true;
- }
- else if (par1 == 4 && isItemCircuit(itemstack) == true || par1 == 5 && isItemCircuit(itemstack) == true || par1 == 6 && isItemCircuit(itemstack) == true || par1 == 7 && isItemCircuit(itemstack) == true || par1 == 8 && isItemCircuit(itemstack) == true)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static boolean isItemCircuit(ItemStack itemstack)
- {
- if (itemstack.getItem() == new ItemStack(Construct.circuitImport).getItem() || itemstack.getItem() == new ItemStack(Construct.circuitExport).getItem())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static boolean isItemUpgrade(ItemStack itemstack)
- {
- if (RecipesCarbonCondenser.getPercentage(itemstack) > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static boolean isItemInput(ItemStack itemstack)
- {
- if (RecipesCarbonCondenser.getInputSmeltTime(itemstack) > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static boolean isItemFuel(ItemStack itemstack)
- {
- if (RecipesCarbonCondenser.getFuelBurnTime(itemstack) > 0)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- @Override
- public int[] getAccessibleSlotsFromSide(int side)
- {
- if (side == top || side == left || side == back || side == right || side == bot)
- {
- return slotsAutomation;
- }
- return null;
- }
- @Override
- public boolean canInsertItem(int slot, ItemStack itemstack, int side)
- {
- if (this.getStackInSlot(4) != null)
- {
- if (this.getStackInSlot(4).getItem() == Construct.circuitImport && side == top && this.isItemValidForSlot(slot, itemstack))
- {
- return true;
- }
- }
- if (this.getStackInSlot(5) != null)
- {
- if (this.getStackInSlot(5).getItem() == Construct.circuitImport && side == left && this.isItemValidForSlot(slot, itemstack))
- {
- return true;
- }
- }
- if (this.getStackInSlot(6) != null)
- {
- if (this.getStackInSlot(6).getItem() == Construct.circuitImport && side == back && this.isItemValidForSlot(slot, itemstack))
- {
- return true;
- }
- }
- if (this.getStackInSlot(7) != null)
- {
- if (this.getStackInSlot(7).getItem() == Construct.circuitImport && side == right && this.isItemValidForSlot(slot, itemstack))
- {
- return true;
- }
- }
- if (this.getStackInSlot(8) != null)
- {
- if (this.getStackInSlot(8).getItem() == Construct.circuitImport && side == bot && this.isItemValidForSlot(slot, itemstack))
- {
- return true;
- }
- }
- return false;
- }
- @Override
- public boolean canExtractItem(int slot, ItemStack itemstack, int side)
- {
- if (slot == 2)
- {
- if (this.getStackInSlot(4) != null)
- {
- if (this.getStackInSlot(4).getItem() == Construct.circuitExport && side == top)
- {
- return true;
- }
- }
- if (this.getStackInSlot(5) != null)
- {
- if (this.getStackInSlot(5).getItem() == Construct.circuitExport && side == left)
- {
- return true;
- }
- }
- if (this.getStackInSlot(6) != null)
- {
- if (this.getStackInSlot(6).getItem() == Construct.circuitExport && side == back)
- {
- return true;
- }
- }
- if (this.getStackInSlot(7) != null)
- {
- if (this.getStackInSlot(7).getItem() == Construct.circuitExport && side == right)
- {
- return true;
- }
- }
- if (this.getStackInSlot(8) != null)
- {
- if (this.getStackInSlot(8).getItem() == Construct.circuitExport && side == bot)
- {
- return true;
- }
- }
- }
- return false;
- }
- @Override
- public void writeToNBT(NBTTagCompound compound)
- {
- updateCircuitTextures(); //Updates Circuit Texture Data before write.
- super.writeToNBT(compound);
- NBTTagList items = new NBTTagList();
- for (int i = 0; i < getSizeInventory(); i++)
- {
- ItemStack stack = this.getStackInSlot(i);
- if (stack != null)
- {
- NBTTagCompound item = new NBTTagCompound();
- item.setByte("Slot", (byte) i);
- stack.writeToNBT(item);
- items.appendTag(item);
- }
- }
- compound.setTag("Items", items);
- compound.setLong("CookTime", this.condenserCookTime);
- compound.setLong("BurnTime", this.condenserBurnTime);
- compound.setLong("CBurnTime", this.currentItemBurnTime);
- compound.setInteger("intleft", this.left);
- compound.setInteger("intright", this.right);
- compound.setInteger("intback", this.back);
- compound.setInteger("inttop", this.top);
- compound.setInteger("intbot", this.bot);
- compound.setInteger("intsLeft", this.sLeft);
- compound.setInteger("intsRight", this.sRight);
- compound.setInteger("intsBack", this.sBack);
- compound.setInteger("intsTop", this.sTop);
- compound.setInteger("intsBot", this.sBot);
- }
- @Override
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- NBTTagList items = compound.getTagList("Items", compound.getId());
- for (int i = 0; i < items.tagCount(); i++)
- {
- NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
- byte slot = item.getByte("Slot");
- if (slot >= 0 && slot < this.getSizeInventory())
- {
- this.itemslots[slot] = ItemStack.loadItemStackFromNBT(item);
- }
- }
- this.condenserCookTime = compound.getInteger("CookTime");
- this.condenserBurnTime = compound.getLong("BurnTime");
- this.currentItemBurnTime = compound.getInteger("CBurnTime");
- this.left = compound.getInteger("intleft");
- this.right = compound.getInteger("intright");
- this.back = compound.getInteger("intback");
- this.top = compound.getInteger("inttop");
- this.bot = compound.getInteger("intbot");
- this.sLeft = compound.getInteger("intsLeft");
- this.sRight = compound.getInteger("intsRight");
- this.sBack = compound.getInteger("intsBack");
- this.sTop = compound.getInteger("intsTop");
- this.sBot = compound.getInteger("intsBot");
- }
- @SideOnly(Side.CLIENT)
- public long getSmeltProgressScaled(int GUILength)
- {
- if (itemslots[0] != null)
- {
- return this.condenserCookTime * GUILength / RecipesCarbonCondenser.getInputSmeltTime(itemslots[0]);
- }
- else
- {
- return 0;
- }
- }
- @SideOnly(Side.CLIENT)
- public long getFuelTimeRemainingScaled(int GUIHeight)
- {
- if (this.currentItemBurnTime == 0)
- {
- this.currentItemBurnTime = RecipesCarbonCondenser.getInputSmeltTime(itemslots[0]);
- }
- if (this.currentItemBurnTime > 0)
- {
- return this.condenserBurnTime * GUIHeight / this.currentItemBurnTime;
- }
- else
- {
- return 0;
- }
- }
- public boolean isBurning()
- {
- return this.condenserBurnTime > 0;
- }
- public void updateEntity()
- {
- boolean flag = this.condenserBurnTime > 0;
- boolean flag1 = false;
- speed = this.getSpeed();
- if (this.condenserBurnTime > 0)
- {
- this.condenserBurnTime = this.condenserBurnTime - speed;
- }
- if (!this.worldObj.isRemote)
- {
- if (this.canSmelt())
- {
- // If it is not burning
- if (!this.isBurning())
- {
- double smelt = RecipesCarbonCondenser.getInputSmeltTime(this.itemslots[0]);
- double burn = RecipesCarbonCondenser.getFuelBurnTime(this.itemslots[1]);
- double ratio = smelt / burn;
- if ((int) Math.ceil(ratio) <= this.itemslots[1].stackSize)
- {
- this.currentItemBurnTime = this.condenserBurnTime = RecipesCarbonCondenser.getFuelBurnTime(this.itemslots[1]) * (int) Math.ceil(ratio);
- if (this.isBurning())
- {
- flag1 = true;
- if (this.itemslots[1] != null)
- {
- this.setInventorySlotContents(1, new ItemStack(this.itemslots[1].getItem(), this.itemslots[1].stackSize - (int) Math.ceil(ratio), this.itemslots[1].getItemDamage()));
- if (this.itemslots[1].stackSize == 0)
- {
- this.itemslots[1] = this.itemslots[1].getItem().getContainerItem(this.itemslots[1]);
- }
- }
- }
- }
- }
- // If it is burning
- if (this.isBurning())
- {
- this.condenserCookTime = this.condenserCookTime + speed;
- if (this.condenserCookTime >= RecipesCarbonCondenser.getInputSmeltTime(itemslots[0]))
- {
- this.condenserCookTime = 0;
- this.smeltItem();
- flag1 = true;
- }
- }
- else
- {
- this.condenserCookTime = 0;
- }
- }
- // while burning, update block to look burning.
- if (flag != this.condenserBurnTime > 0)
- {
- flag1 = true;
- CarbonCondenser.updateBlockState(this.condenserBurnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
- }
- if (this.isBurning() && this.getStackInSlot(0) == null)
- {
- this.condenserCookTime = 0;
- }
- }
- if (this.getStackInSlot(0) != null && this.getStackInSlot(1) == null && this.isBurning() == false)
- {
- this.condenserCookTime = 0;
- }
- if (flag1)
- {
- this.markDirty();
- }
- }
- private long getSpeed()
- {
- if (this.getStackInSlot(3) != null)
- {
- double temp = 125 + (this.getStackInSlot(3).stackSize * new Double(125.0 * RecipesCarbonCondenser.getPercentage(this.getStackInSlot(3)) / 100));
- return (long) Math.ceil(temp);
- }
- else
- {
- return 125;
- }
- }
- private void smeltItem()
- {
- ItemStack input = this.getStackInSlot(0);
- ItemStack output = this.getStackInSlot(2);
- if (this.canSmelt())
- {
- // Input
- int in = input.stackSize - RecipesCarbonCondenser.InputSize(input);
- if (in > 0)
- {
- this.setInventorySlotContents(0, new ItemStack(input.getItem(), in, input.getItemDamage()));
- }
- else
- {
- this.setInventorySlotContents(0, null);
- }
- // Output
- if (output != null)
- {
- this.setInventorySlotContents(2, new ItemStack(output.getItem(), output.stackSize + RecipesCarbonCondenser.OutputSize(input), RecipesCarbonCondenser.Output(input).getItemDamage()));
- }
- else
- {
- this.setInventorySlotContents(2, new ItemStack(RecipesCarbonCondenser.Output(input).getItem(), RecipesCarbonCondenser.OutputSize(input), RecipesCarbonCondenser.Output(input).getItemDamage()));
- }
- markDirty();
- }
- }
- private boolean canSmelt()
- {
- ItemStack input = this.getStackInSlot(0);
- ItemStack output = this.getStackInSlot(2);
- if (input != null && this.getStackInSlot(1) != null)
- {
- smeltcount = input.stackSize;
- smeltrequire = RecipesCarbonCondenser.InputSize(input);
- fuelcount = RecipesCarbonCondenser.getFuelBurnTime(this.getStackInSlot(1)) * this.getStackInSlot(1).stackSize;
- fuelrequire = RecipesCarbonCondenser.getInputSmeltTime(input);
- }
- else
- {
- smeltcount = 0;
- smeltrequire = 0;
- fuelcount = 0;
- fuelrequire = 0;
- }
- if (input != null && smeltcount >= smeltrequire && fuelcount >= fuelrequire && fuelrequire > 0)
- {
- if (output == null || output.getItem() == RecipesCarbonCondenser.Output(input).getItem() && output.stackSize <= (this.getInventoryStackLimit() - RecipesCarbonCondenser.OutputSize(input)))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else if (input != null && smeltcount >= smeltrequire && this.isBurning())
- {
- if (output == null || output.getItem() == RecipesCarbonCondenser.Output(input).getItem() && output.stackSize <= (this.getInventoryStackLimit() - RecipesCarbonCondenser.OutputSize(input)))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- /**
- * Updating the Textures
- */
- @Override
- public Packet getDescriptionPacket()
- {
- // also tried running updateCircuitTextures() here with the added worldObj.markBlockForUpdate(xCoord, yCoord, zCoord) but it didn't work :/
- NBTTagCompound tag = new NBTTagCompound();
- writeToNBT(tag);
- return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 0, tag);
- }
- @Override
- public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt)
- {
- readFromNBT(pkt.func_148857_g());
- }
- public void updateCircuitTextures()
- {
- if (!getWorldObj().isRemote)
- {
- // sets orientation of sides
- if (this.meta == 2)
- {
- this.left = 5;
- this.right = 4;
- this.back = 3;
- this.top = 1;
- this.bot = 0;
- }
- else if (this.meta == 3)
- {
- this.left = 4;
- this.right = 5;
- this.back = 2;
- this.top = 1;
- this.bot = 0;
- }
- else if (this.meta == 4)
- {
- this.left = 2;
- this.right = 3;
- this.back = 5;
- this.top = 1;
- this.bot = 0;
- }
- else if (this.meta == 5)
- {
- this.left = 3;
- this.right = 2;
- this.back = 4;
- this.top = 1;
- this.bot = 0;
- }
- // Sets circuit textures
- // Top Side
- if (this.getStackInSlot(4) != null)
- {
- if (this.getStackInSlot(4).getItem() == Construct.circuitImport)
- {
- this.sTop = 1;
- }
- else if (this.getStackInSlot(4).getItem() == Construct.circuitExport)
- {
- this.sTop = 2;
- }
- }
- else
- {
- this.sTop = 0;
- }
- // Left Side
- if (this.getStackInSlot(5) != null)
- {
- if (this.getStackInSlot(5).getItem() == Construct.circuitImport)
- {
- this.sLeft = 1;
- }
- else if (this.getStackInSlot(5).getItem() == Construct.circuitExport)
- {
- this.sLeft = 2;
- }
- }
- else
- {
- this.sLeft = 0;
- }
- // Back Side
- if (this.getStackInSlot(6) != null)
- {
- if (this.getStackInSlot(6).getItem() == Construct.circuitImport)
- {
- this.sBack = 1;
- }
- else if (this.getStackInSlot(6).getItem() == Construct.circuitExport)
- {
- this.sBack = 2;
- }
- }
- else
- {
- this.sBack = 0;
- }
- // Right Side
- if (this.getStackInSlot(7) != null)
- {
- if (this.getStackInSlot(7).getItem() == Construct.circuitImport)
- {
- this.sRight = 1;
- }
- else if (this.getStackInSlot(7).getItem() == Construct.circuitExport)
- {
- this.sRight = 2;
- }
- }
- else
- {
- this.sRight = 0;
- }
- // Bottom Side
- if (this.getStackInSlot(8) != null)
- {
- if (this.getStackInSlot(8).getItem() == Construct.circuitImport)
- {
- this.sBot = 1;
- }
- else if (this.getStackInSlot(8).getItem() == Construct.circuitExport)
- {
- this.sBot = 2;
- }
- }
- else
- {
- this.sBot = 0;
- }
- //tried worldObj.markBlockForUpdate(xCoord, yCoord, zCoord) but it didn't work here :/
- markDirty();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement