Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.bukkit.Material;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- public class InventoryHelper {
- public static int getItemAmount(final Player player, final ItemStack is) {
- int amount = 0;
- ItemStack[] contents;
- for (int length = (contents = player.getInventory().getContents()).length, i = 0; i < length; ++i) {
- final ItemStack itemStack = contents[i];
- if(is.hasItemMeta() && is.getItemMeta().hasDisplayName()) {
- if (itemStack != null && itemStack.getType() == is.getType() && itemStack.getDurability() == is.getDurability()
- && (itemStack.hasItemMeta() && itemStack.getItemMeta().hasDisplayName() && itemStack.getItemMeta().equals(is.getItemMeta()))) {
- amount += itemStack.getAmount();
- }
- }else{
- if (itemStack != null && itemStack.getType() == is.getType() && itemStack.getDurability() == is.getDurability()) {
- amount += itemStack.getAmount();
- }
- }
- }
- return amount;
- }
- public static void removeItemAmount(Player player, Material material, byte data, int amount){
- removeItems(new ItemStack(material, 1, data),player,amount);
- }
- public static void removeItems(final ItemStack is, final Player p, int amount) {
- ItemStack[] contents;
- for (int length = (contents = p.getInventory().getContents()).length, i = 0; i < length; ++i) {
- final ItemStack itemStack = contents[i];
- if(is.hasItemMeta() && is.getItemMeta().hasDisplayName()){
- if (amount > 0 && itemStack != null && itemStack.getType() == is.getType() && itemStack.getDurability() == is.getDurability()
- && (itemStack.hasItemMeta() && itemStack.getItemMeta().hasDisplayName() && itemStack.getItemMeta().equals(is.getItemMeta()))
- ) {
- if (itemStack.getAmount() > amount) {
- itemStack.setAmount(itemStack.getAmount() - amount);
- amount = 0;
- }
- else {
- amount -= itemStack.getAmount();
- p.getInventory().removeItem(itemStack);
- }
- }
- }else{
- if (amount > 0 && itemStack != null && itemStack.getType() == is.getType() && itemStack.getDurability() == is.getDurability()) {
- if (itemStack.getAmount() > amount) {
- itemStack.setAmount(itemStack.getAmount() - amount);
- amount = 0;
- }
- else {
- amount -= itemStack.getAmount();
- p.getInventory().removeItem(itemStack);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement