Won't execute full command from config

Discussion in 'Plugin Development' started by wesley27, Jul 14, 2014.

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

    wesley27

    Hello, I have a plugin that, when you execute a specific command, will give you items or xp or currency, all of which you specify in the config.yml. The code reads it from the config, of course, and gives it to you.

    I'm trying to add the ability to have the console run a command out of the config, when you execute a specific command, as well. So, what I mean is that in the config under where I set the items/xp/currency for it to give you, I want to add a "command: <cmd here>" part that will have the console run that command.

    It is half working, but not correctly. My plugin is making the console execute the first word in the command I write in my config, but not the rest. So it's basically executing a command without any parameters, and then the console returns the command asking for parameters.

    Here's my code, I can give more if needed but I'm almost positive you don't need to see the rest of it. This is where I define and pull the command from the config, and then where it should execute it.
    Code:java
    1. public boolean giveReward(final Player player, final String challenge) {
    2. final String[] cmdList = getChallengeConfig().getString("options.challenges.challengeList." + challenge.toLowerCase() + ".executeCommand").split(" ");

    Code:java
    1. for (final String s : cmdList) {
    2. if (!s.equalsIgnoreCase("none")) {
    3. getServer().dispatchCommand(getServer().getConsoleSender(), s);
    4. }

    Now, here's what the config.yml looks like around where the command is. Note, the other rewards(item/xp/currency) do work, I didn't show the code for those parts.
    Code:
            itemReward: 264:1 3:20 12:20
            rewardText: 1 diamond, 20 dirt, and 20 sand
            currencyReward: 150
            xpReward: 150
            permissionReward: none
            executeCommand: broadcast testing, it's working!
    And finally, here's what it shows in the console after I execute my command that triggers all of that from the config.
    Code:
    Console    : [01:48:44 INFO]: wesley27 issued server command: /c c expertbuilder
    Console    : [01:48:44 INFO]: wesley27 has completed the expertbuilder challenge!
    Console    : [01:48:44 INFO]: CONSOLE issued server command: /broadcast
    Console    : [01:48:44 INFO]: Broadcasts a message to the entire server.
    Console    : [01:48:44 INFO]: /broadcast <msg>
    Console    : [01:48:44 INFO]: Unknown command. Type "/help" for help.
    So it is clearly getting the first part of my command and trying to run it, but not the rest of it, and I don't understand why. Can anyone help with this?
     
  2. Offline

    xTigerRebornx

    wesley27 Take a second to look at your codes logic, think about what it does.
    Code:
    final String[] cmdList = getChallengeConfig().getString("options.challenges.challengeList." + challenge.toLowerCase() + ".executeCommand").split(" ")
    Splits it at a space, meaning it returns a String[] containing "broadcast", "testing,", "it's", and "working!".
    You loop through this, calling execute on each String, meaning you execute "broadcast", "testing,", "it's", and "working!".

    Think of how you would actually do this in console. You'd type the String that is in the config, and "broadcast" would be the command, making everything else after arguments rather then other commands. As they are arguments, think of how you enter them in console. You enter it all as one String through the console, you input
    Code:
    broadcast testing, it's working!
    into the console all on one line, making it one command.
    How would you fix this? By properly mimicing what you would do when running in console (inputting it as one whole String rather then multiple commands)
     
  3. Offline

    wesley27

    xTigerRebornx Thanks! That fixed that, it now runs commands. However, now I'm having an issue with the next thing I wanted to do. I wanted it to be able to run commands that have the players name in them, so for example I would type in my config file this:
    Code:
            executeCommand: say This %player has made it work!!!
    I know there are plugins that do this, that have variables that you can read out of a config. I tried to do it with this code:
    Code:java
    1. for (final String s : cmdList) {
    2. if (!s.equalsIgnoreCase("none")) {
    3. if (s.contains("%player")) {
    4. String pnamevar = player.getName();
    5. s.replaceAll("%player", pnamevar);
    6. }
    7. getServer().dispatchCommand(getServer().getConsoleSender(), s);
    8. }
    9. }

    However, this would not work, it keeps executing the command and saying "[Server] This %player has made it work!!!"

    Any ideas or help with this?

    Nevermind, I fixed it and got it working using this. Simple change in the code :)

    Code:java
    1. for (final String s : cmdList) {
    2. if (!s.equalsIgnoreCase("none")) {
    3. String pnamevar = player.getName();
    4. getServer().dispatchCommand(getServer().getConsoleSender(), s.replace("%player", pnamevar));
    5. }
    6. }


    Thanks :)

    Actually now I have another issue...

    Using the above code it successfully runs any normal command, like /say or /broadcast. But, I really need it to run a McMyAdmin command. So, i have this in my config:
    Code:
    executeCommand: addmember VIP %player
    When it's triggered, it says "Unknown Command" in the console and it won't work. Is this cause it's an MCMA command? Any help with this please?

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

    PhonicUK

    You need to do "pushcommand addmember VIP %player" instead.
     
  5. Offline

    wesley27

Thread Status:
Not open for further replies.

Share This Page