Added makeItem() utilities to GuiUtils.

* NEW: GuiUtils: Added makeItem() utilities.
* NEW: GuiUtils: Re-added the hideAttributes() method.
* NEW: ActionGui: Updated the action() methods to use the 
  GuiUtils.makeItem() utilities.
This commit is contained in:
Adrien Prokopowicz 2015-07-24 19:58:56 +02:00
parent 90968c6009
commit 586644e486
3 changed files with 74 additions and 7 deletions

View File

@ -51,17 +51,12 @@ abstract public class ActionGui extends Gui
protected void action(String name, int slot, ItemStack item, String title, List<String> loreLines)
{
ItemMeta meta = item.getItemMeta();
meta.setDisplayName(title);
meta.setLore(loreLines);
item.setItemMeta(meta);
action(name, slot, item);
action(name, slot, GuiUtils.makeItem(item, title, loreLines));
}
protected void action(String name, int slot, Material material)
{
action(name, slot, new ItemStack(material));
action(name, slot, GuiUtils.makeItem(material));
}
protected void action(String name, int slot)

View File

@ -211,6 +211,7 @@ abstract public class Gui
openGuis = new HashMap<>();
listener = new GuiListener();
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
GuiUtils.init();
}
/**

View File

@ -19,17 +19,59 @@
package fr.moribus.imageonmap.guiproko.core;
import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.PluginLogger;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
/**
* Various utility methods for GUIs.
*/
abstract public class GuiUtils
{
static private Method addItemFlagsMethod = null;
static private Object[] itemFlagValues = null;
/**
* Initializes the GUI utilities.
* This method must be called on plugin enabling.
*/
static public void init()
{
try
{
Class<?> itemFlagClass = Class.forName("org.bukkit.inventory.ItemFlag");
Method valuesMethod = itemFlagClass.getDeclaredMethod("values");
itemFlagValues = (Object[]) valuesMethod.invoke(null);
addItemFlagsMethod = ItemMeta.class.getMethod("addItemFlags", itemFlagClass);
addItemFlagsMethod.setAccessible(true);
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
// Not supported :c
} catch (InvocationTargetException e) {
PluginLogger.error("Exception occurred while looking for the ItemFlag API.", e);
}
}
static public void hideItemAttributes(ItemMeta meta)
{
try
{
addItemFlagsMethod.invoke(meta, itemFlagValues);
}
catch (IllegalAccessException | InvocationTargetException ex)
{
PluginLogger.error("Exception occurred while invoking the ItemMeta.addItemFlags method.", ex);
}
}
/**
* Stores the ItemStack at the given index of a GUI's inventory.
@ -45,6 +87,35 @@ abstract public class GuiUtils
new CreateDisplayItemTask(gui.getInventory(), item, slot));
}
static public ItemStack makeItem(Material material)
{
return makeItem(material, null, (List<String>)null);
}
static public ItemStack makeItem(Material material, String title)
{
return makeItem(material, title, (List<String>)null);
}
static public ItemStack makeItem(Material material, String title, String... loreLines)
{
return makeItem(material, title, Arrays.asList(loreLines));
}
static public ItemStack makeItem(Material material, String title, List<String> loreLines)
{
return makeItem(new ItemStack(material), title, loreLines);
}
static public ItemStack makeItem(ItemStack itemStack, String title, List<String> loreLines)
{
ItemMeta meta = itemStack.getItemMeta();
meta.setDisplayName(title);
meta.setLore(loreLines);
if(itemStack.getType().equals(Material.MAP))
hideItemAttributes(meta);
return itemStack;
}
/**
* Implements a bukkit runnable that updates an inventory slot later.
*/