Getting an argument from PlayerCommandPreprocessEvent

Discussion in 'Plugin Development' started by justin_393, Jun 5, 2015.

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

    justin_393

    So basically, I want to intercept the /ban command and teleport a player somewhere, kill them, causing their inventory to drop allowing anyone to get it. So how would I get arguments from the Event? Or is there a simpler way to do this that I'm missing?

    EDIT: Also, how exactly does PlayerCommandPreprocessEvent#getMessage() work? Does it stop checking after the initial command? For example, if I want to block the command /me would just doing event.getMessage("me") work? Or would that not work correctly?
     
  2. Offline

    Zombie_Striker

    @justin_393
    Use the onCommand Method with the highest Priority. This will be read first then. After that, you just write what you want to happen and return true.
     
  3. Offline

    justin_393

    I didn't think onCommand was an event?
    Also, wouldn't that ruin ban? Because my code would be called first, and then wouldn't that cancel the other plugin from executing what it's supposed to do when /ban is called?
     
  4. Offline

    Zombie_Striker

    @justin_393
    So then you don't want to intercept the command? I don't know what you want.
     
  5. Offline

    justin_393

    Ok, let me rephrase it.

    I want my code to execute first, and when it's done then the other plugin gets to handle it.
     
  6. Offline

    caderape

    @justin_393 e.getmesssage().starthwith("/me");
    You get all the command and arguments. You can message.split(" ") for get back args
     
  7. Offline

    Konato_K

    @caderape Split first, then check the first argument for the command, unless you wanna run login with /message, /me, /mexico, and everything that starts with /me
     
  8. Offline

    Gamecube762

    @justin_393
    PlayerCommandPreprocessEvent#getMessage() returns the whole message typed as a string. Example: "/msg bob HI!!!"

    What we now can do is .split(" ") the message. This will return an array as {"/msg", "bob, "HI!!!"}. We can store this array with String[] args = message.slpit(" ");

    args[0] = "/msg"
    args[1] = "bob"
    args[2] = "HI!!!"

    There are manu things wrong with this.
    First, there is no onCommand event. There is the onCommand method from creating a command, but this is not an event, using @EventHandler will do nothing. onCommand only runs if the command is registered to the class containing this method.

    Also, setting an @EventHandler to Monitor is NOT ran first. It is ran LAST. Monitor is ran last so this plugin can take advantage of all the changes that the other plugins have made to the event. Read up on wiki.bukkit.org/Event_API_Reference.
     
    Xp10d3 likes this.
  9. Offline

    justin_393

    So, something along the lines of
    String message = e.getMessage();
    message.split(" ");
    if (message.startsWith("/ban") {
    String pname = message.getArg[1];
    Player p = Bukkit.getPlayer(pname);

    }

    Sorry if there's any errors, didn't use my IDE for that.
     
  10. Offline

    Konato_K

    @justin_393 Then you will stop all commands that start with /ban, like /banana, /baninfo, /bank, and related.
     
  11. Offline

    justin_393

    @Konato_K
    Alright, how would I get it to work correctly?
     
  12. Offline

    Konato_K

    @justin_393 Using equalsIgnoreCase probably (or just equals if you make it everything lowercase or uppercase at some point)
     
Thread Status:
Not open for further replies.

Share This Page