How to have .replace() in getStringList()

Discussion in 'Spigot Plugin Development' started by DecisionsYT, May 16, 2016.

  1. 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??
  2. for(String text : getConfig().getStringList()){
    text.replace("this", "that")
  3. 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.
  4. Ahh nice! I'll do this tomorrow what at my PC

    Sent from my iPod touch using Tapatalk
  5. sounds good
  6. 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):"Console-Ran-Commands")).forEach(text -> Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), text.replace("{player}", player.getName()));
    #6 Lyxnx, May 16, 2016
    Last edited: May 16, 2016
    • Agree Agree x 1
  7. 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. :)
  8. Code (Java):
    getConfig().getStringList("list").forEach(string -> Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), string.replace("{player}", player.getName()));
    is how I'd do it
    • Agree Agree x 1
  9. Code (Text):
    getConfig().getStringList("Commands").forEach(string -> Bukkit.dispatchCommand(Bukkit.getServer().getConsoleSender(), string.replace("{username}", p.getName())));
    this workign to my plugin :D
    for vote rewards thanks very much ExoticCode