Skip to content

Commit

Permalink
refactor: rename NetworkServer to NetworkInterface and add docs for it
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Nov 28, 2024
1 parent 3ee2cd1 commit 5bf0bfb
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 104 deletions.
8 changes: 4 additions & 4 deletions api/src/main/java/org/allaymc/api/i18n/TrKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,14 @@ public interface TrKeys {
String A_NETWORK_CLIENT_DISCONNECTED = "allay:network.client.disconnected";

/**
* Network server started at %1:%2 (%3 ms)
* Network interface started at %1:%2 (%3 ms)
*/
String A_NETWORK_SERVER_STARTED = "allay:network.server.started";
String A_NETWORK_INTERFACE_STARTED = "allay:network.interface.started";

/**
* Starting up network server...
* Starting up network interface...
*/
String A_NETWORK_SERVER_STARTING = "allay:network.server.starting";
String A_NETWORK_INTERFACE_STARTING = "allay:network.interface.starting";

/**
* Pack auto encrypting is enabled, encryption is starting...
Expand Down
55 changes: 55 additions & 0 deletions api/src/main/java/org/allaymc/api/network/NetworkInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.allaymc.api.network;

/**
* NetworkInterface is used to open an interface on the network that the client can connect to.
* It also holds the information that will be shown to the client in the server list.
* <p>
* Please note that the changes in network interface won't be saved to server settings.
* For example if you set the motd on network interface, it will only put into effect before stop the server.
* That make it suitable for making dynamic motd and dynamic max player count.
*
* @author daoge_cmd
*/
public interface NetworkInterface {
/**
* Set the current motd of the server.
*
* @param motd the new motd.
*/
void setMotd(String motd);

/**
* Get the current motd of the server.
*
* @return the current motd.
*/
String getMotd();

/**
* Set the current sub-motd of the server.
*
* @param subMotd the new sub-motd.
*/
void setSubMotd(String subMotd);

/**
* Get the current sub-motd of the server.
*
* @return the current sub-motd.
*/
String getSubMotd();

/**
* Set the current max player count of the server.
*
* @param maxPlayerCount the new max player count.
*/
void setMaxPlayerCount(int maxPlayerCount);

/**
* Get the current max player count of the server.
*
* @return the current max player count.
*/
int getMaxPlayerCount();
}
47 changes: 0 additions & 47 deletions api/src/main/java/org/allaymc/api/network/NetworkServer.java

This file was deleted.

8 changes: 4 additions & 4 deletions api/src/main/java/org/allaymc/api/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.allaymc.api.i18n.MayContainTrKey;
import org.allaymc.api.i18n.TrContainer;
import org.allaymc.api.i18n.TrKeys;
import org.allaymc.api.network.NetworkServer;
import org.allaymc.api.network.NetworkInterface;
import org.allaymc.api.plugin.PluginManager;
import org.allaymc.api.scheduler.Scheduler;
import org.allaymc.api.scheduler.TaskCreator;
Expand Down Expand Up @@ -144,11 +144,11 @@ default int getOnlinePlayerCount() {
}

/**
* Get the network server.
* Get the network interface.
*
* @return the network server.
* @return the network interface.
*/
NetworkServer getNetworkServer();
NetworkInterface getNetworkInterface();

/**
* Get the online players.
Expand Down
4 changes: 2 additions & 2 deletions data/resources/lang/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
"allay:lang.set": "Server language was set to %1",
"allay:network.client.connected": "Client connected, IP: %1",
"allay:network.client.disconnected": "Client disconnected, IP: %1",
"allay:network.server.started": "Network server started at %1:%2 (%3 ms)",
"allay:network.server.starting": "Starting up network server...",
"allay:network.interface.started": "Network interface started at %1:%2 (%3 ms)",
"allay:network.interface.starting": "Starting up network interface...",
"allay:pack.autoencrypt.enabled": "Pack auto encrypting is enabled, encryption is starting...",
"allay:pack.encrypted": "Pack %1 is encrypted. Key: %2",
"allay:pack.encrypting": "Encrypting pack %1",
Expand Down
4 changes: 2 additions & 2 deletions data/resources/lang/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@
"allay:lang.set": "服务器语言已设置为 %1",
"allay:network.client.connected": "客户端已连接, 地址: %1",
"allay:network.client.disconnected": "客户端断开连接, 地址: %1",
"allay:network.server.started": "网络服务器已在 %1:%2 启动 (%3 毫秒)",
"allay:network.server.starting": "启动网络服务器中...",
"allay:network.interface.started": "网络接口已在 %1:%2 启动 (%3 毫秒)",
"allay:network.interface.starting": "启动网络接口中...",
"allay:pack.autoencrypt.enabled": "资源包自动加密已开启,准备加密资源包...",
"allay:pack.encrypted": "资源包 %1 已加密。密钥为: %2",
"allay:pack.encrypting": "正在加密资源包 %1",
Expand Down
4 changes: 2 additions & 2 deletions data/resources/unpacked/lang_raw/allay/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"server.stopped": "Server stopped",
"lang.set": "Server language was set to %1",
"api.implemented": "This server is running §b%1§r (§a%2§r), implement §ballay-api§r version §a%3",
"network.server.starting": "Starting up network server...",
"network.server.started": "Network server started at %1:%2 (%3 ms)",
"network.interface.starting": "Starting up network interface...",
"network.interface.started": "Network interface started at %1:%2 (%3 ms)",
"network.client.connected": "Client connected, IP: %1",
"network.client.disconnected": "Client disconnected, IP: %1",
"blocktype.loading": "Loading block types...",
Expand Down
4 changes: 2 additions & 2 deletions data/resources/unpacked/lang_raw/allay/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"server.stopped": "服务器已关闭",
"lang.set": "服务器语言已设置为 %1",
"api.implemented": "此服务器正在运行 §b%1§r (§a%2§r), 实现§ballay-api§r版本 §a%3",
"network.server.starting": "启动网络服务器中...",
"network.server.started": "网络服务器已在 %1:%2 启动 (%3 毫秒)",
"network.interface.starting": "启动网络接口中...",
"network.interface.started": "网络接口已在 %1:%2 启动 (%3 毫秒)",
"network.client.connected": "客户端已连接, 地址: %1",
"network.client.disconnected": "客户端断开连接, 地址: %1",
"blocktype.loading": "正在加载方块类型...",
Expand Down
21 changes: 10 additions & 11 deletions server/src/main/java/org/allaymc/server/AllayServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.allaymc.api.i18n.TrKeys;
import org.allaymc.api.math.location.Location3f;
import org.allaymc.api.math.location.Location3fc;
import org.allaymc.api.network.NetworkServer;
import org.allaymc.api.network.NetworkInterface;
import org.allaymc.api.permission.DefaultPermissions;
import org.allaymc.api.permission.tree.PermissionTree;
import org.allaymc.api.plugin.PluginManager;
Expand All @@ -41,7 +41,7 @@
import org.allaymc.server.client.storage.AllayNBTFilePlayerStorage;
import org.allaymc.server.eventbus.AllayEventBus;
import org.allaymc.server.metrics.Metrics;
import org.allaymc.server.network.AllayNetworkServer;
import org.allaymc.server.network.AllayNetworkInterface;
import org.allaymc.server.plugin.AllayPluginManager;
import org.allaymc.server.scheduler.AllayScheduler;
import org.allaymc.server.scroreboard.storage.JsonScoreboardStorage;
Expand Down Expand Up @@ -73,7 +73,6 @@
public final class AllayServer implements Server {

private static final AllayServer INSTANCE = new AllayServer();
;
private static final String BAN_INFO_FILE_NAME = "ban-info.yml";
private static final String WHITELIST_FILE_NAME = "whitelist.yml";
private static final CommandOriginData SERVER_COMMAND_ORIGIN_DATA = new CommandOriginData(CommandOriginType.DEDICATED_SERVER, UUID.randomUUID(), "", 0);
Expand Down Expand Up @@ -102,7 +101,7 @@ public final class AllayServer implements Server {
@Getter
private final Scheduler scheduler;
@Getter
private final NetworkServer networkServer;
private final NetworkInterface networkInterface;
private final AllayTerminalConsole terminalConsole;
private final GameLoop gameLoop;

Expand All @@ -129,7 +128,7 @@ private AllayServer() {
this.whitelist = ConfigManager.create(Whitelist.class, Utils.createConfigInitializer(Path.of(WHITELIST_FILE_NAME)));
this.pluginManager = new AllayPluginManager();
this.scheduler = new AllayScheduler(virtualThreadPool);
this.networkServer = new AllayNetworkServer(this);
this.networkInterface = new AllayNetworkInterface(this);
this.terminalConsole = new AllayTerminalConsole(AllayServer.this);
this.gameLoop = GameLoop.builder()
.loopCountPerSec(20)
Expand Down Expand Up @@ -202,11 +201,11 @@ public void start(long initialTime) {
this.scoreboardService.read();
((AllayPluginManager) pluginManager).enablePlugins();

sendTr(TrKeys.A_NETWORK_SERVER_STARTING);
this.networkServer.start();
sendTr(TrKeys.A_NETWORK_INTERFACE_STARTING);
((AllayNetworkInterface) this.networkInterface).start();
this.startTime = System.currentTimeMillis();
sendTr(
TrKeys.A_NETWORK_SERVER_STARTED,
TrKeys.A_NETWORK_INTERFACE_STARTED,
SETTINGS.networkSettings().ip(),
String.valueOf(SETTINGS.networkSettings().port()),
String.valueOf(startTime - initialTime)
Expand Down Expand Up @@ -242,7 +241,7 @@ public void shutdown() {

private void shutdownReally() {
// Shutdown network server to prevent new client connecting to the server
this.networkServer.shutdown();
((AllayNetworkInterface) this.networkInterface).shutdown();
this.scheduler.shutdown();

new ServerStopEvent().call();
Expand Down Expand Up @@ -280,7 +279,7 @@ public boolean isValid() {

public void onLoggedIn(EntityPlayer player) {
players.put(player.getUUID(), player);
networkServer.setPlayerCount(players.size());
((AllayNetworkInterface) networkInterface).setPlayerCount(players.size());
Server.getInstance().broadcastTr(TextFormat.YELLOW + "%" + TrKeys.M_MULTIPLAYER_PLAYER_JOINED, player.getOriginName());
}

Expand All @@ -299,7 +298,7 @@ public void onDisconnect(EntityPlayer player) {
removeFromPlayerList(player);
}

networkServer.setPlayerCount(players.size());
((AllayNetworkInterface) networkInterface).setPlayerCount(players.size());
}

public void addToPlayerList(EntityPlayer player) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void prepareCommandTree(CommandTree tree) {
var joiner = new StringJoiner(", ");
players.forEach(player -> joiner.add(player.getDisplayName()));

context.getSender().sendTr(TrKeys.M_COMMANDS_PLAYERS_LIST, players.size(), server.getNetworkServer().getMaxPlayerCount());
context.getSender().sendTr(TrKeys.M_COMMANDS_PLAYERS_LIST, players.size(), server.getNetworkInterface().getMaxPlayerCount());
context.getSender().sendText(joiner.toString());
return context.success();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void prepareCommandTree(CommandTree tree) {
maxPlayers = Math.max(Server.getInstance().getOnlinePlayerCount(), maxPlayers);

Server.SETTINGS.genericSettings().maxPlayerCount(maxPlayers);
Server.getInstance().getNetworkServer().setMaxPlayerCount(maxPlayers);
Server.getInstance().getNetworkInterface().setMaxPlayerCount(maxPlayers);
context.addOutput(TrKeys.M_COMMANDS_SETMAXPLAYERS_SUCCESS, maxPlayers);

return context.success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,14 @@ protected static void printMemoryUsageInfo(CommandSender sender) {
protected static void printOnlinePlayerInfo(CommandSender sender) {
var server = Server.getInstance();
var playerColor = TextFormat.GREEN;
if (((float) server.getOnlinePlayerCount() / (float) server.getNetworkServer().getMaxPlayerCount()) > 0.90) {
if (((float) server.getOnlinePlayerCount() / (float) server.getNetworkInterface().getMaxPlayerCount()) > 0.90) {
playerColor = TextFormat.GOLD;
}
if (server.getOnlinePlayerCount() == server.getNetworkServer().getMaxPlayerCount()) {
if (server.getOnlinePlayerCount() == server.getNetworkInterface().getMaxPlayerCount()) {
playerColor = TextFormat.RED;
}

sender.sendText("Players: " + playerColor + server.getOnlinePlayers().size() + "/" + server.getNetworkServer().getMaxPlayerCount());
sender.sendText("Players: " + playerColor + server.getOnlinePlayers().size() + "/" + server.getNetworkInterface().getMaxPlayerCount());
}

protected static String toKB(long bytes) {
Expand Down
Loading

0 comments on commit 5bf0bfb

Please sign in to comment.