1.15.2 HashMap Not working

Discussion in 'Spigot Plugin Development' started by Louietv, Mar 14, 2020.

  1. So I'm trying to make a /back command and I'm getting the players location before they die through an event and then I'm storing their location in a HashMap but when I'm trying to call the variable from the HashMap in the command class it gives me a red line. Any clue why this could be happening?
    Code (Text):

    package me.louie.Back;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import me.louie.louieessentials.Main;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;

    import java.util.HashMap;

    public class Back implements CommandExecutor, Listener{
        static Main plugin;
        public Back(Main instance) {
            plugin = instance;
        }
        public static HashMap<Player, Location> loc = new HashMap<>();
        @EventHandler
        public void onDeath(PlayerDeathEvent event){
            Player p = (Player) event.getEntity();
            loc.put(p, p.getLocation());
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("back")) {
                if (sender.hasPermission("back.use")) {
                    Player Player = (Player) sender;
                    if(args.length == 0){
                        if(loc.containsKey(Player.getName())){
                            Player t = loc.get(p);
                            Player.teleport(t);
                            loc.remove(p);
                        }
                    }
                }
                sender.sendMessage(ChatColor.DARK_RED + "I'm sorry but you do not have permssion to this command!");;
                return true;
            }
            return false;

            }

    }
     
  2. Because "p" doesn't exist, you have it declared as "Player". Does your ide not tell you this?
     
    • Agree Agree x 1
  3. You put the Player in the Map but in your command u check if the map contains the playernamer
     
  4. What does the error say
     
  5. Use the UUID as key.
     
    • Agree Agree x 1
  6. Strahan

    Benefactor

    1. There is no reason to make the plugin instance static. Remove that.
    2. There is no reason to make the HashMap static. Remove that.
    3. According to SOLID design principles, it should be declared Map<Player, Location> loc not HashMap<Player, Location> loc
    4. You really should have events and commands in their own classes, not glued together
    5. You should check if the sender is a Player before casting it thus in the command
    6. Obey Java naming conventions; do not name a variable with PascalCase. Most especially do not name a variable with the same name as an existing class.
    7. As mentioned prior, your Map key is a Player object yet you are attempting to look it up with a String name.
    8. You declare the variable Player to hold the player, yet reference "p" later
     
    • Winner Winner x 2
    • Agree Agree x 1
  7. Here is how to avoid another mistake you (will) probably make: When you register the command and the listener, you have to use the same instance.
     
    #7 patri9ck, Mar 14, 2020
    Last edited: Mar 15, 2020
    • Agree Agree x 1