Compass Tracking not working?!

Discussion in 'Plugin Development' started by WingedMLGPro, May 24, 2015.

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


    Hey all!
    Im trying to make it so when you hold a compass it sets the compasses displayname to the nearest player and the distance,
    I did a test with it, its not setting the DisplayName but it does send the message "test" (which i made it to)
    Here is my code:
    package me.benkea.Compass;
    import me.benkea.HubMain;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import java.util.List;
    * © This Document and Code is STRICTLY copyrited(©) to Ben.
    * © If anyone takes any part of this code and uses it for
    * © Something public, Share with someone, uses it for API's,
    * © implementing it to their code and taking the rights for
    * © it is NOT allowed unless you get permission from me!
    * © This project SwiftyHub was created by 35047
    * © at 24/05/15 at 4:53 PM
    public class Commpass implements Listener {
        public void setCompassTarget(final Player p){
            Bukkit.getScheduler().scheduleSyncRepeatingTask(HubMain.getInstance(), new Runnable() {
                public void run() {
                    List<Entity> near = p.getNearbyEntities(1000000000D, 1000000000D, 1000000000D);
                    for(Entity entity : near) {
                        if(entity instanceof Player) {
                            Player nearest = (Player) entity;
                            if (nearest == null){
                                p.getItemInHand().getItemMeta().setDisplayName("§6NearestPlayer§7: §aNone §6Distance§7: §aNone");
                            else {
                                p.getItemInHand().getItemMeta().setDisplayName("§6NearestPlayer§7: §a"+nearest.getName()+"§6Distance§7: §a"+p.getLocation().distance(nearest.getLocation()));
            }, 20, 300);
        public void onMove(PlayerMoveEvent e){
            Player p = e.getPlayer();
            if (p.getItemInHand().getType().equals(Material.COMPASS)) {
    If you know why this isnt working any help will be appreciated!

  2. Offline


    List<Entity> near = p.getNearbyEntities(1000000000D, 1000000000D, 1000000000D);
    Mhm, this is the best way to cause yourself a headacke... 10milion blocks radius :3 imagine how many entities are there also consider that this fires every time a player moves, that will cause lag.

    Do not spawn tasks in that rate! as you may have seen move event goes off so many times, spawning so many tasks litureally consumes a ton of resources.

    I dont think nearest will ever be null...

    The thing is that you set the display name in the item meta but you dont set the item meta back to the itemstack.
  3. Online

    timtower Moderator Moderator

    @mine-care It will be null when you are the only one around.
    @WingedMLGPro Why not loop through the players and check their distance instead?
  4. Offline


    @mine-care but im checking if the entity is a player, but maybe i could shorten it i guess
    @timtower how would i do that?
  5. Online

    timtower Moderator Moderator

    for(player p : playerlist){
    location = p.getlocation();
    distance = location.getdistancesquared(originalplayer.location())
  6. Offline


    @timtower wouldnt that get all the players location?
    If it does how would i get the closest using that
  7. Online

    timtower Moderator Moderator

    @WingedMLGPro if(distanceofcurrentplayer < stored distance){
    stored distance = distanceofcurrentplayer ;
    stored player = current player;
  8. Offline


    @timtower Will it? the if (entity instanceof Player) (Where entity is null) will return false, therefore there is no point on checking if the entity that has been cast to a player already, is null.

    @WingedMLGPro Have you done this fix?

    EDIT by Timtower: merged posts
  9. Online

    timtower Moderator Moderator

  10. Offline


  11. Offline


    Use a runnable. Update the compass only every 1 to 2 seconds, not everytime a player moves their head a little bit (which can be a reason for firing PlayerMoveEvent). Then store the closest one.

    Also, loop through all players online instead of all entities (./getServer().getOnlinePlayers)

    This will save memory due to the fact that you're only getting the players, and you're only doing that only after one/ two seconds.
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page