Optimising BukkitTelnet

This commit is contained in:
Nathan Curran 2021-06-24 21:25:56 +10:00
parent 3436800815
commit 2c4ad8df02
No known key found for this signature in database
GPG key ID: B3A964B30C2E56B8
9 changed files with 77 additions and 90 deletions

1
.gitignore vendored
View file

@ -29,3 +29,4 @@ manifest.mf
.Trashes .Trashes
ehthumbs.db ehthumbs.db
Thumbs.db Thumbs.db
dependency-reduced-pom.xml

40
pom.xml
View file

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>me.totalfreedom</groupId> <groupId>me.totalfreedom</groupId>
@ -56,38 +57,59 @@
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.16.16</version> <version>1.18.20</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId> <artifactId>log4j-core</artifactId>
<version>2.13.2</version> <version>2.14.1</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>net.ess3</groupId> <groupId>net.ess3</groupId>
<artifactId>EssentialsX</artifactId> <artifactId>EssentialsX</artifactId>
<version>2.16.1</version> <version>2.18.2</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<!-- Compiler --> <!-- Compiler -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version> <version>3.8.1</version>
<configuration> <configuration>
<compilerVersion>1.7</compilerVersion> <source>11</source>
<source>1.7</source> <target>11</target>
<target>1.7</target>
</configuration> </configuration>
</plugin> </plugin>

View file

@ -1,10 +1,6 @@
package me.totalfreedom.bukkittelnet; package me.totalfreedom.bukkittelnet;
import java.math.BigDecimal; import com.earth2me.essentials.Essentials;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import me.totalfreedom.bukkittelnet.api.TelnetRequestDataTagsEvent; import me.totalfreedom.bukkittelnet.api.TelnetRequestDataTagsEvent;
import me.totalfreedom.bukkittelnet.api.TelnetRequestUsageEvent; import me.totalfreedom.bukkittelnet.api.TelnetRequestUsageEvent;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
@ -18,9 +14,13 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import org.json.simple.JSONArray; import org.json.JSONArray;
import org.json.simple.JSONObject; import org.json.JSONObject;
import com.earth2me.essentials.Essentials;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
public class PlayerEventListener implements Listener public class PlayerEventListener implements Listener
{ {
@ -51,7 +51,7 @@ public class PlayerEventListener implements Listener
Plugin ess = Bukkit.getPluginManager().getPlugin("Essentials"); Plugin ess = Bukkit.getPluginManager().getPlugin("Essentials");
if (ess instanceof Essentials) if (ess instanceof Essentials)
{ {
essentials = (Essentials)ess; essentials = (Essentials) ess;
return essentials; return essentials;
} }
return null; return null;
@ -88,35 +88,30 @@ public class PlayerEventListener implements Listener
{ {
final JSONArray players = new JSONArray(); final JSONArray players = new JSONArray();
final Iterator<Map.Entry<Player, Map<String, Object>>> dataTagsIt = dataTags.entrySet().iterator(); for (Map.Entry<Player, Map<String, Object>> playerMapEntry : dataTags.entrySet())
while (dataTagsIt.hasNext())
{ {
final HashMap<String, String> info = new HashMap<>(); final HashMap<String, String> info = new HashMap<>();
final Map.Entry<Player, Map<String, Object>> dataTagsEntry = dataTagsIt.next(); final Player player = playerMapEntry.getKey();
final Player player = dataTagsEntry.getKey(); final Map<String, Object> playerTags = playerMapEntry.getValue();
final Map<String, Object> playerTags = dataTagsEntry.getValue();
info.put("name", player.getName()); info.put("name", player.getName());
info.put("ip", player.getAddress().getAddress().getHostAddress()); info.put("ip", player.getAddress().getAddress().getHostAddress());
info.put("displayName", StringUtils.trimToEmpty(player.getDisplayName())); info.put("displayName", StringUtils.trimToEmpty(player.getDisplayName()));
info.put("uuid", player.getUniqueId().toString()); info.put("uuid", player.getUniqueId().toString());
final Iterator<Map.Entry<String, Object>> playerTagsIt = playerTags.entrySet().iterator(); for (Map.Entry<String, Object> playerTagsEntry : playerTags.entrySet())
while (playerTagsIt.hasNext())
{ {
final Map.Entry<String, Object> playerTagsEntry = playerTagsIt.next();
final Object value = playerTagsEntry.getValue(); final Object value = playerTagsEntry.getValue();
info.put(playerTagsEntry.getKey(), value != null ? value.toString() : "null"); info.put(playerTagsEntry.getKey(), value != null ? value.toString() : "null");
} }
players.add(info); players.put(info);
} }
final JSONObject response = new JSONObject(); final JSONObject response = new JSONObject();
response.put("players", players); response.put("players", players);
return response.toString();
return response.toJSONString();
} }
private static BukkitTask usageUpdateTask = null; private static BukkitTask usageUpdateTask = null;
@ -129,7 +124,7 @@ public class PlayerEventListener implements Listener
return; return;
} }
usageUpdateTask= new BukkitRunnable() usageUpdateTask = new BukkitRunnable()
{ {
@Override @Override
public void run() public void run()
@ -154,7 +149,7 @@ public class PlayerEventListener implements Listener
essentials = getEssentials(); essentials = getEssentials();
} }
final HashMap<String, String> info = new HashMap<>(); final JSONObject info = new JSONObject();
String cpuUsage = null; String cpuUsage = null;
String ramUsage = null; String ramUsage = null;
@ -162,14 +157,10 @@ public class PlayerEventListener implements Listener
if (essentials != null) if (essentials != null)
{ {
tps = String.valueOf(String.valueOf(new BigDecimal(essentials.getTimer().getAverageTPS()).setScale(1, RoundingMode.CEILING))); tps = String.valueOf(String.valueOf(BigDecimal.valueOf(essentials.getTimer().getAverageTPS()).setScale(1, RoundingMode.CEILING)));
} }
info.put("tps", tps); info.put("tps", tps);
return info.toString();
final JSONObject data = new JSONObject();
data.putAll(info);
return data.toJSONString();
} }
} }

View file

@ -48,14 +48,7 @@ public class SocketListener extends Thread
} }
// Remove old entries // Remove old entries
final Iterator<Entry<InetAddress, Long>> it = recentIPs.entrySet().iterator(); recentIPs.entrySet().removeIf(inetAddressLongEntry -> inetAddressLongEntry.getValue() + LISTEN_THRESHOLD_MILLIS < System.currentTimeMillis());
while (it.hasNext())
{
if (it.next().getValue() + LISTEN_THRESHOLD_MILLIS < System.currentTimeMillis())
{
it.remove();
}
}
final InetAddress addr = clientSocket.getInetAddress(); final InetAddress addr = clientSocket.getInetAddress();
if (addr == null) if (addr == null)
@ -94,7 +87,6 @@ public class SocketListener extends Thread
final ClientSession clientSession = new ClientSession(telnet, clientSocket); final ClientSession clientSession = new ClientSession(telnet, clientSocket);
clientSessions.add(clientSession); clientSessions.add(clientSession);
clientSession.start(); clientSession.start();
removeDisconnected(); removeDisconnected();
} }
@ -119,30 +111,14 @@ public class SocketListener extends Thread
public void triggerPlayerListUpdates(final String playerListData) public void triggerPlayerListUpdates(final String playerListData)
{ {
final Iterator<ClientSession> it = clientSessions.iterator(); clientSessions.forEach(session ->
session.syncTriggerPlayerListUpdate(playerListData));
while (it.hasNext())
{
final ClientSession session = it.next();
if (session != null)
{
session.syncTriggerPlayerListUpdate(playerListData);
}
}
} }
public void triggerDataUsageUpdates(final String usageData) public void triggerDataUsageUpdates(final String usageData)
{ {
final Iterator<ClientSession> it = clientSessions.iterator(); clientSessions.forEach(session ->
session.syncUsageUpdate(usageData));
while (it.hasNext())
{
final ClientSession session = it.next();
if (session != null)
{
session.syncUsageUpdate(usageData);
}
}
} }
public void stopServer() public void stopServer()

View file

@ -13,6 +13,7 @@ import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger; import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.appender.AbstractAppender; import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
public class TelnetLogAppender extends AbstractAppender public class TelnetLogAppender extends AbstractAppender
{ {
@ -23,7 +24,7 @@ public class TelnetLogAppender extends AbstractAppender
public TelnetLogAppender() public TelnetLogAppender()
{ {
super("BukkitTelnet", null, null); super("BukkitTelnet", null, null, true, Property.EMPTY_ARRAY);
this.sessions = new HashSet<>(); this.sessions = new HashSet<>();
this.dateFormat = new SimpleDateFormat("HH:mm:ss"); this.dateFormat = new SimpleDateFormat("HH:mm:ss");
@ -74,7 +75,7 @@ public class TelnetLogAppender extends AbstractAppender
continue; continue;
} }
if (session.getFilterMode() == FilterMode.NONCHAT_ONLY && chat) if (session.getFilterMode() == FilterMode.NON_CHAT_ONLY && chat)
{ {
continue; continue;
} }

View file

@ -1,22 +1,23 @@
package me.totalfreedom.bukkittelnet.api; package me.totalfreedom.bukkittelnet.api;
import java.util.List;
import me.totalfreedom.bukkittelnet.SocketListener; import me.totalfreedom.bukkittelnet.SocketListener;
import me.totalfreedom.bukkittelnet.TelnetConfigLoader.TelnetConfig; import me.totalfreedom.bukkittelnet.TelnetConfigLoader.TelnetConfig;
import me.totalfreedom.bukkittelnet.session.ClientSession; import me.totalfreedom.bukkittelnet.session.ClientSession;
import java.util.List;
public interface Server public interface Server
{ {
public void startServer(); void startServer();
public void stopServer(); void stopServer();
@Deprecated @Deprecated
public SocketListener getSocketListener(); SocketListener getSocketListener();
public TelnetConfig getConfig(); TelnetConfig getConfig();
public List<ClientSession> getSessions(); List<ClientSession> getSessions();
} }

View file

@ -18,7 +18,7 @@ public class TelnetRequestDataTagsEvent extends Event
super(!Bukkit.getServer().isPrimaryThread()); super(!Bukkit.getServer().isPrimaryThread());
for (final Player player : Bukkit.getServer().getOnlinePlayers()) for (final Player player : Bukkit.getServer().getOnlinePlayers())
{ {
dataTags.put(player, new HashMap<String, Object>()); dataTags.put(player, new HashMap<>());
} }
} }

View file

@ -23,9 +23,9 @@ import org.bukkit.scheduler.BukkitRunnable;
public final class ClientSession extends Thread public final class ClientSession extends Thread
{ {
public static final Pattern NONASCII_FILTER = Pattern.compile("[^\\x20-\\x7E]"); public static final Pattern NON_ASCII_FILTER = Pattern.compile("[^\\x20-\\x7E]");
public static final Pattern AUTH_INPUT_FILTER = Pattern.compile("[^_a-zA-Z0-9]"); public static final Pattern AUTH_INPUT_FILTER = Pattern.compile("[^_a-zA-Z0-9]");
public static final Pattern COMMAND_INPUT_FILTER = Pattern.compile("^[^_a-zA-Z0-9/\\?!\\.]+"); public static final Pattern COMMAND_INPUT_FILTER = Pattern.compile("^[^_a-zA-Z0-9/?!.]+");
// //
private final TelnetServer telnet; private final TelnetServer telnet;
private final Socket clientSocket; private final Socket clientSocket;
@ -121,17 +121,12 @@ public final class ClientSession extends Thread
synchronized (clientSocket) synchronized (clientSocket)
{ {
if (clientSocket == null)
{
return;
}
writeLine("Closing connection..."); writeLine("Closing connection...");
try try
{ {
clientSocket.close(); clientSocket.close();
} }
catch (IOException ex) catch (IOException ignored)
{ {
} }
} }
@ -174,7 +169,7 @@ public final class ClientSession extends Thread
writer.write(":" + ChatColor.stripColor(message) + "\r\n"); writer.write(":" + ChatColor.stripColor(message) + "\r\n");
writer.flush(); writer.flush();
} }
catch (IOException ex) catch (IOException ignored)
{ {
} }
} }
@ -190,7 +185,7 @@ public final class ClientSession extends Thread
{ {
writer.flush(); writer.flush();
} }
catch (IOException ex) catch (IOException ignored)
{ {
} }
} }
@ -361,7 +356,7 @@ public final class ClientSession extends Thread
{ {
Thread.sleep(2000); Thread.sleep(2000);
} }
catch (InterruptedException ex) catch (InterruptedException ignored)
{ {
} }
} }
@ -404,7 +399,7 @@ public final class ClientSession extends Thread
continue; continue;
} }
command = COMMAND_INPUT_FILTER.matcher(NONASCII_FILTER.matcher(command).replaceAll("")).replaceFirst("").trim(); command = COMMAND_INPUT_FILTER.matcher(NON_ASCII_FILTER.matcher(command).replaceAll("")).replaceFirst("").trim();
if (command.isEmpty()) if (command.isEmpty())
{ {
continue; continue;
@ -449,11 +444,11 @@ public final class ClientSession extends Thread
} }
case CHAT_ONLY: case CHAT_ONLY:
{ {
filterMode = FilterMode.NONCHAT_ONLY; filterMode = FilterMode.NON_CHAT_ONLY;
writeLine("Showing only non-chat logs."); writeLine("Showing only non-chat logs.");
break; break;
} }
case NONCHAT_ONLY: case NON_CHAT_ONLY:
{ {
filterMode = FilterMode.NONE; filterMode = FilterMode.NONE;
writeLine("Showing all logs."); writeLine("Showing all logs.");

View file

@ -4,6 +4,6 @@ public enum FilterMode
{ {
NONE, NONE,
NONCHAT_ONLY, NON_CHAT_ONLY,
CHAT_ONLY; CHAT_ONLY;
} }