TotalFreedomMod/src/me/totalfreedom/totalfreedommod/commands/Command_radar.java

111 lines
3.1 KiB
Java
Raw Normal View History

package me.totalfreedom.totalfreedommod.commands;
2011-10-19 00:37:00 +00:00
import me.totalfreedom.totalfreedommod.permission.PlayerRank;
2011-10-19 00:37:00 +00:00
import java.util.ArrayList;
import java.util.Collections;
2013-08-03 20:08:16 +00:00
import java.util.Comparator;
2011-10-19 00:37:00 +00:00
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@CommandPermissions(level = PlayerRank.NON_OP, source = SourceType.ONLY_IN_GAME)
@CommandParameters(description = "Shows nearby people sorted by distance.", usage = "/<command> [range]")
public class Command_radar extends FreedomCommand
2011-10-19 00:37:00 +00:00
{
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
2011-10-19 16:04:44 +00:00
Location sender_pos = sender_p.getLocation();
2011-10-19 00:37:00 +00:00
List<TFM_RadarData> radar_data = new ArrayList<TFM_RadarData>();
2013-08-14 14:01:42 +00:00
for (Player player : sender_pos.getWorld().getPlayers())
2011-10-19 00:37:00 +00:00
{
2013-08-14 14:01:42 +00:00
if (!player.equals(sender_p))
2011-10-19 00:37:00 +00:00
{
2011-10-28 00:37:30 +00:00
try
{
2013-08-14 14:01:42 +00:00
radar_data.add(new TFM_RadarData(player, sender_pos.distance(player.getLocation()), player.getLocation()));
2011-10-28 00:37:30 +00:00
}
catch (IllegalArgumentException ex)
2011-10-28 00:37:30 +00:00
{
}
2011-10-19 00:37:00 +00:00
}
}
2011-10-19 16:04:44 +00:00
if (radar_data.isEmpty())
{
playerMsg("You are the only player in this world. (" + ChatColor.GREEN + "Forever alone..." + ChatColor.YELLOW + ")", ChatColor.YELLOW); //lol
2011-10-19 16:04:44 +00:00
return true;
}
2011-10-19 00:37:00 +00:00
Collections.sort(radar_data, new TFM_RadarData());
playerMsg("People nearby in " + sender_pos.getWorld().getName() + ":", ChatColor.YELLOW);
2011-10-19 00:37:00 +00:00
int countmax = 5;
if (args.length == 1)
{
try
{
countmax = Math.max(1, Math.min(64, Integer.parseInt(args[0])));
2011-10-19 00:37:00 +00:00
}
catch (NumberFormatException nfex)
{
}
}
2011-10-19 00:37:00 +00:00
for (TFM_RadarData i : radar_data)
{
playerMsg(String.format("%s - %d",
2011-10-19 00:37:00 +00:00
i.player.getName(),
Math.round(i.distance)), ChatColor.YELLOW);
2011-10-19 16:04:44 +00:00
if (--countmax <= 0)
{
break;
}
2011-10-19 00:37:00 +00:00
}
return true;
}
2013-08-03 20:08:16 +00:00
private class TFM_RadarData implements Comparator<TFM_RadarData>
{
public Player player;
public double distance;
public Location location;
public TFM_RadarData(Player player, double distance, Location location)
{
this.player = player;
this.distance = distance;
this.location = location;
}
public TFM_RadarData()
{
}
@Override
public int compare(TFM_RadarData t1, TFM_RadarData t2)
{
if (t1.distance > t2.distance)
{
return 1;
}
else if (t1.distance < t2.distance)
{
return -1;
}
else
{
return 0;
}
}
}
2011-10-19 00:37:00 +00:00
}