Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xenocorpse.wulfenite.tileentities;
- import net.minecraft.block.Block;
- import net.minecraft.block.material.Material;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.init.Blocks;
- 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.ItemTool;
- import net.minecraft.nbt.NBTTagCompound;
- import net.minecraft.nbt.NBTTagList;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.EnumFacing;
- import net.minecraft.util.IChatComponent;
- import net.minecraftforge.fml.common.registry.GameRegistry;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- import com.xenocorpse.wulfenite.blocks.BlockGrinder;
- import com.xenocorpse.wulfenite.init.GrinderRecipes;
- import com.xenocorpse.wulfenite.init.wulfeniteBlocks;
- import com.xenocorpse.wulfenite.init.wulfeniteItems;
- public class TileEntityGrinder extends TileEntity implements ISidedInventory{
- 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 ItemStack[] grinderItemStacks = new ItemStack[3];
- public int grinderBurnTime;
- public int currentBurnTime;
- public int grinderGrindTime;
- private String grinderName;
- public void grinderName(String string){
- this.grinderName = string;
- }
- @Override
- public int getSizeInventory() {
- return this.grinderItemStacks.length;
- }
- @Override
- public ItemStack getStackInSlot(int slot) {
- return this.grinderItemStacks[slot];
- }
- @Override
- public ItemStack decrStackSize(int par1, int par2) {
- if(this.grinderItemStacks[par1] != null){
- ItemStack itemstack;
- if(this.grinderItemStacks[par1].stackSize <= par2){
- itemstack = this.grinderItemStacks[par1];
- this.grinderItemStacks[par1] = null;
- return itemstack;
- }else{
- itemstack = this.grinderItemStacks[par1].splitStack(par2);
- if(this.grinderItemStacks[par1].stackSize == 0){
- this.grinderItemStacks[par1] = null;
- }
- return itemstack;
- }
- }else{
- return null;
- }
- }
- @Override
- public ItemStack getStackInSlotOnClosing(int par1) {
- if(this.grinderItemStacks[par1] != null){
- ItemStack itemstack = this.grinderItemStacks[par1];
- this.grinderItemStacks[par1] = null;
- return itemstack;
- }else{
- return null;
- }
- }
- @Override
- public void setInventorySlotContents(int par1, ItemStack itemstack) {
- this.grinderItemStacks[par1] = itemstack;
- if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()){
- itemstack.stackSize = this.getInventoryStackLimit();
- }
- }
- @Override
- public String getName() {
- return this.hasCustomName() ? this.grinderName : "Grinder";
- }
- @Override
- public boolean hasCustomName() {
- return this.grinderName != null && this.grinderName.length() > 0;
- }
- @Override
- public int getInventoryStackLimit() {
- return 64;
- }
- public void readFromNBT(NBTTagCompound tagCompound) {
- super.readFromNBT(tagCompound);
- NBTTagList tagList = tagCompound.getTagList("Items", 10);
- this.grinderItemStacks = new ItemStack[this.getSizeInventory()];
- for (int i = 0; i < tagList.tagCount(); ++i) {
- NBTTagCompound tabCompound1 = tagList.getCompoundTagAt(i);
- byte byte0 = tabCompound1.getByte("Slot");
- if (byte0 >= 0 && byte0 < this.grinderItemStacks.length) {
- this.grinderItemStacks[byte0] = ItemStack.loadItemStackFromNBT(tabCompound1);
- }
- }
- this.grinderBurnTime = tagCompound.getShort("BurnTime");
- this.grinderGrindTime = tagCompound.getShort("GrindTime");
- this.currentBurnTime = getItemBurnTime(this.grinderItemStacks[1]);
- if (tagCompound.hasKey("CustomName", 8)) {
- this.grinderName = tagCompound.getString("CustomName");
- }
- }
- public void writeToNBT(NBTTagCompound tagCompound) {
- super.writeToNBT(tagCompound);
- tagCompound.setShort("BurnTime", (short) this.grinderBurnTime);
- tagCompound.setShort("GrindTime", (short) this.grinderBurnTime);
- NBTTagList tagList = new NBTTagList();
- for (int i = 0; i < this.grinderItemStacks.length; ++i) {
- if (this.grinderItemStacks[i] != null) {
- NBTTagCompound tagCompound1 = new NBTTagCompound();
- tagCompound1.setByte("Slot", (byte) i);
- this.grinderItemStacks[i].writeToNBT(tagCompound1);
- tagList.appendTag(tagCompound1);
- }
- }
- tagCompound.setTag("Items", tagList);
- if (this.hasCustomName()) {
- tagCompound.setString("CustomName", this.grinderName);
- }
- }
- @SideOnly(Side.CLIENT)
- public int getCookProgressScaled(int par1) {
- return this.grinderGrindTime * par1 / 200;
- }
- @SideOnly(Side.CLIENT)
- public int getBurnTimeRemainingScaled(int par1) {
- if (this.currentBurnTime == 0) {
- this.currentBurnTime = 200;
- }
- return this.grinderBurnTime * par1 / this.currentBurnTime;
- }
- public boolean isBurning() {
- return this.grinderBurnTime > 0;
- }
- public void updateEntity() {
- boolean flag = this.grinderBurnTime > 0;
- boolean flag1 = false;
- if (this.grinderBurnTime > 0) {
- --this.grinderBurnTime;
- }
- if (!this.worldObj.isRemote) {
- if (this.grinderBurnTime == 0 && this.canSmelt()) {
- this.currentBurnTime = this.grinderBurnTime = getItemBurnTime(this.grinderItemStacks[1]);
- if (this.grinderBurnTime > 0) {
- flag1 = true;
- if (this.grinderItemStacks[1] != null) {
- --this.grinderItemStacks[1].stackSize;
- if (this.grinderItemStacks[1].stackSize == 0) {
- this.grinderItemStacks[1] = grinderItemStacks[1].getItem().getContainerItem(this.grinderItemStacks[1]);
- }
- }
- }
- }
- if (this.isBurning() && this.canSmelt()) {
- ++this.grinderGrindTime;
- if (this.grinderGrindTime == 200) {
- this.grinderGrindTime = 0;
- this.smeltItem();
- flag1 = true;
- }
- } else {
- this.grinderGrindTime = 0;
- }
- }
- if (flag != this.grinderBurnTime > 0) {
- flag1 = true;
- BlockGrinder.updateBlockState(this.grinderBurnTime > 0, this.pos, this.worldObj);
- }
- if (flag1) {
- this.markDirty();
- }
- }
- private boolean canSmelt() {
- if (this.grinderItemStacks[0] == null) {
- return false;
- } else {
- ItemStack itemstack = GrinderRecipes.smelting().getSmeltingResult(this.grinderItemStacks[0]);
- if (itemstack == null) return false;
- if (this.grinderItemStacks[2] == null) return true;
- if (!this.grinderItemStacks[2].isItemEqual(itemstack)) return false;
- int result = grinderItemStacks[2].stackSize + itemstack.stackSize;
- return result <= getInventoryStackLimit() && result <= this.grinderItemStacks[2].getMaxStackSize();
- }
- }
- public void smeltItem() {
- if (this.canSmelt()) {
- ItemStack itemstack = GrinderRecipes.smelting().getSmeltingResult(this.grinderItemStacks[0]);
- if (this.grinderItemStacks[2] == null) {
- this.grinderItemStacks[2] = itemstack.copy();
- } else if (this.grinderItemStacks[2].getItem() == itemstack.getItem()) {
- this.grinderItemStacks[2].stackSize += itemstack.stackSize;
- }
- --this.grinderItemStacks[0].stackSize;
- if(this.grinderItemStacks[0].stackSize >= 0){
- this.grinderItemStacks[0] = null;
- }
- }
- }
- public static int getItemBurnTime(ItemStack itemstack){
- if(itemstack == null){
- return 0;
- }else{
- Item item = itemstack.getItem();
- if(item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.air){
- Block block = Block.getBlockFromItem(item);
- if(block == wulfeniteBlocks.wulfenite_block){
- return 200;
- }
- if(block.getMaterial() == Material.rock){
- return 300;
- }
- }
- if(item == wulfeniteItems.item_wulfenite) return 1600;
- if(item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("EMERALD")) return 300;
- return GameRegistry.getFuelValue(itemstack);
- }
- }
- public static boolean isItemFuel(ItemStack itemstack){
- return getItemBurnTime(itemstack) > 0;
- }
- @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 par1, ItemStack itemstack) {
- return par1 == 2 ? false : (par1 == 1 ? isItemFuel(itemstack) : true);
- }
- @Override
- public int getField(int id) {
- return 0;
- }
- @Override
- public void setField(int id, int value) {
- }
- @Override
- public int getFieldCount() {
- return 0;
- }
- @Override
- public void clear() {
- }
- @Override
- public IChatComponent getDisplayName() {
- return null;
- }
- @Override
- public int[] getSlotsForFace(EnumFacing side) {
- return side == EnumFacing.UP ? slotsTop : slotsSides;
- }
- @Override
- public boolean canInsertItem(int par1, ItemStack itemstack, EnumFacing par3) {
- return this.isItemValidForSlot(par1, itemstack);
- }
- @Override
- public boolean canExtractItem(int par1, ItemStack itemstack, EnumFacing par3) {
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment