PlayerInteractEvent called twice?

Discussion in 'Plugin Development' started by SgtStud, Jan 11, 2013.

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

    SgtStud

    Hello. I'm developing a plugin, and for some reason I have come across a strange bug. When a player right clicks, the PlayerInteractMethod and all my code is called twice. Any ideas?

    I can post the code if necessary.
     
  2. Offline

    chasechocolate

    Do you have two PlayerInteractEvent methods in your plugin?
     
  3. Offline

    SgtStud

    Nope. I have checked everything. The only thing i can think is that the server calls the method twice for some reason
     
  4. Offline

    chasechocolate

    Can I see the code?
     
  5. Offline

    Fuzzwolf

    What version are you building against and using in your server? It may also help to post your code ^
     
  6. Offline

    SgtStud

    I am using the most recent Beta build. R-0.3

    Code:
        @EventHandler
        public void onClick(PlayerInteractEvent e) {
            if (!e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) return;
            if (!e.getPlayer().hasPermission("redstoneinvis.cangoinvis")) return;
            if (!e.getPlayer().getItemInHand().getType().equals(Material.REDSTONE)) return;
            final Player p = e.getPlayer();
            p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, p.getItemInHand().getAmount()*20, 0));
            p.sendMessage("You are now invisible!");
            decrementRedstone(p);
            e.setCancelled(true);
           
            getServer().getScheduler().runTaskTimer(this, new BukkitRunnable() {
                    @Override 
                    public void run() {
                        p.sendMessage("1 redstone has been used!");
                        if (!decrementRedstone(p)) {
                            try {
                                cancel();
                            }
                            catch (Exception e) {
                               
                            }
                        }
                    }
                }, 20L, 20L);
        }
     
  7. Offline

    jayfella

    firstly, a player is not "read-only", so don't declare it as "final", secondly, there is no reason why your code would cause the interact event to fire twice. It is not your code that is the cause of the issue, or at least, not this code you posted.
     
  8. Offline

    theguynextdoor

    It's only final because the scheduler requires it to be since it's used in it.

    The only other thing i can say is maybe you registered the listener twice. I think more code may be needed.
    Also you can combine all those if statements into 1 by just using the or operator.
     
  9. Offline

    jayfella

    That makes no sense. You declare it "read-only" - a.k.a "final" - and then modify it. The scheduler does not require you declare it final. Synchronous task allow you to modify bukkit properties. Async tasks dont. Declaring it final doesnt "overcome" the async problem, because you still modify the contents of it.
     
  10. Offline

    theguynextdoor

    This is just a copy paste of the scheduler part of his code, minus the methods etc, and just includes sending the player a message. Mainly just to show that you need to make player final to prevent a compilation error.

    http://i.imgur.com/ypTe1.png

    Of course what he could do is make his own class which extends BukkitRunnable and have a player in the constructor, use that in the class, and then make an instance of that class instead of a plain new BukkitRunnable
     
    ZeusAllMighty11 likes this.
  11. Offline

    jayfella

    Interesting. I'm a c# programmer primarily, and have been taught that using nested classes is only useful in very specific cases, so i generally stay away from it, and have never come across this problem before. Thanks for the heads up :)
     
  12. Offline

    SgtStud

    Thanks for looking over the code guys. I am 100% positive I only registered the listener once. The reason I leave the statements broken up is for ease of reading and changing settings. And yes the player is final as it is required.

    A couple questions:
    1- The method decrement redstone actually modifies the players inventory. Is this ok given that the player is final? I think it should be as I'm not actually modifying the player's references.

    2- Is RunTaskTimer async? I was thinking it was Sync?
     
Thread Status:
Not open for further replies.

Share This Page