Solved Click Combinations

Discussion in 'Plugin Development' started by khave, Dec 10, 2014.

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

    khave

    Hello, why does this not work?

    Code:
        int task01;
    
        @EventHandler
        public void onClick(final PlayerInteractEvent e){
            final Player p = e.getPlayer();
            if(e.getAction() == Action.RIGHT_CLICK_AIR){
                p.sendMessage("1");
    
                task01 = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(Main.plugin, new BukkitRunnable() {
                    @Override
                    public void run() {
                        if(e.getAction() == Action.LEFT_CLICK_AIR){
                            p.sendMessage("2");
                            cancel();
                        }
                    }
                }, 0, 3);
    
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.plugin, new BukkitRunnable() {
                    @Override
                    public void run() {
                        Bukkit.getScheduler().cancelTask(task01);
                        p.sendMessage("3. End");
                    }
                }, 60);
            }
        }
    When I click it does start up and say "1", and it does execute my repeating task, as I have tested that, but it doesn't check when I click in the repeating task? After the 60 ticks it then cancels my task correctly.
    Why doesn't it check every time I click in the repeating task?
     
  2. Offline

    JordyPwner

    If im not wrong you cant do that. Checking if player clicks in a runnable ;3 Maybe @Skionz does know the Good answer if im wrong
     
  3. Offline

    khave

    Yea, that's what I thought too, but how come not?
     
  4. Offline

    JordyPwner

    As far as i know you cant check if the player does something in a runnable ;3
     
  5. Offline

    Avygeil

    @khave You are accessing the same final variable "e" from an anonymous class, so e.getAction() will always be Action.RIGHT_CLICK_AIR. But your code is confusing, I'm not sure what you actually want to do : check every 3 ticks if the player is left clicking...? It makes no sense to me. :p Please explain so we can suggest alternatives.

    NB : That's a good example of when you shouldn't ignore Java's limitations and not just apply "final" so that the code compiles. ;)
     
  6. Offline

    khave

    What I'm trying to do is make a combo.
    First I check if the player right clicks and then I need a repeating task to check when/if the player left clicks. If he left clicks. Cancel the task. If he doesn't left click, cancel the task after a few seconds.
    The 3 is not 3 seconds, but 3 ticks, it could be anything just a low tick count, so it check very often.
    Yea declaring the whole event final may be why it doesn't work, but I have tried changing it so I just declare the Action as final, but it didn't work either:

    Code:
    final Action action = e.getAction();
    Then I avoid having to call the entire event final, but even that seems to not work.
     
  7. Offline

    mythbusterma

    @JordyPwner

    A Runnable is (in this case) nothing more than an (anonymous) class. There is no reason you can't check what a Player is doing at any point in time. The issue is that you're checking the same event at a later point in time, why would it have changed?

    @khave

    The final modifier isn't why this doesn't work, a critical failure in your logic is why. An event is called once for each action performed, and you're checking the same event at a later point in time, which is still the same event, therefore having the same action.

    If you want to do this, add the Player to a Set or List and then make another case in the EventHandler for if the Player does the second action, and check whether or not they're in the Collection.
     
  8. Offline

    khave

    So what you're saying is that I'm basically checking if the player presses both keys. I think I see how to fix it now. I just need to change it a bit around, so the repeating task isn't inside of the check for right clicking. Right?
     
  9. Offline

    mythbusterma

    @khave

    You shouldn't have any repeating tasks, this is not something that requires scheduling.
     
  10. Offline

    Avygeil

    @khave The problem is that when a player right clicks, all that is fired is an event. There is no way to just check if a button is held, afaik. So you can't just simply check it in a Runnable for example. You need to have a reference to the said event, and yours never changes with your current anonymous Runnable. On top of that, a repeating task for this would be terribly inefficient.

    Instead, you could store when the player last right clicked in a Map, using system time. Then, when you detect a left click, check for the stored time instead : if it's over 60 seconds, then do nothing and clear the key. And you would do all of that in your event.

    Or you could achieve the same with a Metadata attached to the Player.
     
  11. Offline

    khave

    Yea, I get it now. Thank you!

    This is what I did if anyone wants to know:
    Code:
            //Hand Seal
            if(e.getAction() == Action.RIGHT_CLICK_AIR){
                p.sendMessage("1");
                handSeal.put(p.getDisplayName(), 1);
    
    
    
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(Main.plugin, new BukkitRunnable() {
                    @Override
                    public void run() {
                        p.sendMessage("3. End");
                       handSeal.remove(p.getDisplayName());
                    }
                }, 60);
            }
    
            if(e.getAction() == Action.LEFT_CLICK_AIR && handSeal.containsKey(p.getDisplayName()) && handSeal.get(p.getDisplayName()) == 1){
                p.sendMessage("2");
            }
    Where handSeal is a Hashmap with the string of the players name and the integer of which state the player is at.
    The reason for using a HashMap is just because I plan to make the combination longer, so I'll need to check how far in it I am.
     
Thread Status:
Not open for further replies.

Share This Page