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;
|
2018-10-23 01:42:57 +02:00
|
|
|
import com.massivecraft.factions.SavageFactions;
|
2015-05-13 03:34:00 +02:00
|
|
|
import com.massivecraft.factions.util.MiscUtil;
|
2018-02-17 19:17:37 +01:00
|
|
|
import me.clip.placeholderapi.PlaceholderAPI;
|
2015-05-25 22:46:18 +02:00
|
|
|
import mkremins.fanciful.FancyMessage;
|
2015-05-13 03:34:00 +02:00
|
|
|
import org.bukkit.ChatColor;
|
2018-02-17 19:17:37 +01:00
|
|
|
import org.bukkit.entity.Player;
|
2015-05-13 03:34:00 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
* @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
|
|
|
|
* @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)) {
|
2015-08-06 23:59:54 +02:00
|
|
|
String value = tagReplacer.getValue(faction, fplayer);
|
|
|
|
if (value != null) {
|
|
|
|
line = tagReplacer.replace(line, value);
|
|
|
|
} else {
|
|
|
|
return null; // minimal show, entire line to be ignored
|
|
|
|
}
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-25 22:46:18 +02:00
|
|
|
* Scan a line and parse the fancy variable into a fancy list
|
2015-05-13 03:34:00 +02:00
|
|
|
*
|
|
|
|
* @param faction for faction (viewers faction)
|
|
|
|
* @param fme for player (viewer)
|
2015-05-25 22:46:18 +02:00
|
|
|
* @param line fancy message prefix
|
2015-05-13 03:34:00 +02:00
|
|
|
* @return
|
|
|
|
*/
|
2015-05-25 22:46:18 +02:00
|
|
|
public static List<FancyMessage> parseFancy(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 22:46:18 +02:00
|
|
|
return getFancy(faction, fme, tagReplacer, clean);
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-17 19:17:37 +01:00
|
|
|
public static String parsePlaceholders(Player player, String line) {
|
2018-10-23 01:42:57 +02:00
|
|
|
if (SavageFactions.plugin.isClipPlaceholderAPIHooked() && player.isOnline()) {
|
2018-02-17 19:17:37 +01:00
|
|
|
line = PlaceholderAPI.setPlaceholders(player, line);
|
|
|
|
}
|
|
|
|
|
2018-10-23 01:42:57 +02:00
|
|
|
if (SavageFactions.plugin.isMVdWPlaceholderAPIHooked() && player.isOnline()) {
|
2018-02-17 19:17:37 +01:00
|
|
|
line = be.maximvdw.placeholderapi.PlaceholderAPI.replacePlaceholders(player, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2015-05-13 03:34:00 +02:00
|
|
|
/**
|
2015-05-25 22:46:18 +02:00
|
|
|
* Checks if a line has fancy variables
|
2015-05-13 03:34:00 +02:00
|
|
|
*
|
|
|
|
* @param line raw line from config with variables
|
|
|
|
* @return if the line has fancy variables
|
|
|
|
*/
|
2015-05-25 22:46:18 +02:00
|
|
|
public static boolean hasFancy(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 22:46:18 +02:00
|
|
|
* Lets get fancy.
|
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
|
|
|
|
* @return list of fancy messages to send
|
|
|
|
*/
|
2015-05-25 22:46:18 +02:00
|
|
|
protected static List<FancyMessage> getFancy(Faction target, FPlayer fme, TagReplacer type, String prefix) {
|
2017-12-19 11:18:13 +01:00
|
|
|
List<FancyMessage> fancyMessages = new ArrayList<>();
|
2018-10-23 01:42:57 +02:00
|
|
|
boolean minimal = SavageFactions.plugin.getConfig().getBoolean("minimal-show", false);
|
2015-08-06 23:59:54 +02:00
|
|
|
|
2015-05-13 03:34:00 +02:00
|
|
|
switch (type) {
|
|
|
|
case ALLIES_LIST:
|
2018-10-23 01:42:57 +02:00
|
|
|
FancyMessage currentAllies = SavageFactions.plugin.txt.parseFancy(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 22:46:18 +02:00
|
|
|
currentAllies.then(firstAlly ? s : ", " + s);
|
|
|
|
currentAllies.tooltip(tipFaction(otherFaction)).color(fme.getColorTo(otherFaction));
|
2015-05-13 03:34:00 +02:00
|
|
|
firstAlly = false;
|
2015-05-25 22:46:18 +02:00
|
|
|
if (currentAllies.toJSONString().length() > ARBITRARY_LIMIT) {
|
|
|
|
fancyMessages.add(currentAllies);
|
|
|
|
currentAllies = new FancyMessage("");
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 22:46:18 +02:00
|
|
|
fancyMessages.add(currentAllies);
|
2015-08-06 23:59:54 +02:00
|
|
|
return firstAlly && minimal ? null : fancyMessages; // we must return here and not outside the switch
|
2015-05-13 03:34:00 +02:00
|
|
|
case ENEMIES_LIST:
|
2018-10-23 01:42:57 +02:00
|
|
|
FancyMessage currentEnemies = SavageFactions.plugin.txt.parseFancy(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 22:46:18 +02:00
|
|
|
currentEnemies.then(firstEnemy ? s : ", " + s);
|
|
|
|
currentEnemies.tooltip(tipFaction(otherFaction)).color(fme.getColorTo(otherFaction));
|
2015-05-13 03:34:00 +02:00
|
|
|
firstEnemy = false;
|
2015-05-25 22:46:18 +02:00
|
|
|
if (currentEnemies.toJSONString().length() > ARBITRARY_LIMIT) {
|
|
|
|
fancyMessages.add(currentEnemies);
|
|
|
|
currentEnemies = new FancyMessage("");
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 22:46:18 +02:00
|
|
|
fancyMessages.add(currentEnemies);
|
2015-08-06 23:59:54 +02:00
|
|
|
return firstEnemy && minimal ? null : fancyMessages; // we must return here and not outside the switch
|
2018-01-05 09:25:50 +01:00
|
|
|
case TRUCES_LIST:
|
2018-10-23 01:42:57 +02:00
|
|
|
FancyMessage currentTruces = SavageFactions.plugin.txt.parseFancy(prefix);
|
2018-01-05 09:25:50 +01:00
|
|
|
boolean firstTruce = true;
|
|
|
|
for (Faction otherFaction : Factions.getInstance().getAllFactions()) {
|
|
|
|
if (otherFaction == target) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
String s = otherFaction.getTag(fme);
|
|
|
|
if (otherFaction.getRelationTo(target).isTruce()) {
|
|
|
|
currentTruces.then(firstTruce ? s : ", " + s);
|
|
|
|
currentTruces.tooltip(tipFaction(otherFaction)).color(fme.getColorTo(otherFaction));
|
|
|
|
firstTruce = false;
|
|
|
|
if (currentTruces.toJSONString().length() > ARBITRARY_LIMIT) {
|
|
|
|
fancyMessages.add(currentTruces);
|
|
|
|
currentTruces = new FancyMessage("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fancyMessages.add(currentTruces);
|
|
|
|
return firstTruce && minimal ? null : fancyMessages; // we must return here and not outside the switch
|
2015-05-13 03:34:00 +02:00
|
|
|
case ONLINE_LIST:
|
2018-10-23 01:42:57 +02:00
|
|
|
FancyMessage currentOnline = SavageFactions.plugin.txt.parseFancy(prefix);
|
2015-05-13 03:34:00 +02:00
|
|
|
boolean firstOnline = true;
|
2018-03-05 02:40:36 +01:00
|
|
|
for (FPlayer p : MiscUtil.rankOrder(target.getFPlayersWhereOnline(true, fme))) {
|
2018-03-04 21:53:52 +01:00
|
|
|
if (fme.getPlayer() != null && !fme.getPlayer().canSee(p.getPlayer())) {
|
|
|
|
continue; // skip
|
|
|
|
}
|
2015-05-13 03:34:00 +02:00
|
|
|
String name = p.getNameAndTitle();
|
2015-05-25 22:46:18 +02:00
|
|
|
currentOnline.then(firstOnline ? name : ", " + name);
|
|
|
|
currentOnline.tooltip(tipPlayer(p)).color(fme.getColorTo(p));
|
2015-05-13 03:34:00 +02:00
|
|
|
firstOnline = false;
|
2015-05-25 22:46:18 +02:00
|
|
|
if (currentOnline.toJSONString().length() > ARBITRARY_LIMIT) {
|
|
|
|
fancyMessages.add(currentOnline);
|
|
|
|
currentOnline = new FancyMessage("");
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
2015-05-25 22:46:18 +02:00
|
|
|
fancyMessages.add(currentOnline);
|
2015-08-06 23:59:54 +02:00
|
|
|
return firstOnline && minimal ? null : fancyMessages; // we must return here and not outside the switch
|
2015-05-13 03:34:00 +02:00
|
|
|
case OFFLINE_LIST:
|
2018-10-23 01:42:57 +02:00
|
|
|
FancyMessage currentOffline = SavageFactions.plugin.txt.parseFancy(prefix);
|
2015-05-13 03:34:00 +02:00
|
|
|
boolean firstOffline = true;
|
|
|
|
for (FPlayer p : MiscUtil.rankOrder(target.getFPlayers())) {
|
|
|
|
String name = p.getNameAndTitle();
|
2018-03-04 21:53:52 +01:00
|
|
|
// Also make sure to add players that are online BUT can't be seen.
|
|
|
|
if (!p.isOnline() || (fme.getPlayer() != null && p.isOnline() && !fme.getPlayer().canSee(p.getPlayer()))) {
|
2015-05-25 22:46:18 +02:00
|
|
|
currentOffline.then(firstOffline ? name : ", " + name);
|
|
|
|
currentOffline.tooltip(tipPlayer(p)).color(fme.getColorTo(p));
|
2015-05-13 03:34:00 +02:00
|
|
|
firstOffline = false;
|
2015-05-25 22:46:18 +02:00
|
|
|
if (currentOffline.toJSONString().length() > ARBITRARY_LIMIT) {
|
|
|
|
fancyMessages.add(currentOffline);
|
|
|
|
currentOffline = new FancyMessage("");
|
2015-05-13 03:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-25 22:46:18 +02:00
|
|
|
fancyMessages.add(currentOffline);
|
2015-08-06 23:59:54 +02:00
|
|
|
return firstOffline && minimal ? null : fancyMessages; // 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
|
|
|
|
* @return list of tooltips for a fancy message
|
|
|
|
*/
|
|
|
|
private static List<String> tipFaction(Faction faction) {
|
2017-12-19 11:18:13 +01:00
|
|
|
List<String> lines = new ArrayList<>();
|
2018-10-23 01:42:57 +02:00
|
|
|
for (String line : SavageFactions.plugin.getConfig().getStringList("tooltips.list")) {
|
2015-05-13 03:34:00 +02:00
|
|
|
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
|
|
|
|
* @return list of tooltips for a fancy message
|
|
|
|
*/
|
|
|
|
private static List<String> tipPlayer(FPlayer fplayer) {
|
2017-12-19 11:18:13 +01:00
|
|
|
List<String> lines = new ArrayList<>();
|
2018-10-23 01:42:57 +02:00
|
|
|
for (String line : SavageFactions.plugin.getConfig().getStringList("tooltips.show")) {
|
2015-05-13 03:34:00 +02:00
|
|
|
lines.add(ChatColor.translateAlternateColorCodes('&', TagUtil.parsePlain(fplayer, line)));
|
|
|
|
}
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
}
|