Sending Messages Twice

Discussion in 'Plugin Development' started by Mrjuanblack, Jan 7, 2020.

  1. Offline

    Mrjuanblack

    Hello, I'm trying to make a compass that can track the nearest player based on a list in the config files, and I got that working, the problem is that every time I track someone i get the message of me tracking a player two times, and I don't know how to fix it :(
    I just got started on developling plugins so im kinda noob at this :/

    The compass code:
    Code:
    package me.juan.freezing;
    
    import java.util.List;
    
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class Compass implements Listener
    {
        private Main plugin;
       
        public Compass(Main plugin)
        {
            this.plugin = plugin;
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @EventHandler
        public void onPlayerRightClic(PlayerInteractEvent event)
        {
            Player p = event.getPlayer();
            List<String> fPlayersList = plugin.getConfig().getStringList("freezing");
            if(!(fPlayersList.contains(p.getName())))
            {
                if(p.getInventory().getItemInMainHand().getType() == Material.COMPASS)
                {
                    Player nPlayer = null;
                    Location pLoc = p.getLocation();
                    double dist = 0.0D;
                    double nDist = 0.0D;
                    for (Player t : p.getWorld().getPlayers())
                    {
                        if(fPlayersList.contains(t.getName()))
                        {
                            Location tLoc = t.getLocation();
                            dist = pLoc.distance(tLoc);
                            if(nDist == 0.0D || dist < nDist)
                            {
                                nDist = dist;
                                nPlayer = t;
                            }
                        }
                    }
                    if (nPlayer == null)
                    {
                        p.sendMessage("There are no trackable players online!");
                    }
                    else
                    {
                        p.setCompassTarget(nPlayer.getLocation());
                        p.sendMessage("You are now tracking " + nPlayer.getName());
                    }
                }
            }
        }
    }
    
    The Main code:
    Code:
    package me.juan.freezing;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;
    
    import me.juan.freezing.commands.FreezingCommand;
    
    
    
    public class Main extends JavaPlugin
    {
        @Override
        public void onEnable()
        {
            new FreezingCommand(this);
            new PlayerAttack(this);
            new Compass(this);
            Bukkit.getScheduler().runTaskTimer(this, new Runnable()
            {
    
                @Override
                public void run()
                {
                    freeze();
                }
            
            }, 0, 1L);
           
        }
       
        public void loadConfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
       
        public void freeze()
        {
            List<String> fPlayersList = getConfig().getStringList("freezing");
            if(!(fPlayersList.isEmpty()))
            {
                for (int i = 0; i < fPlayersList.size(); i++)
                {
                    Player p = Bukkit.getServer().getPlayer(fPlayersList.get(i));
                    if(!(p == null))
                    {
                        for(Player t : Bukkit.getServer().getOnlinePlayers())
                        {
                          String tName = t.getName();
                          if(!(fPlayersList.contains(tName)))
                          {
                              Location tLoc = t.getLocation();
                              if(getLookingAt(p, t))
                              {
                                  t.teleport(tLoc);
                              }
                          }
                        }
                    } 
                }
            }
        }
        public boolean getLookingAt(Player player, Player player1)
        {
          Location eye = player.getEyeLocation();
          Vector toEntity = player1.getEyeLocation().toVector().subtract(eye.toVector());
          double dot = toEntity.normalize().dot(eye.getDirection());
        
          return dot > 0.99D;
        }
    }
     
  2. Offline

    DerDonut

    Check if PlayerInteractEvent#getHand() is EquipmentSlot.HAND
     
    KarimAKL likes this.
  3. Offline

    c7dev

    Sometimes PlayerInteractEvent appears to fire twice, maybe from a long click. To solve this you can add a cooldown.
     

Share This Page