How to stop players from typing any commands?!

Discussion in 'Plugin Development' started by PerezHD, Dec 20, 2014.

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

    PerezHD

    Hey guys, I need a way to stop players from typing any commands, like so they can't just type any commands? Any way you guys could help me?
     
  2. Offline

    WampyCakes

    You could check when a player does a command then cancel the event.
     
  3. Offline

    TheMrGong

    Eh, #SpoonFeed
    [​IMG]
     
    GrandmaJam likes this.
  4. Offline

    PerezHD

    @TheMrGong @WampyCakes Yah I was gonna do that XD

    Is there any way to allow certain commands though?

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

    PreFiXAUT

    @PerezHD Check which Command it is and allow it? Use PlayerCommandPreprocessEvent#getMessage() to check which Command it is.
     
  6. Offline

    PerezHD

    I am not sure where you're going with this really. I just don't understand the #getMessage() part.
     
  7. Offline

    PreFiXAUT

    @PerezHD Where should I go with this? Did you even read the JavaDoc to the Method?

    The getMessage() get's the Original Message which was send by the Player. Split it by spaces (" ") and check the 1st Entry in the Array. That's the Command he used (You gotta try if it's with or without a "/" at the beginning). Then check if you want to allow the Command. That's it.
     
  8. Offline

    TheDiamondGuy

    Code:
    @EventHandler
    public void onCmd(CommandPreProcessEvent e) {
    String msg = e.getMessage();
    
    if (msg.contains("/spawn");
    e.setCancelled(true);
     
  9. Offline

    PreFiXAUT

    @TheDiamondGuy Well, that would work kinda, but you only check if it contains it. What if a Plugin or something has a weird Syntax for it's Commands and has something like: "/cool /spawn"? Then you'ld also block this, but the Command is /cool and not /spawn.
     
  10. Offline

    TheDiamondGuy

    You could always:
    Code:
    if (m.matches("/spawn");
     
  11. Offline

    teej107

    Permissions.
     
    Konato_K likes this.
  12. Offline

    PerezHD

    @TheDiamondGuy @PreFiXAUT

    So do you think this would work?
    Code:
        @EventHandler
        public void onCommandType(PlayerCommandPreprocessEvent e){
            Player p = e.getPlayer();
            String msg = e.getMessage();
           
            if (this.plugin.variables.ffa.contains(e.getPlayer().getName())) {
                if (msg.matches("/spawn")){
                    e.setCancelled(true);
                    p.sendMessage(this.plugin.variables.prefix() + ChatColor.RED + " You cannot use commands while in FFA!");
                }
            }
        }
     
  13. Offline

    teej107

    Nope

    But this is proven to work
     
    HeadGam3z likes this.
  14. Offline

    TheMrGong

    [​IMG]
    Here's a better version that catches arguments:
    [​IMG]
    You can add as many commands as you want.

    Code:
        @EventHandler
        public void onCmd(PlayerCommandPreprocessEvent ev) {
            List<String> commands = new ArrayList<String>(); //Allowed commands
            commands.add("your_command"); //Replace "your_command" with the commands
                                          //you want to allow through
            String command = ev.getMessage().contains(" ") ? ev.getMessage().split(" ")[0] : ev.getMessage();
            ev.setCancelled(commands.contains(command) ? false : true);
        }
     
    Last edited: Dec 20, 2014
  15. Offline

    SuperOriginal

     
    Konato_K and teej107 like this.
  16. Offline

    PreFiXAUT

    @SuperOriginal @teej107 It's not 100% the Solution. It depends on what he want's to achieve. Deactive all Commands except a Player has a certain Permission (Your Answere), or only disable / allow certain Commands (My Answere).

    Since we don't know what he wants to do, only he can decide which one is "the solution".
     
  17. Offline

    SuperOriginal

    @PreFiXAUT Permissions can achieve, and is meant for this exact purpose.
     
  18. Offline

    PreFiXAUT

    @SuperOriginal Allowing CERTAIN Commands...and you wanna do that with Permissions...srsly?
     
  19. Offline

    leon3001

    If he wants to disable/enable commands for a short period of time, this might be the way he wants to go.
     
  20. Offline

    SuperOriginal

    @PreFiXAUT Yes.

    @leon3001 Yes that is true, but nothing was mentioned about a limited time period.
     
  21. Offline

    Bloxcraft

    Why can't you just

    Code:
    if (e.getMessage().toLowerCase().startsWith("/command")) {
    e.setCancelled(true);
    }
    Permissions will only work with commands that have permissions.
     
  22. Offline

    teej107

    @Bloxcraft because that will block commands that start with "/command" like "/commander"
     
  23. Offline

    WampyCakes

    Just use the event shown up there somewhere ^^. Then check if it equals the command you want to block. like
    Code:
    if(event.getMessage().equalsIgnoreCase("your command")
    or something like that. And then cancel the event.
    Code:
    event.setCancelled(true)
     
  24. Offline

    SuperOriginal

  25. Offline

    teej107

    @SuperOriginal What do you mean? That was just an example. The correct way would be to split the message by single whitespaces and then compare the values at the preferred index.
     
    Konato_K likes this.
  26. Offline

    SuperOriginal

    @teej107 Yes you are correct. And I wasn't talking about your post, I meant the person who suggested using .equals() straight after your point.
     
  27. Offline

    teej107

  28. Offline

    SuperOriginal

    @teej107 Because I was talking to you.. About that post. It was an attempt at humor relating to the constant suggestion of "iffy" methods.
     
    Skionz and teej107 like this.
  29. Offline

    PreFiXAUT

    I was saying that from the beginning and nobody noticed it :confused:
     
    SuperOriginal likes this.
  30. Offline

    Konato_K

    I kinda agree that the best way to do this is with permissions, but there is no context given.

    About what everything says, you may want to use
    Code:
    if(e.getMessage().split(" ")[0].toLowerCase().equals("/mycommand"))
    {
    }
    
    Instead of any "startswith" or "contains" stuff, however, then you'll need to do a check (or make a HashSet and use cnotains) for all cases, and this will not prevent the "/myplugin:mycommand" syntax.
     
Thread Status:
Not open for further replies.

Share This Page