From c7427f492f3e0484d01a1c08b110c8203970a4d0 Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Sun, 25 Sep 2011 02:17:32 -0400 Subject: [PATCH] Added radar command. Signed-off-by: Steven Lawson --- .../TotalFreedomMod/RadarData.java | 28 +++++++++ .../TotalFreedomMod/TotalFreedomMod.java | 63 ++++++++++++++++--- src/plugin.yml | 3 + 3 files changed, 86 insertions(+), 8 deletions(-) create mode 100644 src/me/StevenLawson/TotalFreedomMod/RadarData.java diff --git a/src/me/StevenLawson/TotalFreedomMod/RadarData.java b/src/me/StevenLawson/TotalFreedomMod/RadarData.java new file mode 100644 index 0000000..007371d --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/RadarData.java @@ -0,0 +1,28 @@ +package me.StevenLawson.TotalFreedomMod; + +import java.util.Comparator; +import org.bukkit.entity.Player; + +public class RadarData implements Comparator +{ + Player player; + double distance; + + public RadarData(Player inplayer, double indistance) + { + this.player = inplayer; + this.distance = indistance; + } + + public RadarData() + { + } + + @Override + public int compare(RadarData t1, RadarData t2) + { + if (t1.distance > t2.distance) return 1; + else if (t1.distance < t2.distance) return -1; + else return 0; + } +} diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index 0fb136d..de48096 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -2,12 +2,14 @@ package me.StevenLawson.TotalFreedomMod; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; +import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -185,7 +187,7 @@ public class TotalFreedomMod extends JavaPlugin if (isUserSuperadmin(sender.getName()) || player == null) { tfBroadcastMessage(String.format("(%s: De-opping everyone)", sender.getName()), ChatColor.GRAY); - + for (Player p : Bukkit.getOnlinePlayers()) { if (!isUserSuperadmin(p.getName()) && !p.getName().equals(sender.getName())) @@ -207,7 +209,7 @@ public class TotalFreedomMod extends JavaPlugin if (isUserSuperadmin(sender.getName()) || player == null) { tfBroadcastMessage(String.format("(%s: Opping everyone)", sender.getName()), ChatColor.GRAY); - + boolean doSetGamemode = false; GameMode targetGamemode = GameMode.CREATIVE; if (args.length != 0) @@ -228,7 +230,7 @@ public class TotalFreedomMod extends JavaPlugin { p.setOp(true); p.sendMessage(YOU_ARE_OP); - + if (doSetGamemode) { p.setGameMode(targetGamemode); @@ -255,7 +257,7 @@ public class TotalFreedomMod extends JavaPlugin for (Player p : Bukkit.matchPlayer(args[0])) { matched_player = true; - + tfBroadcastMessage(String.format("(%s: Opping %s)", sender.getName(), p.getName()), ChatColor.GRAY); p.setOp(true); p.sendMessage(YOU_ARE_OP); @@ -285,7 +287,7 @@ public class TotalFreedomMod extends JavaPlugin for (Player p : Bukkit.matchPlayer(args[0])) { matched_player = true; - + tfBroadcastMessage(String.format("(%s: De-opping %s)", sender.getName(), p.getName()), ChatColor.GRAY); p.setOp(false); p.sendMessage(YOU_ARE_NOT_OP); @@ -480,7 +482,7 @@ public class TotalFreedomMod extends JavaPlugin if (player == null || isUserSuperadmin(sender.getName())) { tfBroadcastMessage("Server is going offline.", ChatColor.GRAY); - + for (Player p : Bukkit.getOnlinePlayers()) { p.kickPlayer("Server is going offline, come back in a few minutes."); @@ -576,6 +578,51 @@ public class TotalFreedomMod extends JavaPlugin return true; } + else if (cmd.getName().equalsIgnoreCase("radar")) + { + if (player == null) + { + sender.sendMessage("This command can only be used in-game."); + return true; + } + + Player sender_player = Bukkit.getPlayerExact(sender.getName()); + Location sender_pos = sender_player.getLocation(); + String sender_world = sender_player.getWorld().getName(); + + List radar_data = new ArrayList(); + + for (Player p : Bukkit.getOnlinePlayers()) + { + if (sender_world.equals(p.getWorld().getName())) + { + radar_data.add(new RadarData(p, sender_pos.distance(p.getLocation()))); + } + } + + Collections.sort(radar_data, new RadarData()); + + sender.sendMessage(ChatColor.YELLOW + "People nearby in " + sender_world + ":"); + + int countmax = 5; + if (args.length == 1) + { + countmax = Integer.parseInt(args[0]); + } + + int count = 0; + for (RadarData i : radar_data) + { + if (count++ > countmax) + { + break; + } + + sender.sendMessage(ChatColor.YELLOW + String.format("%s - %d", i.player.getName(), Math.round(i.distance))); + } + + return true; + } return false; } @@ -583,13 +630,13 @@ public class TotalFreedomMod extends JavaPlugin public static void tfBroadcastMessage(String message, ChatColor color) { log.info(message); - + for (Player p : Bukkit.getOnlinePlayers()) { p.sendMessage(color + message); } } - + private static String implodeStringList(String glue, List pieces) { StringBuilder output = new StringBuilder(); diff --git a/src/plugin.yml b/src/plugin.yml index a2af082..45fef6b 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -40,6 +40,9 @@ commands: qop: description: Quick Op - op someone based on a partial name. usage: / + radar: + description: Shows nearby people sorted by distance. + usage: / say: description: Broadcasts the given message as the console, includes sender. usage: /