Waterfall/Waterfall-Proxy-Patches/0025-Custom-motd-system.patch
2022-10-31 17:06:55 +01:00

205 lines
9.6 KiB
Diff

From b406218a681d715b932c80458b4164e4d4cd6db5 Mon Sep 17 00:00:00 2001
From: LinsaFTW <25271111+linsaftw@users.noreply.github.com>
Date: Thu, 7 Oct 2021 21:37:24 -0300
Subject: [PATCH] Custom motd system
diff --git a/flamecord/src/main/java/dev/_2lstudios/flamecord/configuration/FlameConfig.java b/flamecord/src/main/java/dev/_2lstudios/flamecord/configuration/FlameConfig.java
index cfbd55c3..360305b9 100644
--- a/flamecord/src/main/java/dev/_2lstudios/flamecord/configuration/FlameConfig.java
+++ b/flamecord/src/main/java/dev/_2lstudios/flamecord/configuration/FlameConfig.java
@@ -11,6 +11,10 @@ public class FlameConfig {
return (int) setIfUnexistant(arg1, (Object) arg2, configuration);
}
+ String setIfUnexistant(final String arg1, final String arg2, final Configuration configuration) {
+ return (String) setIfUnexistant(arg1, (Object) arg2, configuration);
+ }
+
boolean setIfUnexistant(final String arg1, final boolean arg2, final Configuration configuration) {
return (boolean) setIfUnexistant(arg1, (Object) arg2, configuration);
}
diff --git a/flamecord/src/main/java/dev/_2lstudios/flamecord/configuration/FlameCordConfiguration.java b/flamecord/src/main/java/dev/_2lstudios/flamecord/configuration/FlameCordConfiguration.java
index c327841a..de2cc947 100644
--- a/flamecord/src/main/java/dev/_2lstudios/flamecord/configuration/FlameCordConfiguration.java
+++ b/flamecord/src/main/java/dev/_2lstudios/flamecord/configuration/FlameCordConfiguration.java
@@ -3,15 +3,80 @@ package dev._2lstudios.flamecord.configuration;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
+import java.util.List;
+import java.util.Random;
import lombok.Getter;
+import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
public class FlameCordConfiguration extends FlameConfig {
+ private List<String> colors(final List<String> strings) {
+ for (int i = 0; i < strings.size(); i++) {
+ strings.set(i, ChatColor.translateAlternateColorCodes('&', strings.get(i)));
+ }
+
+ return strings;
+ }
+
+ public String getMOTD(final int maxPlayers, final int onlinePlayers) {
+ final String motd = motds.get(new Random().nextInt(motds.size()));
+
+ return motd.replace("%maxplayers%", String.valueOf(maxPlayers)).replace("%onlineplayers%", String.valueOf(onlinePlayers));
+ }
+
+ public String[] getSample(final int maxPlayers, final int onlinePlayers) {
+ final String sample = samples.get(new Random().nextInt(samples.size()));
+
+ return sample.replace("%maxplayers%", String.valueOf(maxPlayers)).replace("%onlineplayers%", String.valueOf(onlinePlayers)).split("\n");
+ }
+
+ public int getFakePlayersAmount(final int players) {
+ switch (fakePlayersMode) {
+ case "STATIC":
+ return fakePlayersAmount;
+ case "RANDOM":
+ return (int) (Math.floor(Math.random() * fakePlayersAmount) + 1);
+ case "DIVISION":
+ return players / fakePlayersAmount;
+ default:
+ return 0;
+ }
+ }
+
+ @Getter
+ private boolean motdEnabled = false;
+ @Getter
+ private List<String> motds = Collections.singletonList("&eDefault &cFlameCord&e custom motd!\n&eChange me in &cflamecord.yml&e file!");
+
+ @Getter
+ private boolean sampleEnabled = false;
+ @Getter
+ private List<String> samples = Collections.singletonList("&eDefault &cFlameCord&e sample!\n&eChange me in &cflamecord.yml&e file!");
+
+ @Getter
+ private boolean protocolEnabled = false;
+ @Getter
+ private String protocolName = "FlameCord 1.7-1.18";
+
+ @Getter
+ private boolean maxPlayersEnabled = false;
+ @Getter
+ private int maxPlayersAmount = 1000;
+ @Getter
+ private boolean maxPlayersOneMore = false;
+
+ @Getter
+ private boolean fakePlayersEnabled = false;
+ @Getter
+ private int fakePlayersAmount = 3;
+ private String fakePlayersMode = "DIVISION";
+
@Getter
private boolean loggerInitialhandler = false;
@Getter
@@ -36,6 +101,19 @@ public class FlameCordConfiguration extends FlameConfig {
configuration = configurationProvider.load(configurationFile);
}
+ this.motdEnabled = setIfUnexistant("custom-motd.motd.enabled", this.motdEnabled, configuration);
+ this.motds = colors(new ArrayList<>(setIfUnexistant("custom-motd.motd.motds", this.motds, configuration)));
+ this.sampleEnabled = setIfUnexistant("custom-motd.sample.enabled", this.sampleEnabled, configuration);
+ this.samples = colors(new ArrayList<>(setIfUnexistant("custom-motd.sample.samples", this.samples, configuration)));
+ this.protocolEnabled = setIfUnexistant("custom-motd.protocol.enabled", this.protocolEnabled, configuration);
+ this.protocolName = setIfUnexistant("custom-motd.protocol.name", this.protocolName, configuration);
+ this.maxPlayersEnabled = setIfUnexistant("custom-motd.maxplayers.enabled", this.maxPlayersEnabled, configuration);
+ this.maxPlayersAmount = setIfUnexistant("custom-motd.maxplayers.amount", this.maxPlayersAmount, configuration);
+ this.maxPlayersOneMore = setIfUnexistant("custom-motd.maxplayers.justonemore", this.maxPlayersOneMore, configuration);
+ this.fakePlayersEnabled = setIfUnexistant("custom-motd.fakeplayers.enabled", this.fakePlayersEnabled, configuration);
+ this.fakePlayersAmount = setIfUnexistant("custom-motd.fakeplayers.amount", this.fakePlayersAmount, configuration);
+ this.fakePlayersMode = setIfUnexistant("custom-motd.fakeplayers.mode", this.fakePlayersMode, configuration);
+
this.loggerInitialhandler = setIfUnexistant("logger.initialhandler", this.loggerInitialhandler, configuration);
this.loggerExceptions = setIfUnexistant("logger.exceptions", this.loggerExceptions, configuration);
this.loggerDump = setIfUnexistant("logger.dump", this.loggerDump, configuration);
diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
index 198086e0..d99cb5d0 100644
--- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
+++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java
@@ -23,6 +23,8 @@ import javax.crypto.spec.SecretKeySpec;
import dev._2lstudios.flamecord.FlameCord;
+import dev._2lstudios.flamecord.configuration.FlameConfig;
+import dev._2lstudios.flamecord.configuration.FlameCordConfiguration;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.md_5.bungee.BungeeCord;
@@ -273,7 +275,6 @@ public class InitialHandler extends PacketHandler implements PendingConnection
Preconditions.checkState( thisState == State.STATUS, "Not expecting STATUS" );
ServerInfo forced = AbstractReconnectHandler.getForcedHost( this );
- final String motd = ( forced != null ) ? forced.getMotd() : listener.getMotd();
final int protocol = ( ProtocolConstants.SUPPORTED_VERSION_IDS.contains( handshake.getProtocolVersion() ) ) ? handshake.getProtocolVersion() : bungee.getProtocolVersion();
Callback<ServerPing> pingBack = new Callback<ServerPing>()
@@ -336,7 +337,52 @@ public class InitialHandler extends PacketHandler implements PendingConnection
( (BungeeServerInfo) forced ).ping( pingBack, handshake.getProtocolVersion() );
} else
{
- pingBack.done( getPingInfo( motd, protocol ), null );
+ // FlameCord - Custom MOTD
+ final FlameCordConfiguration config = FlameCord.getInstance().getFlameCordConfiguration();
+
+ String motd;
+ String protocolName;
+
+ ServerPing.PlayerInfo[] sample = null;
+
+ int maxPlayers = listener.getMaxPlayers();
+ int onlinePlayers = bungee.getOnlineCount();
+
+ if (config.isFakePlayersEnabled()) {
+ onlinePlayers += config.getFakePlayersAmount(onlinePlayers);
+ }
+
+ if (config.isMaxPlayersEnabled()) {
+ maxPlayers = config.isMaxPlayersOneMore() ? onlinePlayers + 1 : config.getMaxPlayersAmount();
+ }
+
+ if (config.isMotdEnabled()) {
+ motd = config.getMOTD(maxPlayers, onlinePlayers);
+ } else {
+ motd = ( forced != null ) ? forced.getMotd() : listener.getMotd();
+ }
+
+ if (config.isProtocolEnabled()) {
+ protocolName = config.getProtocolName();
+ } else {
+ protocolName = bungee.getName() + " " + bungee.getGameVersion();
+ }
+
+ if (config.isSampleEnabled()) {
+ final UUID fakeUuid = new UUID(0, 0);
+ final String[] sampleString = config.getSample(maxPlayers, onlinePlayers);
+
+ sample = new ServerPing.PlayerInfo[sampleString.length];
+
+ for (int i = 0; i < sampleString.length; i++) {
+ sample[i] = new ServerPing.PlayerInfo(sampleString[i], fakeUuid);
+ }
+ }
+
+ pingBack.done( new ServerPing(
+ new ServerPing.Protocol( protocolName, protocol ),
+ new ServerPing.Players( maxPlayers, onlinePlayers, sample ),
+ motd, BungeeCord.getInstance().config.getFaviconObject() ), null );
}
thisState = State.PING;
--
2.37.3.windows.1