22 package com.realtime.crossfire.jxclient.scripts;
26 import java.nio.ByteBuffer;
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.regex.Pattern;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
43 private final Collection<String>
commands =
new HashSet<>();
73 public void processEmpty(@NotNull
final String command) {
80 public void processAscii(@NotNull
final String command, @NotNull
final ByteBuffer packet) {
82 final byte[] data =
new byte[packet.remaining()];
84 scriptProcess.
commandSent(
"watch "+command+
" "+
new String(data));
89 public void processShortArray(@NotNull
final String command, @NotNull
final ByteBuffer packet) {
91 final StringBuilder sb =
new StringBuilder(
"watch ");
93 for (
int i = 0; i < 100 && packet.remaining() >= 2; i++) {
95 sb.append(getShort(packet));
102 public void processIntArray(@NotNull
final String command, @NotNull
final ByteBuffer packet) {
104 final StringBuilder sb =
new StringBuilder(
"watch ");
106 while (packet.remaining() >= 4) {
108 sb.append(getInt(packet));
115 public void processShortInt(@NotNull
final String command, @NotNull
final ByteBuffer packet) {
117 scriptProcess.
commandSent(
"watch "+command+
" "+getShort(packet)+
" "+getInt(packet));
122 public void processMixed(@NotNull
final String command, @NotNull
final ByteBuffer packet) {
125 final StringBuilder sb =
new StringBuilder(
"watch ");
128 sb.append(packet.remaining());
134 public void processStats(@NotNull
final String command,
final int stat, @NotNull
final Object[] args) {
136 final StringBuilder sb =
new StringBuilder(
"watch ");
140 for (
final Object arg : args) {
149 public void processNoData(@NotNull
final String command, @NotNull
final ByteBuffer packet) {
150 processMixed(command, packet);
158 private int getShort(@NotNull
final ByteBuffer packet) {
159 return packet.getShort()&0xFFFF;
167 private int getInt(@NotNull
final ByteBuffer packet) {
168 return packet.getInt();
189 if (pattern != null) {
199 final StringBuilder sb =
new StringBuilder();
200 for (
final String command : commands) {
201 sb.append(Pattern.quote(command));
204 final int length = sb.length();
206 if (pattern != null) {
211 if (pattern == null) {
214 sb.setLength(length-1);
215 pattern = Pattern.compile(sb.toString());
224 if (commands.add(command)) {
234 if (commands.remove(command)) {
245 return pattern != null && pattern.matcher(command).matches();
void destroy()
Releases allocated resources.
boolean matchesCommand(@NotNull final CharSequence command)
Returns whether a command matches the currently watched commands.
void removePacketWatcherListener(@NotNull ReceivedPacketListener listener)
Removes a listener to be notified about received packets.
void rebuildPattern()
Rebuilds pattern from commands.
void addCommand(@NotNull final String command)
Adds a command to watch for.
static String getStatNames(final int stat)
Returns the stat name for a stat value.
final CrossfireServerConnection crossfireServerConnection
The CrossfireServerConnection to watch.
final Collection< String > commands
The commands to watch for.
final ReceivedPacketListener receivedPacketListener
The ReceivedPacketListener attached to crossfireServerConnection.
Utility class for converting stat values to stat names.
void removeCommand(@NotNull final String command)
Removes a command to watch for.
PacketWatcher(@NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final ScriptProcess scriptProcess)
Creates a new instance.
Interface for listeners interested in received packets.
Implements the "watch" function for client-sided scripts.
void addPacketWatcherListener(@NotNull ReceivedPacketListener listener)
Adds a listener to be notified about received packets.
void commandSent(@NotNull String cmd)
Sends a message to the script process.
Pattern pattern
A Pattern matching all commands.
An external command executed as a client-sided script.
Adds encoding/decoding of crossfire protocol packets to a ServerConnection.
final ScriptProcess scriptProcess
The ScriptProcess for sending commands.