Solved Cancelling an event but it still happens?

Discussion in 'Spigot Plugin Development' started by bluecreeper111, Mar 16, 2019.

  1. Hello SpigotMC forum,
    Normally I don't request development help as I am pretty good at analyzing an issue and solving it myself.
    However, this issue I have been having is tearing me apart and I have not solved it for the last 5 hours.
    Now, to the issue.

    The code below is for an essentials plugin I am developing, and I am trying to add configurable command cooldowns.
    I am doing this by using a PlayerCommandPreprocessEvent and checking if the command is on cooldown, etc.
    The code works perfectly as intended, however, if the command is on cooldown and I try to cancel the event to stop the player executing the command
    Code (Text):
    else {
        String replace = Long.toString(Math.abs(TimeUnit.MILLISECONDS.toSeconds(timeLeft) - time));
        p.sendMessage(api.getLangString("onCooldown").replaceAll("%command%", command[0]).replaceAll("%timeLeft%", replace));
        e.setCancelled(true);
        return;
    }
    for some god forsaken reason event is not cancelled and the command is still executed. Thus, bypassing my cooldowns no matter what.
    In the chat, the "Command is currently on cooldown!" message is displayed and shows the cooldown timer correctly, so that block of code is also executed correctly.

    If there is any extra code needed, let me know. Though I am pretty sure I have supplied enough.
    Please inform my on anything that might be causing this issue, cause I cannot find one.
    Code (Java):
    package com.bluecreeper111.jessentials.event;

    import java.util.concurrent.TimeUnit;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;

    import com.bluecreeper111.jessentials.Main;
    import com.bluecreeper111.jessentials.api.api;

    public class commandCooldown implements Listener {
     
        private Main plugin;
        private cooldownManager cooldownManager = new cooldownManager();
     
        public commandCooldown(Main pl) {
            plugin = pl;
        }
     
        @EventHandler
        public void commandSend(PlayerCommandPreprocessEvent e) {
            Player p = e.getPlayer();
            String commandSent = e.getMessage();
            if (!plugin.getConfig().getStringList("commandCooldowns").isEmpty()) {
                for (String t : plugin.getConfig().getStringList("commandCooldowns")) {
                    String[] command = t.split(",");
                    if (p.hasPermission(api.perp() + ".cooldown.bypass.*")) {
                        return;
                    }
                    if (!api.isLong(command[1].trim())) {
                        Bukkit.getLogger().severe("[JEssentials] Configuration error! Check commandCooldowns");
                        return;
                    }
           
                    if (commandSent.equalsIgnoreCase(command[0])) {
                        int time = Integer.parseInt(command[1].trim());
                        if (p.hasPermission(api.perp() + ".cooldown.bypass." + command[0])) {
                            return;
                        }
                        long timeLeft = System.currentTimeMillis() - cooldownManager.getCooldown(p);
                        if (TimeUnit.MILLISECONDS.toSeconds(timeLeft) >= time) {
                            cooldownManager.setCooldown(p, System.currentTimeMillis());
                            return;
                        } else {
                            String replace = Long.toString(Math.abs(TimeUnit.MILLISECONDS.toSeconds(timeLeft) - time));
                            p.sendMessage(api.getLangString("onCooldown").replaceAll("%command%", command[0]).replaceAll("%timeLeft%", replace));
                            e.setCancelled(true);
                            return;
                        }
                    }
                }
            } else {
                return;
            }
     
        }

    }
     
    In-game example of what I mean
    [​IMG]
    [​IMG]

    PS: Yes, the event is registered in the Main class
     
    #1 bluecreeper111, Mar 16, 2019
    Last edited: Mar 16, 2019
  2. Weaves

    Resource Staff

    @bluecreeper111 Could there be another plugin or listener that is re-enabling it? Have you tried with
    @EventHandler(ignoreCancelled = true , priority = EventPriority.HIGHEST)

    Also, class names should be UpperCamelCase. ;)
     
  3. The fact that I spent 5 hours attempting to solve an issue, then realizing the problem with my code was because I didn't set the EventPriority, is honestly demoralizing.
    Haha, all jokes aside thank you for your help! You have solved my problem :)
     
    • Like Like x 1

Share This Page