Turn several triggers into one?

Discussion in 'Plugin Development' started by JeroenV, Dec 23, 2012.

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

    JeroenV

    Hello,

    I'm using the newest version of the IconMenu api, wich can be found here: http://forums.bukkit.org/threads/icon-menu.108342/#post-1460341

    It has this 'oninventoryclick' event:

    Code:
        @EventHandler(priority=EventPriority.MONITOR)
        void onInventoryClick(InventoryClickEvent event) {
            if (event.getInventory().getTitle().equals(name)) {
                event.setCancelled(true);
                int slot = event.getRawSlot();
                if (slot >= 0 && slot < size && optionNames[slot] != null) {
                    Plugin plugin = this.plugin;
                    OptionClickEvent e = new OptionClickEvent((Player)event.getWhoClicked(), slot, optionNames[slot]);
                    handler.onOptionClick(e);
                    if (e.willClose()) {
                        final Player p = (Player)event.getWhoClicked();
                        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            public void run() {
                                p.closeInventory();
                            }
                        }, 1);
                    }
                    if (e.willDestroy()) {
                        destroy();
                    }
                }
            }
        }
    Everything works fine, however when I click on one of the Icon's from the menu I can see it's triggered mutliple times, how should I go about making this only trigger once?
     
  2. Offline

    CorrieKay

    from the looks of this, it throws its own event, the OptionClickEvent. Listen for that?
     
  3. Offline

    JeroenV

    Yeah, I'm already doing that. That's actually where the problem is at, when I'm listening for it and a player clicks on it once it gets triggered depending on the duration of how long he held down his left mouse button.

    So if I make it send a message on click, it could send that message 10 times even though he only clicked once.
     
  4. Offline

    CorrieKay

    That doesnt make much sense.. It can tell how long someone holds the mouse button down for?
     
  5. Offline

    tommycake50

    i just wanna say dont use eventpriority.MONITOR for some reason someone was telling someone off for doing that the other day.
     
  6. Offline

    JeroenV

    Yeah I don't understand it either, but that's exacly what happens.
    The code was written by someone else, I know how to utilise it for my own needs but I don't understand some things that are actually used inside the api. What does eventpriority.MONITOR do?

    Code:
            IconMenu menu = new IconMenu("Townhall", 45, new IconMenu.OptionClickEventHandler() {
                @Override
                public void onOptionClick(IconMenu.OptionClickEvent event) {
                    event.getPlayer().sendMessage("You have chosen " + event.getName());
                    event.setWillClose(true);
                }
            }, this);
    This for example will give the message "You have chosen" + .. a few times depending on how long you clicked on it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  7. Offline

    fireblast709

    Just check if the item has changed (and ignore null/AIR)
     
  8. Offline

    CorrieKay

    MONITOR is fine to use, You're just not supposed to edit anything in the event during monitor priority. The reason monitor exists is for your event to only need to fire if the event happens, not to change the outcome of the event.

    For instance, if you have a locking plugin that logs when people access the chest, if you cancel the event in monitor, after the locking plugin sees that it was allowed, and logs them accessing the chest, it will create an incorrect log that they accessed the chest, when in the end, they didnt because of the cancel in MONITOR.
     
  9. Offline

    LaxWasHere

    Having this problem as well, let me know if you fixed it JeroenV :D
     
Thread Status:
Not open for further replies.

Share This Page