Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Improve config and remove {playerName}
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed Jun 23, 2022
1 parent 240c2cd commit aa77901
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 66 deletions.
9 changes: 9 additions & 0 deletions bungeecord/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@
<shadedPattern>ch.andre601.advancedserverlist.bungeecord.depends.adventure</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void onProxyPing(ProxyPingEvent event){
if(protocol == null)
return;

ServerListProfile profile = plugin.getCore().getServerListProfile(protocol.getName(), protocol.getProtocol());
ServerListProfile profile = plugin.getCore().getServerListProfile(protocol.getProtocol());
if(!profile.getMotd().isEmpty()){
List<String> motd = profile.getMotd();
if(motd.size() > 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import ch.andre601.advancedserverlist.core.interfaces.PluginCore;
import ch.andre601.advancedserverlist.core.interfaces.ProxyLogger;
import ch.andre601.advancedserverlist.core.parsing.ComponentParser;
import ch.andre601.advancedserverlist.core.profiles.Condition;
import ch.andre601.advancedserverlist.core.profiles.ConditionHolder;
import ch.andre601.advancedserverlist.core.profiles.ServerListProfile;

import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -62,13 +62,13 @@ public CommandHandler getCommandHandler(){
return commandHandler;
}

public ServerListProfile getServerListProfile(String name, int protocol){
public ServerListProfile getServerListProfile(int protocol){
for(ServerListProfile profile : getFileHandler().getProfiles()){
if(profile.getMotd().isEmpty() && profile.getPlayers().isEmpty() && profile.getPlayerCount().isEmpty())
continue;
Condition condition = profile.getCondition();
if(condition.eval(name, protocol, getProxyLogger()))

ConditionHolder conditions = profile.getConditions();
if(conditions.eval(protocol, getProxyLogger()))
return profile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,87 @@

import ch.andre601.advancedserverlist.core.interfaces.ProxyLogger;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class Condition{
public class ConditionHolder{

private final String expression;
private final List<String> expressions = new ArrayList<>();

public Condition(String expression){
this.expression = expression;
public ConditionHolder(List<String> expressions){
if(expressions != null && !expressions.isEmpty())
this.expressions.addAll(expressions);
}

public boolean eval(String name, int protocol, ProxyLogger logger){
public boolean eval(int protocol, ProxyLogger logger){
if(expressions.isEmpty())
return true;

for(String expression : expressions){
if(!parseExpression(expression, protocol, logger))
return false;
}

return true;
}

private boolean parseExpression(String expression, int protocol, ProxyLogger logger){
if(expression == null || expression.isEmpty())
return true;

String newExpression = replacePlaceholders(expression, protocol);

String newExpression = replacePlaceholders(name, protocol);
char[] chars = newExpression.toCharArray();

StringBuilder left = new StringBuilder();
StringBuilder operator = new StringBuilder();
StringBuilder right = new StringBuilder();

boolean operatorFound = false;

for(int i = 0; i < chars.length; i++){
char c = chars[i];

if(c == ' ')
continue;

if((c != '<' && c != '>' && c != '=' && c != '!') || i + 1 >= chars.length){
if(operatorFound){
right.append(c);
continue;
}

left.append(c);
continue;
}

if(operatorFound){
logger.warn("Encountered second operator in condition! AdvancedServerList currently only supports one operator!");
return false;
}

operatorFound = true;

char next = chars[i + 1];
if(c == '!' && next != '='){
logger.warn("Invalid condition found. Expected '!=' but found '!" + next + "' in condition " + newExpression);
return false;
}

operator.append(c);
i++;

if(c == '!' || ((c == '<' || c == '>') && next == '=')){
operator.append(next);
}
}

if(left.isEmpty() || operator.isEmpty() || right.isEmpty()){
logger.warn("Failed to evaluate condition. One part of the expression was empty!");
return false;
}

int leftInt = tryParse(left.toString());
int rightInt = tryParse(right.toString());

Expand All @@ -82,7 +98,7 @@ public boolean eval(String name, int protocol, ProxyLogger logger){

}

private String replacePlaceholders(String name, int protocol){
private String replacePlaceholders(String expression, int protocol){
char[] chars = expression.toCharArray();

StringBuilder builder = new StringBuilder(expression.length());
Expand Down Expand Up @@ -119,9 +135,6 @@ private String replacePlaceholders(String name, int protocol){
continue;
}

if(finalPlaceholder.equals("playerName")){
builder.append(name);
}else
if(finalPlaceholder.equals("playerVersion")){
builder.append(protocol);
}else{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
package ch.andre601.advancedserverlist.core.profiles;

import ch.andre601.advancedserverlist.core.interfaces.ProxyLogger;
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;

public class ServerListProfile{

private final Condition condition;
private final ConditionHolder conditions;
private final int priority;

private final List<String> motd;
private final List<String> players;
private final String playerCount;

public ServerListProfile(ConfigurationNode node){
this.condition = new Condition(node.node("condition").getString(""));
this.conditions = new ConditionHolder(getList(node, "conditions"));
this.priority = node.node("priority").getInt();

this.motd = getList(node, "motd");
this.players = getList(node, "players");
this.playerCount = node.node("playerCount").getString("");
}

public Condition getCondition(){
return condition;
public ConditionHolder getConditions(){
return conditions;
}

public int getPriority(){
Expand Down
50 changes: 18 additions & 32 deletions core/src/main/resources/profiles/default.yml
Original file line number Diff line number Diff line change
@@ -1,59 +1,45 @@
#
# The priority of this profile. Higher number equals higher priority.
# Set the priority of this profile.
# Higher number = Higher priority.
#
# Read more: https://github.com/Andre601/AdvancedServerList/wiki/Profiles#priority
#
priority: 0

#
# Allows you to set a condition for this profile.
# If the condition evaluates to true will it be displayed, given that no other profile with higher priority is available.
#
# It is recommended to keep a profile with a low priority and no condition set.
#
# The following placeholders can be used:
# - {playerVersion} to replace with the Player's Client version (Protocol version, NOT MC version such as 1.19!)
# - {playerName} to replace with the Player's name.
#
# The following operators are available:
# - < Returns true if the left value is less than the right value.
# When the provided values are NOT numbers will it simply compare the lengths of each String.
# - <= Returns true if the left value is less than OR equal to the right value.
# When the provided values are NOT numbers will it simply compare the lengths of each String.
# - = Returns true if the left value is equal to the right value.
# - != Returns true if the left value is NOT equal to the right value.
# - > Returns true if the left value is larger than the right value.
# When the provided values are NOT numbers will it simply compare the lengths of each String.
# - >= Returns true if the left value is larger than OR equal to the right value.
# When the provided values are NOT numbers will it simply compare the lengths of each String
# Set conditions that needs to be met.
# Only when all conditions return true will the profile be shown (Given no profile with higher priority is used).
#
# Due to current limitations can you only use one operator within a condition. This may get changed in the future.
# Read more: https://github.com/Andre601/AdvancedServerList/wiki/Profiles#conditions
#
#condition: '{playerVersion} < 759'
#conditions:
# - '{playerVersion} < 759'

#
# The MOTD to display.
# Remove this option or set it to "motd: []" for no changes.
# Set the MOTD that should be displayed.
# Can be removed or set to "motd: []" to not alter the MOTD.
#
# Supports 24-Bit HEX colors.
# Read more: https://github.com/Andre601/AdvancedServerList/wiki/Profiles#motd
#
motd:
- 'Line 1'
- 'Line 2'

#
# The text to display when hovering over the "<online>/<total>" player count.
# Remove or set this to "players: []" for no changes.
# Set the hover text that usually displays online players.
# Can be removed or set to "players: []" to not alter the player list.
#
# Only supports basic colors.
# Read more: https://github.com/Andre601/AdvancedServerList/wiki/Profiles#players
#
players:
- 'Line 1'
- 'Line 2'
- 'Line 3'

#
# The text that usually shows the current and total amount of players.
# Remove this option or set it to "playerCount: ''" for no changes.
# Set the text that usually shows online players and max that can join.
# Can be removed or set to "playerCount: ''" to not alter the player count text.
#
# Only supports basic colors.
# Read more: https://github.com/Andre601/AdvancedServerList/wiki/Profiles#playercount
#
playerCount: 'Text'
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void onProxyPing(ProxyPingEvent event){
return;

ServerPing.Builder builder = ping.asBuilder();
ServerListProfile profile = plugin.getCore().getServerListProfile(protocol.getName(), protocol.getProtocol());
ServerListProfile profile = plugin.getCore().getServerListProfile(protocol.getProtocol());
if(!profile.getMotd().isEmpty()){
List<String> motd = profile.getMotd();
if(motd.size() > 2)
Expand Down

0 comments on commit aa77901

Please sign in to comment.