From c12c1ac7652a1dfc49686f6d4fa6cb8e10970045 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 22 Mar 2013 10:20:11 +0100 Subject: [PATCH] Working with InventoryUtil and removing NMS references. --- .../mcore/adapter/InventoryAdapter.java | 22 +------- .../mcore/util/InventoryUtil.java | 55 +++++++++++++++---- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/com/massivecraft/mcore/adapter/InventoryAdapter.java b/src/com/massivecraft/mcore/adapter/InventoryAdapter.java index a76a23ef..5f5c9c45 100644 --- a/src/com/massivecraft/mcore/adapter/InventoryAdapter.java +++ b/src/com/massivecraft/mcore/adapter/InventoryAdapter.java @@ -2,7 +2,7 @@ package com.massivecraft.mcore.adapter; import java.lang.reflect.Type; -import org.bukkit.craftbukkit.v1_5_R1.inventory.CraftInventoryCustom; +import org.bukkit.Bukkit; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -80,29 +80,11 @@ public class InventoryAdapter implements JsonDeserializer, JsonSerial itemStacks[i] = itemStack; } - Inventory ret = new CraftInventoryCustom(null, size, "items"); + Inventory ret = Bukkit.createInventory(null, size); ret.setContents(itemStacks); return ret; } - // -------------------------------------------- // - // UTIL - // -------------------------------------------- // - - // This utility is nice to have in many cases :) - public static boolean isInventoryEmpty(Inventory inv) - { - if (inv == null) return true; - for (ItemStack stack : inv.getContents()) - { - if (stack == null) continue; - if (stack.getAmount() == 0) continue; - if (stack.getTypeId() == 0) continue; - return false; - } - return true; - } - // -------------------------------------------- // // INSTANCE // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore/util/InventoryUtil.java b/src/com/massivecraft/mcore/util/InventoryUtil.java index 48c418c3..c50d1bda 100644 --- a/src/com/massivecraft/mcore/util/InventoryUtil.java +++ b/src/com/massivecraft/mcore/util/InventoryUtil.java @@ -3,7 +3,7 @@ package com.massivecraft.mcore.util; import java.util.HashMap; import org.bukkit.Bukkit; -import org.bukkit.craftbukkit.v1_5_R1.inventory.CraftInventoryCustom; +import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; @@ -11,27 +11,58 @@ import org.bukkit.inventory.ItemStack; public class InventoryUtil { // -------------------------------------------- // - // CREATE + // EVENT INTERPRETATION // -------------------------------------------- // - public static Inventory create(int size) + public static boolean isGiving(InventoryClickEvent event) { - return create(null, size, "Chest"); + // Was the upper inventory clicked? + boolean upperClicked = event.getRawSlot() < event.getInventory().getSize(); + + boolean ret = false; + + if (upperClicked) + { + ret = (event.getCursor() != null && event.getCursor().getAmount() > 0); + } + else + { + ret = event.isShiftClick(); + } + + return ret; } - public static Inventory create(InventoryHolder owner, int size) + public static boolean isTaking(InventoryClickEvent event) { - return create(owner, size, "Chest"); + // Was the upper inventory clicked? + boolean upperClicked = event.getRawSlot() < event.getInventory().getSize(); + + boolean ret = false; + + if (upperClicked) + { + ret = (event.getCurrentItem() != null && event.getCurrentItem().getAmount() > 0); + } + + return ret; } - public static Inventory create(int size, String title) - { - return create(null, size, title); - } + // -------------------------------------------- // + // IS EMPTY? + // -------------------------------------------- // - public static Inventory create(InventoryHolder owner, int size, String title) + public static boolean isEmpty(Inventory inv) { - return new CraftInventoryCustom(owner, size, title); + if (inv == null) return true; + for (ItemStack stack : inv.getContents()) + { + if (stack == null) continue; + if (stack.getAmount() == 0) continue; + if (stack.getTypeId() == 0) continue; + return false; + } + return true; } // -------------------------------------------- //