From c4f4036e1d6c211b3f531005b925cdfae59871f9 Mon Sep 17 00:00:00 2001 From: drtshock Date: Thu, 13 Nov 2014 13:49:13 -0600 Subject: [PATCH] Add tooltips for f show. Refactor tooltip methods to be shared across command classes. --- .../java/com/massivecraft/factions/P.java | 17 +++ .../massivecraft/factions/cmd/CmdList.java | 17 --- .../massivecraft/factions/cmd/CmdShow.java | 134 +++--------------- .../factions/integration/Econ.java | 4 + .../massivecraft/factions/zcore/MCommand.java | 42 +++++- src/main/resources/config.yml | 24 +++- 6 files changed, 103 insertions(+), 135 deletions(-) diff --git a/src/main/java/com/massivecraft/factions/P.java b/src/main/java/com/massivecraft/factions/P.java index 7a4ab8e1..8e643fa3 100644 --- a/src/main/java/com/massivecraft/factions/P.java +++ b/src/main/java/com/massivecraft/factions/P.java @@ -12,10 +12,14 @@ import com.massivecraft.factions.struct.ChatMode; import com.massivecraft.factions.util.*; import com.massivecraft.factions.zcore.MPlugin; import com.massivecraft.factions.zcore.util.TextUtil; +import net.milkbowl.vault.permission.Permission; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.plugin.RegisteredServiceProvider; import java.lang.reflect.Modifier; import java.lang.reflect.Type; @@ -42,6 +46,8 @@ public class P extends MPlugin { // Persistence related private boolean locked = false; + public static Permission perms = null; + public boolean getLocked() { return this.locked; } @@ -112,6 +118,7 @@ public class P extends MPlugin { getServer().getPluginManager().registerEvents(blockListener, this); saveDefaultConfig(); + setupPermissions(); // since some other plugins execute commands directly through this command interface, provide it this.getCommand(this.refCommand).setExecutor(this); @@ -120,6 +127,12 @@ public class P extends MPlugin { this.loadSuccessful = true; } + private boolean setupPermissions() { + RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Permission.class); + perms = rsp.getProvider(); + return perms != null; + } + @Override public GsonBuilder getGsonBuilder() { Type mapFLocToStringSetType = new TypeToken>>() { @@ -306,6 +319,10 @@ public class P extends MPlugin { return players; } + public String getPrimaryGroup(OfflinePlayer player) { + return perms == null ? " " : perms.getPrimaryGroup(Bukkit.getWorlds().get(0).toString(), player); + } + public void debug(Level level, String s) { if (getConfig().getBoolean("debug", false)) { getLogger().log(level, s); diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdList.java b/src/main/java/com/massivecraft/factions/cmd/CmdList.java index 05efa434..82f2f46c 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdList.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdList.java @@ -1,7 +1,6 @@ package com.massivecraft.factions.cmd; import com.massivecraft.factions.Conf; -import com.massivecraft.factions.FPlayer; import com.massivecraft.factions.Faction; import com.massivecraft.factions.Factions; import com.massivecraft.factions.struct.Permission; @@ -11,7 +10,6 @@ import org.bukkit.ChatColor; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import java.util.List; public class CmdList extends FCommand { @@ -111,19 +109,4 @@ public class CmdList extends FCommand { sendFancyMessage(lines); } - - private List getToolTips(Faction faction) { - List lines = new ArrayList(); - for (String s : p.getConfig().getStringList("tooltips.list")) { - lines.add(ChatColor.translateAlternateColorCodes('&',replaceFInfoTags(s, faction))); - } - return lines; - } - - private String replaceFInfoTags(String s, Faction faction) { - boolean raidable = faction.getLandRounded() > faction.getPower(); - FPlayer fLeader = faction.getFPlayerAdmin(); - String leader = fLeader == null ? "Server" : fLeader.getName().substring(0, fLeader.getName().length() > 14 ? 13 : fLeader.getName().length()); - return s.replace("{power}", String.valueOf(faction.getPowerRounded())).replace("{maxPower}", String.valueOf(faction.getPowerMaxRounded())).replace("{leader}", leader).replace("{chunks}", String.valueOf(faction.getLandRounded())).replace("{raidable}", String.valueOf(raidable)).replace("{warps}", String.valueOf(faction.getWarps().size())); - } } diff --git a/src/main/java/com/massivecraft/factions/cmd/CmdShow.java b/src/main/java/com/massivecraft/factions/cmd/CmdShow.java index 5c6655ae..86b6defc 100644 --- a/src/main/java/com/massivecraft/factions/cmd/CmdShow.java +++ b/src/main/java/com/massivecraft/factions/cmd/CmdShow.java @@ -8,6 +8,8 @@ import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.struct.Permission; import com.massivecraft.factions.struct.Relation; import com.massivecraft.factions.struct.Role; +import mkremins.fanciful.FancyMessage; +import org.bukkit.ChatColor; import java.util.Collection; @@ -85,131 +87,39 @@ public class CmdShow extends FCommand { } } - String listpart; - - // List relation - String allyList = p.txt.parse("Allies: "); - String enemyList = p.txt.parse("Enemies: "); + FancyMessage allies = new FancyMessage("Allies: ").color(ChatColor.GOLD); + FancyMessage enemies = new FancyMessage("Enemies: ").color(ChatColor.GOLD); for (Faction otherFaction : Factions.getInstance().getAllFactions()) { if (otherFaction == faction) { continue; } Relation rel = otherFaction.getRelationTo(faction); - if (!rel.isAlly() && !rel.isEnemy()) { - continue; // if not ally or enemy, drop out now so we're not wasting time on it; good performance boost - } - - listpart = otherFaction.getTag(fme) + p.txt.parse("") + ", "; + String s = otherFaction.getTag(fme); if (rel.isAlly()) { - allyList += listpart; + allies.then(s).tooltip(getToolTips(otherFaction)); } else if (rel.isEnemy()) { - enemyList += listpart; - } - } - if (allyList.endsWith(", ")) { - allyList = allyList.substring(0, allyList.length() - 2); - } - if (enemyList.endsWith(", ")) { - enemyList = enemyList.substring(0, enemyList.length() - 2); - } - - if (allyList.length() > 2048) { - String[] lines = splitString(allyList, 2048, 256000); - for (int i = 0; i < lines.length; i++) { - sendMessage(lines[i]); - } - } else { - sendMessage(allyList); - } - if (enemyList.length() > 2048) { - String[] lines = splitString(enemyList, 2048, 256000); - for (int i = 0; i < lines.length; i++) { - sendMessage(lines[i]); - } - } else { - sendMessage(enemyList); - } - - // List the members... - String onlineList = p.txt.parse("") + "Members online: "; - String offlineList = p.txt.parse("") + "Members offline: "; - boolean canSeePower = Permission.POWER_ANY.has(me); - for (FPlayer follower : admins) { - listpart = follower.getNameAndTitle(fme); - if (canSeePower) { - listpart += p.txt.parse("(%d), ", follower.getPowerRounded()); - } else { - listpart += p.txt.parse(", "); - } - if (follower.isOnlineAndVisibleTo(me)) { - onlineList += listpart; - } else { - offlineList += listpart; - } - } - for (FPlayer follower : mods) { - listpart = follower.getNameAndTitle(fme); - if (canSeePower) { - listpart += p.txt.parse("(%d), ", follower.getPowerRounded()); - } else { - listpart += p.txt.parse(", "); - } - if (follower.isOnlineAndVisibleTo(me)) { - onlineList += listpart; - } else { - offlineList += listpart; - } - } - for (FPlayer follower : normals) { - listpart = follower.getNameAndTitle(fme); - if (canSeePower) { - listpart += p.txt.parse("(%d), ", follower.getPowerRounded()); - } else { - listpart += p.txt.parse(", "); - } - if (follower.isOnlineAndVisibleTo(me)) { - onlineList += listpart; - } else { - offlineList += listpart; + enemies.then(s).tooltip(getToolTips(otherFaction)); } } - if (onlineList.endsWith(", ")) { - onlineList = onlineList.substring(0, onlineList.length() - 2); - } - if (offlineList.endsWith(", ")) { - offlineList = offlineList.substring(0, offlineList.length() - 2); + + FancyMessage online = new FancyMessage("Members online: ").color(ChatColor.GOLD); + FancyMessage offline = new FancyMessage("Members offline: ").color(ChatColor.GOLD); + for (FPlayer p : faction.getFPlayers()) { + String name = p.getNameAndTitle(); + if (p.isOnline()) { + online.then(name).tooltip(getToolTips(p)); + } else { + offline.then(name).tooltip(getToolTips(p)); + } } - if (onlineList.length() > 2048) { - String[] lines = splitString(onlineList, 2048, 256000); - for (int i = 0; i < lines.length; i++) { - sendMessage(lines[i]); - } - } else { - sendMessage(onlineList); - } - if (offlineList.length() > 2048) { - String[] lines = splitString(offlineList, 2048, 256000); - for (int i = 0; i < lines.length; i++) { - sendMessage(lines[i]); - } - } else { - sendMessage(offlineList); - } - } - - private String[] splitString(String text, int chunkSize, int maxLength) { - char[] data = text.toCharArray(); - int len = Math.min(data.length, maxLength); - String[] result = new String[(len + chunkSize - 1) / chunkSize]; - int linha = 0; - for (int i = 0; i < len; i += chunkSize) { - result[linha] = new String(data, i, Math.min(chunkSize, len - i)); - linha++; - } - return result; + // Send all at once ;D + sendFancyMessage(allies); + sendFancyMessage(enemies); + sendFancyMessage(online); + sendFancyMessage(offline); } } diff --git a/src/main/java/com/massivecraft/factions/integration/Econ.java b/src/main/java/com/massivecraft/factions/integration/Econ.java index 383ba034..060dd9a8 100644 --- a/src/main/java/com/massivecraft/factions/integration/Econ.java +++ b/src/main/java/com/massivecraft/factions/integration/Econ.java @@ -379,6 +379,10 @@ public class Econ { return format.format(econ.getBalance(Bukkit.getOfflinePlayer(uuid))); } + public static String getFriendlyBalance(FPlayer player) { + return format.format(econ.getBalance(Bukkit.getOfflinePlayer(player.getName()))); + } + public static boolean setBalance(String account, double amount) { double current = econ.getBalance(Bukkit.getOfflinePlayer(account)); if (current > amount) { diff --git a/src/main/java/com/massivecraft/factions/zcore/MCommand.java b/src/main/java/com/massivecraft/factions/zcore/MCommand.java index d04ba048..cb10dc1b 100644 --- a/src/main/java/com/massivecraft/factions/zcore/MCommand.java +++ b/src/main/java/com/massivecraft/factions/zcore/MCommand.java @@ -1,8 +1,14 @@ package com.massivecraft.factions.zcore; +import com.massivecraft.factions.FPlayer; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.P; +import com.massivecraft.factions.integration.Econ; import com.massivecraft.factions.zcore.util.TextUtil; import mkremins.fanciful.FancyMessage; +import org.apache.commons.lang.time.DurationFormatUtils; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -263,11 +269,45 @@ public abstract class MCommand { } public void sendFancyMessage(List messages) { - for(FancyMessage m : messages) { + for (FancyMessage m : messages) { sendFancyMessage(m); } } + public List getToolTips(FPlayer player) { + List lines = new ArrayList(); + for (String s : p.getConfig().getStringList("tooltips.show")) { + lines.add(ChatColor.translateAlternateColorCodes('&', replaceFPlayerTags(s, player))); + } + return lines; + } + + public List getToolTips(Faction faction) { + List lines = new ArrayList(); + for (String s : p.getConfig().getStringList("tooltips.list")) { + lines.add(ChatColor.translateAlternateColorCodes('&', replaceFactionTags(s, faction))); + } + return lines; + } + + public String replaceFPlayerTags(String s, FPlayer player) { + String humanized = DurationFormatUtils.formatDurationWords(System.currentTimeMillis() - player.getLastLoginTime(), true, true) + " ago"; + String lastSeen = player.isOnline() ? ChatColor.GREEN + "Online" : (System.currentTimeMillis() - player.getLastLoginTime() < 432000000 ? ChatColor.YELLOW + humanized : ChatColor.RED + humanized); + String balance = Econ.isSetup() ? Econ.getFriendlyBalance(player) : "no balance"; + String power = player.getPowerRounded() + "/" + player.getPowerMaxRounded(); + String group = P.p.getPrimaryGroup(Bukkit.getOfflinePlayer(player.getName())); + return s.replace("{balance}", balance).replace("{lastSeen}", lastSeen).replace("{power}", power).replace("{group}", group); + } + + public String replaceFactionTags(String s, Faction faction) { + boolean raidable = faction.getLandRounded() > faction.getPower(); + FPlayer fLeader = faction.getFPlayerAdmin(); + String online = String.valueOf(faction.getFPlayersWhereOnline(true).size()); + String members = String.valueOf(faction.getFPlayers().size()); + String leader = fLeader == null ? "Server" : fLeader.getName().substring(0, fLeader.getName().length() > 14 ? 13 : fLeader.getName().length()); + return s.replace("{power}", String.valueOf(faction.getPowerRounded())).replace("{maxPower}", String.valueOf(faction.getPowerMaxRounded())).replace("{leader}", leader).replace("{chunks}", String.valueOf(faction.getLandRounded())).replace("{raidable}", String.valueOf(raidable)).replace("{warps}", String.valueOf(faction.getWarps().size())).replace("{online}", online).replace("{members}", members); + } + // -------------------------------------------- // // Argument Readers // -------------------------------------------- // diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5f9011a3..e01bc011 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -42,15 +42,29 @@ disable-pistons-in-territory: false # This section is to configure tooltips for things like /f list tooltips: + # List # This shows up when someone does /f list for the top factions. # It will not sure up for factionless of course, just actual factions. # You can use color codes here. list: - - "Leader: {leader}" - - "Claimed: {chunks}" - - "Raidable: {raidable}" - - "Warps: {warps}" - - "Power: {power}/{maxPower}" + - "&6Leader: &f{leader}" + - "&6Claimed: &f{chunks}" + - "&6Raidable: &f{raidable}" + - "&6Warps: &f{warps}" + - "&6Power: &f{power}/{maxPower}" + - "&6Members: &f{online}/{members}" + + # Show + # This shows up when someone does /f show. + # It adds tooltips to each player in the list here, nothing else. + # {group} will show the players primary group if you have vault installed. + # {balance} will show their balance if you have vault installed. + # {lastSeen} will show human readable info on when the player was last seen, or online. + show: + - "&6Last Seen: &f{lastSeen}" + - "&6Power: &f{power}" + - "&6Rank: &f{group}" + - "&6Balance: &a${balance}" # Configuration section for Scoreboards # This will allow you to completely customize how your scoreboards look.