Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.42 KB | None | 0 0
  1. public class AnvilListener implements Listener {
  2.  
  3.     public AnvilListener(Main instance) {
  4.         instance.getServer().getPluginManager().registerEvents(this, instance);
  5.     }
  6.  
  7.     @EventHandler(priority = EventPriority.MONITOR)
  8.     public void onInventoryClick(InventoryClickEvent e) {
  9.         // check if the event has been cancelled by another plugin
  10.         if (!e.isCancelled()) {
  11.             HumanEntity ent = e.getWhoClicked();
  12.  
  13.             // not really necessary
  14.             if (ent instanceof Player) {
  15.                 Player player = (Player) ent;
  16.                 Inventory inv = e.getInventory();
  17.  
  18.                 // see if the event is about an anvil
  19.                 if (inv instanceof AnvilInventory) {
  20.                     InventoryView view = e.getView();
  21.                     int rawSlot = e.getRawSlot();
  22.  
  23.                     // compare the raw slot with the inventory view to make sure we are talking about the upper inventory
  24.                     if (rawSlot == view.convertSlot(rawSlot)) {
  25.                     /*
  26.                     slot 0 = left item slot
  27.                     slot 1 = right item slot
  28.                     slot 2 = result item slot
  29.  
  30.                     see if the player clicked in the result item slot of the anvil inventory
  31.                     */
  32.                         if (rawSlot == 2) {
  33.                         /*
  34.                         get the current item in the result slot
  35.                         I think inv.getItem(rawSlot) would be possible too
  36.                         */
  37.                             ItemStack item = e.getCurrentItem();
  38.  
  39.                             // check if there is an item in the result slot
  40.                             if (item != null) {
  41.                                 ItemMeta meta = item.getItemMeta();
  42.  
  43.                                 // it is possible that the item does not have meta data
  44.                                 if (meta != null) {
  45.                                     // see whether the item is beeing renamed
  46.                                     if (meta.hasDisplayName()) {
  47.                                         String displayName = meta.getDisplayName();
  48.                                         //if (item.getType() == Material.NAME_TAG) {
  49.                                             Main.getPluginConfig().set("playerdata." + player.getUniqueId().toString() + ".prefix", displayName);
  50.                                             Main.getInstance().saveConfig();
  51.                                             player.closeInventory();
  52.                                         //} else if (item.getType() == Material.PAPER) {
  53.                                             /*Main.getPluginConfig().set("playerdata." + player.getUniqueId().toString() + ".nick", displayName);
  54.                                             Main.getInstance().saveConfig();
  55.                                             player.closeInventory();*/
  56.                                         //}
  57.                                     }
  58.                                 }
  59.                             }
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }
  66.  
  67.  
  68.  
  69.     /*@EventHandler
  70.     public void onClose(InventoryCloseEvent e) {
  71.         Player player = (Player) e.getPlayer();
  72.         Inventory inv = e.getInventory();
  73.  
  74.         if (inv instanceof AnvilInventory) {
  75.             player.openInventory(Main.effector);
  76.         }
  77.     }*/
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement