Обновление - теперь больше слушателей портов

This commit is contained in:
Dereku 2023-05-17 21:51:44 +07:00
parent d3a21672fd
commit 520e143a37
4 changed files with 20 additions and 14 deletions

View file

@ -6,7 +6,7 @@
<groupId>net.rubeta</groupId> <groupId>net.rubeta</groupId>
<artifactId>LegacyPing</artifactId> <artifactId>LegacyPing</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>LegacyPing</name> <name>LegacyPing</name>

View file

@ -4,28 +4,31 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
public final class LegacyPing extends JavaPlugin { public final class LegacyPing extends JavaPlugin {
private ServerThread serverThread; private final List<ServerThread> threads = new ArrayList<>();
@Override @Override
public void onEnable() { public void onEnable() {
this.saveDefaultConfig(); this.saveDefaultConfig();
final FileConfiguration config = this.getConfig(); final FileConfiguration config = this.getConfig();
try { final String host = config.getString("host", "0.0.0.0");
this.serverThread = new ServerThread(this.getServer(), config.getString("host", "0.0.0.0"), config.getInt("port", 25566)); final List<Integer> ports = config.getIntegerList("port");
this.serverThread.start(); for (int port : ports) {
} catch (IOException e) { try {
this.getLogger().log(Level.WARNING, "Failed to create ServerSocket", e); this.threads.add(new ServerThread(this.getServer(), host, port));
this.getPluginLoader().disablePlugin(this); } catch (IOException e) {
this.getLogger().log(Level.WARNING, "Failed to create ServerSocket", e);
}
} }
this.threads.forEach(Thread::start);
} }
@Override @Override
public void onDisable() { public void onDisable() {
if (this.serverThread != null) { this.threads.forEach(ServerThread::shutdown);
this.serverThread.shutdown();
}
} }
} }

View file

@ -20,6 +20,7 @@ public class ServerThread extends Thread {
public ServerThread(final Server server, final String host, int port) throws IOException { public ServerThread(final Server server, final String host, int port) throws IOException {
this.minecraftServer = server; this.minecraftServer = server;
this.setName("LegacyPing thread listening " + host + ":" + port);
final InetAddress address = InetAddress.getByName(host); final InetAddress address = InetAddress.getByName(host);
this.serverSocket = new ServerSocket(port, 10, address); this.serverSocket = new ServerSocket(port, 10, address);
this.serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L)); this.serverSocket.setSoTimeout((int) TimeUnit.SECONDS.toMillis(10L));
@ -93,8 +94,8 @@ public class ServerThread extends Thread {
} }
private void responseForModernPing(final OutputStream outputStream) throws IOException { private void responseForModernPing(final OutputStream outputStream) throws IOException {
final String responseJson = MODERN_RESPONSE.replace("%max%", "" + this.minecraftServer.getMaxPlayers()) final String responseJson = MODERN_RESPONSE.replace("%max%", String.valueOf(this.minecraftServer.getMaxPlayers()))
.replace("%now%", "" + this.minecraftServer.getOnlinePlayers().length) .replace("%now%", String.valueOf(this.minecraftServer.getOnlinePlayers().length))
.replace("%motd%", ((CraftServer) this.minecraftServer).getServer().motd); .replace("%motd%", ((CraftServer) this.minecraftServer).getServer().motd);
byte[] bytes = responseJson.getBytes(); byte[] bytes = responseJson.getBytes();
final DataOutputStream clientOutput = new DataOutputStream(outputStream); final DataOutputStream clientOutput = new DataOutputStream(outputStream);

View file

@ -1,2 +1,4 @@
host: '0.0.0.0' host: '0.0.0.0'
port: 25566 port:
- 25566
- 25567