Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.skyvox.api;
- import java.util.Collection;
- import java.util.Iterator;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- /**
- *
- * @author SkyVox
- *
- */
- public class InventoryItemManipulation {
- private boolean full;
- private int rest;
- // CONSTRUCTOR
- public InventoryItemManipulation(boolean full, int rest) {
- this.full = full;
- this.rest = rest;
- }
- public boolean isFull() {
- return full;
- }
- public boolean hasRest() {
- return rest > 0;
- }
- public int getRest() {
- return rest;
- }
- public void send(Player player, ItemStack item, Location location) {
- ItemStack clone = item.clone();
- if (!full) {
- clone.setAmount(clone.getAmount() - getRest());
- player.getInventory().addItem(clone);
- }
- if (hasRest()) {
- clone.setAmount(getRest());
- send(clone, location);
- }
- }
- private void send(ItemStack item, Location location) {
- location.getWorld().dropItem(location, item);
- }
- /*
- * Static Methods.
- */
- /**
- * Check if ItemStacks are similar.
- *
- * @param paramOne - Item to compare
- * @param paramTwo - Item to compare
- * @return if @paramOne is similar than @paramTwo
- */
- private static boolean isSimilar(ItemStack paramOne, ItemStack paramTwo) {
- return isSimilar(paramOne, paramTwo, true);
- }
- public static boolean isSimilar(ItemStack paramOne, ItemStack paramTwo, boolean value) {
- if ((paramOne == null) || (paramTwo == null)) return false;
- boolean meta = (paramOne.hasItemMeta() == paramTwo.hasItemMeta());
- if (meta && value) {
- meta = Bukkit.getItemFactory().equals(paramOne.getItemMeta(), paramTwo.getItemMeta());
- }
- if ((paramOne.getDurability() == paramTwo.getDurability()) && (paramOne.getType().toString().toUpperCase().equals(paramTwo.getType().toString().toUpperCase())) && (paramOne.getEnchantments().equals(paramTwo.getEnchantments())) && ((value && meta) || (!value))) return true;
- // These items are not similar.
- return false;
- }
- /**
- * This method will simple check
- * if player's inventory is full.
- *
- * @param player
- * @return if player inventory is full
- */
- public static boolean isFull(Player player) {
- ItemStack item = null;
- // Loop all inventory items, This will start on the hotbar slot.
- for (int i = player.getInventory().getSize(); i > 0; i--) {
- item = player.getInventory().getItem(i);
- // If the item is equals null or the Material type is AIR
- // This mean that have an empty slot.
- if ((item == null) || (item.getType() == Material.AIR)) {
- return false;
- }
- }
- // Player's inventory is full.
- return true;
- }
- /**
- * Check if player's inventory is full.
- * This will compare two items and its amount.
- *
- * @param player
- * @param drop - Item to compare
- * @return new Object of InventoryAPI
- */
- public static InventoryItemManipulation isFull(Player player, ItemStack drop) {
- ItemStack item = null;
- int item_amount = 0;
- int drop_amount = drop.getAmount();
- // Loop all inventory items, This will start on the hotbar slot.
- for (int i = player.getInventory().getSize(); i > 0; i--) {
- item = player.getInventory().getItem(i);
- if ((item == null) || (item.getType() == Material.AIR)) {
- if (drop_amount > 64) {
- drop_amount-=64;
- continue;
- }
- return new InventoryItemManipulation(false, 0);
- } else if (isSimilar(item, drop)) {
- item_amount = item.getAmount();
- if (item_amount >= item.getMaxStackSize()) {
- continue;
- } else {
- while (item_amount < item.getMaxStackSize()) {
- // Check if drop amount is less or equals than zero.
- if (drop_amount <= 0) return new InventoryItemManipulation(false, 0);
- drop_amount-=1;
- item_amount+=1;
- }
- }
- // Again check if drop amount is less or equals than zero.
- if (drop_amount <= 0) return new InventoryItemManipulation(false, 0);
- }
- }
- // Player's inventory is full.
- return new InventoryItemManipulation(drop_amount == drop.getAmount(), drop_amount);
- }
- /**
- * Check if player's inventory is full.
- * This will compare two items and its amount.
- *
- * @param player
- * @param drop - Item to compare instanced a Block
- * @return if player's inventory is full
- */
- public static InventoryItemManipulation isFull(Player player, Block drop) {
- Collection<ItemStack> items = drop.getDrops(player.getItemInHand());
- if ((items == null) || (items.isEmpty())) {
- return new InventoryItemManipulation(false, 0);
- }
- Iterator<ItemStack> item = items.iterator();
- if (item.hasNext()) {
- return isFull(player, item.next());
- }
- return new InventoryItemManipulation(false, 0);
- }
- /**
- * Check if a player has certain amount of
- * a item on his inventory.
- *
- * @param player
- * @param item - item to check
- * @param amount - amount to check
- * @return if player has certain amount of a item.
- */
- public static boolean has(Player player, ItemStack item, int amount) {
- return has(player, item, amount, true);
- }
- /**
- * Check if a player has certain amount of
- * a item on his inventory.
- *
- * @param player
- * @param item - item to check
- * @param amount - amount to check
- * @param value - compare display-name
- * @return if player has certain amount of a item.
- */
- public static boolean has(Player player, ItemStack item, int amount, boolean value) {
- if (item == null) return false;
- int player_item_amount = 0;
- for (ItemStack items : player.getInventory().all(item.getType()).values()) {
- if (player_item_amount >= amount) break;
- if (isSimilar(item, items, value)) {
- player_item_amount+=items.getAmount();
- }
- }
- return player_item_amount >= amount;
- }
- /**
- * Remove an specific item from player's inventory.
- *
- * @param player
- * @param item - item to compare
- * @param bypass - remove if item has different name?
- */
- public static void take(Player player, ItemStack item, boolean bypass) {
- // Call the take method.
- take(player, item, 1, bypass);
- }
- /**
- * Remove an specific item from player's inventory.
- *
- * If bypass==false, We'll check the name as well.
- * If bypass==true, Item name doesn't matter in this case.
- *
- * @param player
- * @param item - item to compare
- * @param amount - amount of item to take
- * @param bypass - remove if item has different name?
- */
- public static void take(Player player, ItemStack item, int amount, boolean bypass) {
- if (item == null) return;
- boolean display = false;
- if (!bypass) {
- if ((!(item.hasItemMeta())) || (!(item.getItemMeta().hasDisplayName()))) return;
- display = true;
- }
- if (!has(player, item, amount, display)) return;
- // Take this item.
- takeFromInventory(player, item, amount, display);
- }
- /**
- * Take a certain amount of item from
- * player's inventory.
- *
- * @param player
- * @param item - item to take
- * @param amount - amount of item to take
- * @param value - compare display-name
- * @throws NullPointerException
- */
- private static void takeFromInventory(Player player, ItemStack item, int amount, boolean value) throws NullPointerException {
- ItemStack items = null;
- int remove = 0;
- for (int i = player.getInventory().getSize(); i > -1; i--) {
- items = player.getInventory().getItem(i);
- if ((items == null) || (items.getType() == Material.AIR)) continue;
- if (!isSimilar(item, items, value)) continue;
- while ((items.getAmount() - remove) > 0) {
- if (amount <= 0) break;
- remove+=1;
- amount-=1;
- }
- // Set item amount.
- items.setAmount(items.getAmount() - remove);
- // Take this item.
- player.getInventory().clear(i);
- if (items.getAmount() > 0) {
- // Add the item again if item amount is more than one.
- player.getInventory().addItem(items);
- }
- remove = 0;
- if (amount <= 0) break;
- }
- // Update player inventory.
- player.updateInventory();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment