Solved Adding to an exsiting config node.

Discussion in 'Plugin Development' started by HackintoshMan, Apr 14, 2013.

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

    HackintoshMan

    So I am going to be keeping track of every players kills and deaths in a config. The format will be as follows:
    Code:
    Stats
        Players
            playerXX
                kills : 0
                deaths : 0
            playerYY
                kills : 0
                deaths : 0
    and so on for each player. When there is a player death, I want to get the killer and add 1 to the killers kills section and add 1 to the deceased deaths section. How can I do this?
     
  2. Offline

    Jake0oo0

    Here's a small snippit from one of my plugins, this should give you an idea of what you want to do.

    PHP:
    @EventHandler
    public void playerDeath(PlayerDeathEvent event) {
    String killer event.getEntity().getKiller().getName();
    String dead event.getEntity().getName();
    Player player event.getEntity();
    int level plugin.getConfig().getInt("players." killer ".level");
    int deadLevel plugin.getConfig().getInt("players." dead ".level");
    plugin.getConfig().set("players." killer ".kills",
    plugin.getConfig().getInt("players." killer ".kills") + 1);
     
  3. Offline

    crushh87

    getConfig().set("Stats.Players." + player.getName() + ".kills");
     
  4. Offline

    HackintoshMan

    Ok thanks both of you. One more question. I have two teams, red and blue. when a player joins they are put into one. When I check to see if they are in a team nothing happens.

    Code:java
    1.  
    2. public void onPlayerMove(PlayerMoveEvent event) {
    3. Player player = event.getPlayer();
    4. getPointOneLocation();
    5. getPointTwoLocation();
    6. getPointThreeLocation();
    7. if (player.getLocation().getBlockX() == x1// point 1
    8. && player.getLocation().getBlockY() == y1
    9. && player.getLocation().getBlockZ() == z1
    10. && player.getLocation().getWorld().getName() == world1) {
    11. System.out.println("Player location = point location!");
    12. if (MCFTF2.blue.contains(player)) {
    13. System.out.println("player == blue team");
    14. if (hasBeenCaptured1 == false && hasBeenCaptured2 == false
    15. && hasBeenCaptured3 == false) {
    16. Bukkit.getScheduler().runTaskTimer(plugin,
    17. new BukkitRunnable() {
    18.  
    19. @Override
    20. public void run() {
    21.  
    22. if (captureTime > 0) {
    23. System.out.println(captureTime);
    24. captureTime--;
    25. } else {
    26. this.cancel();
    27. }
    28.  
    29. }
    30.  
    31. }, 20, 20);
    32. Bukkit.broadcastMessage(plugin.PREFIX + player
    33. + " is capturing point 1!");
    34. if (captureTime == 0) {
    35. Bukkit.broadcastMessage(plugin.PREFIX + player
    36. + " has captured point 1!");
    37. hasBeenCaptured1 = true;
    38. }
    39. if (player.getLocation().getBlockX() != x1
    40. && player.getLocation().getBlockY() != y1
    41. && player.getLocation().getBlockZ() != z1
    42. && player.getLocation().getWorld().getName() != world1) {
    43. Bukkit.broadcastMessage(plugin.PREFIX + player
    44. + " has lost control of point 1!");
    45. captureTime = 3;
    46. }
    47. } else {
    48. player.sendMessage(plugin.PREFIX
    49. + "You must capture point 1!");
    50. }
    51. } else {
    52. player.sendMessage(plugin.PREFIX
    53. + "This is your own point! Try to defend this point.");
    54. }
    55. }
    56.  


    Code:
    getConfig().set("Stats.Players." + killed.getKiller().getName() + ".Kills", +1);
    this sets the players kills from 0 to 1, but it won't set it from 1 to 2. Is it possible to do this?

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

    davejavu

    Code:
    int currentKills = getConfig().getInt("Stats.players." + killed.getKiller().getName() + ".Kills");
    getConfig().set("Stats.players." + killed.getKiller().getName() +".Kills", currentKills + 1);
     
    //other stuff
     
  6. Offline

    HackintoshMan

    Ahhh I though I was going to have to do something like that. Thanks!

    Code:
    if (plugin.getConfig()
                                    .getInt("Stats.Players." + sender.getName()
                                            + ".Deaths") != 0) {
                                System.out.println("has died");
                                sender.sendMessage(plugin.PREFIX + ChatColor.GOLD
                                        + "[]--- K/D of player " + ChatColor.AQUA
                                        + sender.getName() + ChatColor.GOLD
                                        + " ---[]");
                                sender.sendMessage(plugin.PREFIX
                                        + ChatColor.GREEN
                                        + "Kills: "
                                        + ChatColor.DARK_AQUA
                                        + plugin.getConfig().getInt(
                                                "Stats.Players." + sender.getName()
                                                        + ".Kills"));
                                sender.sendMessage(plugin.PREFIX
                                        + ChatColor.GREEN
                                        + "Deaths: "
                                        + ChatColor.DARK_AQUA
                                        + plugin.getConfig().getInt(
                                                "Stats.Players." + sender.getName()
                                                        + ".Deaths"));
                               
                                cd = plugin.getConfig().getInt(
                                        "Stats.Players." + sender.getName()
                                        + ".Deaths");
                                ck = plugin.getConfig().getInt(
                                        "Stats.Players." + sender.getName()
                                        + ".Kills");
                               
                                sender.sendMessage(plugin.PREFIX
                                        + ChatColor.GREEN
                                        + "K/D: "
                                        + ChatColor.DARK_AQUA
                                        + ck/cd);
    near the bottom. It is supposed to calculate the KD ration of the player. But I have 2 deaths and 3 kills but it says I have a kd of 0. What is wrong?

    FIXED:

    I was getting an int from the config, so I changed it to double so that it could allow a decimal place. 2/3= .666666666666 with the double while 2/3=0 with the int.

    Now how to round…

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

Share This Page