Guest User

TileEntityAutoCraftingTable

a guest
Jul 20th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.02 KB | None | 0 0
  1. package com.earthcomputer.farmersheaven;
  2.  
  3. import net.minecraft.block.state.IBlockState;
  4. import net.minecraft.entity.item.EntityItem;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.inventory.Container;
  7. import net.minecraft.inventory.IInventory;
  8. import net.minecraft.inventory.ISidedInventory;
  9. import net.minecraft.inventory.InventoryCrafting;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.item.crafting.CraftingManager;
  12. import net.minecraft.item.crafting.IRecipe;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.server.gui.IUpdatePlayerListBox;
  16. import net.minecraft.tileentity.TileEntity;
  17. import net.minecraft.tileentity.TileEntityFurnace;
  18. import net.minecraft.util.BlockPos;
  19. import net.minecraft.util.ChatComponentText;
  20. import net.minecraft.util.ChatComponentTranslation;
  21. import net.minecraft.util.EnumFacing;
  22. import net.minecraft.util.IChatComponent;
  23. import net.minecraft.world.World;
  24. import net.minecraftforge.common.util.Constants;
  25. import net.minecraftforge.fml.relauncher.Side;
  26. import net.minecraftforge.fml.relauncher.SideOnly;
  27.  
  28. public class TileEntityAutoCraftingTable extends TileEntity implements IUpdatePlayerListBox, ISidedInventory
  29. {
  30.    
  31.     private Container container = new Container() {
  32.         @Override
  33.         public boolean canInteractWith(EntityPlayer player)
  34.         {
  35.             return true;
  36.         }
  37.        
  38.         @Override
  39.         public void onCraftMatrixChanged(IInventory inventory)
  40.         {
  41.             changeOutput();
  42.         }
  43.     };
  44.    
  45.     private InventoryCrafting invCrafting = new InventoryCrafting(container, 3, 3);
  46.     private ItemStack result;
  47.     private ItemStack fuelStack;
  48.     private IRecipe currentRecipe;
  49.     private int craftTime = 0;
  50.     public static final int MAX_CRAFT_TIME = 200;
  51.     private int clientSyncTime = 0;
  52.     private static final int MAX_CLIENT_SYNC_TIME = 10;
  53.     private int burnTime = 0;
  54.     private int maxBurnTime = 200;
  55.     private int powered = -1;
  56.    
  57.     private String customName;
  58.    
  59.     /** Whether to use packets to update the output slot of the tile entity */
  60.     private static final boolean USE_PACKETS = false;
  61.    
  62.     @Override
  63.     public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newState)
  64.     {
  65.         return oldState.getBlock() != newState.getBlock();
  66.     }
  67.    
  68.     @Override
  69.     public String getName()
  70.     {
  71.         return hasCustomName() ? customName : "container.autocrafting_table";
  72.     }
  73.    
  74.     @Override
  75.     public boolean hasCustomName()
  76.     {
  77.         return customName != null && customName.length() > 0;
  78.     }
  79.    
  80.     @Override
  81.     public IChatComponent getDisplayName()
  82.     {
  83.         return hasCustomName() ? new ChatComponentText(getName()) : new ChatComponentTranslation(getName());
  84.     }
  85.    
  86.     @Override
  87.     public int getSizeInventory()
  88.     {
  89.         return 11;
  90.     }
  91.    
  92.     public boolean getPowered()
  93.     {
  94.         if(!hasWorldObj()) return true;
  95.         if(powered == -1)
  96.         {
  97.             IBlockState state = worldObj.getBlockState(pos);
  98.             powered = (Boolean) state.getValue(BlockAutoCraftingTable.POWERED) ? 1 : 0;
  99.         }
  100.         return powered > 0;
  101.     }
  102.    
  103.     void setPowered(boolean powered)
  104.     {
  105.         this.powered = powered ? 1 : 0;
  106.     }
  107.    
  108.     public InventoryCrafting getInventoryCrafting()
  109.     {
  110.         return invCrafting;
  111.     }
  112.    
  113.     public int getCraftTime()
  114.     {
  115.         return craftTime;
  116.     }
  117.    
  118.     public int getBurnTime()
  119.     {
  120.         return burnTime;
  121.     }
  122.    
  123.     public int getMaxBurnTime()
  124.     {
  125.         return maxBurnTime;
  126.     }
  127.    
  128.     public void setCraftTime(int craftTime)
  129.     {
  130.         this.craftTime = craftTime;
  131.     }
  132.    
  133.     public void setBurnTime(int burnTime)
  134.     {
  135.         this.burnTime = burnTime;
  136.     }
  137.    
  138.     public void setMaxBurnTime(int maxBurnTime)
  139.     {
  140.         this.maxBurnTime = maxBurnTime;
  141.     }
  142.    
  143.     @Override
  144.     public ItemStack getStackInSlot(int index)
  145.     {
  146.         return index == 9 ? result : (index == 10 ? fuelStack : invCrafting.getStackInSlot(index));
  147.     }
  148.    
  149.     private void setStackInSlot(int index, ItemStack stack)
  150.     {
  151.         if(index == 9)
  152.         {
  153.             result = stack;
  154.         }
  155.         else if(index == 10)
  156.         {
  157.             fuelStack = stack;
  158.         }
  159.         else
  160.         {
  161.             invCrafting.setInventorySlotContents(index, stack);
  162.         }
  163.     }
  164.    
  165.     @Override
  166.     public ItemStack decrStackSize(int index, int count)
  167.     {
  168.         if(getStackInSlot(index) != null)
  169.         {
  170.             ItemStack stack;
  171.            
  172.             if(getStackInSlot(index).stackSize <= count)
  173.             {
  174.                 stack = getStackInSlot(index);
  175.                 setStackInSlot(index, null);
  176.                 return stack;
  177.             }
  178.             else
  179.             {
  180.                 stack = getStackInSlot(index).splitStack(count);
  181.                
  182.                 if(getStackInSlot(index).stackSize == 0)
  183.                 {
  184.                     setStackInSlot(index, null);
  185.                 }
  186.                
  187.                 return stack;
  188.             }
  189.         }
  190.         else
  191.         {
  192.             return null;
  193.         }
  194.     }
  195.    
  196.     @Override
  197.     @SideOnly(Side.CLIENT)
  198.     public ItemStack getStackInSlotOnClosing(int index)
  199.     {
  200.         if(getStackInSlot(index) != null)
  201.         {
  202.             ItemStack itemstack = getStackInSlot(index);
  203.             setStackInSlot(index, null);
  204.             return itemstack;
  205.         }
  206.         else
  207.         {
  208.             return null;
  209.         }
  210.     }
  211.    
  212.     @Override
  213.     public void setInventorySlotContents(int index, ItemStack stack)
  214.     {
  215.         boolean sameStackInSlot = stack != null && stack.isItemEqual(getStackInSlot(index))
  216.             && ItemStack.areItemStackTagsEqual(stack, getStackInSlot(index));
  217.         setStackInSlot(index, stack);
  218.        
  219.         if(stack != null && stack.stackSize > getInventoryStackLimit())
  220.         {
  221.             stack.stackSize = getInventoryStackLimit();
  222.         }
  223.        
  224.         if(index < 9 && !sameStackInSlot)
  225.         {
  226.             changeOutput();
  227.             markDirty();
  228.         }
  229.     }
  230.    
  231.     @Override
  232.     public int getInventoryStackLimit()
  233.     {
  234.         return 64;
  235.     }
  236.    
  237.     @Override
  238.     public boolean isUseableByPlayer(EntityPlayer player)
  239.     {
  240.         return worldObj.getTileEntity(pos) != this ? false : player.getDistanceSq(pos.getX() + 0.5D, pos.getY() + 0.5D,
  241.             pos.getZ() + 0.5D) <= 64.0D;
  242.     }
  243.    
  244.     @Override
  245.     public void openInventory(EntityPlayer player)
  246.     {
  247.     }
  248.    
  249.     @Override
  250.     public void closeInventory(EntityPlayer player)
  251.     {
  252.     }
  253.    
  254.     @Override
  255.     public boolean isItemValidForSlot(int index, ItemStack stack)
  256.     {
  257.         // true && (false || false)
  258.         return index != 9 && (index != 10 || TileEntityFurnace.isItemFuel(stack));
  259.     }
  260.    
  261.     @Override
  262.     public int getField(int id)
  263.     {
  264.         switch(id)
  265.         {
  266.             case 0:
  267.                 return craftTime;
  268.             case 1:
  269.                 return burnTime;
  270.             case 2:
  271.                 return maxBurnTime;
  272.             default:
  273.                 return 0;
  274.         }
  275.     }
  276.    
  277.     @Override
  278.     public void setField(int id, int value)
  279.     {
  280.         switch(id)
  281.         {
  282.             case 0:
  283.                 craftTime = value;
  284.                 break;
  285.             case 1:
  286.                 burnTime = value;
  287.                 break;
  288.             case 2:
  289.                 maxBurnTime = value;
  290.                 break;
  291.         }
  292.     }
  293.    
  294.     @Override
  295.     public int getFieldCount()
  296.     {
  297.         return 3;
  298.     }
  299.    
  300.     @Override
  301.     public void clear()
  302.     {
  303.         for(int i = 0; i < getSizeInventory(); i++)
  304.         {
  305.             setStackInSlot(i, null);
  306.         }
  307.     }
  308.    
  309.     @Override
  310.     public void readFromNBT(NBTTagCompound compound)
  311.     {
  312.         super.readFromNBT(compound);
  313.         NBTTagList craftMatrix = compound.getTagList("CraftMatrix", Constants.NBT.TAG_COMPOUND);
  314.         for(int i = 0; i < craftMatrix.tagCount(); i++)
  315.         {
  316.             NBTTagCompound stack = craftMatrix.getCompoundTagAt(i);
  317.             int slot = stack.getByte("Slot") & 255;
  318.             if(slot >= 0 && slot < 9) setStackInSlot(slot, ItemStack.loadItemStackFromNBT(stack));
  319.         }
  320.         NBTTagCompound result = compound.getCompoundTag("Result");
  321.         if(result.hasNoTags())
  322.             this.result = null;
  323.         else this.result = ItemStack.loadItemStackFromNBT(result);
  324.         NBTTagCompound fuel = compound.getCompoundTag("Fuel");
  325.         if(fuel.hasNoTags())
  326.             this.fuelStack = null;
  327.         else this.fuelStack = ItemStack.loadItemStackFromNBT(fuel);
  328.         changeOutput();
  329.         craftTime = compound.getInteger("CraftTime");
  330.         burnTime = compound.getInteger("BurnTime");
  331.         maxBurnTime = compound.getInteger("MaxBurnTime");
  332.         if(maxBurnTime <= 0) maxBurnTime = 200;
  333.         if(compound.hasKey("CustomName")) customName = compound.getString("CustomName");
  334.     }
  335.    
  336.     @Override
  337.     public void writeToNBT(NBTTagCompound compound)
  338.     {
  339.         super.writeToNBT(compound);
  340.         NBTTagList craftMatrix = new NBTTagList();
  341.         for(int i = 0; i < 9; i++)
  342.         {
  343.             if(getStackInSlot(i) != null)
  344.             {
  345.                 NBTTagCompound stack = new NBTTagCompound();
  346.                 getStackInSlot(i).writeToNBT(stack);
  347.                 stack.setByte("Slot", (byte) i);
  348.                 craftMatrix.appendTag(stack);
  349.             }
  350.         }
  351.         compound.setTag("CraftMatrix", craftMatrix);
  352.         if(result != null)
  353.         {
  354.             NBTTagCompound result = new NBTTagCompound();
  355.             this.result.writeToNBT(result);
  356.             compound.setTag("Result", result);
  357.         }
  358.         if(fuelStack != null)
  359.         {
  360.             NBTTagCompound fuel = new NBTTagCompound();
  361.             fuelStack.writeToNBT(fuel);
  362.             compound.setTag("Fuel", fuel);
  363.         }
  364.         compound.setInteger("CraftTime", craftTime);
  365.         compound.setInteger("BurnTime", burnTime);
  366.         compound.setInteger("MaxBurnTime", maxBurnTime);
  367.         if(hasCustomName()) compound.setString("CustomName", customName);
  368.     }
  369.    
  370.     @Override
  371.     public void update()
  372.     {
  373.         if(!worldObj.isRemote) updateOutput();
  374.         updateComparators();
  375.     }
  376.    
  377.     private void changeOutput()
  378.     {
  379.         if(currentRecipe == null || !currentRecipe.matches(invCrafting, worldObj)) craftTime = 0;
  380.         currentRecipe = null;
  381.         for(Object object : CraftingManager.getInstance().getRecipeList())
  382.         {
  383.             IRecipe recipe = (IRecipe) object;
  384.             if(recipe.matches(invCrafting, worldObj))
  385.             {
  386.                 currentRecipe = recipe;
  387.             }
  388.         }
  389.     }
  390.    
  391.     private void updateOutput()
  392.     {
  393.         burnTime = Math.max(burnTime - 1, 0);
  394.         boolean flag = currentRecipe == null;
  395.         flag = flag || currentRecipe.getRecipeOutput() == null;
  396.         if(result != null)
  397.         {
  398.             flag = flag || !ItemStack.areItemsEqual(currentRecipe.getRecipeOutput(), result);
  399.             flag = flag || !ItemStack.areItemStackTagsEqual(currentRecipe.getRecipeOutput(), result);
  400.             flag = flag || result.stackSize > result.getMaxStackSize() - currentRecipe.getRecipeOutput().stackSize;
  401.         }
  402.         if(flag)
  403.         {
  404.             craftTime = 0;
  405.         }
  406.         else
  407.         {
  408.             if(!isBurning())
  409.                 craftTime = Math.max(craftTime - 2, 0);
  410.             else
  411.             {
  412.                 if(burnTime <= 0 && !getPowered())
  413.                 {
  414.                     burnTime = TileEntityFurnace.getItemBurnTime(fuelStack);
  415.                     maxBurnTime = burnTime;
  416.                     ItemStack stack = fuelStack.copy();
  417.                     decrStackSize(10, 1);
  418.                     if(stack.getItem().hasContainerItem(fuelStack))
  419.                     {
  420.                         ItemStack containerItem = stack.getItem().getContainerItem(fuelStack).copy();
  421.                         if(fuelStack == null)
  422.                             setInventorySlotContents(10, containerItem);
  423.                         else
  424.                         {
  425.                             EntityItem entity = new EntityItem(worldObj);
  426.                             entity.setLocationAndAngles(pos.getX() + 0.5, pos.getY() + 0.2, pos.getZ() + 0.5, 0, 0);
  427.                             entity.setEntityItemStack(containerItem);
  428.                             worldObj.spawnEntityInWorld(entity);
  429.                         }
  430.                     }
  431.                 }
  432.                 if(!getPowered()) craftTime++;
  433.                 if(craftTime >= MAX_CRAFT_TIME)
  434.                 {
  435.                     craftTime = 0;
  436.                     ItemStack recipeOutput = currentRecipe.getRecipeOutput();
  437.                     if(result == null)
  438.                         result = recipeOutput.copy();
  439.                     else result.stackSize += recipeOutput.stackSize;
  440.                     ItemStack[] additionalOutputs = CraftingManager.getInstance().func_180303_b(invCrafting, worldObj);
  441.                     for(int i = 0; i < 9; i++)
  442.                     {
  443.                         decrStackSize(i, 1);
  444.                         if(additionalOutputs[i] != null)
  445.                         {
  446.                             if(getStackInSlot(i) == null)
  447.                             {
  448.                                 setInventorySlotContents(i, additionalOutputs[i]);
  449.                             }
  450.                             else
  451.                             {
  452.                                 EntityItem entity = new EntityItem(worldObj);
  453.                                 entity.setLocationAndAngles(pos.getX() + 0.5, pos.getY() + 0.2, pos.getZ() + 0.5, 0, 0);
  454.                                 entity.setEntityItemStack(additionalOutputs[i]);
  455.                                 worldObj.spawnEntityInWorld(entity);
  456.                             }
  457.                            
  458.                         }
  459.                     }
  460.                 }
  461.             }
  462.         }
  463.         if(USE_PACKETS)
  464.         {
  465.             clientSyncTime++;
  466.             if(clientSyncTime >= MAX_CLIENT_SYNC_TIME)
  467.             {
  468.                 FarmersHeaven.NETWORK.sendToAll(new AutoCraftingTableUpdateMessage(this));
  469.                 clientSyncTime = 0;
  470.             }
  471.         }
  472.     }
  473.    
  474.     void updateComparators()
  475.     {
  476.         worldObj.updateComparatorOutputLevel(pos, worldObj.getBlockState(pos).getBlock());
  477.         for(int x = -1; x <= 1; x++)
  478.             for(int z = -1; z <= 1; z++)
  479.             {
  480.                 BlockPos pos = new BlockPos(x, 1, z).add(this.pos);
  481.                 worldObj.updateComparatorOutputLevel(pos, worldObj.getBlockState(pos).getBlock());
  482.             }
  483.     }
  484.    
  485.     @Override
  486.     public int[] getSlotsForFace(EnumFacing side)
  487.     {
  488.         return side == EnumFacing.DOWN ? new int[] { 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 10 }
  489.             : (side == EnumFacing.UP ? new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 } : new int[] { 10 });
  490.     }
  491.    
  492.     @Override
  493.     public boolean canInsertItem(int index, ItemStack itemStack, EnumFacing direction)
  494.     {
  495.         return isItemValidForSlot(index, itemStack);
  496.     }
  497.    
  498.     @Override
  499.     public boolean canExtractItem(int index, ItemStack stack, EnumFacing direction)
  500.     {
  501.         if(index == 9)
  502.             return true;
  503.         else if(index == 10)
  504.             return !TileEntityFurnace.isItemFuel(getStackInSlot(10));
  505.         else return currentRecipe == null;
  506.     }
  507.    
  508.     public void setCustomName(String customName)
  509.     {
  510.         this.customName = customName;
  511.     }
  512.    
  513.     public boolean isCrafting()
  514.     {
  515.         return craftTime > 0;
  516.     }
  517.    
  518.     public int getScaleCraftingWidth(int maxWidth)
  519.     {
  520.         return craftTime * maxWidth / MAX_CRAFT_TIME;
  521.     }
  522.    
  523.     public boolean isBurning()
  524.     {
  525.         return fuelStack != null || burnTime > 0;
  526.     }
  527.    
  528.     public int getBurnTimeHeight(int maxHeight)
  529.     {
  530.         if(maxBurnTime == 0) maxBurnTime = 200;
  531.         return burnTime * maxHeight / maxBurnTime;
  532.     }
  533.    
  534.     public boolean shouldRenderFlame()
  535.     {
  536.         return isBurning() && burnTime > 0;
  537.     }
  538. }
Advertisement
Add Comment
Please, Sign In to add comment