Solved Using config.yml as a list of players

Discussion in 'Spigot Plugin Development' started by bengalman211, Jun 8, 2017.

  1. I'd like to use the config.yml as a list of players, so far, in the onEnable, I call the saveDefaultConfig() method, I have a blank config.yml in my jar, and it works, it generates a blank config. Next, in my method I try to add a players UUID (Ideally to a list of UUIDS) using:
    Code (Text):
    this.getConfig().addDefault("Escaped", pUUID.toString());
                 this.saveConfig();
                 this.reloadConfig();
    However when my method is called (which using debug messages, I know it's called, and successfully makes it to the end) the file is not updated with the players UUID. Thanks for any help you can provide.
     
  2. Show me your whole class.
     
  3. This is the event that does everything pertaining to the config, it is not accessed anywhere else.
    Code (Text):
    @EventHandler
        public void onPlayerMoveEvent(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            UUID pUUID = (UUID) p.getUniqueId();
            this.getLogger().log(Level.INFO, "Player move event detected by " + pUUID.toString());
            Vector playerLoc = Plugins.getWorldGuard().wrapPlayer(e.getPlayer()).getPosition();
            // If they're already in the list, ignore
            if (this.getConfig().contains(pUUID.toString())) {
                return;
            }
            ProtectedRegion prisonRegion = WGBukkit.getRegionManager(p.getWorld()).getRegion("prison");
                // if they are an escapable rank
                if (p.hasPermission("prisonescape.canescape")) {
                    this.getLogger().log(Level.INFO, "They have permission");
                    // if they are not in the prison region
                    if (!prisonRegion.contains(playerLoc)) {
                        this.getLogger().log(Level.INFO, "They are not in the region");
                        nameColorChange(p);
                        this.getConfig().addDefault("Escaped", pUUID.toString());
                        this.saveConfig();
                        this.reloadConfig();
                        this.getLogger().log(Level.INFO, "End of method");
                    }
                } else {
                    if (this.getConfig().contains(pUUID.toString())) {
                        // remove player from config (?) + change name color back to normal
                    }
                    return;
                }
            }
     
  4. Code (Text):

    public List<String> players = new ArrayList<>();

    Declare this in the top of your class so its global. Under public class blah blah.

    Next, you can use this list to add your UUID's.

    players.add(player.getUniqueId().toString()); // I think this is the correct method to get a UUID, not sure

    lastly

    getConfig().set("players", players);

    getConfig().save(fileVariableHere);
     
    Now, this will technically create a new list every time and erase the old, to stop this,

    Code (Text):

    public void loadPlayers(){
    if (getConfig().contains("players")){
      players = getConfig().getStringList("players");
     }
    }
     
    put this method somewhere in your code, and add it to the very bottom of onEnabled();.
     
  5. But, if the server restarts, will this method add on to what is already in the file, or, once the server restarts and the list is at 1 person, will it set the file to just that one person? I need it to carry over.
     
  6. I edited my post to carry over.
     
  7. This is because you are adding a value to your default config "addDefault", but saving your blank config "saveConfig" use "set("Escaped", pUUID.toString());" instead of "addDefault("Escaped", pUUID.toString());"
     
  8. Got it, makes sense. However, the save method only takes a type of File, but I'm trying to save a ConfigurationFile, how would I do this?

    -EDIT- Fixed
     
    #8 bengalman211, Jun 9, 2017
    Last edited: Jun 9, 2017
    • Like Like x 1