diff --git a/src/com/massivecraft/massivecore/chestgui/ChestGui.java b/src/com/massivecraft/massivecore/chestgui/ChestGui.java index 3644c6df..83dd441d 100644 --- a/src/com/massivecraft/massivecore/chestgui/ChestGui.java +++ b/src/com/massivecraft/massivecore/chestgui/ChestGui.java @@ -17,20 +17,52 @@ public class ChestGui // REGISTRY // -------------------------------------------- // - private static final Map inventoryToGui = new MassiveMap<>(); + protected static final Map inventoryToGui = new MassiveMap<>(); public static Map getInventoryToGui() { return inventoryToGui; } - public static ChestGui remove(Inventory inventory) { return inventoryToGui.remove(inventory); } - public static ChestGui set(Inventory inventory, ChestGui gui) { return inventoryToGui.put(inventory, gui); } + public static ChestGui get(Inventory inventory) { return inventoryToGui.get(inventory); } public static ChestGui getCreative(Inventory inventory) { + if (inventory == null) throw new NullPointerException("inventory"); + ChestGui gui = get(inventory); if (gui != null) return gui; + gui = new ChestGui(); - set(inventory, gui); + gui.setInventory(inventory); + gui.add(); + return gui; } + private static void add(Inventory inventory, ChestGui gui) { inventoryToGui.put(inventory, gui); } + private static void remove(Inventory inventory) { inventoryToGui.remove(inventory); } + + // -------------------------------------------- // + // ADD & REMOVE + // -------------------------------------------- // + // Done through instance for override possibilities. + + public void add() + { + add(this.getInventory(), this); + } + + public void remove() + { + remove(this.getInventory()); + } + + // -------------------------------------------- // + // INVENTORY + // -------------------------------------------- // + // It is useful to provide a link back from the GUI to the inventory. + // This way we have can look up between Inventory and ChestGui both ways. + + private Inventory inventory = null; + public Inventory getInventory() { return this.inventory; } + public void setInventory(Inventory inventory) { this.inventory = inventory; } + // -------------------------------------------- // // ACTIONS // -------------------------------------------- // @@ -88,18 +120,18 @@ public class ChestGui // The sound you should hear when clicking an action slot. private SoundEffect soundClick = MassiveCoreMConf.get().clickSound; public SoundEffect getSoundClick() { return this.soundClick; } - public ChestGui setSoundClick(SoundEffect soundClick) { this.soundClick = soundClick; return this; } + public void setSoundClick(SoundEffect soundClick) { this.soundClick = soundClick; } // The sound you should hear when opening the GUI. private SoundEffect soundOpen = SoundEffect.valueOf("CHEST_OPEN", 0.75f, 1.0f); public SoundEffect getSoundOpen() { return this.soundOpen; } - public ChestGui setSoundOpen(SoundEffect soundOpen) { this.soundOpen = soundOpen; return this; } + public void setSoundOpen(SoundEffect soundOpen) { this.soundOpen = soundOpen; } // The sound you should hear when closing the GUI. // This sound will be skipped if another inventory was opened by the GUI action. private SoundEffect soundClose = SoundEffect.valueOf("CHEST_CLOSE", 0.75f, 1.0f); public SoundEffect getSoundClose() { return this.soundClose; } - public ChestGui setSoundClose(SoundEffect soundClose) { this.soundClose= soundClose; return this; } + public void setSoundClose(SoundEffect soundClose) { this.soundClose= soundClose; } // -------------------------------------------- // // AUTOCLOSING @@ -108,7 +140,16 @@ public class ChestGui private boolean autoclosing = true; public boolean isAutoclosing() { return this.autoclosing; } - public ChestGui setAutoclosing(boolean autoclosing) { this.autoclosing = autoclosing; return this; } + public void setAutoclosing(boolean autoclosing) { this.autoclosing = autoclosing; } + + // -------------------------------------------- // + // AUTOREMOVING + // -------------------------------------------- // + // Should the GUI be automatically removed upon the inventory closing? + + private boolean autoremoving = true; + public boolean isAutoremoving() { return this.autoremoving; } + public void setAutoremoving(boolean autoremoving) { this.autoremoving = autoremoving; } // -------------------------------------------- // // CONSTRUCT diff --git a/src/com/massivecraft/massivecore/engine/EngineMassiveCoreChestGui.java b/src/com/massivecraft/massivecore/engine/EngineMassiveCoreChestGui.java index 2933e86c..385ea65b 100644 --- a/src/com/massivecraft/massivecore/engine/EngineMassiveCoreChestGui.java +++ b/src/com/massivecraft/massivecore/engine/EngineMassiveCoreChestGui.java @@ -139,16 +139,19 @@ public class EngineMassiveCoreChestGui extends Engine } }); - // We save the inventory in the map for a little while. - // A plugin may want to do something upon the chest gui closing. - Bukkit.getScheduler().runTaskLater(this.getPlugin(), new Runnable() + if (gui.isAutoremoving()) { - @Override - public void run() + // We save the inventory in the map for a little while. + // A plugin may want to do something upon the chest gui closing. + Bukkit.getScheduler().runTaskLater(this.getPlugin(), new Runnable() { - ChestGui.remove(inventory); - } - }, 20); + @Override + public void run() + { + gui.remove(); + } + }, 20); + } } } diff --git a/src/com/massivecraft/massivecore/item/DataItemStack.java b/src/com/massivecraft/massivecore/item/DataItemStack.java index 074cda0f..594a85a9 100644 --- a/src/com/massivecraft/massivecore/item/DataItemStack.java +++ b/src/com/massivecraft/massivecore/item/DataItemStack.java @@ -434,8 +434,6 @@ public class DataItemStack implements Comparable { if ( ! (object instanceof DataItemStack)) return false; DataItemStack that = (DataItemStack)object; - - // TODO: Use compare instead to avoid bugs? return MUtil.equals( this.getId(), that.getId(), this.getCount(), that.getCount(), @@ -463,6 +461,50 @@ public class DataItemStack implements Comparable ); } + public boolean equalsItem(ItemStack item) + { + if (item == null) return false; + DataItemStack that = DataItemStack.fromBukkit(item); + return this.equals(that); + } + + public boolean isSimilar(DataItemStack that) + { + // Just copy equals and comment out count check. + return MUtil.equals( + this.getId(), that.getId(), + // this.getCount(), that.getCount(), + this.getDamage(), that.getDamage(), + this.getName(), that.getName(), + this.getLore(), that.getLore(), + this.getEnchants(), that.getEnchants(), + this.getRepaircost(), that.getRepaircost(), + this.getTitle(), that.getTitle(), + this.getAuthor(), that.getAuthor(), + this.getPages(), that.getPages(), + this.getColor(), that.getColor(), + this.isScaling(), that.isScaling(), + this.getPotionEffects(), that.getPotionEffects(), + this.getSkull(), that.getSkull(), + this.getFireworkEffect(), that.getFireworkEffect(), + this.getFireworkEffects(), that.getFireworkEffects(), + this.getFireworkFlight(), that.getFireworkFlight(), + this.getStoredEnchants(), that.getStoredEnchants(), + this.isUnbreakable(), that.isUnbreakable(), + this.getFlags(), that.getFlags(), + this.getBannerBase(), that.getBannerBase(), + this.getBannerPatterns(), that.getBannerPatterns(), + this.getPotion(), that.getPotion() + ); + } + + public boolean isSimilarItem(ItemStack item) + { + if (item == null) return false; + DataItemStack that = DataItemStack.fromBukkit(item); + return this.isSimilar(that); + } + @Override public int hashCode() { diff --git a/src/com/massivecraft/massivecore/util/InventoryUtil.java b/src/com/massivecraft/massivecore/util/InventoryUtil.java index d9f3190d..9eaf3985 100644 --- a/src/com/massivecraft/massivecore/util/InventoryUtil.java +++ b/src/com/massivecraft/massivecore/util/InventoryUtil.java @@ -583,21 +583,28 @@ public class InventoryUtil TAKE, NONE, BOTH, - ; public boolean isAltering() { return this != NONE; } + public boolean isGiving() { return this == GIVE || this == BOTH; } + public boolean isTaking() { return this == TAKE || this == BOTH; } + + public boolean isNone() + { + return this == NONE; + } + } /**