Scheduled Event

Discussion in 'Plugin Development' started by np98765, Aug 26, 2012.

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

    np98765

    This is my first time using a delayed task... I tried using the wiki, but the example given no longer works. I searched around this forum, and came up with something which gave me no errors, and I was hoping it would work.

    Well, it doesn't. I wasn't really sure where to put it, so that might actually be the problem.

    If you could point out what I did wrong, that'd be great. :) Thanks!


    Code:
        HashMap<Player, Boolean> invincible = new HashMap<Player, Boolean>();
     
        @EventHandler
        public void onPlayerInteractEvent(PlayerInteractEvent event) {
            final Player p = event.getPlayer();
            if (p.getItemInHand().getType().equals(Material.SPIDER_EYE)) {
                if (event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                    p.getInventory().remove(new ItemStack(Material.SPIDER_EYE, 1));
                    invincible.put(p, true);
                    p.sendMessage("Invincibility enabled!");
     
                }
            }
         
            Bukkit.getScheduler().scheduleSyncDelayedTask((Plugin) this, new Runnable() {
                public void run() {
                    invincible.remove(p);
                    p.sendMessage("No more invincibility");
                }
            }, 100L);
        }
     
  2. Offline

    HON95

    I'm guessing that wasn't your plugins main class? Because you need to use the one that extends JavaPlugin, not "(Plugin) this". And use a set instead of a map.
     
  3. Offline

    np98765

    Aah, ok... It works! :D

    Why should I use a set in this case? :confused:

    Thanks! :)
     
  4. Cause a set has two states:
    - Something in it
    and
    - Something not in it
    In your event you set it: set.add(key) and in the runnable you remove it: set.remove(key) and to get a true/false case you simply do if(set.contains(key)).
    So no need to waste memory with a boolean map.
     
Thread Status:
Not open for further replies.

Share This Page