Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ContainerAdvanced extends Container
- {
- // Should hold all recipes that can be crafted...
- private Map<Map<IRecipe, ItemStack>, List<ItemStack>> recipesMapped = Maps.<Map<IRecipe, ItemStack>, List<ItemStack>> newLinkedHashMap();
- public IInventory outputInventory;
- public IInventory requiredItems;
- private InventoryPlayer playerInv;
- private World theWorld;
- private BlockPos pos;
- public ContainerAdvanced(InventoryPlayer playerInv, World world, BlockPos pos)
- {
- this.playerInv = playerInv;
- this.theWorld = world;
- this.pos = pos;
- this.outputInventory = new InventoryBasic("Crafting", true, 9)
- {
- public boolean isItemValidForSlot(int index, ItemStack stack)
- {
- return false;
- }
- public int getInventoryStackLimit()
- {
- return 64;
- }
- public void markDirty()
- {
- super.markDirty();
- ContainerAdvanced.this.onCraftMatrixChanged(this);
- }
- };
- this.requiredItems = new InventoryBasic("ReqItems", true, 9)
- {
- public boolean isItemValidForSlot(int index, ItemStack stack)
- {
- return false;
- }
- };
- for (int i = 0; i < 9; ++i)
- {
- this.addSlotToContainer(new Slot(this.requiredItems, i, 8 + i * 18, 10)
- {
- public boolean canTakeStack(EntityPlayer player)
- {
- return false;
- }
- public boolean isItemValid(ItemStack stack)
- {
- return false;
- }
- });
- }
- for (int i = 0; i < 9; ++i)
- {
- int i1 = i;
- this.addSlotToContainer(new Slot(this.outputInventory, i, 8 + i * 18, 53)
- {
- public boolean canTakeStack(EntityPlayer player)
- {
- if (i1 == 4)
- {
- return true;
- }
- return false;
- }
- public boolean isItemValid(ItemStack stack)
- {
- return false;
- }
- });
- }
- for (int y = 0; y < 3; ++y)
- {
- for (int x = 0; x < 9; ++x)
- {
- this.addSlotToContainer(new Slot(playerInv, x + y * 9 + 9, 8 + x * 18, 84 + y * 18)
- {
- public void onSlotChanged()
- {
- super.onSlotChanged();
- ContainerAdvanced.this.onCraftMatrixChanged(this.inventory);
- }
- });
- }
- }
- for (int x = 0; x < 9; ++x)
- {
- this.addSlotToContainer(new Slot(playerInv, x, 8 + x * 18, 142)
- {
- public void onSlotChanged()
- {
- super.onSlotChanged();
- ContainerAdvanced.this.onCraftMatrixChanged(this.inventory);
- }
- });
- }
- this.updateCraftables();
- this.scrollTo(0.0F);
- }
- private void updateCraftables()
- {
- this.recipesMapped.clear();
- for (IRecipe irecipe : CraftingManager.getInstance().getRecipeList())
- {
- if (irecipe.getRecipeOutput() == null || irecipe.getRecipeOutput().getItem() == null)
- {
- continue;
- }
- // Only care about these types of recipes as others get more complicated (may add support for others later)
- if (irecipe instanceof ShapedRecipes || irecipe instanceof ShapelessRecipes)
- {
- ItemStack output = irecipe.getRecipeOutput();
- Item item = output.getItem();
- List<ItemStack> subItems = Lists.<ItemStack> newArrayList();
- item.getSubItems(item, null, subItems);
- // Only add if player has the required items
- if (this.playerHasItems(this.getRequiredItemsFromRecipe(irecipe)))
- {
- Map<IRecipe, ItemStack> recipe = Maps.<IRecipe, ItemStack> newHashMap();
- recipe.put(irecipe, output);
- this.recipesMapped.put(recipe, subItems);
- }
- }
- }
- }
- public void scrollTo(float scrollPos)
- {
- int i = this.recipesMapped.size() - 1;
- int j = (int) ((double) (scrollPos * (float) i) + 0.5D) - 4;
- if (j < -4)
- {
- j = -4;
- }
- for (int k = 0; k < 9; ++k)
- {
- int index = k + j;
- Object[] keySet = this.recipesMapped.keySet().toArray();
- if (index >= 0 && index < this.recipesMapped.size())
- {
- ItemStack stack = this.recipesMapped.get(keySet[index]).get(0);
- if (!(stack.stackSize >= 1))
- {
- stack.stackSize = 1;
- }
- this.outputInventory.setInventorySlotContents(k, stack);
- // Index of the slot that items can be taken out of
- if (k == 4)
- {
- Object[] keys = ((Map<IRecipe, List<ItemStack>>) keySet[index]).keySet().toArray();
- for (int i1 = 0; i1 < keys.length; ++i1)
- {
- IRecipe recipe = (IRecipe) keys[i1];
- ItemStack[] items = this.getRequiredItemsFromRecipe(recipe);
- for (int j1 = 0; j1 < 9; ++j1)
- {
- if (j1 < items.length)
- {
- this.requiredItems.setInventorySlotContents(j1, items[j1]);
- }
- else
- {
- this.requiredItems.setInventorySlotContents(j1, null);
- }
- }
- }
- }
- }
- else
- {
- this.outputInventory.setInventorySlotContents(k, null);
- }
- }
- }
- private ItemStack[] getRequiredItemsFromRecipe(IRecipe recipe)
- {
- List<ItemStack> list = Lists.<ItemStack> newArrayList();
- if (recipe instanceof ShapedRecipes)
- {
- ShapedRecipes shapedrecipe = (ShapedRecipes) recipe;
- ItemStack[] recipeItems = shapedrecipe.recipeItems;
- for (ItemStack stack : recipeItems)
- {
- if (stack != null)
- {
- if (stack.getMetadata() == Short.MAX_VALUE)
- {
- // Fixes Blocks with no meta having a meta of 32767, and appearing with no texture
- list.add(new ItemStack(stack.getItem(), stack.stackSize, 0));
- }
- else
- {
- list.add(stack);
- }
- }
- }
- }
- else if (recipe instanceof ShapelessRecipes)
- {
- ShapelessRecipes shapelessrecipe = (ShapelessRecipes) recipe;
- ItemStack[] recipeItems = shapelessrecipe.recipeItems.toArray(new ItemStack[shapelessrecipe.recipeItems.size()]);
- for (ItemStack stack : recipeItems)
- {
- if (stack != null)
- {
- list.add(stack);
- }
- }
- }
- return list.toArray(new ItemStack[list.size()]);
- }
- private boolean playerHasItems(ItemStack[] items)
- {
- // If the player is in creative, then they can craft everything with no cost.
- if (!this.playerInv.player.capabilities.isCreativeMode)
- {
- for (ItemStack stack : items)
- {
- ItemStack match = this.findMatchingItems(stack, items);
- for (int i = 0; i < this.playerInv.mainInventory.length; ++i)
- {
- ItemStack invStack = this.findMatchingItems(this.playerInv.getStackInSlot(i), items);
- if (invStack != null)
- {
- if (match != null && invStack.stackSize >= match.stackSize)
- {
- if (invStack.getItem() == match.getItem() && invStack.getMetadata() == match.getMetadata())
- {
- if (!this.playerInv.hasItemStack(match))
- {
- return false;
- }
- }
- else
- {
- System.out.println("Match [" + match + "] does not equal invStack [" + invStack + "]");
- return false;
- }
- }
- else if (stack != null && invStack.stackSize >= stack.stackSize)
- {
- if (invStack.getItem() == stack.getItem() && invStack.getMetadata() == stack.getMetadata())
- {
- if (!this.playerInv.hasItemStack(stack))
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- }
- }
- return true;
- }
- public boolean canScroll()
- {
- return this.recipesMapped.size() > 9;
- }
- public void onContainerClosed(EntityPlayer player)
- {
- super.onContainerClosed(player);
- this.recipesMapped.clear();
- }
- public void onCraftMatrixChanged(IInventory inventory)
- {
- super.onCraftMatrixChanged(inventory);
- this.updateCraftables();
- }
- public Map<Map<IRecipe, ItemStack>, List<ItemStack>> getRecipes()
- {
- return Collections.<Map<IRecipe, ItemStack>, List<ItemStack>> unmodifiableMap(this.recipesMapped);
- }
- public boolean canInteractWith(EntityPlayer player)
- {
- 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;
- }
- public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
- {
- return super.transferStackInSlot(playerIn, index);
- }
- public boolean canMergeSlot(ItemStack stack, Slot slot)
- {
- return slot.yDisplayPosition > 84;
- }
- public boolean canDragIntoSlot(Slot slot)
- {
- return slot.inventory instanceof InventoryPlayer || slot.yDisplayPosition > 84;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement