Solved Blocking the /plugin:command syntax

Discussion in 'Plugin Development' started by BagduFagdu, May 31, 2015.

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

    BagduFagdu

    So I'm trying to block the mentioned above syntax, but I can't seem to get it to work. This is my current code:

    Code:
    @EventHandler
        public void onCommandPreprocess(PlayerCommandPreprocessEvent event) {
            String[] args = event.getMessage().split(" ");
            if(args[0].contains(":")) {
                String[] cmd = args[0].split(":");
                String pluginname = cmd[0].replaceFirst("/", "");
                if(getPlugins().contains(pluginname.toLowerCase())) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You are not allowed to use the /pluginname:command syntax.");
                }
            }
        }
       
        private List<String> getPlugins() {
            List<String> list = new ArrayList<String>();
            for(Plugin plugins : Bukkit.getServer().getPluginManager().getPlugins()) {
                list.add(plugins.getName().toLowerCase());
            }
            return list;
        }
     
  2. Offline

    Dudemister1999

    @BagduFagdu
    Instead of storing a list of plugin names, just do this:

    Code:
    @EventHandler
        public void onCommandPreprocess(PlayerCommandPreprocessEvent event) {
            String[] args = event.getMessage().split(" ");
            if(args[0].contains(":")) {
                String[] cmd = args[0].split(":");
                String pluginname = cmd[0].replaceFirst("/", "");
                if(Bukkit.getPluginManager().getPlugin(pluginname.toLowerCase()) != null) {
                    event.setCancelled(true);
                    event.getPlayer().sendMessage("You are not allowed to use the /pluginname:command syntax.");
                }
            }
        }
    That way you can eliminate an entire section of unnecessary code.

    As far as the actual problem, I think you're going about it wrong. Instead of checking if it has a colon and then assuming it's a plugin command (WorldEdit's subcommands have colons), try using a regex pattern and checking if the command matches it.
     
  3. Offline

    BagduFagdu

    I forgot to add that it'll only cancel the event if the player is not an OP, so it won't matter about World Edit. And thanks, @Dudemister1999 would ^[a-zA-Z0-9]*$ be the most appropriate for it?
     
  4. Offline

    Dudemister1999

    @BagduFagdu
    It doesn't look like the regex would work, here's the results I got while testing it:
    [​IMG]
     
    BagduFagdu likes this.
  5. Offline

    AdamQpzm

    @BagduFagdu Why do you want to do this? Just revoke the permission from the people you don't want to use the command. It's a much more reliable way of doing it.
     
    timtower likes this.
  6. Offline

    1Rogue Retired Staff

    Literally all you need to block all direct plugin aliases:

    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    2. public void onCommand(PlayerCommandPreprocessEvent event) {
    3. if (event.getMessage().split(" ")[0].contains(":")) {
    4. event.setCancelled(true);
    5. }
    6. }
     
    BagduFagdu likes this.
  7. Offline

    BagduFagdu

    Don't want players to use /essentials:, /minecraft:, /bukkit:.
     
  8. Offline

    AdamQpzm

    @BagduFagdu Then block the permissions. It's as simple as that.
     
Thread Status:
Not open for further replies.

Share This Page