Saving Multiple Lines Configuration

Discussion in 'Plugin Development' started by XinityDevs, Apr 14, 2016.

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

    XinityDevs

    Hi,

    I've been trying to work out how to set multiple strings in a config for my plugin which allows you to create commands with text. I wish it to do the following:

    Code:
    commands:
       commandname:
        - '&cThis is line 1'
        - '&cThis is line 2'
        - '&cThis is line 3'
    
    and etc.

    Note: I want the user to be able to create an unlimited amount of strings/text/lines.

    I will put my code below if someone can show me/teach me how to do this, it would be greatly appreciated as I only have the following:

    Code:
    commands:
       commandname: '&cThis is the only line'
    
    This is my code below:

    Code:
    package com.xinity.potatocommands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener
    {
        public void onEnable() {
          loadConfigFile();
    
          getServer().getPluginManager().registerEvents(this, this);
        }
    
        private void loadConfigFile() {
            // Load the config file.
            saveDefaultConfig();
    
            // If the Configuration Section doesn't exist, create it and save the config.
            if (getConfig().getConfigurationSection("commands") == null) {
                getConfig().createSection("commands");
                // I am going to be adding some example commands.
                getConfig().set("commands.regions", "&cRegion Help!");
                saveConfig();
            }
        }
    
        @EventHandler
        public void onPreprocess(PlayerCommandPreprocessEvent e) {
            ConfigurationSection s = getConfig().getConfigurationSection("commands");
    
             // Loop through all the commands.
            for (String command : s.getKeys(false)) {
                // If the command equals the message. We need to specify "/" because that isn't in our config.
                if (e.getMessage().equalsIgnoreCase("/" + command)) {
                    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("commands." + command)));
                    // Cancelling the event avoids the "Unknown Command" message.
                    e.setCancelled(true);
                }
            }
    
            // If the section doesn't contain the message sent, we don't want to cancel. Otherwise, the command won't go through.
            if (!s.getKeys(false).contains(e.getMessage().replace("/", "")))
                e.setCancelled(false);
        }
    }
     
  2. Offline

    Zombie_Striker

    @XinityDevs
    Save a StringList (I.E an List of strings) to a path in order to store multiple strings.
     
  3. Offline

    XinityDevs

    @Zombie_Striker
    Hey, I have never dealt with this before, I know you don't like showing examples but I may need one sorry. I've tried to save a string list and it just isn't working 0_0 Sorry again.
     
  4. Offline

    mcdorli

    Show what you tried
     
  5. Offline

    XinityDevs

    @mcdorli
    I already reverted back my code from what I tried, but I tried to .setStringList which is completely incorrect as I have never worked with a StringList in whole time coding since I'm going for a different type of plugin. I'm being honest, I havent coding that long but I have gone through and learned a fair bit of java liked people told me to and yet I still don't understand this. Sorry.

    This is my new code, I've been playing around with it, and it still doesn't work. Oh, and it isn't a command based plugin anymore, it is to override the command /regions which is already in use for worldguard to display region help for players but I need it configurable. I'll put my code below:

    Code:
    package com.xinity.potatoregions;
    
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
      
        List<String> regions = this.getConfig().getStringList("regions");
      
        public void onEnable() {
          loadConfigFile();
    
          getServer().getPluginManager().registerEvents(this, this);
        }
    
        private void loadConfigFile() {
            // Load the config file.
            saveDefaultConfig();
    
            // If the Configuration Section doesn't exist, create it and save the config.
            if (getConfig().getConfigurationSection("text") == null) {
                getConfig().createSection("text");
                // I am going to be adding some example commands.
                getConfig().set("text.regions", regions = this.getConfig().getStringList("regions"));
                saveConfig();
            }
        }
    
        @EventHandler
        public void onPreprocess(PlayerCommandPreprocessEvent e) {
            ConfigurationSection s = getConfig().getConfigurationSection("text");
    
             // Loop through all the commands.
            for (String command : s.getKeys(false)) {
                // If the command equals the message. We need to specify "/" because that isn't in our config.
                if (e.getMessage().equalsIgnoreCase("/" + command)) {
                    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("text." + command)));
                    // Cancelling the event avoids the "Unknown Command" message.
                    e.setCancelled(true);
                }
            }
    
            // If the section doesn't contain the message sent, we don't want to cancel. Otherwise, the command won't go through.
            if (!s.getKeys(false).contains(e.getMessage().replace("/", "")))
                e.setCancelled(false);
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 15, 2016
  6. Offline

    mcdorli

    A string list is a list of strings.
     
  7. Offline

    XinityDevs

    I'm doing a list of strings and it still isn't working.
     
  8. Offline

    mcdorli

    gteConfig.set("yourPath", yourList)? Do you have an error
     
  9. Offline

    XinityDevs

    @mcdorli
    getConfig().set("commands.regions", regions = this.getConfig().getStringList("regions"));

    Can someone please help? It is driving me nuts 0_0

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 15, 2016
  10. Offline

    mcdorli

    Check what getStringList returns and post your config.
     
  11. Offline

    XinityDevs

    My getStringList refers to the List<String> up the top? I'll post my code and my config below:

    Main.class
    Code:
    package com.xinity.potatoregions;
    
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        List<String> regions = this.getConfig().getStringList("regions");
    
        public void onEnable() {
          loadConfigFile();
    
          getServer().getPluginManager().registerEvents(this, this);
        }
    
        private void loadConfigFile() {
            // Load the config file.
            saveDefaultConfig();
    
            // If the Configuration Section doesn't exist, create it and save the config.
            if (getConfig().getConfigurationSection("commands") == null) {
                getConfig().createSection("commands");
                // I am going to be adding some example commands.
                getConfig().set("commands.regions", regions = this.getConfig().getStringList("regions"));
                saveConfig();
            }
        }
    
        @EventHandler
        public void onPreprocess(PlayerCommandPreprocessEvent e) {
            ConfigurationSection s = getConfig().getConfigurationSection("");
    
             // Loop through all the commands.
            for (String command : s.getKeys(false)) {
                // If the command equals the message. We need to specify "/" because that isn't in our config.
                if (e.getMessage().equalsIgnoreCase("/" + command)) {
                    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("commands." + command)));
                    // Cancelling the event avoids the "Unknown Command" message.
                    e.setCancelled(true);
                }
            }
    
            // If the section doesn't contain the message sent, we don't want to cancel. Otherwise, the command won't go through.
            if (!s.getKeys(false).contains(e.getMessage().replace("/", "")))
                e.setCancelled(false);
        }
    }
    config.yml
    Code:
    commands:
      regions:
      - '&cRegion Help!'
     
    Last edited: Apr 15, 2016
  12. Offline

    mcdorli

    You want to get a stringlist from the path "regions", that doesn't exist.
     
  13. Offline

    XinityDevs

    So wait, can you please show me how to connect this? I am getting more confused every time I try this 0_0
     
  14. Offline

    mcdorli

    What do yoh want to receive with getStribgList("regions")? There's no list with this path in the config
     
  15. Offline

    XinityDevs

    How do I get a list to go there is basically what I am trying to do. I'm trying to get a list into the configuration so someone can keep adding an unlimited amount of lines.

    @mcdorli @Zombie_Striker
    I got the configuration and stuff working, but in-game the list shows as:

    [Region Help, Line 2]

    I want the text to come out on individual lines and without the brackets like this:

    Region Help
    Line 2

    I will post my code below:

    Code:
    package com.xinity.potatoregions;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
      
        List<String> regions = new ArrayList<String>();
      
        public void onEnable() {
          loadConfigFile();
    
          getServer().getPluginManager().registerEvents(this, this);
        }
    
        private void loadConfigFile() {
            // Load the config file.
            saveDefaultConfig();
    
            // If the Configuration Section doesn't exist, create it and save the config.
            if (getConfig().getConfigurationSection("commands") == null) {
                getConfig().createSection("commands");
                getConfig().set("commands.regions", getConfig().getStringList("regions"));
                saveConfig();
            }
        }
    
        @EventHandler
        public void onPreprocess(PlayerCommandPreprocessEvent e) {
            ConfigurationSection s = getConfig().getConfigurationSection("commands");
    
             // Loop through all the commands.
            for (String command : s.getKeys(false)) {
                // If the command equals the message. We need to specify "/" because that isn't in our config.
                if (e.getMessage().equalsIgnoreCase("/" + command)) {
                    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("commands." + command)));
                    // Cancelling the event avoids the "Unknown Command" message.
                    e.setCancelled(true);
                }
            }
    
            // If the section doesn't contain the message sent, we don't want to cancel. Otherwise, the command won't go through.
            if (!s.getKeys(false).contains(e.getMessage().replace("/", "")))
                e.setCancelled(false);
        }
    }
    Config.yml
    Code:
    commands:
      regions: 
      - '&cRegion Help!'
      - '&cLine 2'
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Apr 15, 2016
  16. Offline

    XinityDevs

  17. Offline

    xDeeKay

    @XinityDevs Loop through getConfig().getStringList("commands."+ command)
     
  18. Offline

    XinityDevs

    What do you mean @xDeeKay

    P.S, the plugin is working and is returning information, but not how I want to.

    I want the plugin so when you do /regions, it will show a message like this:

    Region Help!
    Derp
    Heya

    I want them all on seperate lines and in a list in the config.

    But instead, it is outputting as:

    [Region Help!, Derp, Heya]

    I'm not that great at coding, but I like to have a go and I've been stuck on this for days now.
     
    Last edited: Apr 18, 2016
  19. Offline

    mcdorli

    Loop trough it with a foreach loop, maybe concatenate every element of the list with a string builder.
     
  20. Offline

    DoggyCode™

    You'd have to loop through your string list treating the values

    for (String s : getConfig().getStringList("cmds")){
    //performCommand(s);
    }

    This would loop through your list. s would be the entries.

    Sent from my SM-N9005 using Tapatalk
     
  21. Offline

    XinityDevs

    @DoggyCode™ It doesn't work, it says it is depreciated. 0_0 Can someone show me in my code please? I just cant work it out and I am going crazy :/
     
  22. Offline

    mcdorli

    Deprecated doesn't mean it's bad to use (at least with bukkit, this is the number 3 reason why I hate Bukkit), I doubt it will get removed.
     
  23. Offline

    XinityDevs

    @mcdorli I just tested it on my local server and it didn't worth with the depreciated method in there :/
     
  24. Offline

    Zombie_Striker

    @XinityDevs
    Please post your code. Just because it's deprecated doesn't mean it should not work.
     
  25. Offline

    DoggyCode™

    The method I showed him shouldn't be deprecated anyways? When did string list ever become deprecated?

    Sent from my SM-N9005 using Tapatalk
     
  26. Offline

    XinityDevs

    @Zombie_Striker
    My Code:

    Code:
    package com.xinity.potatoregions;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
      
        List<String> regions = new ArrayList<String>();
      
        public void onEnable() {
          loadConfigFile();
    
          getServer().getPluginManager().registerEvents(this, this);
        }
    
        private void loadConfigFile() {
            // Load the config file.
            saveDefaultConfig();
    
            // If the Configuration Section doesn't exist, create it and save the config.
            if (getConfig().getConfigurationSection("commands") == null) {
                getConfig().createSection("commands");
                getConfig().set("commands.regions", regions = this.getConfig().getStringList("regions"));
                saveConfig();
                    }
                }
            }
    
        @EventHandler
        public void onPreprocess(PlayerCommandPreprocessEvent e) {
            ConfigurationSection s = getConfig().getConfigurationSection("commands");
    
             // Loop through all the commands.
            for (String command : s.getKeys(false)) {
          
                // If the command equals the message. We need to specify "/" because that isn't in our config.
                if (e.getMessage().equalsIgnoreCase("/" + command)) {
                    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("commands." + command)));
                    // Cancelling the event avoids the "Unknown Command" message.
                    e.setCancelled(true);
                }
            }
    
            // If the section doesn't contain the message sent, we don't want to cancel. Otherwise, the command won't go through.
            if (!s.getKeys(false).contains(e.getMessage().replace("/", "")))
                e.setCancelled(false);
            }
        }
     
    Last edited: Apr 19, 2016
  27. Offline

    XinityDevs

  28. Offline

    Zombie_Striker

    @XinityDevs
    Do you mean this line? If so, which method /value is deprecated?
     
  29. Offline

    XinityDevs

    @Zombie_Striker
    I took the line out sorry, I put it where for (String command: s.getKeys(false)) { was

    Here is what I had:

    Code:
    package com.xinity.potatoregions;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.ConfigurationSection;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerCommandPreprocessEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
       
        List<String> regionlist = new ArrayList<String>();
       
        public void onEnable() {
          loadConfigFile();
    
          getServer().getPluginManager().registerEvents(this, this);
        }
    
        private void loadConfigFile() {
            // Load the config file.
            saveDefaultConfig();
    
            // If the Configuration Section doesn't exist, create it and save the config.
            if (getConfig().getConfigurationSection("commands") == null) {
                getConfig().createSection("commands");
                getConfig().set("commands.regions", this.getConfig().getStringList("regionlist"));
                saveConfig();
                    }
                }
    
    
        @EventHandler
        public void onPreprocess(PlayerCommandPreprocessEvent e) {
            ConfigurationSection s = getConfig().getConfigurationSection("commands");
    
             // Loop through all the commands.
            for (String command : getConfig().getStringList("regionlist")) {
                s.getKeys(false);
           
                // If the command equals the message. We need to specify "/" because that isn't in our config.
                if (e.getMessage().equalsIgnoreCase("/" + command)) {
                    e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("commands." + command)));
                    // Cancelling the event avoids the "Unknown Command" message.
                    e.setCancelled(true);
                }
            }
    
            // If the section doesn't contain the message sent, we don't want to cancel. Otherwise, the command won't go through.
            if (!s.getKeys(false).contains(e.getMessage().replace("/", "")))
                e.setCancelled(false);
            }
        }
    
     
    Last edited: Apr 21, 2016
  30. Offline

    Zombie_Striker

    Do you mean that this line has a deprecated method? If so, then:
    1. You are getting the commands from a stringlist called "regionlist", which I do not think is right.
    2. Either ignore that warning, as it should still work even if it is deprecated, or add the SuppressWarnings("deprecation") annotation above that line/method to remove the error.
     
Thread Status:
Not open for further replies.

Share This Page