Advertisement
Incomprehendable

Edited IconMenu class (Bukkit)

Jun 16th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. public class IconMenu implements Listener {
  2.  
  3.     private String name;
  4.     private int size;
  5.     private OptionClickEventHandler handler;
  6.     private Plugin plugin;
  7.     private Player player;
  8.  
  9.     private String[] optionNames;
  10.     private ItemStack[] optionIcons;
  11.  
  12.     public IconMenu(String name, int size, OptionClickEventHandler handler,
  13.             Plugin plugin) {
  14.         this.name = name;
  15.         this.size = size;
  16.         this.handler = handler;
  17.         this.plugin = plugin;
  18.         this.optionNames = new String[size];
  19.         this.optionIcons = new ItemStack[size];
  20.         plugin.getServer().getPluginManager().registerEvents(this, plugin);
  21.     }
  22.  
  23.     public IconMenu setOption(int position, ItemStack icon, String name,
  24.             String... info) {
  25.         optionNames[position] = name;
  26.         optionIcons[position] = setItemNameAndLore(icon, name, info);
  27.         return this;
  28.     }
  29.  
  30.     public IconMenu setOption(int position, boolean color, ItemStack icon,
  31.             String name, String... info) {
  32.         if (color) {
  33.             optionNames[position] = ChatColor.translateAlternateColorCodes('&',
  34.                     name);
  35.             optionIcons[position] = setItemNameAndLoreWithColor(icon, name,
  36.                     info);
  37.         } else {
  38.             optionNames[position] = name;
  39.             optionIcons[position] = setItemNameAndLore(icon, name, info);
  40.         }
  41.         return this;
  42.     }
  43.  
  44.     public IconMenu setOption(int position, ItemStack icon) {
  45.         optionNames[position] = icon.getItemMeta().getDisplayName();
  46.         optionIcons[position] = icon;
  47.         return this;
  48.     }
  49.  
  50.     public IconMenu setOptions(int[] positions, ItemStack icon) {
  51.         for (int i : positions) {
  52.             optionNames[i] = icon.getItemMeta().getDisplayName();
  53.             optionIcons[0] = icon;
  54.         }
  55.         return this;
  56.     }
  57.  
  58.     public void setSpecificTo(Player player) {
  59.         this.player = player;
  60.     }
  61.  
  62.     public boolean isSpecific() {
  63.         return player != null;
  64.     }
  65.  
  66.     public void open(Player player) {
  67.         Inventory inventory = Bukkit.createInventory(player, size, name);
  68.         for (int i = 0; i < optionIcons.length; i++) {
  69.             if (optionIcons[i] != null) {
  70.                 inventory.setItem(i, optionIcons[i]);
  71.             }
  72.         }
  73.         player.openInventory(inventory);
  74.     }
  75.  
  76.     public void destroy() {
  77.         HandlerList.unregisterAll(this);
  78.         handler = null;
  79.         plugin = null;
  80.         optionNames = null;
  81.         optionIcons = null;
  82.     }
  83.  
  84.     @SuppressWarnings("deprecation")
  85.     @EventHandler(priority = EventPriority.HIGHEST)
  86.     void onInventoryClick(InventoryClickEvent event) {
  87.         if (event.getInventory().getTitle().equals(name)
  88.                 && (player == null || event.getWhoClicked() == player)) {
  89.             event.setCancelled(true);
  90.             if (event.getClick() != ClickType.LEFT)
  91.                 return;
  92.             int slot = event.getRawSlot();
  93.             if (slot >= 0 && slot < size && optionNames[slot] != null) {
  94.                 Plugin plugin = this.plugin;
  95.                 OptionClickEvent e = new OptionClickEvent(
  96.                         (Player) event.getWhoClicked(), slot,
  97.                         optionNames[slot], optionIcons[slot]);
  98.                 handler.onOptionClick(e);
  99.                 ((Player) event.getWhoClicked()).updateInventory();
  100.                 if (e.willClose()) {
  101.                     final Player p = (Player) event.getWhoClicked();
  102.                     Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,
  103.                             new Runnable() {
  104.                                 public void run() {
  105.                                     p.closeInventory();
  106.                                 }
  107.                             });
  108.                 }
  109.                 if (e.willDestroy()) {
  110.                     destroy();
  111.                 }
  112.             }
  113.         }
  114.     }
  115.  
  116.     public interface OptionClickEventHandler {
  117.         public void onOptionClick(OptionClickEvent event);
  118.     }
  119.  
  120.     public class OptionClickEvent {
  121.         private Player player;
  122.         private int position;
  123.         private String name;
  124.         private boolean close;
  125.         private boolean destroy;
  126.         private ItemStack item;
  127.  
  128.         public OptionClickEvent(Player player, int position, String name,
  129.                 ItemStack item) {
  130.             this.player = player;
  131.             this.position = position;
  132.             this.name = name;
  133.             this.close = true;
  134.             this.destroy = false;
  135.             this.item = item;
  136.         }
  137.  
  138.         public Player getPlayer() {
  139.             return player;
  140.         }
  141.  
  142.         public int getPosition() {
  143.             return position;
  144.         }
  145.  
  146.         public String getName() {
  147.             return name;
  148.         }
  149.  
  150.         public boolean willClose() {
  151.             return close;
  152.         }
  153.  
  154.         public boolean willDestroy() {
  155.             return destroy;
  156.         }
  157.  
  158.         public void setWillClose(boolean close) {
  159.             this.close = close;
  160.         }
  161.  
  162.         public void setWillDestroy(boolean destroy) {
  163.             this.destroy = destroy;
  164.         }
  165.  
  166.         public ItemStack getItem() {
  167.             return item;
  168.         }
  169.     }
  170.  
  171.     private ItemStack setItemNameAndLore(ItemStack item, String name,
  172.             String[] lore) {
  173.         ItemMeta im = item.getItemMeta();
  174.         im.setDisplayName(name);
  175.         im.setLore(Arrays.asList(lore));
  176.         item.setItemMeta(im);
  177.         return item;
  178.     }
  179.  
  180.     private ItemStack setItemNameAndLoreWithColor(ItemStack item, String name,
  181.             String[] lore) {
  182.         ItemMeta im = item.getItemMeta();
  183.         im.setDisplayName(ChatColor.translateAlternateColorCodes('&', name));
  184.         List<String> newLores = new ArrayList<String>();
  185.         for (String s : lore) {
  186.             newLores.add(ChatColor.translateAlternateColorCodes('&', s));
  187.         }
  188.         im.setLore(newLores);
  189.         item.setItemMeta(im);
  190.         return item;
  191.     }
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement