Advertisement
HalestormXV

Untitled

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