Save Kills and Deaths in Config

Discussion in 'Spigot Plugin Development' started by Itznewer, May 31, 2017.

  1. Hi guys, well I've been wanting to do this for a long time and I do not know how, I tried several methods but many of them were only for kills.
    I'm doing an FFA plugin, which I want to keep all the kills and deaths of the players in data.yml I do not know how to do it, can you give me some help?

    Thank you very much Here is a part of my code:

    Code (Text):
         @EventHandler
         public void morir(PlayerDeathEvent e){
             Player m = e.getEntity();
             Player k = e.getEntity().getKiller();
             data d = data.getConfig();
             int killCount = d.getInt("Players." + k.getName() + ".kills") + 1;
             int deathCount = d.getInt("Players." + m.getName() + ".deaths") + 1;
             if(!(k instanceof Player)){
                 d.set("Players." + m.getName() + ".deaths", deathCount);
                 d.save();
             } else {
                 d.set("Players." + m.getName() + ".deaths", deathCount);
                 d.set("Players." + k.getName() + ".kills", killCount);
                 d.save();
             }
     
  2. What exactly is the problem here?
     
  3. Have an object representing player for easier manipulation. Also. Player#getStatistic(Statistic.DEATHS) returns deaths
     
  4. Until we know the problem we can't help, but I noticed a slight inefficiency in your code;

    Code (Text):

    if(!(k instanceof Player)){
        d.set("Players." + m.getName() + ".deaths", deathCount);
        d.save();
    } else {
        d.set("Players." + m.getName() + ".deaths", deathCount);
        d.set("Players." + k.getName() + ".kills", killCount);
        d.save();
    }
    As there are 2 duplicate lines which don't depend on the condition, they can be moved outside of the If statement and only written once;

    Code (Text):

    d.set("Players." + m.getName() + ".deaths", deathCount);

    if(k instanceof Player)
    {
        d.set("Players." + k.getName() + ".kills", killCount);
    }

    d.save();
     
  5. The error is that when I die by falling or killed by some mob, I do not tell the death
     
  6. The error is that when I die by falling or killed by some mob, I do not save the death
     
  7. @Itznewer
    Do you have an error in your console?
     
  8. Ofcourse it won't save it since the killer isn't an instance of a player...
     
  9. Cause the killer is null..
     
  10. You define k (the killer) as a player at the beginning of your code! This would have to be an entity. Else you'll get an error since a mob isnt a player.
     
  11. This would solve the problem anyway because the deathcount would be increased always a player dies.