I am just getting the "Unable to find the last time you died!" message when i type /return can someone explain to me what i am doing wrong? i suspect the player isnt being added to the hashmap properly ReturnCommand: Code: public class ReturnCommand extends AbstractCommand implements Listener { public ReturnCommand(TG tg) { super(tg, "return"); tg.getServer().getPluginManager().registerEvents(this, tg); } @EventHandler public boolean handlePlayer(TGPlayer p, String[] args) { if (TGPlayerListener.canreturn.containsKey(p)) { p.teleport(TGPlayerListener.canreturn.get(p)); p.sendMessage(ChatColor.GREEN + "Successfully teleported you to your death point!"); TGPlayerListener.canreturn.remove(p); } else { p.sendMessage(ChatColor.RED + "Unable to find the last time you died!"); } return true; } } TGPlayerListener: Code: public class TGPlayerListener implements Listener { public static HashMap<Player, Location> canreturn = new HashMap<Player, Location>(); private TG plugin; public TGPlayerListener(TG instance) { this.plugin = instance; } @EventHandler public void onDeath(PlayerDeathEvent event){ Player p = event.getEntity(); canreturn.put(p, p.getLocation()); } @EventHandler public void onPlayerRespawn(PlayerRespawnEvent event){ Player p = event.getPlayer(); p.sendMessage(ChatColor.GREEN + "You may Type /return to return to your death point!"); } }
I don't know what are you doing there but try using this in your main class. Code: public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) { if (commandLabel.equalsIgnoreCase("return")) { // do stuff here } return true; }
Code:java @EventHandler public boolean handlePlayer(TGPlayer p, String[] args) { if (TGPlayerListener.canreturn.containsKey(p)) { p.teleport(TGPlayerListener.canreturn.get(p)); p.sendMessage(ChatColor.GREEN + "Successfully teleported you to your death point!"); TGPlayerListener.canreturn.remove(p); } else { p.sendMessage(ChatColor.RED + "Unable to find the last time you died!"); } return true; } } should be @EventHandler public boolean handlePlayer(Player p, String[] args) { if (TGPlayerListener.canreturn.containsKey(p)) { p.teleport(TGPlayerListener.canreturn.get(p)); p.sendMessage(ChatColor.GREEN + "Successfully teleported you to your death point!"); TGPlayerListener.canreturn.remove(p); } else { p.sendMessage(ChatColor.RED + "Unable to find the last time you died!"); } return true; } }
ice374 On death uses Player player = event.getPlayer(); because it is PlayerDeathEvent, you know for sure it's a player that died. Also if you want to use that don't you need a (Player) cast?
The TGPlayerListener is totally fine. There is no getPlayer() for the PlayerDeathEvent. getEntity() returns the player and there is no cast needed. See here: http://jd.bukkit.org/rb/apidocs/org/bukkit/event/entity/PlayerDeathEvent.html
Code: Player p = event.getEntity(); canreturn.put(p, p.getLocation()); Code: public boolean handlePlayer(TGPlayer p, String[] args) { if (TGPlayerListener.canreturn.containsKey(p)) { You are putting in a Player and are checking for a TGPlayer. That can't work. Also, do not saver Player reference but player.getName() to refer to players in HashMaps.