Skip to content

Commit

Permalink
Implement protocol specific motd
Browse files Browse the repository at this point in the history
  • Loading branch information
linsaftw committed May 7, 2021
1 parent c385cf8 commit 4f13ced
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 31 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
<id>dmulloy2-repo</id>
<url>https://repo.dmulloy2.net/repository/public/</url>
</repository>
<repository>
<id>viaversion-repo</id>
<url>https://repo.viaversion.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
Expand All @@ -63,5 +67,10 @@
<artifactId>bungeecord-api</artifactId>
<version>1.16-R0.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>us.myles</groupId>
<artifactId>viaversion</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
</project>
3 changes: 2 additions & 1 deletion src/dev/_2lstudios/cleanmotd/bukkit/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public void onEnable() {
getCommand("cleanmotd").setExecutor(new CleanMotDCommand(variables, messages));

if (pluginManager.isPluginEnabled("ProtocolLib")) {
ProtocolLibrary.getProtocolManager().addPacketListener(new ServerInfoListener(this, variables));
ProtocolLibrary.getProtocolManager().addPacketListener(
new ServerInfoListener(this, variables, pluginManager.isPluginEnabled("ViaVersion")));
} else {
pluginManager.registerEvents(new ServerListPingListener(variables), this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
import org.bukkit.plugin.Plugin;

import dev._2lstudios.cleanmotd.bukkit.variables.Variables;
import us.myles.ViaVersion.api.Via;

public class ServerInfoListener extends PacketAdapter {
private final Variables variables;
private final boolean viaversionEnabled;

public ServerInfoListener(final Plugin plugin, final Variables variables) {
public ServerInfoListener(final Plugin plugin, final Variables variables, final boolean viaversionEnabled) {
super(plugin, ListenerPriority.HIGH, Arrays.asList(PacketType.Status.Server.OUT_SERVER_INFO),
ListenerOptions.ASYNC);
this.variables = variables;
this.viaversionEnabled = viaversionEnabled;
}

@Override
Expand All @@ -34,7 +37,7 @@ public void onPacketSending(final PacketEvent event) {

final WrappedServerPing ping = event.getPacket().getServerPings().read(0);
final String protocol = variables.getProtocol();
int maxPlayers = ping.getPlayersMaximum();
int maxPlayers = ping.getPlayersMaximum();
int onlinePlayers = ping.getPlayersOnline();

if (variables.isFakePlayersEnabled()) {
Expand All @@ -47,15 +50,21 @@ public void onPacketSending(final PacketEvent event) {
ping.setVersionName(protocol);
}

if (variables.isMaxPlayersEnabled()) {
maxPlayers = variables.isMaxPlayersJustOneMore() ? onlinePlayers + 1 : variables.getMaxPlayers();
if (variables.isMaxPlayersEnabled()) {
maxPlayers = variables.isMaxPlayersJustOneMore() ? onlinePlayers + 1 : variables.getMaxPlayers();

ping.setPlayersMaximum(maxPlayers);
}
ping.setPlayersMaximum(maxPlayers);
}

if (variables.isMotdEnabled()) {
if (viaversionEnabled) {
final int playerVersion = Via.getAPI().getPlayerVersion(event.getPlayer().getUniqueId());

if (variables.isMotdEnabled()) {
ping.setMotD(variables.getMOTD(maxPlayers, onlinePlayers));
}
ping.setMotD(variables.getMOTD(maxPlayers, onlinePlayers, String.valueOf(playerVersion)));
} else {
ping.setMotD(variables.getMOTD(maxPlayers, onlinePlayers));
}
}

if (variables.isSampleEnabled()) {
final UUID fakeUuid = new UUID(0, 0);
Expand Down
3 changes: 2 additions & 1 deletion src/dev/_2lstudios/cleanmotd/bungee/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public void onEnable() {
final Messages messages = new Messages(configurationUtil);
final PluginManager pluginManager = proxy.getPluginManager();

pluginManager.registerListener(this, new ProxyPingListener(variables));
pluginManager.registerListener(this,
new ProxyPingListener(variables, pluginManager.getPlugin("ViaVersion") != null));
pluginManager.registerCommand(this, new CleanMotDCommand("cleanmotd", variables, messages));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import net.md_5.bungee.api.plugin.Cancellable;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import us.myles.ViaVersion.api.Via;

import java.util.UUID;

public class ProxyPingListener implements Listener {
private final Variables variables;
private final boolean viaversionEnabled;

public ProxyPingListener(final Variables variables) {
public ProxyPingListener(final Variables variables, final boolean viaversionEnabled) {
this.variables = variables;
this.viaversionEnabled = viaversionEnabled;
}

@EventHandler(priority = 64)
Expand Down Expand Up @@ -42,7 +45,14 @@ public void onProxyPing(final ProxyPingEvent event) {
}

if (variables.isMotdEnabled()) {
response.setDescriptionComponent(new TextComponent(variables.getMOTD(maxPlayers, onlinePlayers)));
if (viaversionEnabled) {
final int playerVersion = Via.getAPI().getPlayerVersion(event.getConnection().getUniqueId());

response.setDescriptionComponent(
new TextComponent(variables.getMOTD(maxPlayers, onlinePlayers, String.valueOf(playerVersion))));
} else {
response.setDescriptionComponent(new TextComponent(variables.getMOTD(maxPlayers, onlinePlayers)));
}
}

if (variables.isProtocolEnabled()) {
Expand Down
45 changes: 27 additions & 18 deletions src/dev/_2lstudios/cleanmotd/bungee/variables/Variables.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
import net.md_5.bungee.config.Configuration;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import dev._2lstudios.cleanmotd.bungee.utils.ConfigurationUtil;

public class Variables {
private static final String[] DEFAULT_MOTD = new String[] { "CleanMoTD default generated MoTD",
"Whoops... No MoTD has been specified!" };

private final ConfigurationUtil configurationUtil;
final Map<String, String[]> motds = new HashMap<>();
private Collection<String> pinged = new HashSet<>();
private String[] motdMotds, sampleSamples;
private String[] sampleSamples;
private int maxPlayers, fakePlayersAmount;
private boolean motdEnabled, sampleEnabled, protocolEnabled, maxPlayersJustOneMore, maxPlayersEnabled,
fakePlayersEnabled;
Expand All @@ -26,7 +32,6 @@ public void reloadConfig() {
final Configuration configuration = configurationUtil.getConfiguration("%datafolder%/config.yml");

motdEnabled = configuration.getBoolean("motd.enabled");
motdMotds = configuration.getStringList("motd.motds").toArray(new String[0]);
sampleEnabled = configuration.getBoolean("sample.enabled");
sampleSamples = configuration.getStringList("sample.samples").toArray(new String[0]);
protocolEnabled = configuration.getBoolean("protocol.enabled");
Expand All @@ -37,32 +42,36 @@ public void reloadConfig() {
fakePlayersEnabled = configuration.getBoolean("fakeplayers.enabled");
fakePlayersAmount = configuration.getInt("fakeplayers.amount");
fakePlayersMode = configuration.getString("fakeplayers.mode");

for (int i = 0; i < motdMotds.length; i++) {
final String motd = motdMotds[i];

if (motd.contains("%centered%")) {
motdMotds[i] = replaceCentered(motd.replace("%centered%", ""));
}
}
}

// TODO: Do something!
private String replaceCentered(final String string) {
return string;
}

public boolean isMotdEnabled() {
return motdEnabled;
}

public String getMOTD(final int maxPlayers, final int onlinePlayers) {
public String getMOTD(final int maxPlayers, final int onlinePlayers, final String[] protocolMotds) {
final int randomIndex = (int) (Math.floor(Math.random() * protocolMotds.length));

return ChatColor.translateAlternateColorCodes('&',
motdMotds[(int) (Math.floor(Math.random() * motdMotds.length))]
.replace("%maxplayers%", String.valueOf(maxPlayers))
protocolMotds[randomIndex].replace("%maxplayers%", String.valueOf(maxPlayers))
.replace("%onlineplayers%", String.valueOf(onlinePlayers)));
}

public String getMOTD(final int maxPlayers, final int onlinePlayers, final String protocol) {
final String[] protocolMotds;

if (motds.containsKey(protocol)) {
protocolMotds = motds.get(protocol);
} else {
protocolMotds = motds.getOrDefault("default", DEFAULT_MOTD);
}

return getMOTD(maxPlayers, onlinePlayers, protocolMotds);
}

public String getMOTD(final int maxPlayers, final int onlinePlayers) {
return getMOTD(maxPlayers, onlinePlayers, "default");
}

public boolean isSampleEnabled() {
return sampleEnabled;
}
Expand Down

0 comments on commit 4f13ced

Please sign in to comment.