ChestGui and Item utility improvements.

This commit is contained in:
Olof Larsson 2016-08-26 17:55:03 +02:00
parent 51eee0e247
commit 2941e9b9b8
No known key found for this signature in database
GPG Key ID: BBEF14F97DA52474
4 changed files with 112 additions and 19 deletions

View File

@ -17,20 +17,52 @@ public class ChestGui
// REGISTRY
// -------------------------------------------- //
private static final Map<Inventory, ChestGui> inventoryToGui = new MassiveMap<>();
protected static final Map<Inventory, ChestGui> inventoryToGui = new MassiveMap<>();
public static Map<Inventory, ChestGui> 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

View File

@ -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);
}
}
}

View File

@ -434,8 +434,6 @@ public class DataItemStack implements Comparable<DataItemStack>
{
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<DataItemStack>
);
}
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()
{

View File

@ -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;
}
}
/**