Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.sciencecraft.tileentity;
- import cpw.mods.fml.common.registry.GameRegistry;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.ISidedInventory;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemBlock;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.ItemSword;
- import net.minecraft.item.ItemTool;
- import net.minecraft.item.crafting.FurnaceRecipes;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntity;
- import net.sciencecraft.machines.Grinder;
- import net.sciencecraft.main.ScienceCraft;
- import net.sciencecraft.recipes.ScienceCraft_recipes;
- public class TileEntityGrinder extends TileEntity implements ISidedInventory
- {
- private String localizedName;
- private static final int[] slots_top = new int[] {0};
- private static final int[] slots_bottom = new int[] {2};
- private static final int[] slots_sides = new int[] {1};
- private ItemStack[] slots = new ItemStack[3];
- /** Furnace speed */
- public int furnaceSpeed = 450; //200 is normaal, hoe kleiner, hoe sneller
- /** How long this furnace will continue to burn for (fuel) */
- public int power;
- public final int maxPower = 10000;
- /** How long time left before cooked */
- public int cookTime;
- public int getSizeInventory()
- {
- return this.slots.length;
- }
- public String getInventoryName()
- {
- return this.hasCustomInventoryName() ? this.localizedName : "container.Grinder";
- }
- public boolean hasCustomInventoryName()
- {
- return this.localizedName != null && this.localizedName.length() > 0;
- }
- public void setGuiDisplayName(String displayName)
- {
- this.localizedName = displayName;
- }
- public ItemStack getStackInSlot(int i)
- {
- return this.slots[i];
- }
- public ItemStack decrStackSize(int i, int j)
- {
- if (this.slots[i] != null)
- {
- ItemStack itemstack;
- if (this.slots[i].stackSize <= j)
- {
- itemstack = this.slots[i];
- this.slots[i] = null;
- return itemstack;
- }
- else
- {
- itemstack = this.slots[i].splitStack(j);
- if (this.slots[i].stackSize == 0)
- {
- this.slots[i] = null;
- }
- return itemstack;
- }
- }
- return null;
- }
- public ItemStack getStackInSlotOnClosing(int i)
- {
- if (this.slots[i] != null)
- {
- ItemStack itemstack = this.slots[i];
- this.slots[i] = null;
- return itemstack;
- }
- return null;
- }
- public void setInventorySlotContents(int i, ItemStack itemstack)
- {
- this.slots[i] = itemstack;
- if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
- {
- itemstack.stackSize = this.getInventoryStackLimit();
- }
- }
- public int getInventoryStackLimit()
- {
- return 64;
- }
- public void readFromNBT(NBTTagCompound nbt)
- {
- super.readFromNBT(nbt);
- NBTTagList list = nbt.getTagList("Items", 10);
- this.slots = new ItemStack[this.getSizeInventory()];
- for (int i = 0; i < list.tagCount(); i++)
- {
- NBTTagCompound compound = (NBTTagCompound) list.getCompoundTagAt(i);
- byte b = compound.getByte("Slot");
- if (b >= 0 && b < this.slots.length)
- {
- this.slots[b] = ItemStack.loadItemStackFromNBT(compound);
- }
- }
- this.power = nbt.getShort("BurnTime");
- this.cookTime = nbt.getShort("CookTime");
- if (nbt.hasKey("CustomName"))
- {
- this.localizedName = nbt.getString("CustomName");
- }
- }
- public void writeToNBT(NBTTagCompound nbt)
- {
- super.writeToNBT(nbt);
- nbt.setShort("BurnTime", (short)this.power);
- nbt.setShort("CookTime", (short)this.cookTime);
- NBTTagList list = new NBTTagList();
- for (int i = 0; i < this.slots.length; i++)
- {
- if (this.slots[i] != null)
- {
- NBTTagCompound compound = new NBTTagCompound();
- compound.setByte("Slot", (byte)i);
- this.slots[i].writeToNBT(compound);
- list.appendTag(compound);
- }
- }
- nbt.setTag("Items", list);
- if (this.hasCustomInventoryName())
- {
- nbt.setString("CustomName", this.localizedName);
- }
- }
- public boolean isUseableByPlayer(EntityPlayer entityplayer)
- {
- return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : entityplayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
- }
- public void openInventory() {}
- public void closeInventory() {}
- public boolean hasPower()
- {
- return this.power > 0;
- }
- public void updateEntity()
- {
- boolean flag = this.hasPower();
- boolean flag1 = false;
- if (this.hasPower() && this.isMacerating())
- {
- this.power--;
- }
- if (!this.worldObj.isRemote)
- {
- if (this.hasItemPower(this.slots[1]) && this.power < (this.maxPower - this.getItemPower(this.slots[1])))
- {
- this.power += getItemPower(this.slots[1]);
- if (this.slots[1] != null)
- {
- flag1 = true;
- this.slots[1].stackSize--;
- if (this.slots[1].stackSize == 0)
- {
- this.slots[1] = this.slots[1].getItem().getContainerItem(this.slots[1]);
- }
- }
- }
- if (this.hasPower() && this.canSmelt())
- {
- this.cookTime++;
- if (this.cookTime == this.furnaceSpeed)
- {
- this.cookTime = 0;
- this.smeltItem();
- flag1 = true;
- }
- }
- else
- {
- this.cookTime = 0;
- }
- if (flag != this.hasPower())
- {
- flag1 = true;
- Grinder.updateGrinderBlockState(this.hasPower(), this.worldObj, this.xCoord, this.yCoord, this.zCoord);
- }
- }
- if (flag1)
- {
- this.markDirty();
- }
- }
- private boolean canSmelt()
- {
- if (this.slots[0] == null)
- {
- return false;
- }
- else
- {
- ItemStack itemstack = ScienceCraft_recipes.smelting().getSmeltingResult(this.slots[0]);
- if (itemstack == null)
- {
- return false;
- }
- if (this.slots[2] == null)
- {
- return true;
- }
- if (!this.slots[2].isItemEqual(itemstack))
- {
- return false;
- }
- int result = this.slots[2].stackSize + 2;
- return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
- }
- }
- public void smeltItem()
- {
- if (this.canSmelt())
- {
- ItemStack itemstack = ScienceCraft_recipes.smelting().getSmeltingResult(this.slots[0]);
- itemstack.stackSize = 2;
- if (this.slots[2] == null)
- {
- this.slots[2] = itemstack.copy();
- }
- else if (this.slots[2].isItemEqual(itemstack))
- {
- this.slots[2].stackSize += itemstack.stackSize;
- }
- this.slots[0].stackSize--;
- if (this.slots[0].stackSize <= 0)
- {
- this.slots[0] = null;
- }
- }
- }
- public static int getItemPower(ItemStack itemstack)
- {
- if (itemstack == null)
- {
- return 0;
- }
- else
- {
- Item i = itemstack.getItem();
- //if(item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD")) return 200;
- if (i == ScienceCraft.EnergyCrystal)
- {
- return 500; //200 is 1 item
- }
- return 0;
- }
- }
- public boolean isMacerating()
- {
- return this.cookTime > 0;
- }
- public static boolean hasItemPower(ItemStack itemstack)
- {
- return getItemPower(itemstack) > 0;
- }
- public boolean isItemValidForSlot(int i, ItemStack itemstack)
- {
- return i == 2 ? false : (i == 1 ? hasItemPower(itemstack) : true);
- }
- public int[] getAccessibleSlotsFromSide(int var1)
- {
- return var1 == 0 ? slots_bottom : (var1 == 1 ? slots_top : slots_sides);
- }
- public boolean canInsertItem(int i, ItemStack itemstack, int j)
- {
- return this.isItemValidForSlot(i, itemstack);
- }
- public boolean canExtractItem(int i, ItemStack itemstack, int j)
- {
- return j != 0 || i != 1 || itemstack.getItem() == Items.bucket;
- }
- public int getPowerRemainingScaled(int i)
- {
- return this.power * i / this.maxPower;
- }
- public int getMacerationProgressScaled(int i)
- {
- return this.cookTime * i / this.furnaceSpeed;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement