Solved RegisterEvents, the code runs twice

Discussion in 'Plugin Development' started by Maxpnl, May 17, 2017.

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

    Maxpnl

    Hello everyone, I have a little problem over here, I basically need to call a class from another class, but the class that needs to be called requires a RegisterEvents in the main class, because of that I have to choose between having a bugged plugin (the events doesn't always get catched) or having a ItemClick event running twice....

    Here's the code of the Main class (the RegisterEvents is temporarely //ed):
    //Bukkit.getServer().getPluginManager().registerEvents(new OpenChest(this), this);
    Bukkit.getServer().getPluginManager().registerEvents(new Give(this), this);

    And here's the Give class that calls the OpenChest class code:
    else if (event.getItem() != null) {
    Player p = event.getPlayer();
    if (p.hasPermission("clot.give")) {
    Set<String> keys = plugin.getConfig().getConfigurationSection("chests").getKeys(false);
    for (String key : keys) {
    ItemStack item = getAllItems(key);
    if (event.getItem().equals(item)) {
    new OpenChest(plugin).openInventory(p);
    event.setCancelled(true);
    }

    }
    } else {
    p.sendMessage("You don't have permissions!");
    }
    }

    How do I register events in the main class without running the ItemClick event code twice? Thanks in advance :D
     
  2. Offline

    Caderape2

    @Maxpnl
    Create a field in your main class for the event openchest.
    Since you passed your main class in your constructor, you can easily get the class openchest without a new instance. from your class Give
     
  3. Offline

    Maxpnl

    Sorry I just figured out I hade another register events in the main class, I'll close the topic
     
Thread Status:
Not open for further replies.

Share This Page