public void fuelRemove(Player player, Material block, Material single, int cost) {

    Inventory fuelInventory = getFuelInventory(player);

    for (ItemStack item : fuelInventory.getContents())
      if (item != null) if (item.getAmount() <= 0) item.setType(Material.AIR);

    if (!blockStacks.split(fuelInventory, block, single, cost, (9 - cost))) {
      commonString.messageSend(
          RocketInit.getPlugin(),
          player,
          true,
          RocketLanguage.FuelOutage(single.toString().toLowerCase()));
      disableRocketBoots(player, true, true, true, true, true);
    }

    try {

      Material jacketType = player.getInventory().getChestplate().getType();
      FileConfiguration fuelConfig = getFuelConfig(player);

      fuelConfig.set(getFuelJacketConfigString(jacketType), fuelInventory.getContents());
      fuelConfig.save(getFuelFile(player));

    } catch (IOException e) {
      commonString.messageSend(
          RocketInit.getPlugin(), player, true, RocketLanguage.RB_JACKET_SAVE_ERROR);
      e.printStackTrace();
    }
  }
 @EventHandler
 public void onEnderChestOpen(InventoryOpenEvent e) {
   if (!(e.getPlayer() instanceof Player)) return;
   Player p = (Player) e.getPlayer();
   Inventory i = e.getInventory();
   if (i.getType() != InventoryType.ENDER_CHEST) return;
   Inventory ender = getEnderInventory(p);
   if (ender == null || ender.getContents().length < 1) return;
   i.setContents(ender.getContents());
 }
Esempio n. 3
0
 public static boolean equals(Inventory one, Inventory two) {
   if (one == null) return two == null;
   if (two == null) return false;
   if (!equals(one.getContents(), two.getContents())) return false;
   if (one instanceof PlayerInventory) {
     PlayerInventory pone = (PlayerInventory) one;
     if (two instanceof PlayerInventory) {
       PlayerInventory ptwo = (PlayerInventory) two;
       return equals(pone.getArmorContents(), ptwo.getArmorContents());
     } else {
       return false;
     }
   }
   return true;
 }
 private void checkInventory(Inventory inv, Set<BlockData> goals, Location loc) {
   for (ItemStack item : inv.getContents()) {
     BlockData bd = BlockData.fromItemStack(item);
     for (BlockData data : goals)
       if (data.equals(bd)) master.found.add(new _Entry<BlockData, Vector>(data, loc.toVector()));
   }
 }
Esempio n. 5
0
  private int findSpace(
      ItemStack item, Map<Integer, StackWithAmount> lookaside, boolean findEmpty) {
    // 1.9.4 and later extend living entity slots beyond 36, but some of these are read-only.
    // We could use .getStorageContents(), but this breaks 1.8.8 compatibility
    int length = (inventory.getHolder() instanceof HumanEntity) ? 36 : inventory.getSize();
    ItemStack[] stacks = Arrays.copyOfRange(inventory.getContents(), 0, length);

    if (item == null) {
      return -1;
    }

    for (int i = 0; i < stacks.length; i++) {
      boolean contains = lookaside.containsKey(i);
      if (findEmpty && !contains) {
        return i;
      } else if (!findEmpty && contains) {
        StackWithAmount compareWith = lookaside.get(i);
        if (compareWith != null) {
          ItemStack compareWithStack = compareWith.getStack();
          if (compareWith.getAmount() < compareWithStack.getMaxStackSize()
              && compareWithStack.isSimilar(item)) {
            return i;
          }
        }
      }
    }

    return -1;
  }
Esempio n. 6
0
 public void setInventory(int id, Inventory i) {
   List<ItemStack> actualItems = new ArrayList<ItemStack>();
   for (ItemStack is : i.getContents())
     if (is != null && is.getType() != Material.AIR) actualItems.add(is);
   this.inventory.put(
       id, new SerInventory(actualItems.toArray(new ItemStack[actualItems.size()])));
 }
Esempio n. 7
0
 /**
  * Shuffle the contents of the inventory, moving all items to random slots. 12
  *
  * @param inventory inventory to shuffle.
  */
 public static void shuffleContents(Inventory inventory) {
   ItemStack[] contents = inventory.getContents();
   ArrayUtils.shuffleArray(contents);
   for (int i = 0; i < contents.length; i++) {
     inventory.setItem(i, contents[i]);
   }
 }
Esempio n. 8
0
 /**
  * Counts how many of an item stack an inventory contains
  *
  * @param template the template to use, amount is ignored
  * @param inventory the inventory to check
  * @return the total number of items this inventory contains
  */
 public static int getCount(ItemStack template, Inventory inventory) {
   if (inventory == null) throw new IllegalArgumentException();
   int count = 0;
   for (ItemStack stack : inventory.getContents()) {
     if (stack != null && template.isSimilar(stack)) count += stack.getAmount();
   }
   return count;
 }
Esempio n. 9
0
 /**
  * Check whether or not the inventory is empty.
  *
  * @param inventory inventory to check
  * @return true if there's no items in the inventory, false otherwise.
  */
 public static boolean isEmpty(Inventory inventory) {
   for (ItemStack itemStack : inventory.getContents()) {
     if (itemStack != null) {
       return false;
     }
   }
   return true;
 }
Esempio n. 10
0
  public static void onPlayerDeath(final PlayerDeathEvent event) {
    final Player player = event.getEntity();
    final Inventory enderChest = player.getEnderChest();

    for (final ItemStack item : enderChest.getContents()) ItemUtil.dropItemNaturally(player, item);

    enderChest.clear();
  }
Esempio n. 11
0
 public static int countSimilar(Inventory inventory, ItemStack itemStack) {
   int ret = 0;
   for (ItemStack item : inventory.getContents()) {
     if (item == null) continue;
     if (!item.isSimilar(itemStack)) continue;
     ret += item.getAmount();
   }
   return ret;
 }
Esempio n. 12
0
  public ItemList<NamedItemStack> getOutputs() {
    ItemList<NamedItemStack> outputs = new ItemList<NamedItemStack>();
    Inventory inv = getInventory();
    if (mode.equals(CompactorMode.DECOMPACT)) {
      for (ItemStack is : inv.getContents()) {
        if (canDecompact(is)) {
          NamedItemStack clone =
              new NamedItemStack(
                  is.clone(),
                  is.getItemMeta().hasDisplayName()
                      ? is.getItemMeta().getDisplayName()
                      : is.getType().toString());
          clone.setAmount(clone.getMaxStackSize());
          ItemMeta cloneMeta = clone.getItemMeta();
          cloneMeta.setLore(null);
          clone.setItemMeta(cloneMeta);
          outputs.add(clone);

          return outputs;
        }
      }
    } else if (mode.equals(CompactorMode.COMPACT)) {
      for (ItemStack is : inv.getContents()) {
        if (canCompact(is)) {
          NamedItemStack clone =
              new NamedItemStack(
                  is.clone(),
                  is.getItemMeta().hasDisplayName()
                      ? is.getItemMeta().getDisplayName()
                      : is.getType().toString());
          clone.setAmount(1);
          ItemMeta cloneMeta = clone.getItemMeta();
          List<String> lore = new ArrayList<String>();
          lore.add(cp.getCompactLore());
          cloneMeta.setLore(lore);
          clone.setItemMeta(cloneMeta);

          outputs.add(clone);
          return outputs;
        }
      }
    }
    return outputs;
  }
Esempio n. 13
0
  private Map<Integer, StackWithAmount> getAmountsMapping() {
    Map<Integer, StackWithAmount> map = new LinkedHashMap<Integer, StackWithAmount>();
    ItemStack[] stacks = inventory.getContents();

    for (int i = 0; i < stacks.length; i++) {
      ItemStack stack = stacks[i];
      if (stack != null) map.put(i, new StackWithAmount(stack.getAmount(), stack));
    }

    return map;
  }
Esempio n. 14
0
  public void setWakeInformation(Location wakeLocation, Inventory wakeInventory) {
    this.wakeLocation = wakeLocation;
    if (wakeInventory == null) this.wakeInventory = null;
    else {
      this.wakeInventory = Bukkit.getServer().createInventory(null, InventoryType.PLAYER);
      this.wakeInventory.setContents(wakeInventory.getContents());
    }

    plugin
        .getConfiguration()
        .setDreamPlayerWakeInfo(playerName, this.wakeInventory, this.wakeLocation);
  }
Esempio n. 15
0
 public boolean checkAncientSword(final Inventory inv) {
   final ItemStack[] itemlist = inv.getContents();
   ItemStack item = null;
   int i;
   for (i = 0, i = 0; i <= (itemlist.length - 1); ++i) {
     item = itemlist[i];
     if (isAncientSword(item)) {
       return true;
     }
   }
   return false;
 }
Esempio n. 16
0
  /**
   * Counts the used slots in an inventory
   *
   * @param inventory Inventory to count from
   * @return Amount of used slots
   */
  public static int getStacks(Inventory inventory) {
    int cnt = 0;

    ItemStack[] items = inventory.getContents();
    for (ItemStack itemStack : items) {
      if (itemStack != null && itemStack.getTypeId() > 0 && itemStack.getAmount() > 0) {
        cnt++;
      }
    }

    return cnt;
  }
Esempio n. 17
0
 @EventHandler
 public void onInventoryClick(InventoryClickEvent event) {
   Inventory inventory = event.getInventory();
   if (inventory.getHolder() instanceof dNPC) {
     dNPC npc = (dNPC) inventory.getHolder();
     npc.getInventory().setContents(inventory.getContents());
     Equipment equipment = npc.getEquipmentTrait();
     for (int i = 0; i < 5; i++) {
       equipment.set(i, inventory.getItem(i));
     }
   }
 }
Esempio n. 18
0
  // Storage contents implementation has varied.
  // Before 1.9 it was the same as getContents().
  // From and including 1.9 it became the 36 first of those slots.
  public static ItemStack[] getContentsStorage(Inventory inventory) {
    if (inventory == null) return null;
    ItemStack[] contents = inventory.getContents();
    ItemStack[] ret = contents;

    PlayerInventory playerInventory = asPlayerInventory(inventory);
    if (playerInventory != null) {
      ret = range(contents, INDEX_PLAYER_STORAGE_FROM, INDEX_PLAYER_STORAGE_TO);
    }

    clean(ret);
    return ret;
  }
Esempio n. 19
0
  // All content varies over versions.
  // Before 1.9 it was getContents() + getArmorContents() + new ItemStack[1].
  // From and including 1.9 it's just getContents().
  public static ItemStack[] getContentsAll(Inventory inventory) {
    if (inventory == null) return null;
    ItemStack[] contents = inventory.getContents();
    ItemStack[] ret = contents;

    PlayerInventory playerInventory = asPlayerInventory(inventory);
    if (playerInventory != null && contents.length == SIZE_PLAYER_STORAGE) {
      ret = concat(contents, playerInventory.getArmorContents(), new ItemStack[SIZE_PLAYER_EXTRA]);
    }

    clean(ret);
    return ret;
  }
Esempio n. 20
0
  public static void setContentsStorage(Inventory inventory, ItemStack[] storage) {
    if (inventory == null) return;
    ItemStack[] contents = inventory.getContents();

    // Calculate exclusive maximum
    int max = Math.min(storage.length, contents.length);
    PlayerInventory playerInventory = asPlayerInventory(inventory);
    if (playerInventory != null) max = Math.min(max, INDEX_PLAYER_STORAGE_TO);

    // Set as much as possible
    for (int i = 0; i < max; i++) {
      inventory.setItem(i, storage[i]);
    }
  }
 public boolean upgradePearl(Inventory inv, PrisonPearl pp) {
   final UUID prisonerId = pp.getImprisonedId();
   final String prisoner;
   if (plugin.isNameLayerLoaded()) prisoner = NameAPI.getCurrentName(pp.getImprisonedId());
   else prisoner = Bukkit.getOfflinePlayer(prisonerId).getName();
   ItemStack is = new ItemStack(Material.ENDER_PEARL, 1);
   for (ItemStack existing_is : inv.getContents()) {
     if (existing_is == null || existing_is.getType() != Material.ENDER_PEARL) continue;
     int pearlslot = inv.first(existing_is);
     if (existing_is != null) {
       existing_is.setDurability((short) 0);
       ItemMeta existing_meta = existing_is.getItemMeta();
       if (existing_meta != null) {
         String existing_name = existing_meta.getDisplayName();
         List<String> lore = existing_meta.getLore();
         if (existing_name != null
             && prisoner != null
             && existing_name.compareTo(prisoner) == 0
             && lore != null
             && lore.size() == 3) {
           // This check says all existing stuff is there so return true.
           return true;
         } else if (existing_name != null
             && prisoner != null
             && existing_name.compareTo(prisoner) != 0)
           // If we don't have the right pearl keep looking.
           continue;
         else if (existing_name == null)
           // This pearl can't even be right so just return.
           return true;
       }
     }
     ItemMeta im = is.getItemMeta();
     // Rename pearl to that of imprisoned player
     im.setDisplayName(prisoner);
     List<String> lore = new ArrayList<String>();
     lore.add(prisoner + " is held within this pearl");
     lore.add("UUID: " + pp.getImprisonedId().toString());
     lore.add("Unique: " + pp.getUniqueIdentifier());
     // Given enchantment effect
     // Durability used because it doesn't affect pearl behaviour
     im.addEnchant(Enchantment.DURABILITY, 1, true);
     im.setLore(lore);
     is.setItemMeta(im);
     inv.clear(pearlslot);
     inv.setItem(pearlslot, is);
     return true;
   }
   return false;
 }
Esempio n. 22
0
  public static void setAllContents(Inventory from, Inventory to) {
    to.setContents(from.getContents());
    if (from instanceof PlayerInventory) {
      PlayerInventory pfrom = (PlayerInventory) from;
      if (to instanceof PlayerInventory) {
        PlayerInventory pto = (PlayerInventory) to;

        pto.setHelmet(pfrom.getHelmet());
        pto.setChestplate(pfrom.getChestplate());
        pto.setLeggings(pfrom.getLeggings());
        pto.setBoots(pfrom.getBoots());
      }
    }
  }
Esempio n. 23
0
  public ItemList<NamedItemStack> getInputs() {
    ItemList<NamedItemStack> inputs = new ItemList<NamedItemStack>();
    Inventory inv = getInventory();
    if (mode.equals(CompactorMode.DECOMPACT)) {
      for (ItemStack is : inv.getContents()) {
        if (canDecompact(is)) {
          NamedItemStack clone =
              new NamedItemStack(
                  is.clone(),
                  is.getItemMeta().hasDisplayName()
                      ? is.getItemMeta().getDisplayName()
                      : is.getType().toString());
          clone.setAmount(1);
          inputs.add(clone);

          return inputs;
        }
      }
    } else if (mode.equals(CompactorMode.COMPACT)) {
      for (ItemStack is : inv.getContents()) {
        if (canCompact(is)) {
          NamedItemStack clone =
              new NamedItemStack(
                  is.clone(),
                  is.getItemMeta().hasDisplayName()
                      ? is.getItemMeta().getDisplayName()
                      : is.getType().toString());
          inputs.add(clone);

          inputs.addAll(cp.getRecipeMaterials());
          return inputs;
        }
      }
    }
    return inputs;
  }
 private List<ItemStack> getBooksFromChest() {
   List<ItemStack> list = new ArrayList<ItemStack>();
   Block chest = this.getChest();
   if (Utils.isChest(chest.getType())) {
     Inventory inv = ((Chest) chest.getState()).getInventory();
     for (ItemStack item : inv.getContents()) {
       if (item != null
           && item.getType() == Material.WRITTEN_BOOK
           && this.isBookAuthoredByShopOwner(item)) {
         list.add(item);
       }
     }
   }
   return list;
 }
Esempio n. 25
0
 public void setBankSize(Player player, int size) {
   bank = banks.get(player.getName());
   Inventory inv = bank.getInv();
   ItemStack gt = inv.getItem(inv.getSize() - 2);
   ItemStack st = inv.getItem(inv.getSize() - 1);
   inv.remove(gt);
   inv.remove(st);
   bank.setBankSize(size);
   Inventory in = Bukkit.createInventory(player, size, player.getName() + " - Bank Chest");
   in.setContents(inv.getContents());
   bank.setInv(in);
   in.setItem(in.getSize() - 1, st);
   in.setItem(in.getSize() - 2, gt);
   sql.updateInv(player.getName(), size, in);
 }
Esempio n. 26
0
  /**
   * Creates a new save entry for the current state of the given inventory. The inventory must have
   * {@link BetterEnderInventoryHolder} as its holder.
   *
   * @param inventory The inventory to save.
   * @throws IOException If the inventory could not be converted to JSON, for whatever reason.
   */
  public SaveEntry(Inventory inventory) throws IOException {
    BetterEnderInventoryHolder holder = BetterEnderInventoryHolder.of(inventory);
    this.chestOwner = holder.getChestOwner();
    this.group = holder.getWorldGroup();
    this.chestRestrictions = holder.getChestRestrictions();

    // Store clones of all item stacks (the stacks are going to be
    // serialized on another thread, so we can't use the live sticks)
    ItemStack[] stackView = inventory.getContents();
    stacks = new ItemStack[stackView.length];
    for (int i = 0; i < stacks.length; i++) {
      ItemStack original = stackView[i];
      if (original != null) {
        stacks[i] = stackView[i].clone();
      }
    }
  }
Esempio n. 27
0
  public ItemStack getTool() {
    Dispenser disp = (Dispenser) getDispenser().getState();
    Inventory inv = disp.getInventory();
    for (ItemStack item : inv.getContents()) {
      if (item == null) continue;

      if (item.getType() == Material.DIAMOND_AXE
          || item.getType() == Material.GOLD_AXE
          || item.getType() == Material.IRON_AXE
          || item.getType() == Material.STONE_AXE
          || item.getType() == Material.WOOD_AXE) {
        return item;
      }
    }

    return null;
  }
Esempio n. 28
0
  /**
   * Checks whether the given isItemsToTake can be taken from the inventory as a whole
   *
   * @param isItemsToTake Items to attempt to take
   * @return True if the required items are available
   */
  public boolean hasItems(ItemStack[] isItemsToTake) {
    ItemStack[] isBackup = getBackupItemStack(isItemsToTake);

    HashMap<ItemStack, Integer> mItemsToTake = StackToMap(isBackup);
    HashMap<ItemStack, Integer> mInventory = StackToMap(inventory.getContents());

    for (Map.Entry<ItemStack, Integer> entry : mItemsToTake.entrySet()) {
      if (mInventory.containsKey(entry.getKey())) {
        if (mInventory.get(entry.getKey()) < entry.getValue()) {
          return false;
        }
      } else {
        return false;
      }
    }

    return true;
  }
Esempio n. 29
0
 @Override
 protected void execute(Event event) {
   Location l = location.getSingle(event);
   Player p = player.getSingle(event);
   if (l == null) {
     return;
   } else if (p == null) {
     return;
   }
   Location loc = p.getLocation().clone();
   Inventory inv = p.getInventory();
   for (ItemStack item : inv.getContents()) {
     if (item != null) {
       loc.getWorld().dropItem(l, item.clone());
     }
   }
   inv.clear();
 }
Esempio n. 30
0
  public static boolean isEmpty(Inventory inv) {
    if (inv == null) return true;

    for (ItemStack itemStack : inv.getContents()) {
      if (isSomething(itemStack)) return false;
    }

    if (inv instanceof PlayerInventory) {
      PlayerInventory pinv = (PlayerInventory) inv;

      if (isSomething(pinv.getHelmet())) return false;
      if (isSomething(pinv.getChestplate())) return false;
      if (isSomething(pinv.getLeggings())) return false;
      if (isSomething(pinv.getBoots())) return false;
    }

    return true;
  }