Solved Player Tracker

Discussion in 'Spigot Plugin Development' started by MangoCodes, Oct 12, 2017.

  1. I have this player tracker so when the player is holding a compass it will send an action bar message to the player saying "Nearest Player: NAME" and "Distance: 10 blocks" but when the player stops holding the compass it gets glitchy and doesn't remove the action bar message and continues..

    EDIT: Also if you were 12 blocks away from the player then got off of the compass and then you were 1 block away and go back on the compass it switches between 12 and 1

    Code:

    Code (Text):

    public class PlayerTracker implements Listener {

        Main plugin = Main.getPlugin();

        @EventHandler
        public void onPlayerItemHeld(PlayerItemHeldEvent e) {
            Player player = e.getPlayer();
            int i = e.getNewSlot();
            ItemStack item = e.getPlayer().getInventory().getItem(i);
            if (item != null && item.getItemMeta().getDisplayName() != null) {
                if (item.getType() == Material.COMPASS) {
                    if (item.getItemMeta().getDisplayName().contains("Player Tracker")) {

                        float distance = 100;
                        for (Entity entity : player.getNearbyEntities(distance, 250.0D, distance)) {
                            if (entity instanceof Player) {
                                Player target = (Player) entity;
                                player.setCompassTarget(target.getLocation());
                                int playerDistance = (int) player.getLocation().distance(target.getLocation());

                                Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
                                    @Override
                                    public void run() {

                                        if (target != null) {

                                            sendActionBar(player, plugin.color("&a&l[!] &fNearest Player: " + target.getName() + " &a&l[!] &fDistance: " + playerDistance + " blocks"));
                                        }else {
                                            return;
                                        }
                                    }
                                }, 0L, 20L);
                            }
                        }
                    }
                }
            }
        }

        public static void sendActionBar(Player p, String msg) {
            CraftPlayer cp = (CraftPlayer)p;
            IChatBaseComponent ibc = IChatBaseComponent.ChatSerializer.a("{\"text\": \""+ msg +"\"}");
            PacketPlayOutChat packet = new PacketPlayOutChat(ibc, (byte)2);
            cp.getHandle().playerConnection.sendPacket(packet);
        }
    }
     
     
  2. If (item.getType () != Material.COMPASS) this.cancel();
     
  3. There are possibly two issues here:
    • You start a repeating task and never cancel it, this causes the switching between 12 and 1
    • Does the action bar need some sort of "clear" packet?
     
    • Agree Agree x 1
  4. I am not sure on how to clear the packet but also when I do update it doesn't seem to update the player distance it just glitches

    It's kind of hard to describe if you'd like to test the code out be my guest
     
  5. To fix @Rsl1122 second point, i recommend you to use this code
    Code (Text):
        private static void sendTime(Player player, int ticks) {
            PacketPlayOutTitle p = new PacketPlayOutTitle(EnumTitleAction.TIMES, null, 20, ticks, 20);
            ((CraftPlayer)player).getHandle().playerConnection.sendPacket(p);
        }

        public static void sendActionBar(Player player, String message, int ticks) {
            IChatBaseComponent cbc = ChatSerializer.a("{\"text\": \"" + message + "\"}");
            PacketPlayOutChat ppoc = new PacketPlayOutChat(cbc, (byte) 2);
            ((CraftPlayer)player).getHandle().playerConnection.sendPacket(ppoc);
            sendTime(player, ticks);
        }
     
    • Like Like x 2
  6. That seems to fix it, only issue is to make it automatically update so when the player is holding the compass it updates who is nearest
     
  7. Ow, great :D
    Maybe you should try to insert your for loop into the BukkitRunnable like this
    Code (Text):
    if (item.getItemMeta().getDisplayName().contains("Player Tracker")) {

                        float distance = 100;


                        Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new BukkitRunnable() {
                            @Override
                            public void run() {
                                for (Entity entity : player.getNearbyEntities(distance, 250.0D, distance)) {
                                    if (entity instanceof Player) {
                                        Player target = (Player) entity;
                                        player.setCompassTarget(target.getLocation());
                                        int playerDistance = (int) player.getLocation().distance(target.getLocation());

                                        if(item.getType() != Material.COMPASS) {
                                            this.cancel();
                                        }

                                        if (target != null) {

                                            sendActionBar(player, plugin.color("&a&l[!] &fNearest Player: " + target.getName() + " &a&l[!] &fDistance: " + playerDistance + " blocks"));
                                        }else {
                                            return;
                                        }
                                    }
                                }
                            }
                        }, 0L, 20L);
                    }
     
  8. Doesn't seem to cancel it just keeps displaying the action bar
     
  9. To keep displaying the actionbar, set a huge number to the ticks integer of the sendActionBar function :)
     
  10. put an else statement before if (target != null)
     

Share This Page