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
ehthumbs.db
Thumbs.db
dependency-reduced-pom.xml

40
pom.xml
View file

@ -1,5 +1,6 @@
<?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>
<groupId>me.totalfreedom</groupId>
@ -56,38 +57,59 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.16</version>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.2</version>
<version>2.14.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.ess3</groupId>
<artifactId>EssentialsX</artifactId>
<version>2.16.1</version>
<version>2.18.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 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>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<version>3.8.1</version>
<configuration>
<compilerVersion>1.7</compilerVersion>
<source>1.7</source>
<target>1.7</target>
<source>11</source>
<target>11</target>
</configuration>
</plugin>

View file

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

View file

@ -48,14 +48,7 @@ public class SocketListener extends Thread
}
// Remove old entries
final Iterator<Entry<InetAddress, Long>> it = recentIPs.entrySet().iterator();
while (it.hasNext())
{
if (it.next().getValue() + LISTEN_THRESHOLD_MILLIS < System.currentTimeMillis())
{
it.remove();
}
}
recentIPs.entrySet().removeIf(inetAddressLongEntry -> inetAddressLongEntry.getValue() + LISTEN_THRESHOLD_MILLIS < System.currentTimeMillis());
final InetAddress addr = clientSocket.getInetAddress();
if (addr == null)
@ -94,7 +87,6 @@ public class SocketListener extends Thread
final ClientSession clientSession = new ClientSession(telnet, clientSocket);
clientSessions.add(clientSession);
clientSession.start();
removeDisconnected();
}
@ -119,30 +111,14 @@ public class SocketListener extends Thread
public void triggerPlayerListUpdates(final String playerListData)
{
final Iterator<ClientSession> it = clientSessions.iterator();
while (it.hasNext())
{
final ClientSession session = it.next();
if (session != null)
{
session.syncTriggerPlayerListUpdate(playerListData);
}
}
clientSessions.forEach(session ->
session.syncTriggerPlayerListUpdate(playerListData));
}
public void triggerDataUsageUpdates(final String usageData)
{
final Iterator<ClientSession> it = clientSessions.iterator();
while (it.hasNext())
{
final ClientSession session = it.next();
if (session != null)
{
session.syncUsageUpdate(usageData);
}
}
clientSessions.forEach(session ->
session.syncUsageUpdate(usageData));
}
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.Logger;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
public class TelnetLogAppender extends AbstractAppender
{
@ -23,7 +24,7 @@ public class TelnetLogAppender extends AbstractAppender
public TelnetLogAppender()
{
super("BukkitTelnet", null, null);
super("BukkitTelnet", null, null, true, Property.EMPTY_ARRAY);
this.sessions = new HashSet<>();
this.dateFormat = new SimpleDateFormat("HH:mm:ss");
@ -74,7 +75,7 @@ public class TelnetLogAppender extends AbstractAppender
continue;
}
if (session.getFilterMode() == FilterMode.NONCHAT_ONLY && chat)
if (session.getFilterMode() == FilterMode.NON_CHAT_ONLY && chat)
{
continue;
}

View file

@ -1,22 +1,23 @@
package me.totalfreedom.bukkittelnet.api;
import java.util.List;
import me.totalfreedom.bukkittelnet.SocketListener;
import me.totalfreedom.bukkittelnet.TelnetConfigLoader.TelnetConfig;
import me.totalfreedom.bukkittelnet.session.ClientSession;
import java.util.List;
public interface Server
{
public void startServer();
void startServer();
public void stopServer();
void stopServer();
@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());
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 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 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 Socket clientSocket;
@ -121,17 +121,12 @@ public final class ClientSession extends Thread
synchronized (clientSocket)
{
if (clientSocket == null)
{
return;
}
writeLine("Closing connection...");
try
{
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.flush();
}
catch (IOException ex)
catch (IOException ignored)
{
}
}
@ -190,7 +185,7 @@ public final class ClientSession extends Thread
{
writer.flush();
}
catch (IOException ex)
catch (IOException ignored)
{
}
}
@ -361,7 +356,7 @@ public final class ClientSession extends Thread
{
Thread.sleep(2000);
}
catch (InterruptedException ex)
catch (InterruptedException ignored)
{
}
}
@ -404,7 +399,7 @@ public final class ClientSession extends Thread
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())
{
continue;
@ -449,11 +444,11 @@ public final class ClientSession extends Thread
}
case CHAT_ONLY:
{
filterMode = FilterMode.NONCHAT_ONLY;
filterMode = FilterMode.NON_CHAT_ONLY;
writeLine("Showing only non-chat logs.");
break;
}
case NONCHAT_ONLY:
case NON_CHAT_ONLY:
{
filterMode = FilterMode.NONE;
writeLine("Showing all logs.");

View file

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