For some reason PlayerDeathEvent not reading the item in hand

Discussion in 'Plugin Development' started by OppositeGamer, Jan 2, 2013.

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

    OppositeGamer

    For some reason, out of no where my code use to work, Im pretty sure I did no changes to this class. Then it just stops working, right when it gets to the getting item in hand for the bow itemID 261, please help

    Code:
    package net.gungame.src;
     
    import java.io.File;
    import java.io.IOException;
     
     
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    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;
     
    public class PlayerDeathListener implements Listener {
     
        private GunGame plugin;
        private ChatUtil chatUtil = new ChatUtil();
        private SpawnPoint spawnPoint = new SpawnPoint();
       
        public PlayerDeathListener(GunGame gg) {
            this.plugin = gg;
        }
       
        @EventHandler
        public void onPlayerDeathWinner(PlayerDeathEvent evt) {
           
            Player player = evt.getEntity();
           
            if(evt instanceof Player) {
           
            int next = 2 + (int)(Math.random() * ((6 - 2) + 1));
            evt.setDeathMessage(plugin.message.get(next));
           
            File playersFile = new File("plugins/GunGame/players/" + player.getKiller().getName() + ".yml");
            FileConfiguration players = YamlConfiguration.loadConfiguration(playersFile);
           
            if(player.getKiller().getItemInHand().getType() == Material.BLAZE_ROD) {
                plugin.getServer().broadcastMessage(chatUtil.tag + ChatColor.GREEN + player.getKiller().getName() + ChatColor.LIGHT_PURPLE + " is the winner!");
                players.set("Balance", players.getInt("Balance") +10);
               
                for(String s : plugin.joined) {
                Player p = plugin.getServer().getPlayer(s);
               
                spawnPoint.mainspawnPoint("spawns.yml", plugin, p);
               
                plugin.assigner.clear();
                plugin.gunAssigner.clear();
                plugin.match.clear();
                plugin.start.clear();
                }           
                plugin.joined.clear();
                for(String ss : plugin.spectating) {
                    Player pp = plugin.getServer().getPlayer(ss);
                    plugin.spectating.remove(pp.getName());
                    plugin.joined.add(pp.getName());
                    plugin.inLobby.add(pp.getName());
                    plugin.gunAssigner.put(pp.getName(), 1);
                    int troll = 1 + (int)(Math.random() * ((5 - 1) + 1));
                   
                    plugin.assigner.put(pp.getName(), troll);
                    pp.hidePlayer(pp);
                    pp.setAllowFlight(false);
                    spawnPoint.lobbyspawnPointSpec("spawns.yml", plugin, pp);
                }
               
                try {
                    players.save(playersFile);
                } catch (IOException e) {
                    e.printStackTrace();
                        }
                    }
                }
            }
       
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent evt) {
           
            Player player = evt.getEntity();
           
            if(evt instanceof Player) {
            int next = 2 + (int)(Math.random() * ((6 - 2) + 1));
            evt.setDeathMessage(plugin.message.get(next));
           
            File playersFile = new File("plugins/GunGame/players/" + player.getKiller().getName() + ".yml");
            FileConfiguration players = YamlConfiguration.loadConfiguration(playersFile);
           
                if(player.getKiller().getItemInHand().getType().getId() == 261) {
                    plugin.getServer().broadcastMessage("test");
                        plugin.gunAssigner.put(player.getKiller().getName(), plugin.gunAssigner.get(player.getKiller().getName()) +1);
                        player.getKiller().getInventory().remove(Material.BOW);
                        player.getKiller().getInventory().addItem(plugin.gun.get(plugin.gunAssigner.get(player.getKiller().getName())));
                        player.updateInventory();
                        players.set("Balance", players.getInt("Balance") +1);
                       
                        try {
                            players.save(playersFile);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if(plugin.gunAssigner.get(player.getKiller().getName()) == 23) {
                            plugin.getServer().broadcastMessage(ChatColor.DARK_PURPLE + "Player " + ChatColor.BLUE.toString() + ChatColor.BOLD.toString() + player.getKiller().getName() + ChatColor.DARK_PURPLE + " is on the last weapon!");
                        }
                }
                else if (player.getKiller().getItemInHand().getType() == Material.AIR)
                {
                    if(plugin.gunAssigner.get(player.getName()) == 1)
                    {
                       
                    }
                    player.getInventory().remove(Material.BOW);
                    plugin.gunAssigner.put(player.getName(), plugin.gunAssigner.get(player.getName()) -1);
                    player.getInventory().addItem(plugin.gun.get(plugin.gunAssigner.get(player.getName())));
                    player.updateInventory();
                    }   
                }
            }
        }
    
     
  2. Offline

    Major_Derp

    OppositeGamer
    So what just stopped working? Its not adding the item? And onPlayer death you dont have to check for the entity, because since its a player death then that means the entity is a player. I think thats how it goes.
     
  3. Offline

    OppositeGamer

    I did man. I mean honsetly I have checked all my code and everything. It just doesnt make any damn since! lol Just out of NOWHERE just stopped working. I did do some work on code. But it does add me to the gunAssigner when I join the game. So I am in the game its just when I get a kill with the bow in my hand it doesnt register?

    ok So I changed the code to check for a sword in there hand, instead of a bow. And it works! But it needs to check for a bow. Any ideas? It did work, and now it doesnt..

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

    Major_Derp

    So you checked the same way you did for the bow? If thats so thats pretty weird, whats your new updated code?
     
  5. Offline

    OppositeGamer

    Here let me show you I changed it to check if the killer was an arrow/projectile. But no budge

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent evt) {
           
            Player player = evt.getEntity().getPlayer();
           
            int next = 2 + (int)(Math.random() * ((6 - 2) + 1));
            evt.setDeathMessage(plugin.message.get(next));
           
            File playersFile = new File("plugins/GunGame/players/" + player.getKiller().getName() + ".yml");
            FileConfiguration players = YamlConfiguration.loadConfiguration(playersFile);
           
            if(player.getKiller() instanceof Projectile) {
                Projectile arrow = (Projectile) player;
                if(arrow.getShooter() instanceof Player) {
                    Player BowMan = (Player) arrow.getShooter();
                   
                    plugin.getServer().broadcastMessage("test");
                        plugin.gunAssigner.put(BowMan.getName(), plugin.gunAssigner.get(BowMan.getName()) +1);
                        BowMan.getInventory().remove(Material.BOW);
                        BowMan.getInventory().addItem(plugin.gun.get(plugin.gunAssigner.get(player.getKiller().getName())));
                        BowMan.updateInventory();
                        players.set("Balance", players.getInt("Balance") +1);
                       
                        try {
                            players.save(playersFile);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        if(plugin.gunAssigner.get(player.getKiller().getName()) == 23) {
                            plugin.getServer().broadcastMessage(ChatColor.DARK_PURPLE + "Player " + ChatColor.BLUE.toString() + ChatColor.BOLD.toString() + player.getKiller().getName() + ChatColor.DARK_PURPLE + " is on the last weapon!");
                        }
                }
            }
                if (player.getKiller().getItemInHand().getType() == Material.AIR)
                {
                    if(plugin.gunAssigner.get(player.getName()) == 1)
                    {
                       
                    }
                    player.getInventory().remove(Material.BOW);
                    plugin.gunAssigner.put(player.getName(), plugin.gunAssigner.get(player.getName()) -1);
                    player.getInventory().addItem(plugin.gun.get(plugin.gunAssigner.get(player.getName())));
                    player.updateInventory();
                    }   
                }
        }
     
  6. Code:java
    1. if(player.getKiller() instanceof Projectile) {
    2. Projectile arrow = (Projectile) player;
    Pretty sure it should be something like this:
    Code:java
    1. if(player.getKiller() instanceof Projectile) {
    2. Projectile arrow = (Projectile) player.getKiller();
     
  7. Offline

    fireblast709

    player.getKiller() instanceof Projectile == false, as getKiller() returns a Player (and null if the reason was something else
     
Thread Status:
Not open for further replies.

Share This Page