Advertisement
HalestormXV

Untitled

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