Advertisement
Jedispencer21

Crafting Bench Code

Sep 10th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.26 KB | None | 0 0
  1. public class ContainerAdvanced extends Container
  2. {
  3.     // Should hold all recipes that can be crafted...
  4.     private Map<Map<IRecipe, ItemStack>, List<ItemStack>> recipesMapped = Maps.<Map<IRecipe, ItemStack>, List<ItemStack>> newLinkedHashMap();
  5.     public IInventory outputInventory;
  6.     public IInventory requiredItems;
  7.     private InventoryPlayer playerInv;
  8.     private World theWorld;
  9.     private BlockPos pos;
  10.  
  11.     public ContainerAdvanced(InventoryPlayer playerInv, World world, BlockPos pos)
  12.     {
  13.         this.playerInv = playerInv;
  14.         this.theWorld = world;
  15.         this.pos = pos;
  16.         this.outputInventory = new InventoryBasic("Crafting", true, 9)
  17.         {
  18.             public boolean isItemValidForSlot(int index, ItemStack stack)
  19.             {
  20.                 return false;
  21.             }
  22.  
  23.             public int getInventoryStackLimit()
  24.             {
  25.                 return 64;
  26.             }
  27.  
  28.             public void markDirty()
  29.             {
  30.                 super.markDirty();
  31.                 ContainerAdvanced.this.onCraftMatrixChanged(this);
  32.             }
  33.         };
  34.         this.requiredItems = new InventoryBasic("ReqItems", true, 9)
  35.         {
  36.             public boolean isItemValidForSlot(int index, ItemStack stack)
  37.             {
  38.                 return false;
  39.             }
  40.         };
  41.  
  42.         for (int i = 0; i < 9; ++i)
  43.         {
  44.             this.addSlotToContainer(new Slot(this.requiredItems, i, 8 + i * 18, 10)
  45.             {
  46.                 public boolean canTakeStack(EntityPlayer player)
  47.                 {
  48.                     return false;
  49.                 }
  50.  
  51.                 public boolean isItemValid(ItemStack stack)
  52.                 {
  53.                     return false;
  54.                 }
  55.             });
  56.         }
  57.  
  58.         for (int i = 0; i < 9; ++i)
  59.         {
  60.             int i1 = i;
  61.  
  62.             this.addSlotToContainer(new Slot(this.outputInventory, i, 8 + i * 18, 53)
  63.             {
  64.                 public boolean canTakeStack(EntityPlayer player)
  65.                 {
  66.                     if (i1 == 4)
  67.                     {
  68.                         return true;
  69.                     }
  70.                     return false;
  71.                 }
  72.  
  73.                 public boolean isItemValid(ItemStack stack)
  74.                 {
  75.                     return false;
  76.                 }
  77.             });
  78.         }
  79.  
  80.         for (int y = 0; y < 3; ++y)
  81.         {
  82.             for (int x = 0; x < 9; ++x)
  83.             {
  84.                 this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18)
  85.                 {
  86.                     public void onSlotChanged()
  87.                     {
  88.                         super.onSlotChanged();
  89.                         ContainerAdvanced.this.onCraftMatrixChanged(this.inventory);
  90.                     }
  91.                 });
  92.             }
  93.         }
  94.  
  95.         for (int x = 0; x < 9; ++x)
  96.         {
  97.             this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 142)
  98.             {
  99.                 public void onSlotChanged()
  100.                 {
  101.                     super.onSlotChanged();
  102.                     ContainerAdvanced.this.onCraftMatrixChanged(this.inventory);
  103.                 }
  104.             });
  105.         }
  106.  
  107.         this.updateCraftables();
  108.         this.scrollTo(0.0F);
  109.     }
  110.  
  111.     private void updateCraftables()
  112.     {
  113.         this.recipesMapped.clear();
  114.  
  115.         for (IRecipe irecipe : CraftingManager.getInstance().getRecipeList())
  116.         {
  117.             if (irecipe.getRecipeOutput() == null || irecipe.getRecipeOutput().getItem() == null)
  118.             {
  119.                 continue;
  120.             }
  121.  
  122.             // Only care about these types of recipes as others get more complicated (may add support for others later)
  123.             if (irecipe instanceof ShapedRecipes || irecipe instanceof ShapelessRecipes)
  124.             {
  125.                 ItemStack output = irecipe.getRecipeOutput();
  126.                 Item item = output.getItem();
  127.                 List<ItemStack> subItems = Lists.<ItemStack> newArrayList();
  128.                 item.getSubItems(item, null, subItems);
  129.  
  130.                 // Only add if player has the required items
  131.                 if (this.playerHasItems(this.getRequiredItemsFromRecipe(irecipe)))
  132.                 {
  133.                     Map<IRecipe, ItemStack> recipe = Maps.<IRecipe, ItemStack> newHashMap();
  134.                     recipe.put(irecipe, output);
  135.                     this.recipesMapped.put(recipe, subItems);
  136.                 }
  137.             }
  138.         }
  139.     }
  140.  
  141.     public void scrollTo(float scrollPos)
  142.     {
  143.         int i = this.recipesMapped.size() - 1;
  144.         int j = (int) ((double) (scrollPos * (float) i) + 0.5D) - 4;
  145.  
  146.         if (j < -4)
  147.         {
  148.             j = -4;
  149.         }
  150.  
  151.         for (int k = 0; k < 9; ++k)
  152.         {
  153.             int index = k + j;
  154.             Object[] keySet = this.recipesMapped.keySet().toArray();
  155.  
  156.             if (index >= 0 && index < this.recipesMapped.size())
  157.             {
  158.                 ItemStack stack = this.recipesMapped.get(keySet[index]).get(0);
  159.  
  160.                 if (!(stack.stackSize >= 1))
  161.                 {
  162.                     stack.stackSize = 1;
  163.                 }
  164.  
  165.                 this.outputInventory.setInventorySlotContents(k, stack);
  166.  
  167.                 // Index of the slot that items can be taken out of
  168.                 if (k == 4)
  169.                 {
  170.                     Object[] keys = ((Map<IRecipe, List<ItemStack>>) keySet[index]).keySet().toArray();
  171.  
  172.                     for (int i1 = 0; i1 < keys.length; ++i1)
  173.                     {
  174.                         IRecipe recipe = (IRecipe) keys[i1];
  175.                         ItemStack[] items = this.getRequiredItemsFromRecipe(recipe);
  176.  
  177.                         for (int j1 = 0; j1 < 9; ++j1)
  178.                         {
  179.                             if (j1 < items.length)
  180.                             {
  181.                                 this.requiredItems.setInventorySlotContents(j1, items[j1]);
  182.                             }
  183.                             else
  184.                             {
  185.                                 this.requiredItems.setInventorySlotContents(j1, null);
  186.                             }
  187.                         }
  188.                     }
  189.                 }
  190.             }
  191.             else
  192.             {
  193.                 this.outputInventory.setInventorySlotContents(k, null);
  194.             }
  195.         }
  196.     }
  197.  
  198.     private ItemStack[] getRequiredItemsFromRecipe(IRecipe recipe)
  199.     {
  200.         List<ItemStack> list = Lists.<ItemStack> newArrayList();
  201.  
  202.         if (recipe instanceof ShapedRecipes)
  203.         {
  204.             ShapedRecipes shapedrecipe = (ShapedRecipes) recipe;
  205.             ItemStack[] recipeItems = shapedrecipe.recipeItems;
  206.  
  207.             for (ItemStack stack : recipeItems)
  208.             {
  209.                 if (stack != null)
  210.                 {
  211.                     if (stack.getMetadata() == Short.MAX_VALUE)
  212.                     {
  213.                         // Fixes Blocks with no meta having a meta of 32767, and appearing with no texture
  214.                         list.add(new ItemStack(stack.getItem(), stack.stackSize, 0));
  215.                     }
  216.                     else
  217.                     {
  218.                         list.add(stack);
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.         else if (recipe instanceof ShapelessRecipes)
  224.         {
  225.             ShapelessRecipes shapelessrecipe = (ShapelessRecipes) recipe;
  226.             ItemStack[] recipeItems = shapelessrecipe.recipeItems.toArray(new ItemStack[shapelessrecipe.recipeItems.size()]);
  227.  
  228.             for (ItemStack stack : recipeItems)
  229.             {
  230.                 if (stack != null)
  231.                 {
  232.                     list.add(stack);
  233.                 }
  234.             }
  235.         }
  236.  
  237.         return list.toArray(new ItemStack[list.size()]);
  238.     }
  239.  
  240.     private boolean playerHasItems(ItemStack[] items)
  241.     {
  242.         // If the player is in creative, then they can craft everything with no cost.
  243.         if (!this.playerInv.player.capabilities.isCreativeMode)
  244.         {
  245.             for (ItemStack stack : items)
  246.             {
  247.                 ItemStack match = this.findMatchingItems(stack, items);
  248.  
  249.                 for (int i = 0; i < this.playerInv.mainInventory.length; ++i)
  250.                 {
  251.                     ItemStack invStack = this.findMatchingItems(this.playerInv.getStackInSlot(i), items);
  252.  
  253.                     if (invStack != null)
  254.                     {
  255.                         if (match != null && invStack.stackSize >= match.stackSize)
  256.                         {
  257.                             if (invStack.getItem() == match.getItem() && invStack.getMetadata() == match.getMetadata())
  258.                             {
  259.                                 if (!this.playerInv.hasItemStack(match))
  260.                                 {
  261.                                     return false;
  262.                                 }
  263.                             }
  264.                             else
  265.                             {
  266.                                 System.out.println("Match [" + match + "] does not equal invStack [" + invStack + "]");
  267.                                 return false;
  268.                             }
  269.                         }
  270.                         else if (stack != null && invStack.stackSize >= stack.stackSize)
  271.                         {
  272.                             if (invStack.getItem() == stack.getItem() && invStack.getMetadata() == stack.getMetadata())
  273.                             {
  274.                                 if (!this.playerInv.hasItemStack(stack))
  275.                                 {
  276.                                     return false;
  277.                                 }
  278.                             }
  279.                             else
  280.                             {
  281.                                 return false;
  282.                             }
  283.                         }
  284.                         else
  285.                         {
  286.                             return false;
  287.                         }
  288.                     }
  289.                 }
  290.             }
  291.         }
  292.  
  293.         return true;
  294.     }
  295.  
  296.     public boolean canScroll()
  297.     {
  298.         return this.recipesMapped.size() > 9;
  299.     }
  300.  
  301.     public void onContainerClosed(EntityPlayer player)
  302.     {
  303.         super.onContainerClosed(player);
  304.         this.recipesMapped.clear();
  305.     }
  306.  
  307.     public void onCraftMatrixChanged(IInventory inventory)
  308.     {
  309.         super.onCraftMatrixChanged(inventory);
  310.         this.updateCraftables();
  311.     }
  312.  
  313.     public Map<Map<IRecipe, ItemStack>, List<ItemStack>> getRecipes()
  314.     {
  315.         return Collections.<Map<IRecipe, ItemStack>, List<ItemStack>> unmodifiableMap(this.recipesMapped);
  316.     }
  317.  
  318.     public boolean canInteractWith(EntityPlayer player)
  319.     {
  320.         return this.theWorld.getBlockState(this.pos).getBlock() != ModBlocks.CRAFTING_BENCH ? false : player.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D;
  321.     }
  322.  
  323.     public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
  324.     {
  325.         return super.transferStackInSlot(playerIn, index);
  326.     }
  327.  
  328.     public boolean canMergeSlot(ItemStack stack, Slot slot)
  329.     {
  330.         return slot.yDisplayPosition > 84;
  331.     }
  332.  
  333.     public boolean canDragIntoSlot(Slot slot)
  334.     {
  335.         return slot.inventory instanceof InventoryPlayer || slot.yDisplayPosition > 84;
  336.     }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement