Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.vladan899.tileentity;
- import java.util.Random;
- import com.vladan899.MachinesRecpie.SmasherRecpie;
- import com.vladan899.blocks.properties.BlockMachineSmasher;
- import com.vladan899.container.ContainerOreSmasher;
- import com.vladan899.items.ItemList;
- import net.minecraft.block.Block;
- import net.minecraft.block.BlockFurnace;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.EntityLiving;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.init.Items;
- import net.minecraft.inventory.Container;
- import net.minecraft.inventory.ContainerFurnace;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.SlotFurnaceFuel;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemBlock;
- import net.minecraft.item.ItemHoe;
- 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.minecraft.util.IChatComponent;
- import net.minecraft.util.MathHelper;
- import net.minecraftforge.fml.common.registry.GameRegistry;
- import net.minecraft.util.ChatComponentText;
- public class TileEntitySmasher extends TileEntity implements IInventory {
- private static final int[] slotsTop = new int[] {0};
- private static final int[] slotsBottom = new int[] {2, 1};
- private static final int[] slotsSides = new int[] {1};
- private static final int[] slotsUpgrade = new int[3];
- private ItemStack[] numberOfSlots = new ItemStack[4];
- public int furnaceSpeed = 200;
- public static int burnTime;
- public static int currentItemBurnTime;
- public static int cookTime;
- private String furnaceCustomName;
- //Fuels Burning time
- public static int StoneFuelGrindeer = 200; // 1 item = 200 ticks
- public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
- {
- return new ContainerOreSmasher(playerInventory, this);
- }
- @Override
- public String getName()
- {
- return this.hasCustomName() ? this.furnaceCustomName : "container.oreSmasher";
- }
- public void setCustomInventoryName(String displayName)
- {
- this.furnaceCustomName = displayName;
- }
- @Override
- public boolean hasCustomName()
- {
- return this.furnaceCustomName != null && this.furnaceCustomName.length() > 0;
- }
- @Override
- public IChatComponent getDisplayName()
- {
- return new ChatComponentText(this.getName());
- }
- @Override
- public int getSizeInventory() {
- return this.numberOfSlots.length;
- }
- @Override
- public ItemStack getStackInSlot(int index) {
- return this.numberOfSlots[index];
- }
- @Override
- public ItemStack decrStackSize(int index, int count) {
- if (this.numberOfSlots[index] != null)
- {
- ItemStack itemstack;
- if (this.numberOfSlots[index].stackSize <= count)
- {
- itemstack = this.numberOfSlots[index];
- this.numberOfSlots[index] = null;
- return itemstack;
- }
- else
- {
- itemstack = this.numberOfSlots[index].splitStack(count);
- if (this.numberOfSlots[index].stackSize == 0)
- {
- this.numberOfSlots[index] = null;
- }
- return itemstack;
- }
- }
- else
- {
- return null;
- }
- }
- @Override
- public ItemStack getStackInSlotOnClosing(int index) {
- if (this.numberOfSlots[index] != null)
- {
- ItemStack itemstack = this.numberOfSlots[index];
- this.numberOfSlots[index] = null;
- return itemstack;
- }
- else
- {
- return null;
- }
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack)
- {
- boolean flag = stack != null && stack.isItemEqual(this.numberOfSlots[index]) && ItemStack.areItemStackTagsEqual(stack, this.numberOfSlots[index]);
- this.numberOfSlots[index] = stack;
- if (stack != null && stack.stackSize > this.getInventoryStackLimit())
- {
- stack.stackSize = this.getInventoryStackLimit();
- }
- if (index == 0 && !flag)
- {
- this.currentItemBurnTime = this.TimeToSmashOneItem(stack);
- this.cookTime = 0;
- this.markDirty();
- }
- }
- public int TimeToSmashOneItem(ItemStack stack)
- {
- return 200;
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- @Override
- public boolean isUseableByPlayer(EntityPlayer player) {
- return this.worldObj.getTileEntity(this.pos) != this ? false : player.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
- }
- @Override
- public void openInventory(EntityPlayer player) {}
- @Override
- public void closeInventory(EntityPlayer player) {}
- @Override
- public boolean isItemValidForSlot(int index, ItemStack stack) {
- return index == 2 ? false : (index != 1 ? true : isItemFuel(stack) || SlotFurnaceFuel.isBucket(stack));
- }
- @Override
- public int getField(int id)
- {
- switch (id)
- {
- case 0:
- return this.cookTime;
- case 1:
- return this.burnTime;
- case 2:
- return this.currentItemBurnTime;
- case 3:
- return this.furnaceSpeed;
- default:
- return 0;
- }
- }
- @Override
- public void setField(int id, int value)
- {
- switch (id)
- {
- case 0:
- this.cookTime = value;
- break;
- case 1:
- this.burnTime = value;
- break;
- case 2:
- this.currentItemBurnTime = value;
- break;
- case 3:
- this.furnaceSpeed = value;
- }
- }
- @Override
- public int getFieldCount()
- {
- return 0;
- }
- @Override
- public void clear()
- {
- for (int i = 0; i < this.numberOfSlots.length; ++i)
- {
- this.numberOfSlots[i] = null;
- }
- }
- public static boolean isItemFuel(ItemStack stack)
- {
- /**
- * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
- * fuel
- */
- return getItemBurnTime(stack) > 0;
- }
- public static int getItemBurnTime(ItemStack itemStack)
- {
- if (itemStack == null)
- {
- return 0;
- }
- else
- {
- Item item = itemStack.getItem();
- if (item == ItemList.StoneGrineder) return 200;
- if (item == Items.stick) return 100;
- if (item == Items.coal) return 1600;
- return GameRegistry.getFuelValue(itemStack);
- }
- }
- public void smeltItem()
- {
- if (this.canSmelt())
- {
- ItemStack itemstack = SmasherRecpie.instance().getSmeltingResult(this.numberOfSlots[0]);
- if (this.numberOfSlots[2] == null)
- {
- this.numberOfSlots[2] = itemstack.copy();
- }
- else if (this.numberOfSlots[2].getItem() == itemstack.getItem())
- {
- this.numberOfSlots[2].stackSize += itemstack.stackSize; // Forge BugFix: Results may have multiple items
- }
- if (this.numberOfSlots[0].getItem() == Item.getItemFromBlock(Blocks.sponge) && this.numberOfSlots[0].getMetadata() == 1 && this.numberOfSlots[1] != null && this.numberOfSlots[1].getItem() == Items.bucket)
- {
- this.numberOfSlots[1] = new ItemStack(Items.water_bucket);
- }
- --this.numberOfSlots[0].stackSize;
- if (this.numberOfSlots[0].stackSize <= 0)
- {
- this.numberOfSlots[0] = null;
- }
- }
- }
- private boolean canSmelt()
- {
- if (this.numberOfSlots[0] == null)
- {
- return false;
- }
- else
- {
- ItemStack itemstack = FurnaceRecipes.instance().getSmeltingResult(this.numberOfSlots[0]);
- if (itemstack == null) return false;
- if (this.numberOfSlots[2] == null) return true;
- if (!this.numberOfSlots[2].isItemEqual(itemstack)) return false;
- int result = numberOfSlots[2].stackSize + itemstack.stackSize;
- return result <= getInventoryStackLimit() && result <= this.numberOfSlots[2].getMaxStackSize(); //Forge BugFix: Make it respect stack sizes properly.
- }
- }
- public void update()
- {
- boolean flag = this.isBurning();
- boolean flag1 = false;
- if (this.isBurning())
- {
- --this.burnTime;
- }
- if (!this.worldObj.isRemote)
- {
- if (this.burnTime == 0 && this.canSmelt())
- {
- if (this.isBurning() && this.cookTime > 0)
- {
- this.currentItemBurnTime = this.cookTime = this.getItemBurnTime(this.numberOfSlots[1]);
- if (this.isBurning() )
- {
- flag1 = true;
- if(this.numberOfSlots[1] != null)
- {
- EntityLiving entity = null;
- this.numberOfSlots[1].damageItem(-1,(EntityLiving) entity );
- if(this.numberOfSlots[1].getItemDamage() == 0)
- {
- this.numberOfSlots[1] = this.numberOfSlots[1].getItem().getContainerItem(this.numberOfSlots[1]);
- }
- }
- }
- }
- if(this.isBurning() && this.canSmelt())
- {
- this.cookTime ++;
- if(this.cookTime == this.furnaceSpeed)
- {
- this.cookTime = 0;
- this.smeltItem();
- flag1 = true;
- }
- }
- else
- this.cookTime = 0;
- }
- if(flag1 != this.isBurning())
- {
- flag1= true;
- BlockMachineSmasher.setState(this.isBurning(), this.worldObj, this.pos);
- }
- }
- if (flag1)
- {
- this.markDirty();
- }
- }
- @Override
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- NBTTagList nbttaglist = compound.getTagList("Items", 10);
- numberOfSlots = new ItemStack[getSizeInventory()];
- for (int i = 0; i < nbttaglist.tagCount(); ++i)
- {
- NBTTagCompound nbtTagCompound = nbttaglist.getCompoundTagAt(i);
- byte b0 = nbtTagCompound.getByte("Slot");
- if (b0 >= 0 && b0 < numberOfSlots.length)
- {
- numberOfSlots[b0] = ItemStack.loadItemStackFromNBT(
- nbtTagCompound);
- }
- }
- furnaceSpeed = compound.getShort("GrindTime");
- currentItemBurnTime = compound.getShort("CookTime");
- cookTime = compound.getShort("CookTimeTotal");
- if (compound.hasKey("CustomName", 8))
- {
- furnaceCustomName = compound.getString("CustomName");
- }
- }
- @Override
- public void writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- compound.setShort("GrindTime", (short)furnaceSpeed);
- compound.setShort("ItemBurnTime", (short)currentItemBurnTime);
- compound.setShort("CookTimeTotal", (short)cookTime);
- NBTTagList nbttaglist = new NBTTagList();
- for (int i = 0; i < numberOfSlots.length; ++i)
- {
- if (numberOfSlots[i] != null)
- {
- NBTTagCompound nbtTagCompound = new NBTTagCompound();
- nbtTagCompound.setByte("Slot", (byte)i);
- numberOfSlots[i].writeToNBT(nbtTagCompound);
- nbttaglist.appendTag(nbtTagCompound);
- }
- }
- compound.setTag("Items", nbttaglist);
- if (hasCustomName())
- {
- compound.setString("CustomName", furnaceCustomName);
- }
- }
- public boolean isBurning( )
- {
- return this.burnTime > 0;
- }
- public int getCookProgressScaled(int i)
- {
- return this.burnTime * i / this.furnaceSpeed;
- }
- public int getBurnTimeRemaningScaled(int i)
- {
- if(this.currentItemBurnTime == 0)
- {
- this.currentItemBurnTime = this.furnaceSpeed;
- }
- return this.burnTime * i / this.currentItemBurnTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement