InventoryClickEvent doesn't work.

Discussion in 'Plugin Development' started by Len.Syine, Apr 4, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Len.Syine

    Code:
    @EventHandler
    public void onInventoryClick(InventoryClickEvent E) {
        if (E.getWhoClicked() instanceof Player) {
            Player P = Bukkit.getPlayer(E.getWhoClicked().getName());
    
            ItemStack ULBoots = new ItemStack(Material.DIAMOND_BOOTS);
            ItemMeta UL_Make = ULBoots.getItemMeta();
    
            ArrayList<String> UL_Lore = new ArrayList<String>();
            UL_Lore.add(ChatColor.AQUA+"Survivor");
            UL_Make.setLore(UL_Lore);
            ULBoots.setItemMeta(UL_Make);
    
            if (E.getCurrentItem().getType() == Material.DIAMOND_BOOTS && P.getInventory().getBoots() == null)
                P.getInventory().setBoots(ULBoots);
        }
    }
    I want to make player not unequip diamond boots of which name is §bSurvivor .
    But it doesn't work, so I removed if-condition and executed again, and doesn't work too.
    I think InventoryClickEvent may have a problem, or I made something mistakes?
    Please tell me what I'm wrong..
     
    1. Have you registered the events?
    2. Does the class implement Listener?
    3. Why:
      Code:java
      1. Player P = Bukkit.getPlayer(E.getWhoClicked().getName());
    Should be
    Code:java
    1. Player p = (Player) e.getWhoClicked();
     
  2. Offline

    Len.Syine

    1, 2.
    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2. public final Logger sLog = Logger.getLogger("Minecraft");
    3. public static Main plugin;
    4.  
    5. @Override
    6. public void onEnable() {
    7. PluginDescriptionFile CVpd = this.getDescription();
    8. this.sLog.info(CVpd.getName()+" "+CVpd.getVersion()+" is enabled now.");
    9.  
    10. PluginManager PM = getServer().getPluginManager();
    11. PM.registerEvents(this, this);
    12. }

    is this right?

    3. I changed it thanks
     
  3. Online

    timtower Administrator Administrator Moderator

    @Len.Syine Don't use Logger.getLogger, use getLogger instead.
    If you have a static variable of the plugin then also give it a value. I prefer to pass instances in constructors though.
    The enabled message isn't needed, Bukkit does this for you
     
  4. Offline

    WeeSkilz

    Is your listener in your Main class?
     
  5. Offline

    Len.Syine

    I already added.
     
  6. Show your whole class
     
Thread Status:
Not open for further replies.

Share This Page