Advertisement
HalestormXV

Untitled

Aug 27th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.62 KB | None | 0 0
  1. package halestormxv.eAngelus.tileentity;
  2.  
  3. import halestormxv.eAngelus.blocks.DualFurance;
  4. import halestormxv.eAngelus.crafting.DualFurnaceRecipes;
  5. import halestormxv.eAngelus.main.Reference;
  6. import halestormxv.eAngelus.main.init.eAngelusItems;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.material.Material;
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.init.Blocks;
  11. import net.minecraft.init.Items;
  12. import net.minecraft.inventory.IInventory;
  13. import net.minecraft.inventory.ItemStackHelper;
  14. import net.minecraft.item.*;
  15. import net.minecraft.nbt.NBTTagCompound;
  16. import net.minecraft.tileentity.TileEntity;
  17. import net.minecraft.util.ITickable;
  18. import net.minecraft.util.NonNullList;
  19. import net.minecraft.util.math.MathHelper;
  20. import net.minecraft.util.text.ITextComponent;
  21. import net.minecraft.util.text.TextComponentString;
  22. import net.minecraft.util.text.TextComponentTranslation;
  23. import net.minecraftforge.fml.relauncher.Side;
  24. import net.minecraftforge.fml.relauncher.SideOnly;
  25.  
  26. /**
  27.  * Created by Blaze on 8/26/2017.
  28.  */
  29. public class TileEntityDualFurnace extends TileEntity implements IInventory, ITickable
  30. {
  31.     private NonNullList<ItemStack> inventory = NonNullList.<ItemStack>withSize(4, ItemStack.EMPTY);
  32.     private String customName;
  33.  
  34.     private int burnTime;
  35.     private int currentBurnTime;
  36.     private int cookTime;
  37.     private int totalCookTime;
  38.  
  39.     @Override
  40.     public int getSizeInventory() {
  41.         return this.inventory.size();
  42.     }
  43.  
  44.     @Override
  45.     public boolean isEmpty() {
  46.         for(ItemStack stack : this.inventory)
  47.             if(!stack.isEmpty())
  48.                 return false;
  49.         return true;
  50.     }
  51.  
  52.     @Override
  53.     public ItemStack getStackInSlot(int index) {
  54.         return (ItemStack)this.inventory.get(index);
  55.     }
  56.  
  57.     @Override
  58.     public ItemStack decrStackSize(int index, int count) {
  59.         return ItemStackHelper.getAndSplit(this.inventory, index, count);
  60.     }
  61.  
  62.     @Override
  63.     public ItemStack removeStackFromSlot(int index) {
  64.         return ItemStackHelper.getAndRemove(this.inventory, index);
  65.     }
  66.  
  67.     @Override
  68.     public void setInventorySlotContents(int index, ItemStack stack) {
  69.         ItemStack itemStack = (ItemStack)this.inventory.get(index);
  70.         boolean flag = !stack.isEmpty() && stack.isItemEqual(itemStack) && ItemStack.areItemsEqual(stack, itemStack);
  71.         this.inventory.set(index, stack);
  72.  
  73.         if(stack.getCount() > this.getInventoryStackLimit())
  74.             stack.setCount(this.getInventoryStackLimit());
  75.         if(index == 0 && index + 1 == 1 && !flag)
  76.         {
  77.             ItemStack stack1 = (ItemStack)this.inventory.get(index + 1);
  78.             this.totalCookTime = this.getCookTime(stack, stack1);
  79.             this.cookTime = 0;
  80.             this.markDirty();
  81.         }
  82.     }
  83.  
  84.     @Override
  85.     public int getInventoryStackLimit() {
  86.         return 64;
  87.     }
  88.  
  89.     public boolean isBurning()
  90.     {
  91.         return this.burnTime > 0;
  92.     }
  93.  
  94.     @SideOnly(Side.CLIENT)
  95.     public static boolean isBurning(IInventory inventory)
  96.     {
  97.         return inventory.getField(0) > 0;
  98.     }
  99.  
  100.     @Override
  101.     public boolean isUsableByPlayer(EntityPlayer player) {
  102.         return this.world.getTileEntity(this.pos) != this ? false :
  103.                 player.getDistanceSq((double)this.pos.getX() + 0.5D,
  104.                 (double)this.pos.getY() + 0.5D,
  105.                 (double)this.pos.getZ() + 0.5D) <= 64.0D;
  106.     }
  107.  
  108.     @Override
  109.     public void openInventory(EntityPlayer player) {
  110.  
  111.     }
  112.  
  113.     @Override
  114.     public void closeInventory(EntityPlayer player) {
  115.  
  116.     }
  117.  
  118.     @Override
  119.     public boolean isItemValidForSlot(int index, ItemStack stack) {
  120.         if(index == 3)
  121.             return false;
  122.         else if(index != 2)
  123.             return true;
  124.         else{
  125.             return isItemFuel(stack);
  126.         }
  127.     }
  128.  
  129.     public String getGuiID()
  130.     {
  131.         return Reference.MODID+":dual_furnace";
  132.     }
  133.  
  134.     @Override
  135.     public int getField(int id) {
  136.         switch(id)
  137.         {
  138.             case 0:
  139.                 return this.burnTime;
  140.             case 1:
  141.                 return this.currentBurnTime;
  142.             case 2:
  143.                 return this.cookTime;
  144.             case 3:
  145.                 return this.totalCookTime;
  146.             default:
  147.                 return 0;
  148.         }
  149.     }
  150.  
  151.     @Override
  152.     public void setField(int id, int value)
  153.     {
  154.         switch(id)
  155.         {
  156.             case 0:
  157.                 this.burnTime = value;
  158.                 break;
  159.             case 1:
  160.                 this.currentBurnTime = value;
  161.                 break;
  162.             case 2:
  163.                 this.cookTime = value;
  164.                 break;
  165.             case 3:
  166.                 this.totalCookTime = value;
  167.         }
  168.     }
  169.  
  170.     @Override
  171.     public int getFieldCount() {
  172.         return 4;
  173.     }
  174.  
  175.     public int getCookTime(ItemStack input1, ItemStack input2) //Change to a Switch to Adjust Burn Times
  176.     {
  177.         return 200;
  178.     }
  179.  
  180.     @Override
  181.     public void clear()
  182.     {
  183.         this.inventory.clear();
  184.     }
  185.  
  186.     @Override
  187.     public String getName()
  188.     {
  189.         return this.hasCustomName() ? this.customName : "container.dual_furnace";
  190.     }
  191.  
  192.     @Override
  193.     public boolean hasCustomName() {
  194.         return this.customName != null && !this.customName.isEmpty();
  195.     }
  196.  
  197.     public void setCustomName(String customName)
  198.     {
  199.         this.customName = customName;
  200.     }
  201.  
  202.     @Override
  203.     public ITextComponent getDisplayName()
  204.     {
  205.         return this.hasCustomName() ? new TextComponentString(this.getName()) : new TextComponentTranslation(this.getName());
  206.     }
  207.  
  208.     @Override
  209.     public void readFromNBT(NBTTagCompound compound) {
  210.         super.readFromNBT(compound);
  211.         this.inventory = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
  212.         ItemStackHelper.loadAllItems(compound, this.inventory);
  213.         this.burnTime = compound.getInteger("BurnTime");
  214.         this.cookTime = compound.getInteger("CookTime");
  215.         this.totalCookTime = compound.getInteger("CookTimeTotal");
  216.         this.currentBurnTime = getItemBurnTime((ItemStack)this.inventory.get(2));
  217.  
  218.         if(compound.hasKey("CustomName", 8))
  219.             this.setCustomName(compound.getString("CustomName"));
  220.     }
  221.  
  222.     @Override
  223.     public NBTTagCompound writeToNBT(NBTTagCompound compound)
  224.     {
  225.         super.writeToNBT(compound);
  226.         compound.setInteger("BurnTime", (short)this.burnTime);
  227.         compound.setInteger("CookTime", (short)this.cookTime);
  228.         compound.setInteger("CookTimeTotal", (short)this.totalCookTime);
  229.         ItemStackHelper.loadAllItems(compound, this.inventory);
  230.  
  231.         if(this.hasCustomName())
  232.             compound.setString("CustomName", this.customName);
  233.  
  234.         return compound;
  235.     }
  236.  
  237.     @Override
  238.     public void update()
  239.     {
  240.         boolean flag = this.isBurning();
  241.         boolean flag1 = false;
  242.  
  243.         if(this.isBurning())
  244.             --this.burnTime;
  245.  
  246.         if(!this.world.isRemote)
  247.         {
  248.             ItemStack itemStack = (ItemStack)this.inventory.get(2);
  249.  
  250.             if(this.isBurning() || !itemStack.isEmpty() && !((((ItemStack)this.inventory.get(0)).isEmpty()) || ((ItemStack)this.inventory.get(1)).isEmpty()))
  251.             {
  252.                 if(!this.isBurning() && this.canSmelt())
  253.                 {
  254.                     this.burnTime = getItemBurnTime(itemStack);
  255.                     this.currentBurnTime = this.burnTime;
  256.  
  257.                     if(this.isBurning())
  258.                     {
  259.                         flag1 = true;
  260.  
  261.                         if(!itemStack.isEmpty())
  262.                         {
  263.                             Item item = itemStack.getItem();
  264.                             itemStack.shrink(1);
  265.                             if(itemStack.isEmpty())
  266.                             {
  267.                                 ItemStack item1 = item.getContainerItem(itemStack);
  268.                                 this.inventory.set(2, item1);
  269.                             }
  270.                         }
  271.                     }
  272.                 }
  273.                 if(this.isBurning() && this.canSmelt())
  274.                 {
  275.                     ++this.cookTime;
  276.  
  277.                     if(this.cookTime == this.totalCookTime)
  278.                     {
  279.                         this.cookTime = 0;
  280.                         this.totalCookTime = this.getCookTime((ItemStack)this.inventory.get(0), (ItemStack)this.inventory.get(1));
  281.                         this.smeltItem();
  282.                         flag1 = true;
  283.                     }
  284.                 } else
  285.                     this.cookTime = 0;
  286.             } else if(!this.isBurning() && this.cookTime > 0)
  287.                 this.cookTime = MathHelper.clamp(this.cookTime - 2, 0, this.totalCookTime);
  288.             if(flag != this.isBurning())
  289.             {
  290.                 flag1 = true;
  291.                 DualFurance.setState(this.isBurning(), this.world, this.pos);
  292.             }
  293.         }
  294.         if(flag1)
  295.         this.markDirty();
  296.     }
  297.  
  298.     private boolean canSmelt()
  299.     {
  300.         if( ((ItemStack)this.inventory.get(0)).isEmpty() || ((ItemStack)this.inventory.get(1)).isEmpty() )
  301.             return false;
  302.         else{
  303.             ItemStack result = DualFurnaceRecipes.instance().getDualSmeltingResult((ItemStack)this.inventory.get(0), (ItemStack)this.inventory.get(1));
  304.             if(result.isEmpty())
  305.                 return false;
  306.             else{
  307.                 ItemStack output = (ItemStack)this.inventory.get(3);
  308.  
  309.                 if(output.isEmpty()) return true;
  310.                 if(!output.isItemEqual(result)) return false;
  311.                 int res = output.getCount() + result.getCount();
  312.                 return res <= getInventoryStackLimit() && res <= output.getMaxStackSize();
  313.             }
  314.         }
  315.     }
  316.  
  317.     public void smeltItem()
  318.     {
  319.         if(this.canSmelt())
  320.         {
  321.             ItemStack input1 = (ItemStack)this.inventory.get(0);
  322.             ItemStack input2 = (ItemStack)this.inventory.get(1);
  323.             ItemStack result = DualFurnaceRecipes.instance().getDualSmeltingResult(input1, input2);
  324.             ItemStack output = (ItemStack)this.inventory.get(3);
  325.  
  326.             if(output.isEmpty())
  327.                 this.inventory.set(3, result.copy());
  328.             else if(output.getItem() == result.getItem())
  329.                 output.grow(result.getCount());
  330.  
  331.             input1.shrink(1);
  332.             input2.shrink(1);
  333.         }
  334.     }
  335.  
  336.     public static int getItemBurnTime(ItemStack stack)
  337.     {
  338.         if (stack.isEmpty())
  339.         {
  340.             return 0;
  341.         }
  342.         else
  343.         {
  344.             Item item = stack.getItem();
  345.             if (!item.getRegistryName().getResourceDomain().equals("minecraft"))
  346.             {
  347.                 int burnTime = net.minecraftforge.fml.common.registry.GameRegistry.getFuelValue(stack);
  348.                 if (burnTime != 0) return burnTime;
  349.             }
  350.             return item ==
  351.                     Item.getItemFromBlock(Blocks.WOODEN_SLAB) ? 150
  352.                     : (item == Item.getItemFromBlock(Blocks.WOOL) ? 100
  353.                     : (item == Item.getItemFromBlock(Blocks.CARPET) ? 67
  354.                     : (item == Item.getItemFromBlock(Blocks.LADDER) ? 300
  355.                     : (item == Item.getItemFromBlock(Blocks.WOODEN_BUTTON) ? 100
  356.                     : (Block.getBlockFromItem(item).getDefaultState().getMaterial() == Material.WOOD ? 300
  357.                     : (item == Item.getItemFromBlock(Blocks.COAL_BLOCK) ? 16000
  358.                     : (item instanceof ItemTool && "WOOD".equals(((ItemTool)item).getToolMaterialName()) ? 200
  359.                     : (item instanceof ItemSword && "WOOD".equals(((ItemSword)item).getToolMaterialName()) ? 200
  360.                     : (item instanceof ItemHoe && "WOOD".equals(((ItemHoe)item).getMaterialName()) ? 200
  361.                     : (item == Items.STICK ? 100
  362.                     : (item != Items.BOW && item != Items.FISHING_ROD ? (item == Items.SIGN ? 200
  363.                     : (item == Items.COAL ? 1600
  364.                     : (item == Items.LAVA_BUCKET ? 20000
  365.                     : (item != Item.getItemFromBlock(Blocks.SAPLING) && item != Items.BOWL ? (item == Items.BLAZE_ROD ? 2400
  366.                     : (item instanceof ItemDoor && item != Items.IRON_DOOR ? 200 : (item instanceof ItemBoat ? 400 : 0)))
  367.                     //Custom Items
  368.                     : (item == eAngelusItems.mystalDust ? 3000
  369.  
  370.  
  371.                     : 100)))))
  372.                     : 300)))))))))));
  373.         }
  374.     }
  375.  
  376.     public static boolean isItemFuel(ItemStack fuel)
  377.     {
  378.         return getItemBurnTime(fuel) > 0;
  379.     }
  380. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement