Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package caspermod.objects.blocks.machines.extractor;
- 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.IInventory;
- import net.minecraft.inventory.ItemStackHelper;
- 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.nbt.NBTTagCompound;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.ITickable;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.math.MathHelper;
- import net.minecraft.util.text.ITextComponent;
- import net.minecraft.util.text.TextComponentString;
- import net.minecraft.util.text.TextComponentTranslation;
- import net.minecraftforge.event.ForgeEventFactory;
- import net.minecraftforge.fml.common.registry.GameRegistry;
- import net.minecraftforge.fml.relauncher.Side;
- import net.minecraftforge.fml.relauncher.SideOnly;
- public class TileEntityExtractor extends TileEntity implements IInventory, ITickable
- {
- private NonNullList<ItemStack> inventory = NonNullList.<ItemStack>withSize(4, ItemStack.EMPTY);
- private String customName;
- private int burnTime;
- private int currentBurnTime;
- private int cookTime;
- private int totalCookTime;
- @Override
- public String getName()
- {
- return this.hasCustomName() ? this.customName : "container.extractor";
- }
- @Override
- public boolean hasCustomName()
- {
- return this.customName != null && !this.customName.isEmpty();
- }
- public void setCustomName(String customName)
- {
- this.customName = customName;
- }
- @Override
- public ITextComponent getDisplayName()
- {
- return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
- }
- @Override
- public int getSizeInventory()
- {
- return this.inventory.size();
- }
- @Override
- public boolean isEmpty()
- {
- for(ItemStack stack : this.inventory)
- {
- if(!stack.isEmpty()) return false;
- }
- return true;
- }
- @Override
- public ItemStack getStackInSlot(int index)
- {
- return (ItemStack)this.inventory.get(index);
- }
- @Override
- public ItemStack decrStackSize(int index, int count)
- {
- return ItemStackHelper.getAndSplit(this.inventory, index, count);
- }
- @Override
- public ItemStack removeStackFromSlot(int index)
- {
- return ItemStackHelper.getAndRemove(this.inventory, index);
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack)
- {
- ItemStack itemstack = (ItemStack)this.inventory.get(index);
- boolean flag = !stack.isEmpty() && stack.isItemEqual(itemstack) && ItemStack.areItemStackTagsEqual(stack, itemstack);
- this.inventory.set(index, stack);
- if(stack.getCount() > this.getInventoryStackLimit()) stack.setCount(this.getInventoryStackLimit());
- if(index == 0 && !flag)
- {
- this.totalCookTime = this.getCookTime(stack);
- this.cookTime = 0;
- this.markDirty();
- }
- }
- @Override
- public void readFromNBT(NBTTagCompound compound)
- {
- super.readFromNBT(compound);
- this.inventory = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
- ItemStackHelper.loadAllItems(compound, this.inventory);
- this.burnTime = compound.getInteger("BurnTime");
- this.cookTime = compound.getInteger("CookTime");
- this.totalCookTime = compound.getInteger("CookTimeTotal");
- this.currentBurnTime = getItemBurnTime((ItemStack)this.inventory.get(2));
- if(compound.hasKey("CustomName", 8)) this.setCustomName(compound.getString("CustomName"));
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound compound)
- {
- super.writeToNBT(compound);
- compound.setInteger("BurnTime", (short)this.burnTime);
- compound.setInteger("CookTime", (short)this.cookTime);
- compound.setInteger("CookTimeTotal", (short)this.totalCookTime);
- ItemStackHelper.saveAllItems(compound, this.inventory);
- if(this.hasCustomName()) compound.setString("CustomName", this.customName);
- return compound;
- }
- @Override
- public int getInventoryStackLimit()
- {
- return 64;
- }
- public boolean isBurning()
- {
- return this.burnTime > 0;
- }
- @SideOnly(Side.CLIENT)
- public static boolean isBurning(IInventory inventory)
- {
- return inventory.getField(0) > 0;
- }
- public void update()
- {
- boolean flag = this.isBurning();
- boolean flag1 = false;
- if(this.isBurning()) --this.burnTime;
- if(!this.world.isRemote)
- {
- ItemStack stack = (ItemStack)this.inventory.get(2);
- if(this.isBurning() || !stack.isEmpty() && !((((ItemStack)this.inventory.get(0)).isEmpty()) || ((ItemStack)this.inventory.get(1)).isEmpty()))
- {
- if(!this.isBurning() && this.canSmelt())
- {
- this.burnTime = getItemBurnTime(stack);
- this.currentBurnTime = this.burnTime;
- if(this.isBurning())
- {
- flag1 = true;
- if(!stack.isEmpty())
- {
- Item item = stack.getItem();
- stack.shrink(1);
- if(stack.isEmpty())
- {
- ItemStack item1 = item.getContainerItem(stack);
- this.inventory.set(2, item1);
- }
- }
- }
- }
- if(this.isBurning() && this.canSmelt())
- {
- ++this.cookTime;
- if(this.cookTime == this.totalCookTime)
- {
- this.cookTime = 0;
- this.totalCookTime = this.getCookTime((ItemStack)this.inventory.get(0));
- this.smeltItem();
- flag1 = true;
- }
- }
- else this.cookTime = 0;
- }
- else if(!this.isBurning() && this.cookTime > 0)
- {
- this.cookTime = MathHelper.clamp(this.cookTime - 2, 0, this.totalCookTime);
- }
- if(flag != this.isBurning())
- {
- flag1 = true;
- BlockExtractor.setState(this.isBurning(), this.world, this.pos);
- }
- }
- if(flag1) this.markDirty();
- }
- public int getCookTime(ItemStack input)
- {
- return 200;
- }
- private boolean canSmelt()
- {
- if(((ItemStack)this.inventory.get(0)).isEmpty()) return false;
- else
- {
- ItemStack result1 = ExtractorRecipes.getInstance().getExtractingResult1((ItemStack)this.inventory.get(0));
- ItemStack result2 = ExtractorRecipes.getInstance().getExtractingResult2((ItemStack)this.inventory.get(0));
- if(result1.isEmpty() || result2.isEmpty()) return false;
- else
- {
- boolean out1, out2, out;
- ItemStack output1 = (ItemStack)this.inventory.get(3);
- int res1 = output1.getCount() + result1.getCount();
- ItemStack output2 = (ItemStack)this.inventory.get(1);
- int res2 = output2.getCount() + result2.getCount();
- out1 = output1.isEmpty() || output1.isItemEqual(result1);
- out2 = output2.isEmpty() || output2.isItemEqual(result2);
- out = out1 && out2 && res1 <= getInventoryStackLimit() && res1 <= output1.getMaxStackSize() && res2 <= getInventoryStackLimit() && res2 <= output2.getMaxStackSize();
- return out;
- }
- }
- }
- public void smeltItem()
- {
- if(this.canSmelt())
- {
- ItemStack input = (ItemStack)this.inventory.get(0);
- ItemStack result1 = ExtractorRecipes.getInstance().getExtractingResult1(input);
- ItemStack result2 = ExtractorRecipes.getInstance().getExtractingResult2(input);
- ItemStack output1 = (ItemStack)this.inventory.get(1);
- ItemStack output2 = (ItemStack)this.inventory.get(3);
- if(output1.isEmpty()) this.inventory.set(1, result1.copy());
- else if(output1.getItem() == result1.getItem()) output1.grow(result1.getCount());
- if(output2.isEmpty()) this.inventory.set(3, result2.copy());
- else if(output2.getItem() == result2.getItem()) output2.grow(result2.getCount());
- input.shrink(1);
- }
- }
- public static int getItemBurnTime(ItemStack fuel)
- {
- if(fuel.isEmpty()) return 0;
- else
- {
- Item item = fuel.getItem();
- if (item instanceof ItemBlock && Block.getBlockFromItem(item) != Blocks.AIR)
- {
- Block block = Block.getBlockFromItem(item);
- if (block == Blocks.WOODEN_SLAB) return 150;
- if (block.getDefaultState().getMaterial() == Material.WOOD) return 300;
- if (block == Blocks.COAL_BLOCK) return 16000;
- }
- if (item instanceof ItemTool && "WOOD".equals(((ItemTool)item).getToolMaterialName())) return 200;
- if (item instanceof ItemSword && "WOOD".equals(((ItemSword)item).getToolMaterialName())) return 200;
- if (item instanceof ItemHoe && "WOOD".equals(((ItemHoe)item).getMaterialName())) return 200;
- if (item == Items.STICK) return 100;
- if (item == Items.COAL) return 1600;
- if (item == Items.LAVA_BUCKET) return 20000;
- if (item == Item.getItemFromBlock(Blocks.SAPLING)) return 100;
- if (item == Items.BLAZE_ROD) return 2400;
- return ForgeEventFactory.getItemBurnTime(fuel);
- }
- }
- public static boolean isItemFuel(ItemStack fuel)
- {
- return getItemBurnTime(fuel) > 0;
- }
- @Override
- public boolean isUsableByPlayer(EntityPlayer player)
- {
- return this.world.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)
- {
- if(index == 3) return false;
- else if(index != 2) return true;
- else
- {
- return isItemFuel(stack);
- }
- }
- public String getGuiID()
- {
- return "caspermod:extractor";
- }
- @Override
- public int getField(int id)
- {
- switch(id)
- {
- case 0:
- return this.burnTime;
- case 1:
- return this.currentBurnTime;
- case 2:
- return this.cookTime;
- case 3:
- return this.totalCookTime;
- default:
- return 0;
- }
- }
- @Override
- public void setField(int id, int value)
- {
- switch(id)
- {
- case 0:
- this.burnTime = value;
- break;
- case 1:
- this.currentBurnTime = value;
- break;
- case 2:
- this.cookTime = value;
- break;
- case 3:
- this.totalCookTime = value;
- }
- }
- @Override
- public int getFieldCount()
- {
- return 4;
- }
- @Override
- public void clear()
- {
- this.inventory.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement