PlayerDeathEvent repeating?

Discussion in 'Plugin Development' started by Reteckz, Apr 10, 2013.

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

    Reteckz

    Hello guys.

    I'm writing a plugin, which works with the playerdeathevent.
    The strange thing is, after the server reloaded or something, it executes it once first, as it is supposed to be, and keeps multiplying with 2 afterwards.

    So exe 1x, 2x, 4x, 8x, 16x, 32x etc.

    Anyone has encountered this problem before and found a fix for it?
     
  2. Offline

    Ne0nx3r0

    You might want to include some code samples to explain what's going on.
     
  3. Offline

    Reteckz

    Err I made an example code:

    @EventHandler
    public void onDeath(PlayerDeathEvent event) {
    Player player = (Player) event.getEntity();
    player.sendmessage("test");
    }

    First time a player dies, it just sends you "test" once.
    When a player dies for the 2nd time, it sends you it twice.
    3rd time 4 times
    4th time 8 times.
    etc. etc.

    So it's not a problem in my coding, it's something with the playerdeathevent.
    I also tried it with entitydeathevent, but that one isn't working either.

    EDIT:

    Example screenshot:
    [​IMG]
     
  4. Offline

    StaticE

    There's nothing in that example code that would cause this to happen....are you sure you don't have anything extra in your real code?
     
  5. Offline

    SnipsRevival

    Can you post exactly what you have? I am not understanding how this would be happening from that sample you gave.
     
  6. Offline

    Reteckz

    Code:
    package me.Reteckz.pvpcommand;
     
    import java.io.File;
    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class pvpcommand extends JavaPlugin implements Listener{
        public final Logger logger = Logger.getLogger("Minecraft");
        public static pvpcommand plugin;
       
        @Override
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            FileConfiguration config;
            config = getConfig();
            File configFile = new File("plugins" + File.separator + "PvPCommand" + File.separator + "config.yml");           
            config = YamlConfiguration.loadConfiguration(configFile);
            config.addDefault("1.Kills", "10");
            config.addDefault("1.Commands.1", "give [NAME] diamond 2");
            config.addDefault("1.Commands.2", "give [NAME] stick 5");
            config.addDefault("1.Text", "You got rewarded for 10 kills!");
            config.addDefault("2.Kills", "25");
            config.addDefault("2.Commands.1", "give [NAME] diamond 5");
            config.addDefault("2.Commands.2", "give [NAME] stick 10");
            config.addDefault("2.Text", "You got rewarded for 25 kills!");
            config.options().copyDefaults(true);
            try {
                config.save(configFile);
            } catch (IOException e1) {
            }
        }
       
        @EventHandler
        public void onDeath(PlayerDeathEvent event) {
            getServer().getPluginManager().registerEvents(this, this);
            Player player = (Player) event.getEntity();
            if(player.getKiller() instanceof Player){
            Player killer = player.getKiller();
            String name = killer.getDisplayName();
            FileConfiguration killconfig;
            killconfig = getConfig();
            File killconfigFile = new File("plugins" + File.separator + "PvPCommand" + File.separator + "Kills.yml");           
            killconfig = YamlConfiguration.loadConfiguration(killconfigFile);
            FileConfiguration config;
            config = getConfig();
            File configFile = new File("plugins" + File.separator + "PvPCommand" + File.separator + "config.yml");           
            config = YamlConfiguration.loadConfiguration(configFile);
            if (!(killconfig.contains(name))){
                killconfig.set(name, "0");
                }   
                String killso = killconfig.getString(name);
                int kills = Integer.parseInt(killso);
                kills = kills + 1;
                killso = Integer.toString(kills);
                killconfig.set(name, killso);
                int counter = 1;
                String count = Integer.toString(counter);
                int nkills;
                String command = "";
                String text;
                String killswut;
                int commandcounter = 1;
                String ccount = Integer.toString(commandcounter);
                while(config.contains(count+".Kills")){
                    killswut = config.getString(count+".Kills");
                    nkills = Integer.parseInt(killswut);
                    if(kills == nkills){
                        text = (String) config.get(count+".Text");
                        while(config.contains(count+".Commands."+ccount)){
                            command = (String) config.get(count+".Commands."+ccount);
                            command = command.replace("[NAME]", name);
                            Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
                            commandcounter = commandcounter + 1;
                            ccount = Integer.toString(commandcounter);
                        }
                        killer.sendMessage(ChatColor.GOLD + text);
                    }
                    counter = counter + 1;
                    count = Integer.toString(counter);
                }
            try {
                killconfig.save(killconfigFile);
            } catch (IOException e1) {
            }
            }
        }
    }
    But I even tried it with just the onenable and:

    @EventHandler
    public void onDeath(PlayerDeathEvent event) {
    Player player = (Player) event.getEntity();
    player.sendmessage("test");
    }

    too, same problem.

    For the time-being I fixed it by storing the date of the first time to a .yml, and checking if the current seconds - 1 > old seconds.

    But of course this shouldn't be needed.

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

    MTN

Thread Status:
Not open for further replies.

Share This Page