diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/events/PingEventHandler.java b/core/src/main/java/ch/andre601/advancedserverlist/core/events/PingEventHandler.java index 9c13d48a..786d982b 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/events/PingEventHandler.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/events/PingEventHandler.java @@ -70,23 +70,27 @@ public static void handleEvent(GenericEventWrapper event){ serverPlaceholders = new ServerPlaceholders(online, max, host); - if(!profile.getMotd().isEmpty()){ - event.setMotd( - ComponentParser.list(profile.getMotd()) - .replacements(playerPlaceholders) - .replacements(serverPlaceholders) - .modifyText(text -> event.parsePAPIPlaceholders(text, player)) - .toComponent() - ); + if(!profile.getMOTDs().isEmpty()){ + ServerListProfile.Motd motd = profile.getRandomMOTD(); + + if(motd != null){ + event.setMotd( + ComponentParser.text(motd.getText()) + .replacements(playerPlaceholders) + .replacements(serverPlaceholders) + .modifyText(text -> event.parsePAPIPlaceholders(text, player)) + .toComponent() + ); + } } - if(profile.shouldHidePlayers()){ + if(profile.isHidePlayersEnabled()){ event.hidePlayers(); } - if(!profile.getPlayerCount().isEmpty() && !profile.shouldHidePlayers()){ + if(!profile.getPlayerCountText().isEmpty() && !profile.isHidePlayersEnabled()){ event.setPlayerCount( - ComponentParser.text(profile.getPlayerCount()) + ComponentParser.text(profile.getPlayerCountText()) .replacements(playerPlaceholders) .replacements(serverPlaceholders) .modifyText(text -> event.parsePAPIPlaceholders(text, player)) @@ -94,7 +98,7 @@ public static void handleEvent(GenericEventWrapper event){ ); } - if(!profile.getPlayers().isEmpty() && !profile.shouldHidePlayers()){ + if(!profile.getPlayers().isEmpty() && !profile.isHidePlayersEnabled()){ event.setPlayers(profile.getPlayers(), player, playerPlaceholders, serverPlaceholders); } diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/file/FileHandler.java b/core/src/main/java/ch/andre601/advancedserverlist/core/file/FileHandler.java index 6a3931dc..4c9c3935 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/file/FileHandler.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/file/FileHandler.java @@ -127,7 +127,7 @@ public boolean reloadProfiles(){ if(tmp == null) continue; - profiles.add(new ServerListProfile(tmp, logger)); + profiles.add(ServerListProfile.Builder.resolve(tmp, logger).build()); logger.info("Loaded " + file.getName()); } diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ProfileManager.java b/core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ProfileManager.java index bef61b29..87e73c7b 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ProfileManager.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ProfileManager.java @@ -52,10 +52,10 @@ public ProfileManager replacements(Placeholders placeholders){ public ServerListProfile getProfile(){ for(ServerListProfile profile : core.getFileHandler().getProfiles()){ - if(profile.isInvalid()) + if(profile.isInvalidProfile()) continue; - if(profile.evalConditions(replacements)) + if(profile.evaluateConditions(replacements)) return profile; } diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ServerListProfile.java b/core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ServerListProfile.java index e7f75a45..1e78cfb7 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ServerListProfile.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/profiles/ServerListProfile.java @@ -30,17 +30,14 @@ import org.spongepowered.configurate.ConfigurationNode; import org.spongepowered.configurate.serialize.SerializationException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; public class ServerListProfile{ private final int priority; private final List expressions; - private final List motd; + private final List motds; private final List players; private final String playerCount; private final String favicon; @@ -48,32 +45,47 @@ public class ServerListProfile{ private final boolean extraPlayersEnabled; private final int extraPlayers; - public ServerListProfile(ConfigurationNode node, PluginLogger logger){ - this.priority = node.node("priority").getInt(); - this.expressions = createExpressions(getList(node, false, "conditions"), logger); - - this.motd = getList(node, true, "motd"); - this.players = getList(node, false, "playerCount", "hover"); - this.playerCount = node.node("playerCount", "text").getString(""); - this.favicon = node.node("favicon").getString(""); - this.hidePlayers = node.node("playerCount", "hidePlayers").getBoolean(); - this.extraPlayersEnabled = node.node("playerCount", "extraPlayers", "enabled").getBoolean(); - this.extraPlayers = node.node("playerCount", "extraPlayers", "amount").getInt(); + private final Random random = new Random(); + + public ServerListProfile(int priority, List expressions, List motds, List players, + String playerCount, String favicon, boolean hidePlayers, boolean extraPlayersEnabled, + int extraPlayers){ + this.priority = priority; + this.expressions = expressions; + this.motds = motds; + this.players = players; + this.playerCount = playerCount; + this.favicon = favicon; + this.hidePlayers = hidePlayers; + this.extraPlayersEnabled = extraPlayersEnabled; + this.extraPlayers = extraPlayers; } public int getPriority(){ return priority; } - public List getMotd(){ - return motd; + public List getMOTDs(){ + return motds; + } + + public Motd getRandomMOTD(){ + if(motds.isEmpty()) + return null; + + if(motds.size() == 1) + return motds.get(0); + + synchronized(random){ + return motds.get(random.nextInt(motds.size())); + } } public List getPlayers(){ return players; } - public String getPlayerCount(){ + public String getPlayerCountText(){ return playerCount; } @@ -81,7 +93,7 @@ public String getFavicon(){ return favicon; } - public boolean shouldHidePlayers(){ + public boolean isHidePlayersEnabled(){ return hidePlayers; } @@ -93,59 +105,191 @@ public int getExtraPlayers(){ return extraPlayers; } - public boolean evalConditions(Map replacements){ + public boolean evaluateConditions(Map replacements){ if(expressions.isEmpty()) return true; for(Expression expression : expressions){ - if(!expression.evaluate(replacements)){ + if(!expression.evaluate(replacements)) return false; - } } return true; } - public boolean isInvalid(){ - return getMotd().isEmpty() && + /* + * Returns true if the Profile... + * ...doesn't have any valid MOTD set AND + * ...doesn't have any player hover set AND + * ...doesn't have a player count text set and hidePlayers is false AND + * ...doesn't have a favicon set. + */ + public boolean isInvalidProfile(){ + return getMOTDs().isEmpty() && getPlayers().isEmpty() && - (getPlayerCount().isEmpty() && !shouldHidePlayers()) && + (getPlayerCountText().isEmpty() && !isHidePlayersEnabled()) && getFavicon().isEmpty(); } - private List createExpressions(List list, PluginLogger logger) { - if(list.isEmpty()) - return Collections.emptyList(); + public static class Builder{ + + private final ConfigurationNode node; + + private final int priority; + private final PluginLogger logger; + + private final List expressions = new ArrayList<>(); + private final List motds = new ArrayList<>(); + private List players = new ArrayList<>(); + private String playerCount = null; + private String favicon = null; + private boolean hidePlayers = false; + private boolean extraPlayersEnabled = false; + private int extraPlayers = 0; + + private Builder(ConfigurationNode node, PluginLogger logger){ + this.node = node; + this.priority = node.node("priority").getInt(); + this.logger = logger; + } - List expressions = new ArrayList<>(); - for(String str : list){ - Expression expression = new Expression(str); + public static Builder resolve(ConfigurationNode node, PluginLogger logger){ + return new Builder(node, logger) + .resolveExpressions() + .resolveMOTDs() + .resolvePlayers() + .resolvePlayerCount() + .resolveFavicon() + .resolveHidePlayers() + .resolveExtraPlayers() + .resolveExtraPlayersCount(); + } + + private Builder resolveExpressions(){ + List list = getList("conditions"); + if(list.isEmpty()) + return this; + + for(String str : list){ + Expression expression = new Expression(str); + + if(expression.getResult() != Expression.ExpressionResult.VALID){ + logger.warn("Detected invalid expression in condition '%s'! Reason: %s", str, expression.getResult().getMessage()); + continue; + } + + this.expressions.add(expression); + } - if(expression.getResult() != Expression.ExpressionResult.VALID){ - logger.warn("Detected Invalid condition '%s'! Cause: %s", str, expression.getResult().getMessage()); - continue; + return this; + } + + private Builder resolveMOTDs(){ + List list = getList("motds"); + if(!list.isEmpty()){ + for(String lines : list){ + Motd motd = Motd.resolve(lines); + if(motd == null) + continue; + + this.motds.add(motd); + } + return this; } - expressions.add(expression); + list = getList("motd"); + if(list.isEmpty()) + return this; + + Motd motd = Motd.resolve(list); + if(motd == null) + return this; + + this.motds.add(motd); + return this; + } + + private Builder resolvePlayers(){ + this.players = getList("playerCount", "hover"); + return this; + } + + private Builder resolvePlayerCount(){ + this.playerCount = node.node("playerCount", "text").getString(""); + return this; + } + + private Builder resolveFavicon(){ + this.favicon = node.node("favicon").getString(""); + return this; + } + + private Builder resolveHidePlayers(){ + this.hidePlayers = node.node("playerCount", "hidePlayers").getBoolean(); + return this; + } + + private Builder resolveExtraPlayers(){ + this.extraPlayersEnabled = node.node("playerCount", "extraPlayers", "enabled").getBoolean(); + return this; + } + + private Builder resolveExtraPlayersCount(){ + this.extraPlayers = node.node("playerCount", "extraPlayers", "amount").getInt(); + return this; + } + + public ServerListProfile build(){ + return new ServerListProfile(this.priority, this.expressions, this.motds, this.players, this.playerCount, + this.favicon, this.hidePlayers, this.extraPlayersEnabled, this.extraPlayers); } - return expressions; + private List getList(Object... path){ + List temp; + try{ + temp = node.node(path).getList(String.class); + }catch(SerializationException ex){ + temp = null; + } + + if(temp == null) + return Collections.emptyList(); + + return temp; + } } - private List getList(ConfigurationNode node, boolean trim, Object... path){ - List list; - try{ - list = node.node(path).getList(String.class); - }catch(SerializationException ex){ - return Collections.emptyList(); - } + public static class Motd{ + String text; - if(list == null) - return Collections.emptyList(); + private Motd(String[] lines){ + text = resolveLines(lines); + } - if(trim && list.size() > 2) - return list.subList(0, 2); + public static Motd resolve(String text){ + String[] split = text.split("\n"); + if(split.length == 0) + return null; + + return new Motd(split); + } - return list; + public static Motd resolve(List lines){ + if(lines.size() == 0) + return null; + + return new Motd(lines.toArray(new String[0])); + } + + public String getText(){ + return text; + } + + private String resolveLines(String[] lines){ + if(lines.length > 2) + return String.join("\n", lines[0], lines[1]); + + return String.join("\n", lines); + } } } diff --git a/core/src/main/resources/profiles/default.yml b/core/src/main/resources/profiles/default.yml index 47f43674..b36fd021 100644 --- a/core/src/main/resources/profiles/default.yml +++ b/core/src/main/resources/profiles/default.yml @@ -16,16 +16,17 @@ priority: 0 # - '{playerVersion} < 759' # -# Set the MOTD that should be displayed. +# Set the MOTDs that should be displayed. # Supports HEX colors for 1.16+ servers including gradients using MiniMessage's format. # -# Remove this option or set it to motd: [] to not alter the MOTD. +# Remove this option or set it to motds: [] to not alter the MOTD. # -# Read more: https://github.com/Andre601/AdvancedServerList/wiki/Profiles#motd +# Read more: https://github.com/Andre601/AdvancedServerList/wiki/Profiles#motds # -motd: - - 'Line 1' - - 'Line 2' +motds: + - |- + Line 1 + Line 2 # # Allows you to set a favicon for this server list profile. diff --git a/pom.xml b/pom.xml index d0b4c3f0..ecd482c5 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ UTF-8 - 1.8.1 + 1.9.0 Create multiple Server lists based on conditions. 16