Advertisement
cdrpaste

Untitled

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