From be83383fd5d09b899f07952b9976fba52a1ea19d Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 18 Jul 2011 03:42:21 +0200 Subject: Added option to hide player from /list /who ... login message. This is for other plugins, that want to hide a player. Use IEssentials.getUser(Player p).setHidden(boolean) --- .../src/com/earth2me/essentials/Essentials.java | 22 ++++++++++- Essentials/src/com/earth2me/essentials/User.java | 11 ++++++ .../earth2me/essentials/commands/Commandgod.java | 4 ++ .../earth2me/essentials/commands/Commandheal.java | 6 ++- .../earth2me/essentials/commands/Commandlist.java | 45 +++++++++++++++++++++- .../earth2me/essentials/commands/Commandmsg.java | 4 +- .../earth2me/essentials/commands/Commandpay.java | 4 ++ .../essentials/commands/Commandrealname.java | 4 ++ .../earth2me/essentials/commands/Commandwhois.java | 4 ++ .../essentials/commands/EssentialsCommand.java | 12 ++++-- 10 files changed, 107 insertions(+), 9 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 45a2aff60..2e2ea9786 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -320,15 +320,27 @@ public class Essentials extends JavaPlugin implements IEssentials m = m.replace("{IP}", user.getAddress().toString()); m = m.replace("{BALANCE}", Double.toString(user.getMoney())); m = m.replace("{MAILS}", Integer.toString(user.getMails().size())); + m = m.replace("{WORLD}", user.getLocation().getWorld().getName()); } - - m = m.replace("{ONLINE}", Integer.toString(getServer().getOnlinePlayers().length)); + int playerHidden = 0; + for (Player p : getServer().getOnlinePlayers()) + { + if (getUser(p).isHidden()) + { + playerHidden++; + } + } + m = m.replace("{ONLINE}", Integer.toString(getServer().getOnlinePlayers().length - playerHidden)); if (m.matches(".*\\{PLAYERLIST\\}.*")) { StringBuilder online = new StringBuilder(); for (Player p : getServer().getOnlinePlayers()) { + if (getUser(p).isHidden()) + { + continue; + } if (online.length() > 0) { online.append(", "); @@ -563,6 +575,12 @@ public class Essentials extends JavaPlugin implements IEssentials public User getOfflineUser(String name) { + // Don't create a new offline user, if we already have that user loaded. + User u = users.get(name.toLowerCase()); + if (u != null) + { + return u; + } File userFolder = new File(getDataFolder(), "userdata"); File userFile = new File(userFolder, Util.sanitizeFileName(name) + ".yml"); if (userFile.exists()) diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index 22d8a5705..ccbfff409 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -19,6 +19,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser private boolean teleportRequestHere; private final Teleport teleport; private long lastActivity; + private boolean hidden = false; User(Player base, IEssentials ess) { @@ -331,4 +332,14 @@ public class User extends UserData implements Comparable, IReplyTo, IUser this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : now); return now; } + + public boolean isHidden() + { + return hidden; + } + + public void setHidden(boolean hidden) + { + this.hidden = hidden; + } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandgod.java b/Essentials/src/com/earth2me/essentials/commands/Commandgod.java index b244c3f29..6bb9f1390 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandgod.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandgod.java @@ -43,6 +43,10 @@ public class Commandgod extends EssentialsCommand for (Player p : server.matchPlayer(name)) { User u = ess.getUser(p); + if (u.isHidden()) + { + continue; + } boolean enabled = u.toggleGodModeEnabled(); u.sendMessage(Util.format("godMode", (enabled ? Util.i18n("enabled") : Util.i18n("disabled")))); sender.sendMessage(Util.format("godMode",Util.format(enabled ? "godEnabledFor": "godDisabledFor", p.getDisplayName()))); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandheal.java b/Essentials/src/com/earth2me/essentials/commands/Commandheal.java index a68a357d7..effe64604 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandheal.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandheal.java @@ -53,13 +53,17 @@ public class Commandheal extends EssentialsCommand private void healOtherPlayers(Server server, CommandSender sender, String name) { List players = server.matchPlayer(name); - if(players.isEmpty()) + if (players.isEmpty()) { sender.sendMessage(Util.i18n("playerNotFound")); return; } for (Player p : players) { + if (ess.getUser(p).isHidden()) + { + continue; + } p.setHealth(20); sender.sendMessage(Util.format("healOther", p.getDisplayName())); } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandlist.java b/Essentials/src/com/earth2me/essentials/commands/Commandlist.java index b14136070..445db4597 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandlist.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandlist.java @@ -24,9 +24,33 @@ public class Commandlist extends EssentialsCommand @Override public void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception { + boolean showhidden = false; + if (sender instanceof Player) + { + if (ess.getUser(sender).isAuthorized("essentials.list.hidden")) + { + showhidden = true; + } + } + else + { + showhidden = true; + } + int playerHidden = 0; + for (Player p : server.getOnlinePlayers()) + { + if (ess.getUser(p).isHidden()) + { + playerHidden++; + } + } charge(sender); StringBuilder online = new StringBuilder(); - online.append(ChatColor.BLUE).append("There are ").append(ChatColor.RED).append(server.getOnlinePlayers().length); + online.append(ChatColor.BLUE).append("There are ").append(ChatColor.RED).append(server.getOnlinePlayers().length - playerHidden); + if (showhidden) + { + online.append(ChatColor.GRAY).append("/").append(playerHidden); + } online.append(ChatColor.BLUE).append(" out of a maximum ").append(ChatColor.RED).append(server.getMaxPlayers()); online.append(ChatColor.BLUE).append(" players online."); sender.sendMessage(online.toString()); @@ -37,6 +61,10 @@ public class Commandlist extends EssentialsCommand for (Player p : server.getOnlinePlayers()) { User u = ess.getUser(p); + if (u.isHidden() && !showhidden) + { + continue; + } String group = u.getGroup(); List list = sort.get(group); if (list == null) @@ -69,6 +97,10 @@ public class Commandlist extends EssentialsCommand { groupString.append("§7[AFK]§f"); } + if (user.isHidden()) + { + groupString.append("§7[HIDDEN]§f"); + } groupString.append(user.getDisplayName()); } sender.sendMessage(groupString.toString()); @@ -79,7 +111,12 @@ public class Commandlist extends EssentialsCommand List users = new ArrayList(); for (Player p : server.getOnlinePlayers()) { - users.add(ess.getUser(p)); + final User u = ess.getUser(p); + if (u.isHidden() && !showhidden) + { + continue; + } + users.add(u); } Collections.sort(users); @@ -100,6 +137,10 @@ public class Commandlist extends EssentialsCommand { onlineUsers.append("§7[AFK]§f"); } + if (user.isHidden()) + { + onlineUsers.append("§7[HIDDEN]§f"); + } onlineUsers.append(user.getDisplayName()); } sender.sendMessage(onlineUsers.toString()); diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandmsg.java b/Essentials/src/com/earth2me/essentials/commands/Commandmsg.java index fcedbde83..02b7a6c32 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandmsg.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandmsg.java @@ -5,6 +5,7 @@ import org.bukkit.Server; import org.bukkit.entity.Player; import com.earth2me.essentials.Console; import com.earth2me.essentials.IReplyTo; +import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import org.bukkit.command.CommandSender; @@ -52,7 +53,8 @@ public class Commandmsg extends EssentialsCommand for (Player p : matches) { sender.sendMessage(Util.format("msgFormat", translatedMe, p.getDisplayName(), message)); - if (sender instanceof Player && ess.getUser(p).isIgnoredPlayer(((Player)sender).getName())) + final User u = ess.getUser(p); + if (sender instanceof Player && (u.isIgnoredPlayer(((Player)sender).getName()) || u.isHidden())) { continue; } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandpay.java b/Essentials/src/com/earth2me/essentials/commands/Commandpay.java index e14ff699f..4f52eee58 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandpay.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandpay.java @@ -25,6 +25,10 @@ public class Commandpay extends EssentialsCommand for (Player p : server.matchPlayer(args[0])) { User u = ess.getUser(p); + if (u.isHidden()) + { + continue; + } user.payUser(u, amount); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandrealname.java b/Essentials/src/com/earth2me/essentials/commands/Commandrealname.java index ce327b204..e16d59198 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandrealname.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandrealname.java @@ -26,6 +26,10 @@ public class Commandrealname extends EssentialsCommand for (Player p : server.getOnlinePlayers()) { final User u = ess.getUser(p); + if (u.isHidden()) + { + continue; + } final String displayName = ChatColor.stripColor(u.getDisplayName()).toLowerCase(); if (!whois.equals(displayName) && !displayName.equals(ChatColor.stripColor(ess.getSettings().getNicknamePrefix()) + whois) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandwhois.java b/Essentials/src/com/earth2me/essentials/commands/Commandwhois.java index 1dcd59131..9b5d172f2 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandwhois.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandwhois.java @@ -28,6 +28,10 @@ public class Commandwhois extends EssentialsCommand for (Player p : server.getOnlinePlayers()) { User u = ess.getUser(p); + if (u.isHidden()) + { + continue; + } String dn = ChatColor.stripColor(u.getNick()); if (!whois.equalsIgnoreCase(dn) && !whois.equalsIgnoreCase(dn.substring(prefixLength)) diff --git a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java index ac19f7d74..5ea2bf8b0 100644 --- a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java +++ b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java @@ -53,12 +53,18 @@ public abstract class EssentialsCommand implements IEssentialsCommand for (Player p : matches) { - if (p.getDisplayName().startsWith(args[pos])) + final User u = ess.getUser(p); + if (u.getDisplayName().startsWith(args[pos]) && (getOffline || !u.isHidden())) { - return ess.getUser(p); + return u; } } - return ess.getUser(matches.get(0)); + final User u = ess.getUser(matches.get(0)); + if (!getOffline && u.isHidden()) + { + throw new NoSuchFieldException(Util.i18n("playerNotFound")); + } + return u; } @Override -- cgit v1.2.3