Added functionality for client side player list.

This commit is contained in:
StevenLawson 2014-08-16 20:33:55 -04:00
parent 369792cc1e
commit 6a9d5c9acb
8 changed files with 152 additions and 37 deletions

View file

@ -51,8 +51,7 @@
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar-with-manifest: JAR building (if you are using a manifest)
-do-jar-without-manifest: JAR building (if you are not using a manifest)
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation

View file

@ -1,6 +1,6 @@
build.xml.data.CRC32=ad7c8973
build.xml.script.CRC32=a50ca8cc
build.xml.stylesheet.CRC32=28e38971@1.56.1.46
build.xml.script.CRC32=48b261b8
build.xml.stylesheet.CRC32=8064a381@1.74.2.48
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
nbproject/build-impl.xml.data.CRC32=ad7c8973

View file

@ -52,7 +52,7 @@ javac.classpath=\
${libs.Log4J-core.classpath}:\
${libs.Log4J-api.classpath}
# Space-separated list of extra javac options
javac.compilerargs=-Xlint:unchecked
javac.compilerargs=-Xlint:unchecked -Xlint:deprecation
javac.deprecation=false
javac.processorpath=\
${javac.classpath}

View file

@ -31,6 +31,7 @@ public class BukkitTelnet extends JavaPlugin
TelnetLogger.info(plugin.getName() + " v" + plugin.getDescription().getVersion() + " enabled");
this.getServer().getPluginManager().registerEvents(new PlayerEventListener(), plugin);
}
@Override

View file

@ -0,0 +1,76 @@
package me.StevenLawson.BukkitTelnet;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class PlayerEventListener implements Listener
{
public PlayerEventListener()
{
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerJoin(PlayerJoinEvent event)
{
triggerPlayerListUpdates();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerKick(PlayerKickEvent event)
{
triggerPlayerListUpdates();
}
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event)
{
triggerPlayerListUpdates();
}
private static void triggerPlayerListUpdates()
{
new BukkitRunnable()
{
@Override
public void run()
{
final SocketListener socketListener = TelnetServer.getInstance().getSocketListener();
if (socketListener != null)
{
socketListener.triggerPlayerListUpdates(generatePlayerList());
}
}
}.runTaskLater(BukkitTelnet.plugin, 20L);
}
@SuppressWarnings("unchecked")
private static String generatePlayerList()
{
final JSONArray players = new JSONArray();
for (final Player player : Bukkit.getServer().getOnlinePlayers())
{
final HashMap<String, String> info = new HashMap<String, String>();
info.put("name", player.getName());
info.put("ip", player.getAddress().getAddress().getHostAddress());
players.add(info);
}
final JSONObject response = new JSONObject();
response.put("players", players);
return response.toJSONString();
}
}

View file

@ -50,7 +50,7 @@ public class SocketListener extends Thread
while (it.hasNext())
{
final ClientSession session = it.next();
if (!session.syncIsConnected())
{
TelnetLogAppender.getInstance().removeSession(session);
@ -59,6 +59,20 @@ 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);
}
}
}
public void stopServer()
{
try

View file

@ -81,6 +81,11 @@ public class TelnetServer
socketListener.stopServer();
}
public SocketListener getSocketListener()
{
return socketListener;
}
public static TelnetServer getInstance()
{
return TelnetServerHolder.INSTANCE;

View file

@ -37,6 +37,7 @@ public final class ClientSession extends Thread
private BufferedReader reader;
private String username;
private boolean hasTerminated;
private boolean enhancedMode = false;
public ClientSession(Socket clientSocket)
{
@ -410,58 +411,77 @@ public final class ClientSession extends Thread
}
}
private void executeTelnetCommand(String command)
private void executeTelnetCommand(final String command)
{
if (command.equalsIgnoreCase("telnet.help"))
if ("telnet.help".equalsIgnoreCase(command))
{
println("--- Telnet commands ---");
println("telnet.help - See all of the telnet commands.");
println("telnet.stop - Shut the server down.");
println("telnet.log - Change your logging settings.");
println("telnet.exit - Quit the telnet session.");
return;
}
if (command.equalsIgnoreCase("telnet.stop"))
else if ("telnet.stop".equalsIgnoreCase(command))
{
println("Shutting down the server...");
TelnetLogger.warning(username + ": Shutting down the server...");
System.exit(0);
return;
}
if (command.equalsIgnoreCase("telnet.log"))
else if ("telnet.log".equalsIgnoreCase(command))
{
if (filterMode == FilterMode.FULL)
switch (filterMode)
{
filterMode = FilterMode.CHAT_ONLY;
println("Showing only chat logs.");
return;
case FULL:
{
filterMode = FilterMode.CHAT_ONLY;
println("Showing only chat logs.");
break;
}
case CHAT_ONLY:
{
filterMode = FilterMode.NONCHAT_ONLY;
println("Showing only non-chat logs.");
break;
}
case NONCHAT_ONLY:
{
filterMode = FilterMode.FULL;
println("Showing all logs.");
break;
}
}
if (filterMode == FilterMode.CHAT_ONLY)
{
filterMode = FilterMode.NONCHAT_ONLY;
println("Showing only non-chat logs.");
return;
}
if (filterMode == FilterMode.NONCHAT_ONLY)
{
filterMode = FilterMode.FULL;
println("Showing all logs.");
return;
}
return;
}
if (command.equalsIgnoreCase("telnet.exit"))
else if ("telnet.exit".equalsIgnoreCase(command))
{
println("Goodbye <3");
println("Goodbye.");
syncTerminateSession();
}
else if ("telnet.enhanced".equalsIgnoreCase(command))
{
enhancedMode = !enhancedMode;
println((enhancedMode ? "A" : "Dea") + "ctivated enhanced mode.");
}
else
{
println("Invalid telnet command, use \"telnet.help\" to view help.");
}
}
public void syncTriggerPlayerListUpdate(String playerListData)
{
if (!enhancedMode)
{
return;
}
println("Invalid telnet command, use \"telnet.help\" to view help.");
synchronized (clientSocket)
{
if (clientSocket.isClosed())
{
return;
}
println("playerList~" + playerListData);
}
}
}