Solved Help: Run multiple commands from config.yml

Discussion in 'Spigot Plugin Development' started by Yauke, May 20, 2016.

  1. Hi, guys! I need help with my plugin :(

    I want to make a plugin to get different commands in the same line from the config.yml and that run when you execute a specific command.

    The separator is a semicolon and test is the command in-game.

    Example:
    MultipleCommands Config

    test1: say Test 1; gamemode 0; give {player} iron_ingot 5
    test2: say Test 2; gamemode 1; give {player} iron_ingot 10

    When I write /test1 or /test2, they execute the commands of config.yml
     
  2. Get the string, and split the string at ;, then dispatch the individual entries in the returned array
     
    • Like Like x 1
    • Agree Agree x 1
  3. As @Redrield you can split the String

    Let's say the config looks like this:
    Code (Text):
    commands: 'heal ExpDev;kill ExpDev;kit OverLord'
    Then you can get the whole string by using
    Code (Text):
    String commands = YAMLConfig#getString("commands");
    System.out.println(commands); //should print out our commands
    Now create a String array by splitting it at ";"
    Code (Text):
    String[] arrayCommands = commands.toLowerCase().split(";");
    System.out.println(arrayCommands); //should print out the String array (it looks weird tho when you vanilla print out a Array
    Now you can loop through the String array (arrayCommands) and perform the commands
    Code (Text):
    for (String s : arrayCommands) { //s = 1 command
      /*
      * I don't really remember what the perform command method takes in
      * as args (I know it takes a string) or how it looks like.
      * You might also have to add a "/" at the beginning
      */
      performCommand(s);
    }
     
  4. It's Bukkit#dispatchCommand(Player, String) and no /.
     
  5. You can create a list like this: List<String> commands = getConfig().getStringList("Commands");

    for(String s: comenzi){
    p.performCommand(s);
    }
     
  6. Choco

    Moderator

    Building on Marius here, if you want to use a list instead, you can actually do it in one line
    Code (Java):
    getConfig().getStringList("commands").forEach(c -> player.performCommand(c));
     
    • Like Like x 2
  7. Java 8 is awesome :3
     
    • Agree Agree x 4
    • Like Like x 1
  8. Well, if you read his post (which you obviously didn't), you'd know that he wants it in one line, and that has been answered already.
     
  9. Well then
    Code (Text):
    Arrays.asList(getConfig().getString("test1").split(";")).forEach(c -> Bukkit.dispatchCommand(p, c));
     
     
  10. There you go :)
     
  11. All the replies so far split the string with ";", but that won't allow spaces after the semicolon.

    Use ";[ ]*" for splitting instead. :p
     
  12. Is that regex or something?

    Sent from my SGH-I747M using Tapatalk
     
  13. Yep. String#split() takes regex.
     
  14. Alright, I'm very unfamiliar with regex, so didn't think of something like that

    Sent from my SGH-I747M using Tapatalk
     
  15. If you want to learn about regex (even just the basics), http://www.rexegg.com/ is a great site that explains how regex works, and https://regex101.com/ is awesome for practicing/testing regex :)
     
  16. I want to make a condition if the line in config has semicolon or not
    Example
    Code (Text):

    test1: say Test 1; gamemode 0; give {player} iron_ingot 5
    test2: say Test 2
     
    I create something like that:
    Code (Text):

    if (cmd.contains(";")) {
    String[] split = cmd.split(";");
    for (String cmds : split) {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmds);
    } else {
        /* Execute other stuff */
    }
     
    And how I can add space between semicolon and other command?
    Like that:
    Code (Text):

    test1: say Test 1; gamemode 0; give {player} iron_ingot 5
     
     
    #16 Yauke, May 21, 2016
    Last edited: May 21, 2016
  17. We already explained this... Just do this:
    Code (Text):
    if(cmd.contains(";")) {
        Arrays.asList(cmd.split(";[]*").forEach(c -> Bukkit.dispatchCommand(p, c));
    }else {
        //Whatever
    }
     
    • Like Like x 1
  18. Just FYI, never use a "else" statement on a "for" loop!
     
  19. Choco

    Moderator

    Lol I think he just missed a bracket. There's an if statement, but he accidentally didn't close it :p That else is supposed to be on that if statement, and the for loop is supposed to be nested within the if
     
    • Informative Informative x 1
  20. Yes I missed a bracket LOL :D:D
     
    • Funny Funny x 1