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: Spoiler: config.yml 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
Get the string, and split the string at ;, then dispatch the individual entries in the returned array
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); }
You can create a list like this: List<String> commands = getConfig().getStringList("Commands"); for(String s: comenzi){ p.performCommand(s); }
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));
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.
Well then Code (Text): Arrays.asList(getConfig().getString("test1").split(";")).forEach(c -> Bukkit.dispatchCommand(p, c));
All the replies so far split the string with ";", but that won't allow spaces after the semicolon. Use ";[ ]*" for splitting instead.
Alright, I'm very unfamiliar with regex, so didn't think of something like that Sent from my SGH-I747M using Tapatalk
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
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
We already explained this... Just do this: Code (Text): if(cmd.contains(";")) { Arrays.asList(cmd.split(";[]*").forEach(c -> Bukkit.dispatchCommand(p, c)); }else { //Whatever }
Lol I think he just missed a bracket. There's an if statement, but he accidentally didn't close it That else is supposed to be on that if statement, and the for loop is supposed to be nested within the if