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

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>
<artifactId>LegacyPing</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1</version>
<packaging>jar</packaging>
<name>LegacyPing</name>

View file

@ -4,28 +4,31 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
public final class LegacyPing extends JavaPlugin {
private ServerThread serverThread;
private final List<ServerThread> threads = new ArrayList<>();
@Override
public void onEnable() {
this.saveDefaultConfig();
final FileConfiguration config = this.getConfig();
try {
this.serverThread = new ServerThread(this.getServer(), config.getString("host", "0.0.0.0"), config.getInt("port", 25566));
this.serverThread.start();
} catch (IOException e) {
this.getLogger().log(Level.WARNING, "Failed to create ServerSocket", e);
this.getPluginLoader().disablePlugin(this);
final String host = config.getString("host", "0.0.0.0");
final List<Integer> ports = config.getIntegerList("port");
for (int port : ports) {
try {
this.threads.add(new ServerThread(this.getServer(), host, port));
} catch (IOException e) {
this.getLogger().log(Level.WARNING, "Failed to create ServerSocket", e);
}
}
this.threads.forEach(Thread::start);
}
@Override
public void onDisable() {
if (this.serverThread != null) {
this.serverThread.shutdown();
}
this.threads.forEach(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 {
this.minecraftServer = server;
this.setName("LegacyPing thread listening " + host + ":" + port);
final InetAddress address = InetAddress.getByName(host);
this.serverSocket = new ServerSocket(port, 10, address);
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 {
final String responseJson = MODERN_RESPONSE.replace("%max%", "" + this.minecraftServer.getMaxPlayers())
.replace("%now%", "" + this.minecraftServer.getOnlinePlayers().length)
final String responseJson = MODERN_RESPONSE.replace("%max%", String.valueOf(this.minecraftServer.getMaxPlayers()))
.replace("%now%", String.valueOf(this.minecraftServer.getOnlinePlayers().length))
.replace("%motd%", ((CraftServer) this.minecraftServer).getServer().motd);
byte[] bytes = responseJson.getBytes();
final DataOutputStream clientOutput = new DataOutputStream(outputStream);

View file

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