Trouble Saving Stats in a Config

Discussion in 'Plugin Development' started by Monkey_Swag, Aug 10, 2014.

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

    Monkey_Swag

    Long story short, I'm trying to save a player's stats when they die on my config file, but it doesn't seem to work.
    my onDisable method:
    Code:java
    1. public void onDisable(){
    2. //save points
    3. for(UUID u : points.keySet()){
    4. config.set(u + ".Points", points.get(u));
    5. }
    6. //save kills
    7. for(UUID u : kills.keySet()){
    8. config.set(u + ".Kills", kills.get(u));
    9. }
    10. //save deaths
    11. for(UUID u : deaths.keySet()){
    12. config.set(u + ".Deaths", deaths.get(u));
    13. }
    14. saveConfig();
    15. }

    Methods I made from my API class:
    Code:java
    1. public static void addKills(Player p, int i){
    2. UUID uuid = p.getUniqueId();
    3. Main.kills.put(uuid, Main.kills.get(uuid) + i);
    4. }
    5.  
    6. public static void addDeaths(Player p, int i){
    7. UUID uuid = p.getUniqueId();
    8. Main.kills.put(uuid, Main.kills.get(uuid) + i);
    9. }
    10.  
    11. public static void addPoints(Player p, int i){
    12. UUID uuid = p.getUniqueId();
    13. Main.points.put(uuid, Main.points.get(uuid) + i);
    14. }


    Finally, the PlayerDeathEvent:
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent e){
    3. Player p = (Player) e.getEntity();
    4. if(p.getKiller() instanceof Player){
    5. Player killer = p.getKiller();
    6.  
    7.  
    8. killer.sendMessage("§aAdded +5 points to your account!");
    9. p.sendMessage("§c-2 Points for dying!");
    10.  
    11. API.addPoints(killer, 5);
    12. /*I know I didn't add their deaths, and remove points form the player who died, because I want to do one step at a time*/
    13.  



    EDIT: Forgot to add the config.yml after the plugin generates it:
    d764199f-5de3-4dd2-9a2d-a5a382861a18:
    points: 0
    kills: 0
    deaths: 0
    a94706a4-bf46-4f44-a7db-bfd866dc09b4:
    points: 0
    kills: 0
    deaths: 0
    To generate this I made a PlayerJoinEvent and used the following code:
    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent e){
    3. Player p = e.getPlayer();
    4. //Points
    5. if(!(plugin.getConfig().contains(p.getUniqueId() + ".points"))){
    6. plugin.getConfig().set(p.getUniqueId() + ".points", 0);
    7. plugin.saveConfig();
    8. }
    9. //Kills
    10. if(!(plugin.getConfig().contains(p.getUniqueId() + ".kills"))){
    11. plugin.getConfig().set(p.getUniqueId() + ".kills", 0);
    12. plugin.saveConfig();
    13. }
    14. //Deaths
    15. if(!(plugin.getConfig().contains(p.getUniqueId() + ".deaths"))){
    16. plugin.getConfig().set(p.getUniqueId() + ".deaths", 0);
    17. plugin.saveConfig();
    18. }
    19.  
    20. }
     
  2. Offline

    _LB

    Your onDisable looks very suspicious. Is "config" the same as #getConfig()?
     
  3. Offline

    Monkey_Swag

    sorry yes, I made it so "config" is the same as getConfig();
     
  4. Offline

    _LB

    Monkey_Swag How exactly did you accomplish that? I don't know if getConfig() is guaranteed to always return the same object every time.
     
  5. Offline

    Monkey_Swag


    public class myExampleClass extends JavaPlugin{
    publicFileConfigurationconfig;
    onEnable(){


    config=getConfig();
    }
    }

    There it is.
     
  6. Offline

    _LB

    I suspect that may be the issue. Try removing "config" entirely and using instead #getConfig()
     
  7. Offline

    Monkey_Swag

    still doesn't work :/
     
  8. Offline

    ImDeJay

    try converting the UUID to a string instead of using the UUID.

    uuid.tostring() then get the info from your config.
     
  9. Offline

    Monkey_Swag

    Here's my updated code:
    onDisable method:
    Code:java
    1. public void onDisable(){
    2. //save points
    3. for(UUID u : points.keySet()){
    4. getConfig().set(u + ".Points", points.get(u));
    5. }
    6. //save kills
    7. for(UUID u : kills.keySet()){
    8. getConfig().set(u + ".Kills", kills.get(u));
    9. }
    10. //save deaths
    11. for(UUID u : deaths.keySet()){
    12. getConfig().set(u + ".Deaths", deaths.get(u));
    13. }
    14. saveConfig();
    15. }


    API methods:
    Code:java
    1. public static void addKills(Player p, int i){
    2. UUID uuid = p.getUniqueId();
    3. Main.kills.put(uuid, Main.kills.get(uuid) + i);
    4. }
    5.  
    6. public static void addDeaths(Player p, int i){
    7. UUID uuid = p.getUniqueId();
    8. Main.kills.put(uuid, Main.kills.get(uuid) + i);
    9. }
    10.  
    11. public static void addPoints(Player p, int i){
    12. UUID uuid = p.getUniqueId();
    13. Main.points.put(uuid, Main.points.get(uuid) + i);
    14. p.sendMessage(prefix + i + " points have been §badded §ato your account!");
    15. }
    16.  
    17. public static void removePoints(Player p, int i){
    18. UUID uuid = p.getUniqueId();
    19. Main.points.put(uuid, Main.points.get(uuid) - i);
    20. p.sendMessage(prefix + i + " points have been §cremoved §afrom your account!");
    21. }


    EntityDeathEvent: (note: The player or the killer don't get the messages they're supposed to get from the methods in my API)
    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(EntityDeathEvent e){
    3. Player p = (Player) e.getEntity();
    4. if(p.getKiller() instanceof Player){
    5. Player killer = p.getKiller();
    6.  
    7. API.addDeaths(p, 1);
    8. API.removePoints(p, 2);
    9.  
    10. API.addPoints(killer, 5);
    11. API.addKills(killer, 1);
    12.  
    13.  
    14. }
    15. }


    config.yml will only generate this, even after I kill someone:
    PHP:
    a94706a4-bf46-4f44-a7db-bfd866dc09b4:
      
    Points0
      Kills
    0
      Deaths
    0
    d764199f
    -5de3-4dd2-9a2d-a5a382861a18:
      
    Points0
      Kills
    0
      Deaths
    0
    Yes, all events are registered.
    onEnable method:
    Code:java
    1. public void onEnable(){
    2.  
    3. getConfig().options().copyDefaults(true);
    4. saveConfig();
    5.  
    6. Bukkit.getConsoleSender().sendMessage("§8[§4Wars§8] §cEnabling Wars verison 0.1!");
    7.  
    8. getCommand("wars").setExecutor(new WarsCommands(this));
    9.  
    10. PluginManager pm = getServer().getPluginManager();
    11. pm.registerEvents(new JoinSign(), this);
    12. pm.registerEvents(new FirstJoin(this), this);
    13. pm.registerEvents(new EntityDeath(), this);
    14. }



    Help is very appreciated, thank you
     
  10. Offline

    Chiller

    Monkey_Swag Instead of saving the config in the onDisable method, edit and save the config in the addKills method. Simply get your main instance and do
    Code:java
    1. main.config.set(uuid + ".kills", main.config.getInt(uuid + ".kills", 0) + 1);
    2. main.saveConfig();


    This should work and also if the server crashes your config will already be updated...
     
  11. Offline

    Monkey_Swag

    Chiller now I get this stacktrace: http://prntscr.com/4bmw34
    Line 26 of my API class:
    Code:java
    1. Main.kills.put(uuid, Main.kills.get(uuid) + i);


    Line 24 of my EntityDeath class:
    Code:java
    1. API.addDeaths(p, 1);
     
  12. Offline

    Chiller

  13. Offline

    Monkey_Swag

    the only thing that changed was the onDIsable ( I commented everything out of it) and my API class. Example of one of my methods is here:
    Code:java
    1. public static void addKills(Player p, int i){
    2. UUID uuid = p.getUniqueId();
    3. plugin.getConfig().set(uuid + ".kills", plugin.getConfig().getInt(uuid + ".kills", 0) + 1);
    4. plugin.saveConfig();
    5. }

    Chiller

    this is very weird, it should work but idk why it isn't.

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

    ZodiacTheories

    Monkey_Swag

    It obviously shouldn't work if you have got an error :p
     
  15. Offline

    Monkey_Swag

    what I mean is idk how to fix the error, tried quite a few things but none work.
     
Thread Status:
Not open for further replies.

Share This Page