Where did i go wrong

Discussion in 'Plugin Development' started by PENGUllN, Aug 18, 2015.

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

    PENGUllN

    I need help with EntityDeathEvent this is what I have

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    
    public class WantedList implements Listener {
    
    @EventHandler
    public void playerDeath(EntityDeathEvent event) {
    if ((event instanceof PlayerDeathEvent)) {
    Player player = (Player) event.getEntity();
    if (player.getKiller() instanceof Player) {
        
    }
    
    Bukkit.broadcastMessage("Display Test !" + player.getKiller().getDisplayName());
              }
         }
    }
    
    
    It works fine when player is killed by a player but if I do /kill [name] or another entity kills a player I get errors

    I only want it to getkiller if the killer is a player
    my error is
    Code:
    Could not pass event PlayerDeathEvent to TrustedCops v0.1
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-1649]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1649]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:514) [spigot.jar:git-Spigot-1649]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:499) [spigot.jar:git-Spigot-1649]
            at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerDeathEvent(CraftEventFactory.java:397) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityPlayer.die(EntityPlayer.java:386) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityLiving.damageEntity(EntityLiving.java:745) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityHuman.damageEntity(EntityHuman.java:765) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityPlayer.damageEntity(EntityPlayer.java:465) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityMonster.n(EntityMonster.java:100) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityZombie.n(EntityZombie.java:235) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.PathfinderGoalMeleeAttack.e(PathfinderGoalMeleeAttack.java:100) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.PathfinderGoalSelector.a(PathfinderGoalSelector.java:108) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityInsentient.bn(EntityInsentient.java:420) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityLiving.e(EntityLiving.java:1549) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityInsentient.e(EntityInsentient.java:293) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityMonster.e(EntityMonster.java:21) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityZombie.e(EntityZombie.java:160) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityLiving.h(EntityLiving.java:1409) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityInsentient.h(EntityInsentient.java:154) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityMonster.h(EntityMonster.java:25) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.EntityZombie.h(EntityZombie.java:231) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.World.entityJoinedWorld(World.java:1620) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.World.playerJoinedWorld(World.java:1595) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.World.tickEntities(World.java:1460) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.WorldServer.tickEntities(WorldServer.java:516) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:706) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1649]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1649]
    Caused by: java.lang.NullPointerException
            at com.gmail.chrome2you.TrustedCops.WantedList.playerDeath(WantedList.java:20) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:298) ~[spigot.jar:git-Spigot-1649]
            ... 30 more
    this is not my main class so if you need my main class just ask

    + I'm asking for help with what I did wrong and if possible how to fix

    Thanks PENGUllN
     
  2. Offline

    CraftCreeper6

    @PENGUllN
    Caused by: java.lang.NullPointerException
    at com.gmail.chrome2you.TrustedCops.WantedList.playerDeath(WantedList.java:20) ~[?:?]

    Is that your full WantedList class?

    EDIT --Check before casting.-- Didn't read properly ;p
     
    Last edited: Aug 18, 2015
  3. Offline

    PENGUllN

    I have 3 classes a main(TrustedCops) a CommandClass(CopCommand) and TheClass I Posted Above (WantedList)
    here is my main
    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TrustedCopsMain extends JavaPlugin{
      
        private static Plugin plugin;
      
        public void onEnable() {
            getCommand("cop").setExecutor(new CopCommand());
            registerEvents(this, new WantedList());
            plugin = this;
      
        }
        public void onDisable() {
            plugin = null;
          
        }
      
        public static void registerEvents(org.bukkit.plugin.Plugin plugin, Listener... listeners) {
           for (Listener listener : listeners) {
            Bukkit.getServer().getPluginManager().registerEvents(listener, plugin);
    
        }
    
    }
    
        public static Plugin getPlugin() {
           return plugin;
    
        }
    
    }
    
    + other than the package name yeh that's all my class
     
  4. Offline

    CraftCreeper6

    @PENGUllN
    The static burns. My hair is sticking up ahhh.

    Anyway, can I see your command class?
     
    bwfcwalshy likes this.
  5. Offline

    PENGUllN

    :D:/ Yeh Sure Its Not Done but It Works Fine !?
    Code:
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CopCommand implements CommandExecutor {
      
        List<String> toggled = new ArrayList<String>();
      
        public boolean onCommand(CommandSender sender, Command command,
                String label, String[] args) {
            if(label.equalsIgnoreCase("cop")) {
                if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "" + ChatColor.BOLD + "T" + ChatColor.BLUE + "rusted" + ChatColor.BLUE + "" + ChatColor.BOLD + "C" + ChatColor.BLUE + "ops" + ChatColor.DARK_GRAY + "]" + " " + ChatColor.WHITE + "" + ChatColor.ITALIC + "You Must Be A Playere To Use This");
                return false;
            }
            Player player = (Player) sender;
                    if(toggled.contains(player.getName())) {
    
                    player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "" + ChatColor.BOLD + "T" + ChatColor.BLUE + "rusted" + ChatColor.BLUE + "" + ChatColor.BOLD + "C" + ChatColor.BLUE + "ops" + ChatColor.DARK_GRAY + "]" + " " + ChatColor.WHITE + "" +  ChatColor.ITALIC + "Cop Mode Disabled!");
                    toggled.remove(player.getName());
                    Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "" + ChatColor.BOLD + "T" + ChatColor.BLUE + "rusted" + ChatColor.BLUE + "" + ChatColor.BOLD + "C" + ChatColor.BLUE + "ops" + ChatColor.DARK_GRAY + "]" + " " + ChatColor.GREEN + "" +  ChatColor.BOLD + " " + (player.getDisplayName () + " " + ChatColor.WHITE + "" + ChatColor.ITALIC + "Is No Longer On Duty!"));
    
                    return true;
        }
                  
                player.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "" + ChatColor.BOLD + "T" + ChatColor.BLUE + "rusted" + ChatColor.BLUE + "" + ChatColor.BOLD + "C" + ChatColor.BLUE + "ops" + ChatColor.DARK_GRAY + "]" + " " + ChatColor.WHITE + "" + ChatColor.ITALIC + "Cop Mode Enabled!");
                toggled.add(player.getName());
                Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.BLUE + "" + ChatColor.BOLD + "T" + ChatColor.BLUE + "rusted" + ChatColor.BLUE + "" + ChatColor.BOLD + "C" + ChatColor.BLUE + "ops" + ChatColor.DARK_GRAY + "]" + " " + ChatColor.DARK_RED + "" +  ChatColor.BOLD + " " + (player.getDisplayName () + " " + ChatColor.WHITE + "" + ChatColor.ITALIC + "Is Now On Duty!"));
                return true;
    }
    
        return false;
          
        }
      
    }
    Yeah I know there is a more efficient way to use ChatColor but I live life on the edge :D
     
  6. Offline

    CraftCreeper6

    @PENGUllN
    First of all, create a String for each prefix that is the same, then use that instead of making your code go from America to Australia.

    Second of all, I'm going to take a wild guess and say that because you are casting the event to player death event, I think the server is trying to look for a killer. When there isn't one, considering you are casting the killer to a player. That's what's giving you the NPE. Rather than casting the event and the killer, check the killer is a player first, then cast if it is - if it's not then assume it's the command.
     
  7. Offline

    PENGUllN

    If I Cast Then first
    Code:
    if(entity.getType() == EntityType.PLAYER) {
    
    it would check if its a player first right ?
     
  8. Offline

    CraftCreeper6

    @PENGUllN
    Yes.

    EDIT: Don't create the entity object first though.
     
  9. Offline

    PENGUllN

    I don't suppose you cold show me what you mean ?
     
  10. Offline

    CraftCreeper6

    @PENGUllN
    Unless you want me to technically own part of the plugin?
     
  11. Offline

    PENGUllN

    If Your Asking For Credit and Want Ownership of part of the plugin sure !
     
  12. Offline

    CraftCreeper6

    @PENGUllN
    Well, I don't want part of your plugin. Google is your best friend. Seriously, why are you checking if the event is PlayerDeathEvent but you are using EntityDeathEvent? Just...

    Code:
    @EventHandler
    public void onSomething(PlayerDeathEvent event)
          {
                    //Check if the killer is a player, if so cast to player
                              //Do things
                    //Else if it is a mob then cast to the mob type
                              //Do other things
                    //Else assume it's a command
                              //Do even more things
          }
     
  13. Offline

    PENGUllN

    Hey again I resoleved my last issue now
    I have this
    Code:
    import java.util.ArrayList;
    import java.util.Arrays;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    
        public class WantedList implements Listener, CommandExecutor {
          
            public ArrayList<String> wanted = new ArrayList<String>();
          
            @EventHandler
            public void playerDeath(PlayerDeathEvent event) {
    
                Player deadPlayer = event.getEntity();
    
                Player killer = event.getEntity().getKiller();
              
                if (killer instanceof Player && deadPlayer instanceof Player)
                    wanted.add(event.getEntity().getKiller().getName()) ;
                  
                  
                }
                public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
                    if(label.equalsIgnoreCase("wanted")) ;
                    sender.sendMessage(Arrays.asList(wanted).toString());
                    return false;
            }
    
        }
        
    I'm Trying to get it to displ a list of players that are "wanted" I have this but my issue is
    after the player has died and I type the command /wanted
    I just get
    [​IMG]

    What Am I Doing Wrong ?
     
  14. Offline

    CraftCreeper6

    @PENGUllN
    You can just send the list you know, and by the looks of things, the list is empty.

    You can use sender#sendMessage(myList);
     
  15. Offline

    PENGUllN

    My List Is Empty ?
    How shouldn't
    Code:
    wanted.add(event.getEntity().getKiller().getName()) ;
    add the killers ?
     
  16. Offline

    CraftCreeper6

Thread Status:
Not open for further replies.

Share This Page