+command to restart threads.

This commit is contained in:
Dereku 2024-09-24 17:16:22 +07:00
parent 56d4318dbe
commit 060afeeb36
4 changed files with 47 additions and 11 deletions

View file

@ -6,7 +6,7 @@
<groupId>net.rubeta</groupId>
<artifactId>LegacyPing</artifactId>
<version>1.7</version>
<version>1.8</version>
<packaging>jar</packaging>
<name>LegacyPing</name>

View file

@ -1,5 +1,8 @@
package net.rubeta.legacyping;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
@ -14,6 +17,10 @@ public final class LegacyPing extends JavaPlugin {
@Override
public void onEnable() {
this.saveDefaultConfig();
this.startThreads();
}
private void startThreads() {
final FileConfiguration config = this.getConfig();
final String host = config.getString("host", "0.0.0.0");
final List<Integer> ports = config.getIntegerList("port");
@ -33,4 +40,17 @@ public final class LegacyPing extends JavaPlugin {
public void onDisable() {
this.threads.forEach(ServerThread::shutdown);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!sender.isOp()) {
return true;
}
this.threads.forEach(ServerThread::shutdown);
this.threads.clear();
this.startThreads();
sender.sendMessage(ChatColor.GOLD + "Restarted");
return true;
}
}

View file

@ -72,9 +72,11 @@ public class ServerThread extends Thread {
if (!this.working.get()) {
break;
}
InetAddress address = null;
try (final Socket socket = this.serverSocket.accept()) {
final DataInputStream inputStream = new DataInputStream(socket.getInputStream());
final DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
address = socket.getInetAddress();
socket.setTrafficClass(SocketOptions.TCP_NODELAY & SocketOptions.IP_TOS);
final int packetLength = inputStream.readByte() | inputStream.readByte() << 1; // idk why it doesn't a VarInt
@ -89,6 +91,8 @@ public class ServerThread extends Thread {
this.responseForModernPing(outputStream, MODERN_RESPONSE);
} else {
this.responseForModernPing(outputStream, DISCONNECT_REASON);
socket.close();
continue;
}
outputStream.flush();
@ -106,7 +110,8 @@ public class ServerThread extends Thread {
outputStream.writeLong(inputStream.readLong()); // timestamp
} catch (SocketTimeoutException | EOFException ignore) {
} catch (Exception ex) {
this.logger.log(Level.WARNING, "Unexpectable exception in " + this.getName(), ex);
this.logger.warning("An exception occurred while communicating with " + address);
this.logger.log(Level.WARNING, "Exception in " + this.getName(), ex);
}
}
}
@ -159,17 +164,21 @@ public class ServerThread extends Thread {
return this.minecraftServer == null ? 37 : this.minecraftServer.getMaxPlayers();
}
public int readVarInt(DataInputStream in) throws IOException {
public int readVarInt(DataInputStream in) {
int i = 0;
int j = 0;
byte b0;
do {
b0 = in.readByte();
i |= (b0 & 127) << j++ * 7;
if (j > 5) {
throw new RuntimeException("VarInt too big");
}
} while ((b0 & 128) == 128);
try {
byte b0;
do {
b0 = in.readByte();
i |= (b0 & 127) << j++ * 7;
if (j > 5) {
throw new RuntimeException("VarInt too big");
}
} while ((b0 & 128) == 128);
} catch (Exception ignore) {
return 0;
}
return i;
}

View file

@ -1,3 +1,10 @@
name: LegacyPing
version: '${project.version}'
main: net.rubeta.legacyping.LegacyPing
commands:
legacyping:
permission: legacyping.command.legacyping
permissions:
legacyping.command.legacyping:
default: op