Add tooltips for f show.

Refactor tooltip methods to be shared across command classes.
This commit is contained in:
drtshock 2014-11-13 13:49:13 -06:00
parent 43826d986f
commit c4f4036e1d
6 changed files with 103 additions and 135 deletions

View File

@ -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<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
perms = rsp.getProvider();
return perms != null;
}
@Override
public GsonBuilder getGsonBuilder() {
Type mapFLocToStringSetType = new TypeToken<Map<FLocation, Set<String>>>() {
@ -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);

View File

@ -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<String> getToolTips(Faction faction) {
List<String> lines = new ArrayList<String>();
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()));
}
}

View File

@ -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("<a>Allies: ");
String enemyList = p.txt.parse("<a>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("<i>") + ", ";
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("<a>") + "Members online: ";
String offlineList = p.txt.parse("<a>") + "Members offline: ";
boolean canSeePower = Permission.POWER_ANY.has(me);
for (FPlayer follower : admins) {
listpart = follower.getNameAndTitle(fme);
if (canSeePower) {
listpart += p.txt.parse("<i>(%d), ", follower.getPowerRounded());
} else {
listpart += p.txt.parse("<i>, ");
}
if (follower.isOnlineAndVisibleTo(me)) {
onlineList += listpart;
} else {
offlineList += listpart;
}
}
for (FPlayer follower : mods) {
listpart = follower.getNameAndTitle(fme);
if (canSeePower) {
listpart += p.txt.parse("<i>(%d), ", follower.getPowerRounded());
} else {
listpart += p.txt.parse("<i>, ");
}
if (follower.isOnlineAndVisibleTo(me)) {
onlineList += listpart;
} else {
offlineList += listpart;
}
}
for (FPlayer follower : normals) {
listpart = follower.getNameAndTitle(fme);
if (canSeePower) {
listpart += p.txt.parse("<i>(%d), ", follower.getPowerRounded());
} else {
listpart += p.txt.parse("<i>, ");
}
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);
}
if (onlineList.length() > 2048) {
String[] lines = splitString(onlineList, 2048, 256000);
for (int i = 0; i < lines.length; i++) {
sendMessage(lines[i]);
}
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 {
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);
offline.then(name).tooltip(getToolTips(p));
}
}
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);
}
}

View File

@ -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) {

View File

@ -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<T extends MPlugin> {
}
public void sendFancyMessage(List<FancyMessage> messages) {
for(FancyMessage m : messages) {
for (FancyMessage m : messages) {
sendFancyMessage(m);
}
}
public List<String> getToolTips(FPlayer player) {
List<String> lines = new ArrayList<String>();
for (String s : p.getConfig().getStringList("tooltips.show")) {
lines.add(ChatColor.translateAlternateColorCodes('&', replaceFPlayerTags(s, player)));
}
return lines;
}
public List<String> getToolTips(Faction faction) {
List<String> lines = new ArrayList<String>();
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
// -------------------------------------------- //

View File

@ -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.