Read/Write to config variable lists?

Discussion in 'Plugin Development' started by thebigdolphin1, Nov 17, 2012.

Thread Status:
Not open for further replies.
  1. I want it to work like this:


    Code:
    test:
    test2:
    - player1: <variable>
    - player2: <variable>
    - player3: <variable>

    How can I do this? Thanks!
     
  2. Offline

    fireblast709

    why not like this:
    Code:
    test:
      test2:
        player1: <variable>
        player2: <variable>
        player3: <variable>
     
  3. I basicly need it to store player names, and every 1200 ticks, add 1200 to every username in the "test2" thing.
    I need it to loop, and detect whats there.
    I already have a loop thing, that goes off every 1200 ticks, but how would I make this bit of the code work?
     
  4. Offline

    fireblast709

    to get player1, player2 and player3 do:
    Code:java
    1. Set<String> players = getConfig().getConfigurationSection("test.test2").getKeys(false);
     
  5. fireblast709 Can you give me some example code please?
    Cos I basicly need a 1 min loop, and every 1 min it adds + 1200 to every player in "test2".
     
  6. Offline

    fireblast709

    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    2. {
    3. @Override
    4. public void run()
    5. {
    6. for(Player player : Bukkit.getOnlinePlayers())
    7. {
    8. getConfig().set("test.test2."+player, getConfig().getInt("test.test2."+player, 0)+1200);
    9. saveConfig();
    10. }
    11. }
    12. },20L, 1200L);

    Say, place this in your onEnable() and it should work (make sure you have a config!)
     
  7. fireblast709 I want it to do it to everyone on the config, not just who's online.
    And how would I add a player to the list?
     
  8. Offline

    fireblast709

    if you put this in the config, it will add everyone to the list, and while they are online they get the 1200 added
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    Yaml defines this as an OrderedMap, however, in the Configuration API you have to deal with this as a list of maps.
     
  10. fireblast709 I dont just want it to do it for online players. Thats easy to do using normal config variables.
    I need it to do it to every player in the test2 thing, not who is online.
    :L Im stuck here. lol
     
  11. Offline

    fireblast709

    thebigdolphin1 small adjustment. Now it takes all the players from the config
    Code:java
    1. Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
    2. {
    3. @Override
    4. public void run()
    5. {
    6. for(String player : getConfig().getConfigurationSection("test.test2").getKeys(false))
    7. {
    8. getConfig().set("test.test2."+player, getConfig().getInt("test.test2."+player, 0)+1200);
    9. saveConfig();
    10. }
    11. }
    12. },20L, 1200L);
     
  12. Offline

    aciid

    This may help you, from LockTime plugin http://dev.bukkit.org/server-mods/locktime/

    Code:
    public boolean addWorld(String newWorld, String time, CommandSender commandSender) {
            List<String> worldsList = new ArrayList<String>();
            worldsList = configManager.wtimes;
            for (String s : worldsList) {
                if (s.toLowerCase().startsWith(newWorld.toLowerCase())) {
                    commandSender.sendMessage("§cWorld " + newWorld + " is already on the list!");
                    return false;
                }
            }
            String nworld = (new StringBuilder(String.valueOf(newWorld))).append(" "+time).toString();
            
            worldsList.add(nworld);
            configManager.wtimes.add(nworld);
            this.getConfig().set("Worlds", worldsList);
            this.saveConfig();
            LockTime.configManager.loadConfig();
            return true;
        }
    
    Code:
        public boolean removeWorld(String delWorld, CommandSender commandSender) {
            List<String> worldsList = new ArrayList<String>();
            worldsList = configManager.wtimes;
            for (String s : worldsList) {
                if(s.toLowerCase().startsWith((delWorld.toLowerCase()+" "))) {
                    worldsList.remove(s);
                    configManager.wtimes.remove(s);
                    this.getConfig().set("Worlds", worldsList);
                    this.saveConfig();
                    LockTime.configManager.loadConfig();
                    return true;
                }
            }
            commandSender.sendMessage("§cWorld " + delWorld + " not found on the list!");
            return false;
        }
    
     
  13. fireblast709 thanks a lot for that code! :)
    Really helped.
    How would I add a player to the "test2" list with the value "0", and remove a player from "test2"?
    Thanks! :D
     
  14. Offline

    fireblast709

    getConfig().set("test.test2"+player.getName(), 0); to add,
    getConfig().set("test.test2"+player.getName(), null); to remove
     
  15. Thanks soooo much!
    LIFE SAVER! :D
     
Thread Status:
Not open for further replies.

Share This Page