Working with InventoryUtil and removing NMS references.

This commit is contained in:
Olof Larsson 2013-03-22 10:20:11 +01:00
parent e1687d24a6
commit c12c1ac765
2 changed files with 45 additions and 32 deletions

View File

@ -2,7 +2,7 @@ package com.massivecraft.mcore.adapter;
import java.lang.reflect.Type; 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.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -80,29 +80,11 @@ public class InventoryAdapter implements JsonDeserializer<Inventory>, JsonSerial
itemStacks[i] = itemStack; itemStacks[i] = itemStack;
} }
Inventory ret = new CraftInventoryCustom(null, size, "items"); Inventory ret = Bukkit.createInventory(null, size);
ret.setContents(itemStacks); ret.setContents(itemStacks);
return ret; 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 // INSTANCE
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -3,7 +3,7 @@ package com.massivecraft.mcore.util;
import java.util.HashMap; import java.util.HashMap;
import org.bukkit.Bukkit; 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.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -11,27 +11,58 @@ import org.bukkit.inventory.ItemStack;
public class InventoryUtil 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();
} }
public static Inventory create(InventoryHolder owner, int size) return ret;
{
return create(owner, size, "Chest");
} }
public static Inventory create(int size, String title) public static boolean isTaking(InventoryClickEvent event)
{ {
return create(null, size, title); // 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);
} }
public static Inventory create(InventoryHolder owner, int size, String title) return ret;
}
// -------------------------------------------- //
// IS EMPTY?
// -------------------------------------------- //
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;
} }
// -------------------------------------------- // // -------------------------------------------- //