HashMap problem

Discussion in 'Plugin Development' started by ice374, Jul 7, 2013.

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

    ice374

    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!");
        }
    }
    
     
  2. Offline

    recon88

    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;
        }
     
  3. Offline

    soulofw0lf

    Code:java
    1. @EventHandler
    2. public boolean handlePlayer(TGPlayer p, String[] args)
    3. {
    4. if (TGPlayerListener.canreturn.containsKey(p)) {
    5. p.teleport(TGPlayerListener.canreturn.get(p));
    6. p.sendMessage(ChatColor.GREEN + "Successfully teleported you to your death point!");
    7. TGPlayerListener.canreturn.remove(p);
    8. } else {
    9. p.sendMessage(ChatColor.RED + "Unable to find the last time you died!");
    10. }
    11. return true;
    12. }
    13. }
    14.  
    15.  
    16.  
    17. should be
    18.  
    19. @EventHandler
    20. public boolean handlePlayer(Player p, String[] args)
    21. {
    22. if (TGPlayerListener.canreturn.containsKey(p)) {
    23. p.teleport(TGPlayerListener.canreturn.get(p));
    24. p.sendMessage(ChatColor.GREEN + "Successfully teleported you to your death point!");
    25. TGPlayerListener.canreturn.remove(p);
    26. } else {
    27. p.sendMessage(ChatColor.RED + "Unable to find the last time you died!");
    28. }
    29. return true;
    30. }
    31. }
     
  4. Offline

    xTrollxDudex

    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?
     
  5. Offline

    recon88

    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
     
  6. Offline

    xTrollxDudex

  7. Offline

    AmShaegar

    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.

     
Thread Status:
Not open for further replies.

Share This Page