[Solved] Command Blocking Problem

Discussion in 'Plugin Development' started by Aza24, May 2, 2012.

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

    Aza24

    In my plugin BattleNight, I am adding a feature to block other plugin commands that are not whitelisted while a player is in the Battle. At the moment it is not working and would like your help.

    This is what I have at the moment:
    GitHub
    Pasted code (open)
    PHP:
    package me.limebyte.battlenight.core.Listeners;
     
    import java.util.List;
     
    import me.limebyte.battlenight.core.BattleNight;
     
    import org.bukkit.command.Command;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
     
    public class 
    CommandBlocker implements Listener {
     
        
    // Get Main Class
        
    public static BattleNight plugin;
        public 
    CommandBlocker(BattleNight instance) {
            
    plugin instance;
        }
        
        @
    EventHandler(priority EventPriority.HIGHEST)
        public 
    void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event) {
            if (
    event.isCancelled()) return;
            if (!
    plugin.BattleUsersTeam.containsKey(event.getPlayer())) return;
            if (!
    plugin.config.getBoolean("Commands.Block")) return;
            
            List<
    Stringwhitelist plugin.config.getStringList("Commands.Whitelist");
            
            
    String[] cmdArg event.getMessage().split(" ");
            
    Command command plugin.getServer().getPluginCommand(cmdArg[0].trim().replaceFirst("/"""));
            
            
    // Don't block BattleNight commands
            
    if (command.getName().equals("bn")) return;
            
            
    // Check if the command is listed else cancel
            
    if (whitelist.contains(command.getName().toLowerCase())) {
                return;
            }
            else {
                
    // Check if there is any aliases
                
    if (command.getAliases().isEmpty()) {
                    
    // Block it
                    
    event.setCancelled(true);
                    
    plugin.tellPlayer(event.getPlayer(), "You are not permitted to perform this command while in a Battle.");
                    return;
                }
                
    // Check if an alias is listed else cancel
                
    for (String alias command.getAliases()) {
                    if (
    whitelist.contains(alias.toLowerCase())) {
                        return;
                    }
                    else {
                        
    // Block it
                        
    event.setCancelled(true);
                        
    plugin.tellPlayer(event.getPlayer(), "You are not permitted to perform this command while in a Battle.");
                        return;
                    }
                }
            }
        }
        
    }


    The event is registered, names of Players in the Battle are stored in the key of a HashMap, the whitelist is stored as a Bukkit config StringList.

    No errors are logged to the console.
     
  2. Offline

    ItsHarry

    Add debugging messages everywhere and see where it stops
     
  3. Offline

    Aza24

    Lol, yeah just did that and found the issue :).
     
  4. Offline

    ItsHarry

    Great :)
     
Thread Status:
Not open for further replies.

Share This Page