1.8.8 Death message duplicating

Discussion in 'Spigot Plugin Development' started by iiSupremeGuy, Jun 30, 2020 at 10:10 AM.

  1. I am making a pvp plugin and I am trying to make players earn coins when they kill a player. Here is the code:
    Code (Text):
            @EventHandler

            public void onKill(PlayerDeathEvent e) {
                Player p = e.getEntity();

                if (p.getKiller() instanceof Player) {
                    Player killer = p.getKiller();
                        for (Map.Entry<String, Integer> entry : api.eco.entrySet()) {
                            int after = (entry.getValue() + 10);
                            api.eco.put(killer.getUniqueId().toString(), after);
                            killer.sendMessage("+10 coins for kill!");
                            killer.getScoreboard().getTeam("moneyCounter").setPrefix("$" + api.eco.get(killer.getUniqueId().toString()));
                        }
                }
            }
    The problem is that the message is duplicating and the player is earning 30 coins instead of 10.
     
  2. What's the api.eco map?
    If there are 3 entries in there then the code will run 3 times
     
  3. the api.eco map is on my other economy plugin
    Code (Text):
    public static Map<String, Integer> eco = new HashMap<String, Integer>();
    i tried using api.eco.get but it just shows null
    Code (Text):
            @EventHandler
            public void onKill(PlayerDeathEvent e) {
                Player p = e.getEntity();
                if (p.getKiller() != null) {
                    if (p.getKiller() instanceof Player) {
                        Player killer = p.getKiller();

                        int after = api.eco.get(p.getUniqueId()) + 10;
                        int before = api.eco.get(p.getUniqueId()) - 5;
                        api.eco.put(killer.getUniqueId().toString(), after);
                        api.eco.put(p.getUniqueId().toString(), before);
                        killer.sendMessage("+10 coins for kill!");
                        p.sendMessage("-5 coins for dying!");
                        killer.getScoreboard().getTeam("moneyCounter").setPrefix("$" + api.eco.get(killer.getUniqueId().toString()));
                        p.getScoreboard().getTeam("moneyCounter").setPrefix("$" + api.eco.get(killer.getUniqueId().toString()));

                    }
                }
            }
     
  4. Why are you looping through that map?

    Essentially what you are doing here is:
    Code (Text):
                        for (Map.Entry<String, Integer> entry : api.eco.entrySet()) { // for every entry in this map-
                            int after = (entry.getValue() + 10);
                            api.eco.put(killer.getUniqueId().toString(), after); // -put their value to value + 10
                            killer.sendMessage("+10 coins for kill!");
                            killer.getScoreboard().getTeam("moneyCounter").setPrefix("$" + api.eco.get(killer.getUniqueId().toString()));
                        }
    Also, correct me, someone, if I'm wrong but PlayerDeathEvent may not always have a killer, he might die from something like fall damage for example.
    What you want to listen to is EntityDamageByEntityEvent and do some checks to determine if the entity is dead or not, if so add "coins"
    There are plenty of tutorials just look them up
     
  5. Since I am new with java, I thought that map entries would find information for a target player, but now that i think about it, it doesn't seem that entries get map keys.
    I don't exactly know what you mean by that.
     
  6. It doesn't get much simpler than this
    If the entity is dead and is a player add coins to the killer.
     
  7. I can't seem to get this code to work (it doesn't give the player coins and doesn't send the player a message, also it comes up with no errors)

    Code (Text):
            @EventHandler
            public void onDeath(EntityDamageByEntityEvent e) {
                if (e.getEntity() instanceof Player) {
                    if (e.getDamager() instanceof Player) {
                        Player p = (Player) e.getEntity();
                        Player k = (Player) e.getDamager();
                        if (p.isDead()) {
                            int after = api.eco.get(p.getUniqueId()) + 10;
                            int before = api.eco.get(p.getUniqueId()) - 5;
                            api.eco.put(k.getUniqueId().toString(), after);
                            api.eco.put(p.getUniqueId().toString(), before);
                            k.sendMessage("+10 coins for kill!");
                            p.sendMessage("-5 coins for dying!");
                            k.getScoreboard().getTeam("moneyCounter").setPrefix("$" + api.eco.get(k.getUniqueId().toString()));
                            p.getScoreboard().getTeam("moneyCounter").setPrefix("$" + api.eco.get(k.getUniqueId().toString()));
                        }
                    }
                }
            }
     
  8. Try
    Code (Text):
            @EventHandler
            public void onKill(PlayerDeathEvent e) {
                Player p = e.getEntity();
                if (p.getKiller() != null) {
                    if (p.getKiller() instanceof Player) {
                        Player killer = p.getKiller();

                        int after = api.eco.get(p.getUniqueId().toString()) + 10;
                        int before = api.eco.get(p.getUniqueId().toString()) - 5;
                        api.eco.put(killer.getUniqueId().toString(), after);
                        api.eco.put(p.getUniqueId().toString(), before);
                        killer.sendMessage("+10 coins for kill!");
                        p.sendMessage("-5 coins for dying!");
                        killer.getScoreboard().getTeam("moneyCounter").setPrefix("$" + api.eco.get(killer.getUniqueId().toString()));
                        p.getScoreboard().getTeam("moneyCounter").setPrefix("$" + api.eco.get(killer.getUniqueId().toString()));

                    }
                }