Saving player data...

Discussion in 'Plugin Development' started by ZanderMan9, Jul 2, 2014.

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

    ZanderMan9

    Hey, I've been having dilemma over this for a while now... After looking around on the internet, I'm even more confused than before, because people do these things so many different ways. If someone could tell me what the best way to go about doing this would be, that would be great.
    I want a YML file (it can be in the JAR itself or in the plugin's folder) which has data on banned players. I understand how to use YML files just fine, how to save them and all... but there are so many methods on creating and accessing them that I can't wrap my head around it. What would be the best way to create this file in the onEnable method?

    I've seen people use Bukkit's resources as well as people using java.io.File. Bukkit's resources seem to center around having a single config, however. Any help is much appreciated.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  2. Offline

    Hoolean

    ZanderMan9

    Yo! Bukkit's configuration API covers using a single config (config.yml) but if you wish to go further than that, you need to make a similar class for your needs (as mentioned in the configuration section of the wiki).

    If you don't want to write the code for this yourself - understandable, no point repeating code if you don't need to unless you're practising - then @SagaciousZed has a good class that fulfils this purpose. :)

    The class linked above is used in a similar way to the Bukkit Configuration API. Here is example usage of the class in a practical situation:
    Code:java
    1. private ConfigAccessor bannedPlayersAccessor;
    2.  
    3. ...
    4.  
    5. @Override
    6. public void onEnable() {
    7. this.bannedPlayersAccessor = new ConfigAccessor(this, "bannedPlayers.yml");
    8. this.bannedPlayersAccessor.getConfig().addDefault("bannedUsernames", new ArrayList<String>());
    9. }
    10.  
    11. ...
    12.  
    13. public void onPlayerBeingAMeanyMeanyFoFeeny (Player player) {
    14. List<String> bannedUsernamesList = this.bannedPlayersAccessor.getConfig().getStringList("bannedUsernames");
    15. bannedUsernamesList.add(player.getName()); // don't do this in actuality - in 1.8, players will be able to change their names - use UUIDs instead
    16. this.bannedPlayersAccessor.getConfig().set("bannedUsernames", bannedUsernamesList);
    17. this.bannedPlayersAccessor.saveConfig();
    18. }


    Please feel free to call me out if the code above doesn't work, I haven't tested it just wrote it on the spot. :p Additionally, please don't actually lay your plugin out like that, due to 1.8 and name changes UUIDs should be used now (probably saved in the form of Strings).

    Hope that helps, if you have any more questions, just ask.
     
  3. Offline

    ZanderMan9

    Hoolean OK thanks a lot! As for 1.8, I'll look at that when it happens. I may stay behind in the 1.7 realm for quite a while, so it isn't so much of a concern for me :)
     
  4. Offline

    Hoolean

  5. Offline

    ZanderMan9

    Hoolean
    Hey, the code works great, but another question: How would you implement this to save data under the player's name? Because your code would create a list looking like this, right?:
    bannedPlayers:
    - Meany
    - Griefer
    - etc.
    Does the class you linked me to support fields within fields, like this?:
    Code:
    BannedPlayers:
      Meany:
        Date: Tomorrow
        BannedBy: Moddy
     
  6. Offline

    Hoolean

    ZanderMan9

    The class I linked supports all the methods that the Bukkit Configuration API does, notably these ones. :) However, if you want to store players with data it is not as simple as a String ArrayList.

    Not sure what stage you're at but if you need help in the form of example code for this - I myself learn well by reading other's code - then just gimme a shout and I'll write up an example. :)
     
  7. Offline

    ZanderMan9

    Yeah, I learn very well by example too ;D. It'd be wonderful if you could give me that example! Hoolean (Sorry if it isn't necessary to tag you, just wanting to make sure :))
     
  8. Offline

    ZanderMan9

    Hoolean Hey, just wanting to make sure you are still on board. I hate to sound impatient though :)
    Edit: Will something like this work?
    Code:java
    1. ...onEnable()
    2. {
    3. if (! this.cnfg.getConfig().contains("BannedPlayers")
    4. {
    5. this.cnfg.getConfig().createSection("BannedPlayers");
    6. }
    7. }
    8.  
    9. //On banning do this...
    10.  
    11. this.cnfg.getConfig().createSection("BannedPlayers." + bannee.getName());
     
  9. Offline

    Hoolean

    ZanderMan9

    My bad, forgot. Anyhoo, it would be something like this:

    Code:java
    1. public void onPlayerDidNaughtyThing (Player player) {
    2. this.bannedPlayersAccessor.getConfig().set("BannedPlayers." + player.getName() + ".Date", "Forever >:)");
    3. this.bannedPlayersAccessor.getConfig().set("BannedPlayers." + player.getName() + ".BannedBy", "Me");
    4. this.bannedPlayersAccessor.saveConfig();
    5. }
    6.  
    7. public boolean didPlayerDoNaughtyThing (String playerName) {
    8. return this.bannedPlayersAccessor.getConfig().contains("BannedPlayers." + playerName);
    9. }
    10.  
    11. public String whoSawPlayerDoNaughtyThing (String playerName) {
    12. if (!didPlayerDoNaughtyThing(playerName))
    13. return null;
    14. return this.bannedPlayersAccessor.getConfig().getString("BannedPlayers." + playerName + ".BannedBy");
    15. }


    This is just an example of using basic properties for individual players with using their names as keys in the "BannedPlayers" section but in practise the code should probably be changed to account for different eventualities (e.g. is player still banned). Hopefully this is enough for you to understand though. :)
     
  10. Offline

    ZanderMan9

    OK thanks a lot! looks like I was going at it a bit differently at first. Working on unbanning is going to be a challenge for another day ;)
    Hoolean
    Sorry to drag this out, but it seems setBanned(boolean) is deprecated. Is this in preparation for 1.8, or is it already deprecated? I don't want this to mess up my entire system :)
     
  11. Offline

    RingOfStorms

    if(getConfig().contains("BannedPlayers."+playername))
    //remove
     
  12. Offline

    Hoolean

  13. Offline

    ZanderMan9

    Hoolean Ah it looks like there is no reason for me to use the newer method, as I am only using to set their status to banned, the info is what I'm trying to handle

    Hm that's strange. RingOfStorms 's message showed up after ours for me, yet is above it in the forum... Am I losing my mind?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page