Left Click Air

Discussion in 'Plugin Development' started by flash1110, Jun 4, 2016.

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

    flash1110

    I'm coding a macro checker at the moment and for some reason the event just isn't catching. The event is registered. The code outside of the if is reaching. But for some reason, LEFT_CLICK_AIR isn't caught. For my other events, it is though. Here is my code:

    Code:
    public class MacroChecker implements Listener {
    
        public static EmpirePlugin plugin;
       
        private static HashMap<UUID, ClickLog> PlayerClickLogs = new HashMap<UUID, ClickLog>();
       
        public MacroChecker(EmpirePlugin instance) {
            plugin = instance;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
           
            if (event.isCancelled())
                return;
               
            if (!event.getAction().equals(Action.LEFT_CLICK_AIR)) return;
           
            UUID id = event.getPlayer().getUniqueId();
               
                Click click = new Click(System.currentTimeMillis());
                ClickLog log = getPlayer(id);
                log.addClick(click);
               
                checkPlayer(id);
        }
       
        public static void addPlayer(UUID id) {
            PlayerClickLogs.put(id, new ClickLog());
        }
       
        public static void removePlayer(UUID id) {
            PlayerClickLogs.remove(id);
        }
       
        public static ClickLog getPlayer(UUID id) {
            return PlayerClickLogs.get(id);
        }
       
        public static void checkPlayer(UUID id) {
           
            ClickLog log = getPlayer(id);
                   
            log.cleanseClicks();
           
            if (log.getClickCount() >= 15) {
               
                if (!log.checkAgain())
                    return;
               
                logClickWarning(id, log.getClickCount());
                log.updateLastChecked();
               
            }
           
        }
       
        private static String MacroPrefix = ChatColor.RED + "[" + ChatColor.DARK_AQUA + "Macro" + ChatColor.RED + "]" + ChatColor.WHITE + " ";
       
        public static void logClickWarning(UUID id, int count) {
           
            Player target = Bukkit.getPlayer(id);
           
            for (Player P : Bukkit.getOnlinePlayers()) {
                CombatPlayer cp = EmpirePlugin.getPlayer(P);
               
                if (P.hasPermission("empireplugin.macro") && cp.isMacro())
                    P.sendMessage(MacroPrefix + ChatColor.BLUE + target.getDisplayName() + ChatColor.GOLD + " clicked " + ChatColor.LIGHT_PURPLE + count + ChatColor.GOLD + "");
            }   
        }
    }
     
  2. Offline

    I Al Istannen

    @flash1110
    I think if the action is LEFT_CLICK_AIR the event is cancelled every time. Just replace "@EventHandler" with "@EventHandler(igorecancelled = false)" to also listen to cancelled events.
     
  3. Offline

    flash1110

    @I Al Istannen

    If the action isn't LEFT_CLICK_AIR it is cancelled every time. Why would I want to listen to cancelled events if I return it anyway?
     
  4. Offline

    I Al Istannen

    @flash1110
    Because you said you want to listen to LEFT_CLICK_AIR:
    Or did I understand you totally wrong? What is your problem then? And use "==" for enums, not equals. Looks nice, is null safe and .equals just uses == intern.
     
  5. Offline

    flash1110

    @I Al Istannen

    The code in the PlayerInteract is for a LEFT_CLICK_AIR action. Thus, if I !LEFT_CLICK_AIR and return, only the LEFT_CLICK_AIR will pass. For some reason, even if I do LEFT_CLICK_AIR, it still doesn't work.
     
  6. Offline

    I Al Istannen

    @flash1110
    My code:
    Code:
        @EventHandler
        public void onInt(PlayerInteractEvent e) {
            System.out.println(e.isCancelled() + " " + e.getAction());
        }
    Everytime I hit in the air:
    "true LEFT_CLICK_AIR
    true LEFT_CLICK_AIR"

    Conclusion: As I said a Left click air is cancelled every time, so remove the return if the event is cancelled.
     
  7. Offline

    flash1110

    So remove this?
    Code:
    if (event.isCancelled())
                return;
    Thanks :)
     
  8. Offline

    I Al Istannen

    @flash1110
    Yes, try it :)

    If it solved, please mark the thread as such.
     
Thread Status:
Not open for further replies.

Share This Page