Compass player tracker 1.16.5

Discussion in 'Plugin Development' started by Jeroen123TC, Apr 22, 2021.

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

    Jeroen123TC

    Have someone the code for a simple playertracker plugin that only track the player most nearby with a ny conpass, not any messages or something.

    if someone have this code for me would be awesome!!
     
  2. Offline

    davidclue

    I have this in one of my plugin's code for a hunter vs speedrunner game mode. It will use this method to switch tracking targets in the same world and send an error message if there are none in the world. If a tracked player uses a portal it will point to the last location of the player on the world (The portal).
    Code:
    @EventHandler(priority=EventPriority.HIGH)
        public void onPlayerUse(PlayerInteractEvent e) {
            if (p.getInventory().getItemInMainHand().equals(compass)) {
                if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) return;
                if (Utils.closestPlayerTo(p) == null) {
                    if (trackingTarget != null) p.sendMessage(Utils.chat("&cNo speedrunners found in current world! &7&oCurrently tracking &a"+trackingTarget.getName()+"'s &7&olast known location"));
                    else p.sendMessage(Utils.chat("&cNo locations found to track in world!"));
                    e.setCancelled(true);
                    return;
                }
                trackingTarget = Utils.closestPlayerTo(p);
                p.sendMessage(Utils.chat("&bCurrently tracking &a"+trackingTarget.getName()+"'s &blocation"));
                e.setCancelled(true);
                return;
                }
        }
    I use a custom item for the tracking compass so that's why I used
    Code:
    if (p.getInventory().getItemInMainHand().equals(compass)) {
    But if you want it to be with any compass you will need to modify the code a bit.

    This is the method used for
    Code:
    Utils.closestPlayerTo(p)
    Code:
    public static Player closestPlayerTo(Player player) {
            Player result = null;
            double lastDistance = Double.MAX_VALUE;
            for (UUID uuid : Game.speedrunners) {
                Player p = Bukkit.getPlayer(uuid);
                if (!(player.getWorld().equals(p.getWorld()))) continue;
                double distance = player.getLocation().distanceSquared(p.getLocation());
                if (distance < lastDistance) {
                    lastDistance = distance;
                    result = p;
                }
            }
            return result;
        }
    And finally every I have a repeating task scheduled every 20 ticks that updates the player's location to actively track them without needing to constantly right-click with the compass. This is optional if you want it to auto-track the player and only right-click with the compass to switch targets.
    Code:
    if (trackingTarget != null && p.getWorld().equals(trackingTarget.getWorld())) p.setCompassTarget(trackingTarget.getLocation());
    And at the top of the class I define player trackingTarget as null, you will need to optimize this to suit your needs because this will send messages to the player.
     
  3. Offline

    darthvader1925

    Code:
     if (p.getInventory().getItemInMainHand().equals(compass)) {
    
    Since your using a custom item, if you want to use a regular item just do

    p.getInventory.getItemInMainHand() == Material.COMPASS
     
Thread Status:
Not open for further replies.

Share This Page