Solved Errors in my command

Discussion in 'Spigot Plugin Development' started by Artourious, Jan 22, 2020.

  1. Im just trying to make a compass that tracks my friend. Im getting a few errors and I do not know how to fix them. I know it mostly has something to do with the public class Listener part but like I said I don't know how to fix it.

    Code:
    Code (Java):
    package me.artourious.compasslocator.commands;

    import java.util.HashMap;
    import java.util.Map.Entry;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerQuitEvent;

    import me.artourious.compasslocator.Main;

    public class Listener implements Listener{
        private  Main plugin;
        private HashMap<Player,Player> compassTargets;
        public Listener(Main instance){
            plugin = instance;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            compassTargets = new HashMap<Player,Player>();
        }
        @EventHandler
        public void onLeave(PlayerQuitEvent e){
            for (Entry<Player,Player> entry: compassTargets.entrySet()){
                if (entry.getKey() == e.getPlayer()){
                    compassTargets.remove(entry.getKey());
                }
                if (entry.getValue() == e.getPlayer()){
                    entry.getKey().sendMessage(ChatColor.RED+"Your target has left the game, your compass is no longer tracking anyone.");
                    compassTargets.remove(entry.getKey());
                }
            }
        }
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            Player ply = e.getPlayer();
            Player targ = null;
            if (( (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) && ply.getItemInHand().getType().equals(Material.COMPASS))){
           
                if (!ply.hasPermission("comptrack.track")) return;
           
                // Find the closest player to this player
           
                for (Player player: Bukkit.getOnlinePlayers()){
                    if (ply.getWorld() != player.getWorld()) continue;
                    if (ply == player) continue;
                    if ( (targ == null) || ply.getLocation().distance(player.getLocation()) < targ.getLocation().distance(ply.getLocation())){
                        if (player.getLocation().distance(ply.getLocation()) <= 30) continue;
                        targ = player;
                    }
                }
           
                if (targ == null){
                    ply.sendMessage(ChatColor.RED+"Couldn't find a target!");
                    return;
                }
           
                // Add set the compass location of this player, if a target was found
           
                ply.sendMessage(ChatColor.GRAY+"Targeting "+ChatColor.GREEN+targ.getName()+"!");
                compassTargets.put(ply,targ);
            }
            else if ( (e.getAction().equals(Action.LEFT_CLICK_AIR) || e.getAction().equals(Action.LEFT_CLICK_BLOCK)) && ply.getItemInHand().getType().equals(Material.COMPASS) ){
           
                if (!ply.hasPermission("comptrack.dist")) return;
           
                if (compassTargets.get(ply) != null){
                    ply.sendMessage(ChatColor.GREEN+"Your target is "+ply.getLocation().distance(compassTargets.get(ply).getLocation())+" blocks away.");
                }
                else {
                    ply.sendMessage(ChatColor.RED+"Your compass isn't tracking anyone!");
                }
            }
       
        }
        @EventHandler
        public void onMove(PlayerMoveEvent e){
       
            if (compassTargets.get(e.getPlayer()) != null){
                e.getPlayer().setCompassTarget(compassTargets.get(e.getPlayer()).getLocation());
            }
       
            for (Player ply: Bukkit.getOnlinePlayers()){
           
                Player compassTarget = compassTargets.get(ply);
           
                if (compassTarget == e.getPlayer()){
                    if (ply.getWorld() == e.getPlayer().getWorld()){
                        if (ply.getLocation().distance(e.getPlayer().getLocation()) <= 30){
                            ply.sendMessage(ChatColor.GREEN+"Your target is close.");
                            compassTargets.remove(ply);
                        }
                        ply.setCompassTarget(e.getPlayer().getLocation());
                    }
                    else {
                        ply.sendMessage(ChatColor.RED+"Your target has changed world, and is no longer available.");
                        compassTargets.remove(ply);
                    }
                }
            }
       
        }
    }

    public class CPCommand {

    }
     

    Picture
     

    Attached Files:

  2. It will literally tell you the reason when you hover over the red X.

    Also just change the name of your class to something else.
     
  3. Suggestions:
    1. Don't name your class Listener as you could easily confuse it with the Spigot's Listener class. Name it something like "PlayerQuitListener".
    2. You should register the listener in the Main class (onEnable() method) by instantiating your PlayerQuitListener class and not in the listener itself.

    it should be:
    Code (Java):
    plugin.getServer().getPluginManager().registerEvents(new PlayerQuitListener(this), this);