-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MOTD system for bungee, fixed up some spigot MOTD features
- Loading branch information
1 parent
7348f4f
commit b92e7b1
Showing
19 changed files
with
1,144 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
bungee/src/main/java/net/cybercake/cyberapi/bungee/server/serverlist/ServerListInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package net.cybercake.cyberapi.bungee.server.serverlist; | ||
|
||
import net.cybercake.cyberapi.bungee.server.serverlist.managers.MOTDManager; | ||
import net.cybercake.cyberapi.bungee.server.serverlist.managers.PlayerListManager; | ||
import net.cybercake.cyberapi.bungee.server.serverlist.managers.ProtocolManager; | ||
|
||
public class ServerListInfo { | ||
|
||
/** | ||
* @deprecated Please use {@link ServerListInfo#serverListInfo()} or | ||
*/ | ||
@SuppressWarnings({"all"}) | ||
@Deprecated | ||
public ServerListInfo() { } | ||
|
||
private static ServerListInfo serverListInfo = null; | ||
|
||
/** | ||
* Gets an instance of {@link ServerListInfo} | ||
* @return the {@link ServerListInfo} instance | ||
* @since 3.5 | ||
*/ | ||
public static ServerListInfo serverListInfo() { | ||
if(serverListInfo == null) serverListInfo = new ServerListInfo(); | ||
return new ServerListInfo(); | ||
} | ||
|
||
/** | ||
* Gets an instance of MOTD manager, please use this method instead of instantiating {@link MOTDManager} | ||
* <br> | ||
* This class if for managing the MOTD, allowing you to modify it to what you please | ||
* @return the {@link MOTDManager} instance | ||
* @since 3.5 | ||
*/ | ||
public MOTDManager getMOTDManager() { | ||
return MOTDManager.motdManager(); | ||
} | ||
|
||
/** | ||
* Gets an instance of Player List manager, please use this method instead of instantiating {@link PlayerListManager} | ||
* <br> | ||
* This class is for managing the player list, including player count, max player count, and players online hover | ||
* @return the {@link PlayerListManager} instance | ||
* @since 3.5 | ||
*/ | ||
public PlayerListManager getPlayerListManager() { | ||
return PlayerListManager.playerListManager(); | ||
} | ||
|
||
/** | ||
* Gets an instance of Protocol manager, please use this method instead of instantiating {@link ProtocolManager} | ||
* <br> | ||
* This class is for managing the protocol number and version name, for example, if a server is outdated, it'll show "Paper 1.18" or something, and you can change that by using this class | ||
* @return the {@link ProtocolManager} instance | ||
* @since 3.5 | ||
*/ | ||
public ProtocolManager getProtocolManager() { return ProtocolManager.protocolManager(); } | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
...src/main/java/net/cybercake/cyberapi/bungee/server/serverlist/ServerListInfoListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package net.cybercake.cyberapi.bungee.server.serverlist; | ||
|
||
import net.cybercake.cyberapi.bungee.CyberAPI; | ||
import net.cybercake.cyberapi.bungee.chat.UChat; | ||
import net.cybercake.cyberapi.bungee.server.serverlist.motd.MOTD; | ||
import net.md_5.bungee.api.ChatColor; | ||
import net.md_5.bungee.api.Favicon; | ||
import net.md_5.bungee.api.ProxyServer; | ||
import net.md_5.bungee.api.ServerPing; | ||
import net.md_5.bungee.api.connection.PendingConnection; | ||
import net.md_5.bungee.api.event.ProxyPingEvent; | ||
import net.md_5.bungee.api.plugin.Listener; | ||
import net.md_5.bungee.event.EventHandler; | ||
|
||
import javax.imageio.ImageIO; | ||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.UUID; | ||
|
||
public class ServerListInfoListener implements Listener { | ||
|
||
private static final HashMap<File, Favicon> faviconsFromFile = new HashMap<>(); | ||
private static final HashMap<URL, Favicon> faviconsFromURL = new HashMap<>(); | ||
|
||
@EventHandler | ||
public void onPing(ProxyPingEvent event) { | ||
ServerPing ping = handlePing(event.getConnection(), event.getResponse()); | ||
if(ping == null) return; | ||
event.setResponse(ping); | ||
} | ||
|
||
private ServerPing handlePing(PendingConnection address, ServerPing ping) { | ||
try { | ||
ServerListInfo info = CyberAPI.getInstance().getServerListInfo(); | ||
ServerListPingEvent serverListPingEvent = | ||
new ServerListPingEvent( | ||
address, | ||
info.getProtocolManager().getVersionName(), | ||
info.getProtocolManager().getProtocolNumber(), | ||
info.getPlayerListManager().shouldShowPlayers(), | ||
info.getProtocolManager().shouldAlwaysShowVersion(), | ||
info.getMOTDManager().getRandomMOTD(), | ||
info.getPlayerListManager().getMaxPlayers(), | ||
info.getPlayerListManager().getOnlinePlayers(), | ||
info.getPlayerListManager().getCustomOnlinePlayers() | ||
); | ||
if(serverListPingEvent.isCancelled()) return null; | ||
ProxyServer.getInstance().getPluginManager().callEvent(serverListPingEvent); | ||
|
||
// MOTD | ||
MOTD motd = (serverListPingEvent.getMOTD() == null ? MOTD.builder("default_temp").build() : serverListPingEvent.getMOTD()); | ||
ping.setDescriptionComponent(UChat.bComponent(motd.getFormattedMOTD())); | ||
try { | ||
Favicon image = null; | ||
switch(motd.getMOTDIconType()) { | ||
case FILE -> { | ||
// image = Favicon.create(Base64.getEncoder().encodeToString(Files.readAllBytes(motd.getFileIcon().toPath()))); | ||
if(motd.getFileIcon() != null && faviconsFromFile.get(motd.getFileIcon()) == null) { | ||
CyberAPI.getInstance().getAPILogger().verbose("New icon found (type=FILE)! Caching into temporary storage..."); | ||
CyberAPI.getInstance().getAPILogger().verbose("This may take a second, and Bungee will likely provide a 'Listener took <x>ms...', just ignore it!"); | ||
image = Favicon.create(ImageIO.read(motd.getFileIcon())); | ||
faviconsFromFile.put(motd.getFileIcon(), image); | ||
} else if(faviconsFromFile.get(motd.getFileIcon()) != null) { | ||
image = faviconsFromFile.get(motd.getFileIcon()); | ||
} | ||
} | ||
case URL -> { | ||
if(motd.getURLIcon() != null && faviconsFromURL.get(motd.getURLIcon()) == null) { | ||
CyberAPI.getInstance().getAPILogger().verbose("New icon found (type=URL)! Caching into temporary storage..."); | ||
CyberAPI.getInstance().getAPILogger().verbose("This may take a second, and Bungee will likely provide a 'Listener took <x>ms...', just ignore it!"); | ||
HttpURLConnection connection = (HttpURLConnection) motd.getURLIcon().openConnection(); | ||
connection.connect(); | ||
BufferedImage bufferedImage = ImageIO.read(connection.getInputStream()); | ||
connection.disconnect(); | ||
image = Favicon.create(bufferedImage); | ||
faviconsFromURL.put(motd.getURLIcon(), image); | ||
} else if(faviconsFromURL.get(motd.getURLIcon()) != null) { | ||
image = faviconsFromURL.get(motd.getURLIcon()); | ||
} | ||
} | ||
} | ||
if(image != null) ping.setFavicon(image); | ||
} catch (Exception exception) { | ||
CyberAPI.getInstance().getAPILogger().error("An exception occurred whilst creating the Favicon for the server: " + ChatColor.DARK_GRAY + exception); | ||
CyberAPI.getInstance().getAPILogger().verboseException(exception); | ||
} | ||
|
||
// player count | ||
ArrayList<ServerPing.PlayerInfo> profiles = new ArrayList<>(); | ||
for(String name : serverListPingEvent.getOnlinePlayers()) { | ||
profiles.add(new ServerPing.PlayerInfo(name, UUID.randomUUID())); | ||
} | ||
ping.setPlayers(new ServerPing.Players(serverListPingEvent.getMaxPlayers(), (serverListPingEvent.isPlayerListVisible() ? serverListPingEvent.getOnlinePlayerCount() : Integer.MIN_VALUE), (serverListPingEvent.isPlayerListVisible() ? profiles.toArray(new ServerPing.PlayerInfo[]{}) : null))); | ||
|
||
ping.setVersion(new ServerPing.Protocol(UChat.chat(serverListPingEvent.getVersionName()), (serverListPingEvent.isVersionNameAlwaysVisible() ? 0 : (serverListPingEvent.getProtocolVersion() == Integer.MIN_VALUE ? ProxyServer.getInstance().getProtocolVersion() : serverListPingEvent.getProtocolVersion())))); | ||
|
||
return ping; | ||
} catch (Exception exception){ | ||
CyberAPI.getInstance().getAPILogger().error("An exception occurred whilst sending server ping packet: " + ChatColor.DARK_GRAY + exception); | ||
CyberAPI.getInstance().getAPILogger().verboseException(exception); | ||
} | ||
return null; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ee/src/main/java/net/cybercake/cyberapi/bungee/server/serverlist/ServerListPingEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package net.cybercake.cyberapi.bungee.server.serverlist; | ||
|
||
import net.cybercake.cyberapi.bungee.CyberAPI; | ||
import net.cybercake.cyberapi.bungee.server.serverlist.motd.MOTD; | ||
import net.md_5.bungee.api.connection.PendingConnection; | ||
import net.md_5.bungee.api.plugin.Cancellable; | ||
import net.md_5.bungee.api.plugin.Event; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* The server list ping event for CyberAPI, an alternative way to change the server list. It is, however, still recommended that you use {@link CyberAPI#getServerListInfo()} instead! | ||
*/ | ||
@SuppressWarnings({"unused"}) | ||
public class ServerListPingEvent extends Event implements Cancellable { | ||
private boolean cancelled; | ||
|
||
private final PendingConnection connection; | ||
|
||
private String versionName; | ||
private int protocolVersion; | ||
private boolean versionNameAlwaysVisible; | ||
private boolean playerListVisible; | ||
private MOTD motd; | ||
private int maxPlayers; | ||
private int onlinePlayerCount; | ||
private List<String> onlinePlayers; | ||
|
||
public ServerListPingEvent(PendingConnection connection, String versionName, int protocolVersion, boolean playerListVisible, boolean versionNameAlwaysVisible, MOTD motd, int maxPlayers, int onlinePlayerCount, List<String> onlinePlayers) { | ||
this.connection = connection; | ||
|
||
this.versionName = versionName; | ||
this.protocolVersion = protocolVersion; | ||
this.playerListVisible = playerListVisible; | ||
this.versionNameAlwaysVisible = versionNameAlwaysVisible; | ||
this.motd = motd; | ||
this.maxPlayers = maxPlayers; | ||
this.onlinePlayerCount = onlinePlayerCount; | ||
this.onlinePlayers = onlinePlayers; | ||
this.cancelled = false; | ||
} | ||
|
||
public void setVersionName(String versionName) { this.versionName = versionName; } | ||
public void setProtocolVersion(int protocolVersion) { this.protocolVersion = protocolVersion; } | ||
public void setPlayerListVisible(boolean playerListVisible) { this.playerListVisible = playerListVisible; } | ||
public void setVersionNameAlwaysVisible(boolean versionNameAlwaysVisible) { this.versionNameAlwaysVisible = versionNameAlwaysVisible; } | ||
public void setMOTD(MOTD motd) { this.motd = motd; } | ||
public void setMaxPlayers(int maxPlayers) { this.maxPlayers = maxPlayers; } | ||
public void setOnlinePlayerCount(int onlinePlayerCount) { this.onlinePlayerCount = onlinePlayerCount; } | ||
public void setOnlinePlayers(List<String> onlinePlayers) { this.onlinePlayers = onlinePlayers; } | ||
public void setOnlinePlayers(String... onlinePlayers) { this.onlinePlayers = new ArrayList<>(List.of(onlinePlayers)); } | ||
|
||
public PendingConnection getPendingConnection() { return connection; } | ||
|
||
public String getVersionName() { return this.versionName; } | ||
public int getProtocolVersion() { return this.protocolVersion; } | ||
public boolean isPlayerListVisible() { return this.playerListVisible; } | ||
public boolean isVersionNameAlwaysVisible() { return this.versionNameAlwaysVisible; } | ||
public MOTD getMOTD() { return this.motd; } | ||
public int getMaxPlayers() { return this.maxPlayers; } | ||
public int getOnlinePlayerCount() { return this.onlinePlayerCount; } | ||
public List<String> getOnlinePlayers() { return onlinePlayers; } | ||
|
||
@Override public boolean isCancelled() { return cancelled; } | ||
@Override public void setCancelled(boolean cancelled) { this.cancelled = cancelled; } | ||
} |
Oops, something went wrong.