Esempio n. 1
0
 /**
  * Attempts to empty the specified {@link ItemStack} into the inventory starting at the specified
  * slot. While iterating through the inventories slots, if a slot is null the remainder of the
  * ItemStack will be put in the null slot, if a slot is not null but the {@link
  * org.spout.api.material.Material} of the two ItemStacks do not match, the iteration will
  * continue to the next slot, and if the slot is not null and the Material of the two ItemStacks
  * do match the two ItemStacks will attempt to be 'stacked'. After {@link
  * ItemStack#stack(org.spout.api.inventory.ItemStack)} has been called, if the specified ItemStack
  * is empty the call will return, otherwise it will continue iterating through the slots until
  * either the stack is empty, or the iteration has ended.
  *
  * @param firstSlot slot to start iteration at (inclusive)
  * @param lastSlot slot to end iteration at (inclusive)
  * @param item to attempt to add to the inventory
  */
 public void add(int firstSlot, int lastSlot, ItemStack item) {
   // First pass try to add to existing stacks, second pass, add to empty slots
   final boolean reversed = lastSlot < firstSlot;
   final int incr = reversed ? -1 : 1;
   for (int pass = 0; pass < 2; pass++) {
     for (int index = firstSlot;
         reversed ? (index >= lastSlot) : (index <= lastSlot);
         index += incr) {
       ItemStack slot = get(index);
       if (pass == 1) {
         if (slot == null) {
           set(index, item);
           item.setAmount(0);
           return;
         }
       }
       if (slot != null && slot.equalsIgnoreSize(item)) {
         slot.stack(item);
         set(index, slot);
       }
       if (item.isEmpty()) {
         return;
       }
     }
   }
 }
Esempio n. 2
0
 /**
  * Adds the amount of an {@link ItemStack} at the specified slot to the specified amount
  *
  * @param slot to set item at
  * @param amount to add to
  * @return true if amount was added
  */
 public boolean addAmount(int slot, int amount) {
   ItemStack item = get(slot);
   if (item != null) {
     setAmount(slot, item.getAmount() + amount);
     return true;
   }
   return false;
 }
Esempio n. 3
0
  /**
   * Gets the current music this Jukebox plays
   *
   * @return
   */
  public Music getMusic() {
    ItemStack current = inventory.getMusicSlot();
    if (!canPlay(current)) {
      return Music.NONE;
    }

    return ((MusicDisc) current.getSubMaterial()).getMusic();
  }
Esempio n. 4
0
 @Override
 public ArrayList<ItemStack> getDrops(Block block) {
   ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
   ItemStack held = VanillaPlayerUtil.getCurrentItem(block.getSource());
   if (held != null && held.getMaterial() instanceof Pickaxe) {
     drops.add(new ItemStack(this, 1));
   }
   return drops;
 }
Esempio n. 5
0
 /**
  * Replaces the {@link ItemStack} at the specified slot in this inventory with the specified
  * ItemStack.
  *
  * @param i slot to set the item at
  * @param item to set in the specified slot
  * @return the item previously at the slot
  */
 @Override
 public ItemStack set(int i, ItemStack item) {
   if (item != null && item.isEmpty()) {
     item = null;
   }
   ItemStack old = get(i);
   contents[i] = item == null ? null : item.clone();
   update(i);
   return old;
 }
Esempio n. 6
0
 /**
  * Sets the data of an {@link ItemStack} at the specified slot to the specified data.
  *
  * @param slot to set item at
  * @param data to set to
  */
 public void setData(int slot, int data) {
   ItemStack item = get(slot);
   if (item != null) {
     if (data < 1) {
       item = null;
     } else {
       item.setData(Math.min(data, item.getMaxData()));
     }
     set(slot, item);
   }
 }
  /**
   * Get a block.
   *
   * @param item
   */
  @Override
  public void fetchItem(BaseItem item) throws BlockBagException {
    final int id = item.getType();
    final int damage = item.getDamage();
    int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
    assert (amount == 1);
    boolean usesDamageValue = ItemType.usesDamageValue(id);

    if (id == BlockID.AIR) {
      throw new IllegalArgumentException("Can't fetch air block");
    }

    loadInventory();

    boolean found = false;

    for (int slot = 0; slot < items.length; ++slot) {
      ItemStack bukkitItem = items[slot];

      if (bukkitItem == null) {
        continue;
      }

      if (bukkitItem.getMaterial().getId() != id) {
        // Type id doesn't fit
        continue;
      }

      if (usesDamageValue && bukkitItem.getDamage() != damage) {
        // Damage value doesn't fit.
        continue;
      }

      int currentAmount = bukkitItem.getAmount();
      if (currentAmount < 0) {
        // Unlimited
        return;
      }

      if (currentAmount > 1) {
        bukkitItem.setAmount(currentAmount - 1);
        found = true;
      } else {
        items[slot] = null;
        found = true;
      }

      break;
    }

    if (!found) {
      throw new OutOfBlocksException();
    }
  }
Esempio n. 8
0
 /**
  * Sets the amount of an {@link ItemStack} at the specified slot to the specified amount
  *
  * @param slot to set item at
  * @param amount to set to
  */
 public void setAmount(int slot, int amount) {
   ItemStack item = get(slot);
   if (item != null) {
     if (amount < 1) {
       item = null;
     } else {
       item.setAmount(Math.min(amount, item.getMaxStackSize()));
     }
     set(slot, item);
   }
 }
Esempio n. 9
0
 /**
  * Whether the inventory contains the specified {@link ItemStack}. If the {@link Object} provided
  * is not an ItemStack or a {@link Material} it will return false. If the object passed in is a
  * Material, true will be returned if an ItemStack with that material is found with at least 1
  * amount.
  *
  * @param o ItemStack whose presence is to be tested in this test
  * @return true if specified ItemStack is present
  */
 @Override
 public boolean contains(Object o) {
   for (ItemStack item : contents) {
     if (item == null) {
       continue;
     }
     if (item.equals(o) || item.getMaterial().equals(o)) {
       return true;
     }
   }
   return false;
 }
Esempio n. 10
0
 /**
  * Gets the amount of the specified {@link Material} in the inventory.
  *
  * @param material to check
  * @return amount of specified material
  */
 public int getAmount(Material material) {
   int amount = 0;
   for (ItemStack item : contents) {
     if (item == null) {
       continue;
     }
     if (item.getMaterial().equals(material)) {
       amount += item.getAmount();
     }
   }
   return amount;
 }
Esempio n. 11
0
  @Override
  public boolean onShiftClick(ItemStack stack, int slot, Inventory from) {
    if (getPlayer().getEngine().getPlatform() == Platform.CLIENT) {
      throw new IllegalStateException("Shift click handling is handled server side.");
    }
    final PlayerInventory inventory = getPlayerInventory();

    // From main inventory/quickbar to the dispenser
    if (from instanceof PlayerMainInventory || from instanceof QuickbarInventory) {
      for (InventoryConverter conv : this.getInventoryConverters()) {
        Inventory inv = conv.getInventory();
        if (inv instanceof DispenserInventory) {
          Grid grid = inv.grid(3);
          final int height = grid.getHeight();
          final int length = grid.getLength();
          for (int row = height - 1; row >= 0; row--) {
            int startSlot = length * row;
            int endSlot = startSlot + length - 1;
            inv.add(startSlot, endSlot, stack);
            from.set(slot, stack);
            if (stack.isEmpty()) {
              return true;
            }
          }
        }
      }
    }

    // From chest to inventory/quickbar
    if (from instanceof DispenserInventory) {
      // To quickbar (reversed)
      final QuickbarInventory qbar = inventory.getQuickbar();
      qbar.add(qbar.size() - 1, 0, stack);
      from.set(slot, stack);
      if (stack.isEmpty()) {
        return true;
      }

      // To main inventory (reversed)
      final Inventory main = inventory.getMain();
      for (int row = 0; row < PlayerMainInventory.HEIGHT; row++) {
        int startSlot = PlayerMainInventory.LENGTH * row;
        int endSlot = startSlot + PlayerMainInventory.LENGTH - 1;
        main.add(endSlot, startSlot, stack);
        from.set(slot, stack);
        if (stack.isEmpty()) {
          return true;
        }
      }
    }
    return false;
  }
Esempio n. 12
0
 @Override
 public ArrayList<ItemStack> getDrops(Block block) {
   ArrayList<ItemStack> drops = new ArrayList<ItemStack>();
   if (block.getSource() instanceof Entity) {
     ItemStack held = ((Entity) block.getSource()).getInventory().getCurrentItem();
     if (held != null
         && held.getMaterial()
             .equals(VanillaMaterials.IRON_PICKAXE, VanillaMaterials.DIAMOND_PICKAXE)) {
       drops.add(new ItemStack(this, 1));
     }
   }
   return drops;
 }
Esempio n. 13
0
 /**
  * Returns the slot of the last occurrence of the specified element, or -1 if this inventory does
  * not contain the element.
  *
  * @param o element to search for
  * @return the slot of the last occurrence of the specified element
  */
 @Override
 public int lastIndexOf(Object o) {
   for (int i = contents.length - 1; i > -1; i--) {
     ItemStack item = get(i);
     if (item == null) {
       continue;
     }
     if (item.equals(o) || item.getMaterial().equals(o)) {
       return i;
     }
   }
   return -1;
 }
Esempio n. 14
0
  private void updateOutput() {
    Set<Recipe> recipes = Spout.getEngine().getRecipeManager().getAllRecipes();
    for (Recipe recipe : recipes) {
      boolean craft = false;
      Inventory grid = craftingGrid.getGridInventory();
      int[] gridArray = craftingGrid.getGridArray();
      if (recipe instanceof ShapelessRecipe) {
        List<Material> materials = new ArrayList<Material>(gridArray.length);
        for (int slot : gridArray) {
          ItemStack item = grid.getItem(slot);
          if (item != null) {
            materials.add(item.getMaterial());
          }
        }
        if (materials.containsAll(recipe.getIngredients())) {
          craft = true;
        }
      } else if (recipe instanceof ShapedRecipe) {
        ShapedRecipe shapedRecipe = (ShapedRecipe) recipe;
        List<List<Character>> rows = shapedRecipe.getRows();
        if (rows.size() > craftingGrid.getColumnSize()) {
          break;
        }

        int index = -1;
        for (List<Character> row : rows) {
          if (row.size() > craftingGrid.getRowSize()) {
            break;
          }

          HashMap<Character, Material> ingredientsMap = shapedRecipe.getIngredientsMap();
          for (Character character : row) {
            index++;
            Material req = ingredientsMap.get(character),
                actual = grid.getItem(gridArray[index]).getMaterial();
            if (!req.equals(actual)) {
              craft = false;
              break;
            }
            craft = true;
          }
        }
      }

      int outputSlot = craftingGrid.getOutputSlot();
      if (grid.getItem(outputSlot) == null && craft) {
        grid.setItem(outputSlot, recipe.getResult());
        break;
      }
    }
  }
Esempio n. 15
0
 /**
  * Sets the slot of the first occurrence of this {@link ItemStack} or {@link Material} to null.
  *
  * @param o ItemStack to be removed from the inventory
  * @return true if the inventory contained the specified ItemStack
  */
 @Override
 public boolean remove(Object o) {
   for (int i = 0; i < contents.length; i++) {
     ItemStack item = get(i);
     if (item == null) {
       continue;
     }
     if (item.equals(o) || item.getMaterial().equals(o)) {
       set(i, null);
       return true;
     }
   }
   return false;
 }
Esempio n. 16
0
 /**
  * Sets the slot of each element in the specified collection to null.
  *
  * @param objects to remove
  * @return true
  */
 @Override
 public boolean removeAll(Collection<?> objects) {
   Iterator<?> iter = objects.iterator();
   while (iter.hasNext()) {
     Object o = iter.next();
     for (int i = 0; i < contents.length; i++) {
       ItemStack item = get(i);
       if (item == null) {
         continue;
       }
       if (item.equals(o) || item.getMaterial().equals(o)) {
         set(i, null);
       }
     }
   }
   return true;
 }
Esempio n. 17
0
 @Override
 public boolean canDrop(Block block, ItemStack holding) {
   if (holding != null && holding.getMaterial() instanceof Spade) {
     return super.canDrop(block, holding);
   } else {
     return false;
   }
 }
Esempio n. 18
0
 private boolean updateOutput() {
   RecipeManager recipeManager = Spout.getEngine().getRecipeManager();
   Inventory grid = craftingGrid.getGridInventory();
   int[] gridArray = craftingGrid.getGridArray();
   int rowSize = craftingGrid.getRowSize();
   List<List<Material>> materials = new ArrayList<List<Material>>();
   List<Material> current = new ArrayList<Material>();
   List<Material> shapeless = new ArrayList<Material>();
   int cntr = 0;
   for (int slot : gridArray) {
     cntr++;
     ItemStack item = grid.getItem(slot);
     Material mat = null;
     if (item != null) {
       mat = item.getMaterial();
     }
     current.add(mat);
     if (mat != null) {
       shapeless.add(mat);
     }
     if (cntr >= rowSize) {
       materials.add(current);
       current = new ArrayList<Material>();
       cntr = 0;
     }
   }
   Recipe recipe = null;
   recipe = recipeManager.matchShapedRecipe(materials);
   if (recipe == null) {
     recipe = recipeManager.matchShapelessRecipe(shapeless);
   }
   if (recipe != null) {
     int outputSlot = craftingGrid.getOutputSlot();
     if (grid.getItem(outputSlot) == null) {
       grid.setItem(outputSlot, recipe.getResult());
     }
     return true;
   }
   return false;
 }
Esempio n. 19
0
 public boolean canPlay(ItemStack item) {
   return item != null && this.canPlay(item.getSubMaterial());
 }
Esempio n. 20
0
 public ItemStack clone() {
   ItemStack newStack = new ItemStack(material, amount, damage);
   newStack.setAuxData(auxData);
   return newStack;
 }
Esempio n. 21
0
  @Override
  public void onTick(float dt) {
    BrewingStandInventory inventory = getInventory();

    // Update brewing stand display with number of output slots filled
    int filledSlots = 0;
    if (inventory.hasOutput()) {
      for (int i = 0; i < 3; i++) {
        if (inventory.getOutput(i) != null) {
          filledSlots++;
        }
      }
    }
    VanillaMaterials.BREWING_STAND_BLOCK.setFilledPotionSlots(getBlock(), filledSlots);

    if (getBrewTime() <= 0) {
      // Try to start brewing
      if (inventory.hasInput() && inventory.hasOutput()) {
        if (inventory.hasOutput()) {
          // Ensure the input is able to brew the three output items
          for (int i = 0; i < 3; i++) {
            ItemStack output = inventory.getOutput(i);
            if (output == null) {
              continue;
            }

            if (((PotionReagent) inventory.getInput().getMaterial())
                    .getResult((PotionItem) output.getMaterial())
                == null) {
              return;
            }
          }
          input =
              inventory
                  .getInput(); // Store input just in case it is later removed during the brewing
                               // process
          setBrewTime(1);
          inventory.addAmount(BrewingStandInventory.INPUT_SLOT, -1);
        }
      }
    } else {
      // Continue brewing
      float newBrewTime = getBrewTime() + dt;
      if (newBrewTime > BREW_TIME_INCREMENT) {
        // Brewing has finished
        newBrewTime = -1;

        // Set output
        if (inventory.hasOutput()) {
          for (int i = 0; i < 3; i++) {
            ItemStack output = inventory.getOutput(i);
            if (output == null) {
              continue;
            }

            inventory.set(
                i,
                new ItemStack(
                    ((PotionReagent) input.getMaterial())
                        .getResult((PotionItem) output.getMaterial()),
                    1));
          }
        }
      }
      setBrewTime(newBrewTime);
    }
  }
Esempio n. 22
0
  /**
   * Store a block.
   *
   * @param item
   */
  @Override
  public void storeItem(BaseItem item) throws BlockBagException {
    final int id = item.getType();
    final int damage = item.getDamage();
    int amount = (item instanceof BaseItemStack) ? ((BaseItemStack) item).getAmount() : 1;
    assert (amount <= 64);
    boolean usesDamageValue = ItemType.usesDamageValue(id);

    if (id == BlockID.AIR) {
      throw new IllegalArgumentException("Can't store air block");
    }

    loadInventory();

    int freeSlot = -1;

    for (int slot = 0; slot < items.length; ++slot) {
      ItemStack bukkitItem = items[slot];

      if (bukkitItem == null) {
        // Delay using up a free slot until we know there are no stacks
        // of this item to merge into

        if (freeSlot == -1) {
          freeSlot = slot;
        }
        continue;
      }

      if (bukkitItem.getMaterial().getId() != id) {
        // Type id doesn't fit
        continue;
      }

      if (usesDamageValue && bukkitItem.getDamage() != damage) {
        // Damage value doesn't fit.
        continue;
      }

      int currentAmount = bukkitItem.getAmount();
      if (currentAmount < 0) {
        // Unlimited
        return;
      }
      if (currentAmount >= 64) {
        // Full stack
        continue;
      }

      int spaceLeft = 64 - currentAmount;
      if (spaceLeft >= amount) {
        bukkitItem.setAmount(currentAmount + amount);
        return;
      }

      bukkitItem.setAmount(64);
      amount -= spaceLeft;
    }

    if (freeSlot > -1) {
      items[freeSlot] = new ItemStack(MaterialData.getMaterial((short) id), amount);
      return;
    }

    throw new OutOfSpaceException(id);
  }