2015-05-13 03:34:00 +02:00
|
|
|
package com.massivecraft.factions.zcore.util;
|
|
|
|
|
|
|
|
|
|
|
|
import com.massivecraft.factions.FPlayer;
|
|
|
|
import com.massivecraft.factions.Faction;
|
|
|
|
import com.massivecraft.factions.Factions;
|
|
|
|
import com.massivecraft.factions.P;
|
|
|
|
import com.massivecraft.factions.util.MiscUtil;
|
2015-05-25 01:05:27 +02:00
|
|
|
import net.md_5.bungee.api.chat.BaseComponent;
|
|
|
|
import net.md_5.bungee.api.chat.HoverEvent;
|
|
|
|
import net.md_5.bungee.api.chat.TextComponent;
|
2015-05-13 03:34:00 +02:00
|
|
|
import org.bukkit.ChatColor;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static com.massivecraft.factions.zcore.util.TagReplacer.TagType;
|
|
|
|
|
|
|
|
public class TagUtil {
|
|
|
|
|
|
|
|
private static final int ARBITRARY_LIMIT = 25000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces all variables in a plain raw line for a faction
|
|
|
|
*
|
|
|
|
* @param faction for faction
|
|
|
|
* @param line raw line from config with variables to replace for
|
2015-05-13 06:17:22 +02:00
|
|
|
*
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return clean line
|
|
|
|
*/
|
|
|
|
public static String parsePlain(Faction faction, String line) {
|
|
|
|
for (TagReplacer tagReplacer : TagReplacer.getByType(TagType.FACTION)) {
|
|
|
|
if (tagReplacer.contains(line)) {
|
|
|
|
line = tagReplacer.replace(line, tagReplacer.getValue(faction, null));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces all variables in a plain raw line for a player
|
|
|
|
*
|
|
|
|
* @param fplayer for player
|
|
|
|
* @param line raw line from config with variables to replace for
|
2015-05-13 06:17:22 +02:00
|
|
|
*
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return clean line
|
|
|
|
*/
|
|
|
|
public static String parsePlain(FPlayer fplayer, String line) {
|
|
|
|
for (TagReplacer tagReplacer : TagReplacer.getByType(TagType.PLAYER)) {
|
|
|
|
if (tagReplacer.contains(line)) {
|
|
|
|
String rep = tagReplacer.getValue(fplayer.getFaction(), fplayer);
|
2015-05-13 06:17:22 +02:00
|
|
|
if (rep == null) {
|
2015-05-13 03:34:00 +02:00
|
|
|
rep = ""; // this should work, but it's not a good way to handle whatever is going wrong
|
|
|
|
}
|
|
|
|
line = tagReplacer.replace(line, rep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replaces all variables in a plain raw line for a faction, using relations from fplayer
|
|
|
|
*
|
|
|
|
* @param faction for faction
|
|
|
|
* @param fplayer from player
|
2015-05-13 06:17:22 +02:00
|
|
|
* @param line raw line from config with variables to replace for
|
|
|
|
*
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return clean line
|
|
|
|
*/
|
|
|
|
public static String parsePlain(Faction faction, FPlayer fplayer, String line) {
|
|
|
|
for (TagReplacer tagReplacer : TagReplacer.getByType(TagType.PLAYER)) {
|
|
|
|
if (tagReplacer.contains(line)) {
|
|
|
|
line = tagReplacer.replace(line, tagReplacer.getValue(faction, fplayer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2015-05-25 01:05:27 +02:00
|
|
|
|
2015-05-13 03:34:00 +02:00
|
|
|
/**
|
2015-05-25 01:05:27 +02:00
|
|
|
* Scan a line and parse the component variable into a component list
|
2015-05-13 03:34:00 +02:00
|
|
|
*
|
|
|
|
* @param faction for faction (viewers faction)
|
|
|
|
* @param fme for player (viewer)
|
2015-05-25 01:05:27 +02:00
|
|
|
* @param line component prefix
|
2015-05-13 06:17:22 +02:00
|
|
|
*
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return
|
|
|
|
*/
|
2015-05-25 01:05:27 +02:00
|
|
|
public static List<BaseComponent> parseComponent(Faction faction, FPlayer fme, String line) {
|
2015-05-13 03:34:00 +02:00
|
|
|
for (TagReplacer tagReplacer : TagReplacer.getByType(TagType.FANCY)) {
|
|
|
|
if (tagReplacer.contains(line)) {
|
|
|
|
String clean = line.replace(tagReplacer.getTag(), ""); // remove tag
|
2015-05-25 01:05:27 +02:00
|
|
|
return getComponent(faction, fme, tagReplacer, clean);
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-25 01:05:27 +02:00
|
|
|
* Checks if a line has component variables
|
2015-05-13 03:34:00 +02:00
|
|
|
*
|
|
|
|
* @param line raw line from config with variables
|
2015-05-13 06:17:22 +02:00
|
|
|
*
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return if the line has fancy variables
|
|
|
|
*/
|
2015-05-25 01:05:27 +02:00
|
|
|
public static boolean hasComponent(String line) {
|
2015-05-13 03:34:00 +02:00
|
|
|
for (TagReplacer tagReplacer : TagReplacer.getByType(TagType.FANCY)) {
|
|
|
|
if (tagReplacer.contains(line)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-05-25 01:05:27 +02:00
|
|
|
|
2015-05-13 03:34:00 +02:00
|
|
|
/**
|
2015-05-25 01:05:27 +02:00
|
|
|
* Lets get components?
|
2015-05-13 03:34:00 +02:00
|
|
|
*
|
|
|
|
* @param target Faction to get relate from
|
|
|
|
* @param fme Player to relate to
|
|
|
|
* @param prefix First part of the fancy message
|
2015-05-13 06:17:22 +02:00
|
|
|
*
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return list of fancy messages to send
|
|
|
|
*/
|
2015-05-25 01:05:27 +02:00
|
|
|
protected static List<BaseComponent> getComponent(Faction target, FPlayer fme, TagReplacer type, String prefix) {
|
|
|
|
List<BaseComponent> components = new ArrayList<BaseComponent>();
|
2015-05-13 03:34:00 +02:00
|
|
|
switch (type) {
|
|
|
|
case ALLIES_LIST:
|
2015-05-25 01:05:27 +02:00
|
|
|
BaseComponent currentAllies = P.p.txt.parseComponent(prefix);
|
2015-05-13 03:34:00 +02:00
|
|
|
boolean firstAlly = true;
|
|
|
|
for (Faction otherFaction : Factions.getInstance().getAllFactions()) {
|
|
|
|
if (otherFaction == target) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String s = otherFaction.getTag(fme);
|
|
|
|
if (otherFaction.getRelationTo(target).isAlly()) {
|
2015-05-25 01:05:27 +02:00
|
|
|
TextComponent then = new TextComponent(firstAlly ? s : ", " + s);
|
|
|
|
List<String> list = tipFaction(otherFaction);
|
|
|
|
TextComponent[] hover = new TextComponent[list.size()];
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
TextComponent component = new TextComponent(list.get(i));
|
|
|
|
component.setColor(net.md_5.bungee.api.ChatColor.valueOf(fme.getColorTo(otherFaction).name()));
|
|
|
|
hover[i] = component;
|
|
|
|
}
|
|
|
|
then.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover));
|
|
|
|
currentAllies.addExtra(then);
|
2015-05-13 03:34:00 +02:00
|
|
|
firstAlly = false;
|
2015-05-25 01:05:27 +02:00
|
|
|
if (currentAllies.toPlainText().length() > ARBITRARY_LIMIT) {
|
|
|
|
components.add(currentAllies);
|
|
|
|
currentAllies = new TextComponent("");
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 01:05:27 +02:00
|
|
|
components.add(currentAllies);
|
|
|
|
return components; // we must return here and not outside the switch
|
2015-05-13 03:34:00 +02:00
|
|
|
case ENEMIES_LIST:
|
2015-05-25 01:05:27 +02:00
|
|
|
BaseComponent currentEnemies = P.p.txt.parseComponent(prefix);
|
2015-05-13 03:34:00 +02:00
|
|
|
boolean firstEnemy = true;
|
|
|
|
for (Faction otherFaction : Factions.getInstance().getAllFactions()) {
|
|
|
|
if (otherFaction == target) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String s = otherFaction.getTag(fme);
|
|
|
|
if (otherFaction.getRelationTo(target).isEnemy()) {
|
2015-05-25 01:05:27 +02:00
|
|
|
TextComponent then = new TextComponent(firstEnemy ? s : ", " + s);
|
|
|
|
List<String> list = tipFaction(otherFaction);
|
|
|
|
TextComponent[] hover = new TextComponent[list.size()];
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
TextComponent component = new TextComponent(list.get(i));
|
|
|
|
component.setColor(net.md_5.bungee.api.ChatColor.valueOf(fme.getColorTo(otherFaction).name()));
|
|
|
|
hover[i] = component;
|
|
|
|
}
|
|
|
|
then.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover));
|
|
|
|
currentEnemies.addExtra(then);
|
2015-05-13 03:34:00 +02:00
|
|
|
firstEnemy = false;
|
2015-05-25 01:05:27 +02:00
|
|
|
if (currentEnemies.toPlainText().length() > ARBITRARY_LIMIT) {
|
|
|
|
components.add(currentEnemies);
|
|
|
|
currentEnemies = new TextComponent("");
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 01:05:27 +02:00
|
|
|
components.add(currentEnemies);
|
|
|
|
return components; // we must return here and not outside the switch
|
2015-05-13 03:34:00 +02:00
|
|
|
case ONLINE_LIST:
|
2015-05-25 01:05:27 +02:00
|
|
|
BaseComponent currentOnline = P.p.txt.parseComponent(prefix);
|
2015-05-13 03:34:00 +02:00
|
|
|
boolean firstOnline = true;
|
|
|
|
for (FPlayer p : MiscUtil.rankOrder(target.getFPlayersWhereOnline(true))) {
|
|
|
|
String name = p.getNameAndTitle();
|
2015-05-25 01:05:27 +02:00
|
|
|
TextComponent then = new TextComponent(firstOnline ? name : ", " + name);
|
|
|
|
List<String> list = tipPlayer(p);
|
|
|
|
TextComponent[] hover = new TextComponent[list.size()];
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
TextComponent component = new TextComponent(list.get(i));
|
|
|
|
component.setColor(net.md_5.bungee.api.ChatColor.valueOf(fme.getColorTo(p).name()));
|
|
|
|
hover[i] = component;
|
|
|
|
}
|
|
|
|
then.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover));
|
|
|
|
currentOnline.addExtra(then);
|
2015-05-13 03:34:00 +02:00
|
|
|
firstOnline = false;
|
2015-05-25 01:05:27 +02:00
|
|
|
if (currentOnline.toPlainText().length() > ARBITRARY_LIMIT) {
|
|
|
|
components.add(currentOnline);
|
|
|
|
currentOnline = new TextComponent("");
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-25 01:05:27 +02:00
|
|
|
components.add(currentOnline);
|
|
|
|
return components; // we must return here and not outside the switch
|
2015-05-13 03:34:00 +02:00
|
|
|
case OFFLINE_LIST:
|
2015-05-25 01:05:27 +02:00
|
|
|
BaseComponent currentOffline = P.p.txt.parseComponent(prefix);
|
2015-05-13 03:34:00 +02:00
|
|
|
boolean firstOffline = true;
|
|
|
|
for (FPlayer p : MiscUtil.rankOrder(target.getFPlayers())) {
|
|
|
|
String name = p.getNameAndTitle();
|
|
|
|
if (!p.isOnline()) {
|
2015-05-25 01:05:27 +02:00
|
|
|
TextComponent then = new TextComponent(firstOffline ? name : ", " + name);
|
|
|
|
List<String> list = tipPlayer(p);
|
|
|
|
TextComponent[] hover = new TextComponent[list.size()];
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
TextComponent component = new TextComponent(list.get(i));
|
|
|
|
component.setColor(net.md_5.bungee.api.ChatColor.valueOf(fme.getColorTo(p).name()));
|
|
|
|
hover[i] = component;
|
|
|
|
}
|
|
|
|
then.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hover));
|
|
|
|
currentOffline.addExtra(then);
|
2015-05-13 03:34:00 +02:00
|
|
|
firstOffline = false;
|
2015-05-25 01:05:27 +02:00
|
|
|
if (currentOffline.toPlainText().length() > ARBITRARY_LIMIT) {
|
|
|
|
components.add(currentOffline);
|
|
|
|
currentOffline = new TextComponent("");
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 01:05:27 +02:00
|
|
|
components.add(currentOffline);
|
|
|
|
return components; // we must return here and not outside the switch
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-13 06:17:22 +02:00
|
|
|
* Parses tooltip variables from config <br> Supports variables for factions only (type 2)
|
2015-05-13 03:34:00 +02:00
|
|
|
*
|
|
|
|
* @param faction faction to tooltip for
|
2015-05-13 06:17:22 +02:00
|
|
|
*
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return list of tooltips for a fancy message
|
|
|
|
*/
|
|
|
|
private static List<String> tipFaction(Faction faction) {
|
|
|
|
List<String> lines = new ArrayList<String>();
|
|
|
|
for (String line : P.p.getConfig().getStringList("tooltips.list")) {
|
|
|
|
lines.add(ChatColor.translateAlternateColorCodes('&', TagUtil.parsePlain(faction, line)));
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-13 06:17:22 +02:00
|
|
|
* Parses tooltip variables from config <br> Supports variables for players and factions (types 1 and 2)
|
2015-05-13 03:34:00 +02:00
|
|
|
*
|
|
|
|
* @param fplayer player to tooltip for
|
2015-05-13 06:17:22 +02:00
|
|
|
*
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return list of tooltips for a fancy message
|
|
|
|
*/
|
|
|
|
private static List<String> tipPlayer(FPlayer fplayer) {
|
|
|
|
List<String> lines = new ArrayList<String>();
|
|
|
|
for (String line : P.p.getConfig().getStringList("tooltips.show")) {
|
|
|
|
lines.add(ChatColor.translateAlternateColorCodes('&', TagUtil.parsePlain(fplayer, line)));
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
}
|