Solved [HELP] PlayerCommandPreprocessEvent

Discussion in 'Spigot Plugin Development' started by miguelrebolo, Jun 22, 2021.

  1. Hi, I'm practicing with this event, it turns out that I'm trying to block the gamemode but if I try to add a number I can change the gamemode to 2 or 3.
    Code (Text):
     private void onChat(PlayerCommandPreprocessEvent e) {
           Player user = e.getPlayer();
           String msg = e.getMessage();
               String nb = "1";
    if(msg.equalsIgnoreCase("/gamemode") && msg.contains(nb)) {
           user.sendMessage("Blocked");
           e.setCancelled(true);    
           }
    I want block this commands because im creating one staff build mode plugin.I dont want use the default vanila minecraft commands.
    Then i tried this.
    Code (Text):
    }if (msg.startsWith("/ga") || msg.startsWith("/Ga") || msg.startsWith("/gA") || msg.startsWith("/GA")) {
               user.sendMessage("Blocked");
               e.setCancelled(true);
           }
    It works but blocks all the command who start from ga.So i only want block the integer value.I know how to make this with CommandExecutor
    Code (Text):
    String number = NumberUtils.isNumber(args[1]);
    But im stuck with this event.If someone knows how to make this.I really apreciate it.
     
    • Optimistic Optimistic x 1
  2. SteelPhoenix

    Moderator

    You're missing a lot of edge cases, like command namespace (/minecraft:gamemode) and gamemode aliases (creative). You're honestly better off just setting up permissions properly like any other Minecraft server.

    You can split up the message into arguments and look at those individually similar to how commands are processed.

    Also, your current way of handling case-insensitivity is pretty poor.
     
    • Agree Agree x 3
  3. If I really know that there are permissions to block this command and others such as plugins. But I want to learn the use of this event. Bukkit.command.help
     
  4. I solved following this https://stackoverflow.com/questions...eck-if-a-string-represents-an-integer-in-java