[SOLVED] Command Preprocess Event Help

Discussion in 'Plugin Development' started by slater96, Jul 7, 2012.

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

    slater96

    Hi, I'm trying to disable commands that can be used if they are in a config like:
    Code:
    BlockedCommands:
      - sethome
      - spawn
    I've tried to do it by using this page to help me at the bottom but I still can't get it to work.
    http://wiki.bukkit.org/Introduction_to_the_New_Configuration
    Can anyone see where i'm going wrong?
    Code:
        @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent event) {
            Player p = event.getPlayer();
            if (p.hasPermission("restrictor.bypass")) {
                event.setCancelled(false);
            } else if (p.hasPermission("restrictor.commands.block")) {
                List<String> cmds = plugin.getConfig().getStringList("BlockedCommands");
                for (String command : cmds) {
                    if (command.equalsIgnoreCase("/ " + plugin.getConfig().getStringList("BlockedCommands"))) {
                        event.setCancelled(true);
                        p.sendMessage("test");
                    }
                }
            }
        }
    }
     
  2. Offline

    gjossep

    Im not sure but i don't think you need a / at
    Code:
    command.equalsIgnoreCase("/ " + plugin.getConfig().getStringList("BlockedCommands")))
    It knows that its a command so you don't need it. Try that first and if that fails al check again! :)
     
  3. Offline

    slater96

    No, still didn't work :(
     
  4. Offline

    Firefly

    Your seeing if the command equals the string list. Obviously won't work. You'll need to iterate every string in the string list and compare it to the command.

    Actually, your iterating every string and comparing it to the list your iterating from. Why would you do that?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  5. Offline

    one4me

    Here you are, I fixed it up for you. Also instead of doing this, you could just not give the player the permission to perform the command in the first place.
    Code:
    @EventHandler
    public void onPlayerCommandPreprocessEvent(PlayerCommandPreprocessEvent event) {
      Player p = event.getPlayer();
      if(!p.hasPermission("restrictor.bypass")) {
        if (p.hasPermission("restrictor.commands.block")) {
          List<String> cmds = plugin.getConfig().getStringList("BlockedCommands");
          for (String command : cmds) {
            if (event.getMessage().equalsIgnoreCase("/" + command)) {
              event.setCancelled(true);
              p.sendMessage("§cYou do not have permission to do that!");
            }
          }
        }
      }
    }
    
     
    slater96 likes this.
  6. Offline

    slater96

    Thanks! It changed a bit of it but it works now. There's one bug though, which is that if i block /tpa then it comes up with the message, but if it has more arguments like /tpa slater96, then it's not blocked. Do you know how I do it so that i can get the arguments and if it equal 0, it blocks the whole command. I tried if(args[0].equals but if didn't work.
     
  7. use String.startWith() instead of String,equalsIgnoreCase()
     
    angelofdev, slater96 and one4me like this.
  8. Offline

    one4me

    Oh, that's because you're using if(event.getMessage().equalsIgnoreCase("/" + command)) try if(event.getMessage() .toLowerCase().startsWith() ("/" + command)) and see if it works.
     
  9. Offline

    slater96

    Yay thanks working now :p
    Do you know how to get the worldnames on load of the plugin so they go in the config like they do on multiworld plugins?
    I would like it so you have
    Code:
    BlockedCommands:
        - world1
            - home
            - sethome
        - world2
            - spawn
            - tpa
    Thanks
     
  10. I think it would be easier to do this config:
    Code:YAML
    1. BlockedCommands:
    2. world1:
    3. - home
    4. - sethome
    5. world2:
    6. - spawn
    7. - tpa
    8.  

    then you can do
    Code:java
    1. List<String> cmds = plugin.getConfig().getStringList("BlockedCommands."+event.getPlayer().getWorld().getName());
     
  11. Offline

    slater96

    Ok, is that all you need...?
    Will the admin have to put in the worldname themselves then that way?
     
  12. Offline

    slater96

Thread Status:
Not open for further replies.

Share This Page