Solved Create custom config file and save player kills

Discussion in 'Plugin Development' started by RamonPeek, May 15, 2014.

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

    RamonPeek

    My idea is to create a config file: 'Stats.yml',
    and save the player stats in that file, I hope you guys can help me with my code.
    With stats I mean like in the config file:
    ___________________________
    Players:
    iRamonHD:
    Kills: ...
    Deaths: ...

    ___________________________
    I hope you can give me a code for the generation custom config file, and a code for the set, and retreiving kills from a config.
    I have tried several codes on Google and YouTube, but none of them would work for me.
    I thank you already for reading this thread :).

    (Sorry for my 'possibly' bad English, I'm Dutch.)
     
  2. 1. make in the same directory as your plugin.aml a Stats.yml

    2. make this somwhere in your main class:
    Code:Java
    1.  
    2. File statsFile;
    3. FileConfiguration stats;
    4.  

    2. put this in your onEnable:
    Code:JAva
    1.  
    2. public void onEnable() {
    3. statsFile = new File(getDataFolder(), "Stats.yml");
    4. stats = YamlConfiguration.loadConfiguration(statsFile);
    5.  
    6. saveStats();
    7. }
    8.  


    3. make this method:
    Code:java
    1.  
    2. public void saveStats(){
    3. try {
    4. stats.save(statsFile);
    5. } catch (Exception e) {
    6. }
    7.  

    now youre done. you can use it like the normal config, but instead of getConfig().blablabla and saveConfig(); do stats.blablabla and saveStats();
     
    maxmar628, Orange Tabby and RamonPeek like this.
  3. Offline

    RamonPeek

    FisheyLP

    Thank you very much for your comprehensive reaction,
    I'm going to test this code tomorrow, but it looks good!
    My first question has been anwsered!

    Maybe you can anwser my second question too:
    How do you set a players stats by example:
    OnKill:
    If player kills a player, the killer gets +1 kills.

    If you can't anwser my second question I'll wait for someone else to help me :).

     
  4. Offline

    Azubuso

    RamonPeek An answer to your second question would be:
    Code:java
    1. File statsFile;
    2. FileConfiguration stats;
    3.  
    4. public void onEnable() {
    5. statsFile = new File(getDataFolder(), "stats.yml");
    6. stats = YamlConfiguration.loadConfiguration(statsFile);
    7. }
    8.  
    9. public void addKills(String username, Integer killsToAdd) {
    10. if (stats.getInt(playername + ".kills") == 0) {
    11. stats.set(playername + ".kills", killsToAdd);
    12. saveFile();
    13. return;
    14. }
    15. stats.set(playername + ".kills", stats.getInt(playername + ".kills") + killsToAdd);
    16. saveFile();
    17. }
    18.  
    19. public void saveFile() {
    20. try {
    21. stats.save(statsFile);
    22. } catch (IOException ex) {
    23. ex.printStackTrace();
    24. }
    25. }

    Hope that's understandable enough to not have to make me explain anything :)
     
  5. Offline

    RamonPeek

    Azubuso Thank you very much! This looks very clear to me,
    I will test it today, but thank you for your fast reaction!
     
  6. Azubuso
    but it gives you errors if the player isnt in the Stats.yml...
    RamonPeek make it like this:

    Code:java
    1.  
    2. public int getKills(String player) {
    3. try {
    4. return stats.getInt("Players."+player+".Kills");
    5. } catch (Exception e) {
    6. return 0;
    7. }
    8. }
    9.  
    10. public void addKill(String player) {
    11. stats.set("Players."+player+".Kills", getKills(player)+1+"");
    12. saveStats();
    13. }
    14.  
    15. public int getDeaths(String player) {
    16. try {
    17. return stats.getInt("Players."+player+".Deaths");
    18. } catch (Exception e) {
    19. return 0;
    20. }
    21. }
    22.  
    23. public void addDeath(String player) {
    24. stats.set("Players."+player+".Deaths", getDeaths(player)+1+"");
    25. saveStats();
    26. }
    27.  


    that will always work.
    And for the kill event use that:

    Code:java
    1.  
    2. @EventHandler
    3. public void onKill(PlayerDeathEvent e){
    4. if(e.getEntity().getKiller() instanceof Player){
    5. Player victim = e.getEntity();
    6. Player killer = victim.getKiller();
    7.  
    8. addKill(killer.getName());
    9. addDeath(victim.getName());
    10. }
    11. }
    12.  
     
    maxmar628 likes this.
  7. Offline

    Azubuso

    FisheyLP
    Yeah, I left out the null check in that addKills method, was expecting him to stumble across it and try to solve it on his own (Even if that sounds counterproductive).
     
  8. Offline

    RamonPeek

    Azubuso FisheyLP Thanks both for reacting on my thread, I'm testing it now! :)

    FisheyLP Again thanks for your anwser, but there's 1 little error, on this lign:

    1. } catch () {
    The error says: Syntax error on token "(", FormalParameter ex...

    I don't know what to do, I hope you guys can help me out ;).

    Also the Stats.yml does generate, but it doenst reload, (It keeps empty)

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

    Azubuso

    RamonPeek The "catch" error is because the catch clause isn't actually catching anything, you just need to add in an error to catch ;)
    Show Spoiler

    Code:java
    1. } catch (IOException ex) {
    2. // ex.printStackTrace();
    3. }

     
    RamonPeek likes this.
  10. Offline

    RamonPeek

    Azubuso Thanks for your anwser but it still doesnt work:

    Code:

    public int getKills(String player) {
    try {
    return stats.getInt("Players."+player+".Kills");
    } catch () {
    return 0;
    }
    }

    In here the catch doesnt work...
     
  11. Offline

    Azubuso

    RamonPeek Oh that method, it doesn't need a catch clause, or the try for that matter, just change the entire method to:
    Code:java
    1. public int getKills(String player) {
    2. int kills = stats.get("Players." + player + ".Kills") != null ? stats.getInt("Players."+player+".Kills") : 0;
    3. return kills;
    4. }
     
  12. Offline

    RamonPeek

    Azubuso Alright thanks for your help, untill now everything is working, but it doesnt put it in the stats.yml file,
    It keeps empty. i hope you know how to fix it :x
     
  13. Offline

    Azubuso

    RamonPeek Can you post the code you've got now?
     
  14. Offline

    RamonPeek

    Azubuso This is my 'Main'Code:

    package me.RamonPeek;

    import java.io.File;
    import java.io.IOException;
    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Sound;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Core extends JavaPlugin {

    public final Logger SGLogger = Logger.getLogger("Minecraft");
    public static String Title = ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + "SnorlaxGames" + ChatColor.DARK_GRAY + "]";
    Inventory KitSelector = Bukkit.createInventory(null, 45, ChatColor.DARK_GRAY + "[KitSelector]");
    FileConfiguration stats;
    File statsFile;

    @Override
    public void onEnable() {
    SGLogger.info("-------------------------------------------------");
    SGLogger.info("Plugin is aan het opstarten." );
    SGLogger.info("Plugin is nu klaar voor gebruik!" );
    SGLogger.info("©Copyright: RamonPeek, Alle rechten Voorbehouden!");
    SGLogger.info("LET OP: Gebruik versie 1.7.* of hoger!" );
    SGLogger.info("-------------------------------------------------");
    PluginManager pm = Bukkit.getServer().getPluginManager();
    pm.registerEvents(new JoinLeave(), this);
    pm.registerEvents(new PlayerInteract(), this);
    statsFile = new File(getDataFolder(), "Stats.yml");
    stats = YamlConfiguration.loadConfiguration(statsFile);
    saveStats();
    }

    @EventHandler
    public void onKill(PlayerDeathEvent e){
    if(e.getEntity().getKiller() instanceof Player){
    Player victim = e.getEntity();
    Player killer = victim.getKiller();
    killer.giveExpLevels(5);
    addKill(killer.getName());
    addDeath(victim.getName());
    }
    }

    public void saveStats() {
    try {
    stats.save(statsFile);
    } catch (IOException ex) {
    ex.printStackTrace();
    }
    }

    public int getKills(String player) {
    int kills = stats.get("Players." + player + ".Kills") != null ? stats.getInt("Players."+player+".Kills") : 0;
    return kills;
    }

    public void addKill(String player) {
    stats.set("Players."+player+".Kills", getKills(player)+1+"");
    saveStats();
    }

    public int getDeaths(String player) {
    int deaths = stats.get("Players." + player + ".Deaths") != null ? stats.getInt("Players."+player+".Kills") : 0;
    return deaths;
    }

    public void addDeath(String player) {
    stats.set("Players."+player+".Deaths", getDeaths(player)+1+"");
    saveStats();
    }

    public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    Player p = (Player)sender;
    if(!(sender instanceof Player)) {
    p.sendMessage("Je kunt dit command alleen als speler gebruiken.");
    return false;
    }
    if(alias.equalsIgnoreCase("SnorlaxPVP") || alias.equalsIgnoreCase("sp")) {
    if(args.length == 0) {
    p.sendMessage(Title + ChatColor.RED + "Ongeldig command, gebruik /SnorlaxPVP Help, voor alle commands.");
    }else{
    if(args[0].equalsIgnoreCase("Help")) {
    p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    p.sendMessage(ChatColor.GOLD + " Help Menu " );
    p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Het basis commando." );
    p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Help Geeft dit menu weer." );
    p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Join Ga in DeathMatch." );
    p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Stats Laat je stats zien." );
    p.sendMessage(ChatColor.RED + "/SnorlaxPVP Setspawn Zet de spawn locatie.");
    p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    p.playSound(p.getLocation(), Sound.SPLASH, 1, 2);
    }else{
    if(args[0].equalsIgnoreCase("Setspawn")) {
    if(p.hasPermission("DeathMatch.SetSpawn")) {
    this.getConfig().set("Spawn" + ".X", p.getLocation().getBlockX());
    this.getConfig().set("Spawn" + ".Y", p.getLocation().getBlockY());
    this.getConfig().set("Spawn" + ".Z", p.getLocation().getBlockZ());
    this.getConfig().set("Spawn" + ".Yaw", p.getLocation().getYaw());
    this.getConfig().set("Spawn" + ".Pitch", p.getLocation().getPitch());
    this.getConfig().set("Spawn" + ".World", p.getLocation().getWorld());
    p.sendMessage(Title + ChatColor.GOLD + "Spawn is gezet!");
    }
    }else{
    if(args[0].equalsIgnoreCase("Join")) {
    int spawnX = this.getConfig().getInt("Spawn" + ".X");
    int spawnY = this.getConfig().getInt("Spawn" + ".Y");
    int spawnZ = this.getConfig().getInt("Spawn" + ".Z");
    int spawnYaw = this.getConfig().getInt("Spawn" + ".Yaw");
    int spawnPitch = this.getConfig().getInt("Spawn" + ".Pitch");
    Object world = this.getConfig().get("Spawn" + ".World");

    Location spawn = new Location((World) world, spawnX, spawnY, spawnZ, spawnYaw, spawnPitch);
    p.teleport(spawn);
    p.sendMessage(Title + ChatColor.GREEN + "Je bent nu in SnorlaxPVP!");
    }else{
    int kills = stats.getInt("Players." + p.getName() + ".Kills");
    int deaths = stats.getInt("Players." + p.getName() + ".Deaths");
    if(args[0].equalsIgnoreCase("Stats")) {
    p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    p.sendMessage(ChatColor.DARK_GREEN+ " Statistics " );
    p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    p.sendMessage(ChatColor.GREEN + "Kills: " + ChatColor.WHITE + kills);
    p.sendMessage(ChatColor.GREEN + "Deaths: " + ChatColor.WHITE + deaths);
    p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    }
    }
    }
    }
    }
    }return false;
    }




    }
     
  15. Offline

    Azubuso

    RamonPeek Alright, here I've changed a few things, try this out:
    Show Spoiler
    Code:java
    1. package me.RamonPeek;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Location;
    10. import org.bukkit.Sound;
    11. import org.bukkit.World;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.configuration.file.FileConfiguration;
    15. import org.bukkit.configuration.file.YamlConfiguration;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.EventHandler;
    18. import org.bukkit.event.entity.PlayerDeathEvent;
    19. import org.bukkit.inventory.Inventory;
    20. import org.bukkit.plugin.PluginManager;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class Core extends JavaPlugin {
    24.  
    25. public final Logger SGLogger = Logger.getLogger("Minecraft");
    26. public static String Title = ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + "SnorlaxGames" + ChatColor.DARK_GRAY + "]";
    27. Inventory KitSelector = Bukkit.createInventory(null, 45, ChatColor.DARK_GRAY + "[KitSelector]");
    28.  
    29. @Override
    30. public void onEnable() {
    31. SGLogger.info("-------------------------------------------------");
    32. SGLogger.info("Plugin is aan het opstarten." );
    33. SGLogger.info("Plugin is nu klaar voor gebruik!" );
    34. SGLogger.info("[SIZE=3][FONT=arial][COLOR=#545454]©[/COLOR][/FONT][/SIZE]Copyright: RamonPeek, Alle rechten Voorbehouden!");
    35. SGLogger.info("LET OP: Gebruik versie 1.7.* of hoger!" );
    36. SGLogger.info("-------------------------------------------------");
    37.  
    38. PluginManager pm = Bukkit.getServer().getPluginManager();
    39. pm.registerEvents(new JoinLeave(), this);
    40. pm.registerEvents(new PlayerInteract(), this);
    41.  
    42. File statsFile = new File(getDataFolder(), "stats.yml");
    43. if (!statsFile.exists) {
    44. statsFile.createNewFile();
    45. }
    46. }
    47.  
    48. @EventHandler
    49. public void onKill(PlayerDeathEvent e) {
    50. if(e.getEntity().getKiller() instanceof Player){
    51. Player victim = e.getEntity();
    52. Player killer = victim.getKiller();
    53. killer.giveExpLevels(5);
    54. addKill(killer.getName());
    55. addDeath(victim.getName());
    56. }
    57. }
    58.  
    59. public int getKills(String player) {
    60. int kills = stats.get("Players." + player + ".Kills") != null ? stats.getInt("Players."+player+".Kills") : 0;
    61. return kills;
    62. }
    63.  
    64. public void addKill(String player) {
    65. File statsFile = new File(getDataFolder(), "stats.yml");
    66. FileConfiguration stats = new YamlConfiguration.loadConfiguration(statsFile);
    67. stats.set("Players." + player + ".Kills", getKills(player) + 1);
    68. try {
    69. stats.save(statsFile);
    70. } catch (IOException ex) {
    71. ex.printStackTrac();
    72. }
    73. }
    74.  
    75. public int getDeaths(String player) {
    76. int deaths = stats.get("Players." + player + ".Deaths") != null ? stats.getInt("Players." + player + ".Deaths") : 0;
    77. return deaths;
    78. }
    79.  
    80. public void addDeath(String player) {
    81. File statsFile = new File(getDataFolder(), "stats.yml");
    82. FileConfiguration stats = YamlConfiguration.loadConfiguration(statsFile);
    83. stats.set("Players." + player + ".Deaths", getDeaths(player) + 1);
    84. try {
    85. stats.save(statsFile);
    86. } catch (IOException ex) {
    87. ex.printStackTrac();
    88. }
    89. }
    90.  
    91.  
    92. public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    93. Player p = (Player)sender;
    94. if (!(sender instanceof Player)) {
    95. p.sendMessage("Je kunt dit command alleen als speler gebruiken.");
    96. return false;
    97. }
    98. if (alias.equalsIgnoreCase("SnorlaxPVP") || alias.equalsIgnoreCase("sp")) {
    99. if (args.length == 0) {
    100. p.sendMessage(Title + ChatColor.RED + "Ongeldig command, gebruik /SnorlaxPVP Help, voor alle commands.");
    101. } else {
    102. if(args[0].equalsIgnoreCase("Help")) {
    103. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    104. p.sendMessage(ChatColor.GOLD + " Help Menu " );
    105. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    106. p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Het basis commando." );
    107. p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Help Geeft dit menu weer." );
    108. p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Join Ga in DeathMatch." );
    109. p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Stats Laat je stats zien." );
    110. p.sendMessage(ChatColor.RED + "/SnorlaxPVP Setspawn Zet de spawn locatie.");
    111. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    112. p.playSound(p.getLocation(), Sound.SPLASH, 1, 2);
    113. } else {
    114. if (args[0].equalsIgnoreCase("Setspawn")) {
    115. if (p.hasPermission("DeathMatch.SetSpawn")) {
    116. this.getConfig().set("Spawn" + ".X", p.getLocation().getBlockX());
    117. this.getConfig().set("Spawn" + ".Y", p.getLocation().getBlockY());
    118. this.getConfig().set("Spawn" + ".Z", p.getLocation().getBlockZ());
    119. this.getConfig().set("Spawn" + ".Yaw", p.getLocation().getYaw());
    120. this.getConfig().set("Spawn" + ".Pitch", p.getLocation().getPitch());
    121. this.getConfig().set("Spawn" + ".World", p.getLocation().getWorld());
    122. p.sendMessage(Title + ChatColor.GOLD + "Spawn is gezet!");
    123. }
    124. } else {
    125. if (args[0].equalsIgnoreCase("Join")) {
    126. int spawnX = this.getConfig().getInt("Spawn" + ".X");
    127. int spawnY = this.getConfig().getInt("Spawn" + ".Y");
    128. int spawnZ = this.getConfig().getInt("Spawn" + ".Z");
    129. int spawnYaw = this.getConfig().getInt("Spawn" + ".Yaw");
    130. int spawnPitch = this.getConfig().getInt("Spawn" + ".Pitch");
    131. Object world = this.getConfig().get("Spawn" + ".World");
    132.  
    133. Location spawn = new Location((World) world, spawnX, spawnY, spawnZ, spawnYaw, spawnPitch);
    134. p.teleport(spawn);
    135. p.sendMessage(Title + ChatColor.GREEN + "Je bent nu in SnorlaxPVP!");
    136. } else {
    137. int kills = stats.getInt("Players." + p.getName() + ".Kills");
    138. int deaths = stats.getInt("Players." + p.getName() + ".Deaths");
    139. if (args[0].equalsIgnoreCase("Stats")) {
    140. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    141. p.sendMessage(ChatColor.DARK_GREEN+ " Statistics " );
    142. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    143. p.sendMessage(ChatColor.GREEN + "Kills: " + ChatColor.WHITE + kills);
    144. p.sendMessage(ChatColor.GREEN + "Deaths: " + ChatColor.WHITE + deaths);
    145. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    146. }
    147. }
    148. }
    149. }
    150. }
    151. }
    152. return false;
    153. }
    154. }
    155.  
     
  16. Offline

    RamonPeek

    Azubuso It still doesn't work, it has allot of errors, I hope you have another idea, but i have to go now... I will be on my computer tomorrow again :). (The only thing what has to happen = Show Players.PLAYERNAME.Kills in the Stats.yml file. It keeps an empty file but it says when i die and reload the server: File has been edited, do you want to reload? I hope you guys can help me out :)
     
  17. Offline

    Azubuso

    RamonPeek ..Errors?
    EDIT: Ugh I'm sorry, that code up there is the wrong paste..here:
    Show Spoiler
    Code:java
    1. package me.RamonPeek;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5. import java.util.logging.Logger;
    6.  
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.ChatColor;
    9. import org.bukkit.Location;
    10. import org.bukkit.Sound;
    11. import org.bukkit.World;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandSender;
    14. import org.bukkit.configuration.file.FileConfiguration;
    15. import org.bukkit.configuration.file.YamlConfiguration;
    16. import org.bukkit.entity.Player;
    17. import org.bukkit.event.EventHandler;
    18. import org.bukkit.event.entity.PlayerDeathEvent;
    19. import org.bukkit.inventory.Inventory;
    20. import org.bukkit.plugin.PluginManager;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class Core extends JavaPlugin {
    24.  
    25. public final Logger SGLogger = Logger.getLogger("Minecraft");
    26. public static String Title = ChatColor.DARK_GRAY + "[" + ChatColor.GRAY + "SnorlaxGames" + ChatColor.DARK_GRAY + "]";
    27. Inventory KitSelector = Bukkit.createInventory(null, 45, ChatColor.DARK_GRAY + "[KitSelector]");
    28.  
    29. @Override
    30. public void onEnable() {
    31. SGLogger.info("-------------------------------------------------");
    32. SGLogger.info("Plugin is aan het opstarten." );
    33. SGLogger.info("Plugin is nu klaar voor gebruik!" );
    34. SGLogger.info("[SIZE=3][FONT=arial][COLOR=#545454]©[/COLOR][/FONT][/SIZE]Copyright: RamonPeek, Alle rechten Voorbehouden!");
    35. SGLogger.info("LET OP: Gebruik versie 1.7.* of hoger!" );
    36. SGLogger.info("-------------------------------------------------");
    37.  
    38. PluginManager pm = Bukkit.getServer().getPluginManager();
    39. pm.registerEvents(new JoinLeave(), this);
    40. pm.registerEvents(new PlayerInteract(), this);
    41.  
    42. File statsFile = new File(getDataFolder(), "stats.yml");
    43. if (!statsFile.exists()) {
    44. try {
    45. statsFile.createNewFile();
    46. } catch (IOException ex) {
    47. ex.printStackTrace();
    48. }
    49. }
    50. }
    51.  
    52. @EventHandler
    53. public void onKill(PlayerDeathEvent e) {
    54. if(e.getEntity().getKiller() instanceof Player){
    55. Player victim = e.getEntity();
    56. Player killer = victim.getKiller();
    57. killer.giveExpLevels(5);
    58. addKill(killer.getName());
    59. addDeath(victim.getName());
    60. }
    61. }
    62.  
    63. public int getKills(String player) {
    64. File statsFile = new File(getDataFolder(), "stats.yml");
    65. FileConfiguration stats = YamlConfiguration.loadConfiguration(statsFile);
    66. int kills = stats.get("Players." + player + ".Kills") != null ? stats.getInt("Players."+player+".Kills") : 0;
    67. return kills;
    68. }
    69.  
    70. public void addKill(String player) {
    71. File statsFile = new File(getDataFolder(), "stats.yml");
    72. FileConfiguration stats =YamlConfiguration.loadConfiguration(statsFile);
    73. stats.set("Players." + player + ".Kills", getKills(player) + 1);
    74. try {
    75. stats.save(statsFile);
    76. } catch (IOException ex) {
    77. ex.printStackTrace();
    78. }
    79. }
    80.  
    81. public int getDeaths(String player) {
    82. File statsFile = new File(getDataFolder(), "stats.yml");
    83. FileConfiguration stats = YamlConfiguration.loadConfiguration(statsFile);
    84. int deaths = stats.get("Players." + player + ".Deaths") != null ? stats.getInt("Players." + player + ".Deaths") : 0;
    85. return deaths;
    86. }
    87.  
    88. public void addDeath(String player) {
    89. File statsFile = new File(getDataFolder(), "stats.yml");
    90. FileConfiguration stats = YamlConfiguration.loadConfiguration(statsFile);
    91. stats.set("Players." + player + ".Deaths", getDeaths(player) + 1);
    92. try {
    93. stats.save(statsFile);
    94. } catch (IOException ex) {
    95. ex.printStackTrace();
    96. }
    97. }
    98.  
    99.  
    100. public boolean onCommand(CommandSender sender, Command cmd, String alias, String[] args) {
    101. Player p = (Player)sender;
    102. File statsFile = new File(getDataFolder(), "stats.yml");
    103. FileConfiguration stats = YamlConfiguration.loadConfiguration(statsFile);
    104. if (!(sender instanceof Player)) {
    105. p.sendMessage("Je kunt dit command alleen als speler gebruiken.");
    106. return false;
    107. }
    108. if (alias.equalsIgnoreCase("SnorlaxPVP") || alias.equalsIgnoreCase("sp")) {
    109. if (args.length == 0) {
    110. p.sendMessage(Title + ChatColor.RED + "Ongeldig command, gebruik /SnorlaxPVP Help, voor alle commands.");
    111. } else {
    112. if(args[0].equalsIgnoreCase("Help")) {
    113. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    114. p.sendMessage(ChatColor.GOLD + " Help Menu " );
    115. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    116. p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Het basis commando." );
    117. p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Help Geeft dit menu weer." );
    118. p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Join Ga in DeathMatch." );
    119. p.sendMessage(ChatColor.WHITE + "/SnorlaxPVP Stats Laat je stats zien." );
    120. p.sendMessage(ChatColor.RED + "/SnorlaxPVP Setspawn Zet de spawn locatie.");
    121. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    122. p.playSound(p.getLocation(), Sound.SPLASH, 1, 2);
    123. } else {
    124. if (args[0].equalsIgnoreCase("Setspawn")) {
    125. if (p.hasPermission("DeathMatch.SetSpawn")) {
    126. this.getConfig().set("Spawn" + ".X", p.getLocation().getBlockX());
    127. this.getConfig().set("Spawn" + ".Y", p.getLocation().getBlockY());
    128. this.getConfig().set("Spawn" + ".Z", p.getLocation().getBlockZ());
    129. this.getConfig().set("Spawn" + ".Yaw", p.getLocation().getYaw());
    130. this.getConfig().set("Spawn" + ".Pitch", p.getLocation().getPitch());
    131. this.getConfig().set("Spawn" + ".World", p.getLocation().getWorld());
    132. p.sendMessage(Title + ChatColor.GOLD + "Spawn is gezet!");
    133. }
    134. } else {
    135. if (args[0].equalsIgnoreCase("Join")) {
    136. int spawnX = this.getConfig().getInt("Spawn" + ".X");
    137. int spawnY = this.getConfig().getInt("Spawn" + ".Y");
    138. int spawnZ = this.getConfig().getInt("Spawn" + ".Z");
    139. int spawnYaw = this.getConfig().getInt("Spawn" + ".Yaw");
    140. int spawnPitch = this.getConfig().getInt("Spawn" + ".Pitch");
    141. Object world = this.getConfig().get("Spawn" + ".World");
    142.  
    143. Location spawn = new Location((World) world, spawnX, spawnY, spawnZ, spawnYaw, spawnPitch);
    144. p.teleport(spawn);
    145. p.sendMessage(Title + ChatColor.GREEN + "Je bent nu in SnorlaxPVP!");
    146. } else {
    147. int kills = stats.getInt("Players." + p.getName() + ".Kills");
    148. int deaths = stats.getInt("Players." + p.getName() + ".Deaths");
    149. if (args[0].equalsIgnoreCase("Stats")) {
    150. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    151. p.sendMessage(ChatColor.DARK_GREEN+ " Statistics " );
    152. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    153. p.sendMessage(ChatColor.GREEN + "Kills: " + ChatColor.WHITE + kills);
    154. p.sendMessage(ChatColor.GREEN + "Deaths: " + ChatColor.WHITE + deaths);
    155. p.sendMessage(ChatColor.DARK_GRAY + "------------------------------------------" );
    156. }
    157. }
    158. }
    159. }
    160. }
    161. }
    162. return false;
    163. }
    164. }
     
  18. Offline

    Mr360zack

    Rather than constantly reading/writing from and to a flat file, maybe cache the stats in a hashmap and save/load it on join/quit

    Edit: I also suggest per-player configuration files.
     
  19. Offline

    Code0

    What... dude, imagine Hypixel getting per players config files... #memory = killed
     
  20. Offline

    RamonPeek

    The whole code is working behalve of one thing, the stats.yml keeps empty...
     
  21. maybe add this to the onenable:
    Code:java
    1.  
    2. stats.options().copyDefaults(true);
    3.  
     
  22. Offline

    RamonPeek

    FisheyLP I'll try it in a minute! Thanks for keeping this thread up to date ;).
    (Maybe this sentence isn't quite good, this is because i'm Dutch).

    FisheyLP File keeps empty..

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

    RamonPeek

    Solved, thanks!
     
  24. Offline

    ESSHD

    It's the same, he just adds their files into a MySQL database... That's what they're for.. lol
     
Thread Status:
Not open for further replies.

Share This Page