Advertisement
HalestormXV

Untitled

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