Hey! I'm making the console execute numerous amounts of commands upon an event. When doing: Code (Text): Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), plugin.getConfig().getStringList("Console-Ran-Commands").replace("{player}", player.getName())); I can not do the .replace part. How can I have .replace in a StringList??
You're semi-correct . Code (Java): List<String> list = config.getStringList("Blah"); for(int i = 0; i < list.size(); i++) { list.set(i, list.get(i).replace("this", "that")); } // List has been replaced This code just makes it set the string instead of getting the replaced string and doing nothing with it. This code should work better. @DecisionsYT please mark this as solved once you figure out how to implement what I've said as I know it is correct and will not create issues, as I have used this before and know it works, if you need help implementing it PM me.
Why use one of those loops? Just use Code (Java): for(String text : getConfig().getStringList("Console-Ran-Commands")) { Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), text.replace("{player}", player.getName())); } Or for java 8: Code (Java): Arrays.stream(getConfig().getStringList("Console-Ran-Commands")).forEach(text -> Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), text.replace("{player}", player.getName()));
Giving the OP a way to setup strings replaced gives them a better understanding of how to do other things, if you give a man a fish he can eat for a night, teach him how to fish and he will eat for the rest of his life. Teach him how to replace string lists and he can do many more things including dispatching the command list, teach him how to dispatch a command list and he'll dispatch a command list.
You can also do it this way (much simplier but requires Java 8): Code (Text): getConfig().getStringList("my list").forEach(string -> string.replace("{player}", player.getName())); For more information: https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
Code (Java): getConfig().getStringList("list").forEach(string -> Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), string.replace("{player}", player.getName())); is how I'd do it
Code (Text): getConfig().getStringList("Commands").forEach(string -> Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), string.replace("{username}", p.getName()))); this workign to my plugin for vote rewards thanks very much ExoticCode