1
1
package com .comphenix .protocol ;
2
2
3
- import java .util .ArrayList ;
4
- import java .util .Deque ;
5
- import java .util .HashSet ;
6
- import java .util .Iterator ;
7
- import java .util .List ;
8
- import java .util .Set ;
9
-
10
- import javax .script .Invocable ;
11
- import javax .script .ScriptEngine ;
12
- import javax .script .ScriptEngineManager ;
13
- import javax .script .ScriptException ;
14
-
3
+ import com .comphenix .protocol .MultipleLinesPrompt .MultipleConversationCanceller ;
4
+ import com .comphenix .protocol .error .ErrorReporter ;
5
+ import com .comphenix .protocol .error .Report ;
6
+ import com .comphenix .protocol .error .ReportType ;
7
+ import com .comphenix .protocol .events .PacketEvent ;
15
8
import org .bukkit .ChatColor ;
16
9
import org .bukkit .command .CommandSender ;
17
10
import org .bukkit .conversations .Conversable ;
22
15
import org .bukkit .conversations .ConversationFactory ;
23
16
import org .bukkit .plugin .Plugin ;
24
17
25
- import com .comphenix .protocol .MultipleLinesPrompt .MultipleConversationCanceller ;
26
- import com .comphenix .protocol .error .ErrorReporter ;
27
- import com .comphenix .protocol .error .Report ;
28
- import com .comphenix .protocol .error .ReportType ;
29
- import com .comphenix .protocol .events .PacketEvent ;
18
+ import javax .script .Invocable ;
19
+ import javax .script .ScriptEngine ;
20
+ import javax .script .ScriptEngineManager ;
21
+ import javax .script .ScriptException ;
22
+ import java .util .Deque ;
23
+ import java .util .HashMap ;
24
+ import java .util .HashSet ;
25
+ import java .util .Iterator ;
26
+ import java .util .Map ;
27
+ import java .util .Set ;
30
28
31
29
/**
32
30
* A command to apply JavaScript filtering to the packet command.
@@ -48,7 +46,7 @@ public interface FilterFailedHandler{
48
46
* @param ex - the failure.
49
47
* @return TRUE to keep processing this filter, FALSE to remove it.
50
48
*/
51
- public boolean handle (PacketEvent event , Filter filter , Exception ex );
49
+ boolean handle (PacketEvent event , Filter filter , Exception ex );
52
50
}
53
51
54
52
/**
@@ -57,7 +55,7 @@ public interface FilterFailedHandler{
57
55
* @author Kristian
58
56
*/
59
57
private enum SubCommand {
60
- ADD , REMOVE ;
58
+ ADD , REMOVE
61
59
}
62
60
63
61
/**
@@ -206,13 +204,13 @@ public CompilationSuccessCanceller clone() {
206
204
private FilterFailedHandler defaultFailedHandler ;
207
205
208
206
// Currently registered filters
209
- private List < Filter > filters = new ArrayList < Filter >();
207
+ private final Map < String , Filter > filters = new HashMap < >();
210
208
211
209
// Owner plugin
212
210
private final Plugin plugin ;
213
211
214
- // Whether or not the command is enabled
215
- private ProtocolConfig config ;
212
+ // Whether the command is enabled
213
+ private final ProtocolConfig config ;
216
214
217
215
// Script engine
218
216
private ScriptEngine engine ;
@@ -227,7 +225,7 @@ public CommandFilter(ErrorReporter reporter, Plugin plugin, ProtocolConfig confi
227
225
this .uninitialized = true ;
228
226
}
229
227
230
- private void initalizeScript () {
228
+ private void initializeScript () {
231
229
try {
232
230
// First attempt
233
231
initializeEngine ();
@@ -306,7 +304,7 @@ public boolean handle(PacketEvent event, Filter filter, Exception ex) {
306
304
}
307
305
308
306
/**
309
- * Determine whether or not to pass the given packet event to the packet listeners.
307
+ * Determine whether to pass the given packet event to the packet listeners.
310
308
* <p>
311
309
* Uses a default filter failure handler that simply prints the error message and removes the filter.
312
310
* @param event - the event.
@@ -317,13 +315,13 @@ public boolean filterEvent(PacketEvent event) {
317
315
}
318
316
319
317
/**
320
- * Determine whether or not to pass the given packet event to the packet listeners.
318
+ * Determine whether to pass the given packet event to the packet listeners.
321
319
* @param event - the event.
322
320
* @param handler - failure handler.
323
321
* @return TRUE if we should, FALSE otherwise.
324
322
*/
325
323
public boolean filterEvent (PacketEvent event , FilterFailedHandler handler ) {
326
- for (Iterator <Filter > it = filters .iterator (); it .hasNext (); ) {
324
+ for (Iterator <Filter > it = filters .values (). iterator (); it .hasNext (); ) {
327
325
Filter filter = it .next ();
328
326
329
327
try {
@@ -347,7 +345,7 @@ private void checkScriptStatus() {
347
345
// Start the engine
348
346
if (uninitialized ) {
349
347
uninitialized = false ;
350
- initalizeScript ();
348
+ initializeScript ();
351
349
}
352
350
}
353
351
@@ -370,11 +368,12 @@ protected boolean handleCommand(CommandSender sender, String[] args) {
370
368
371
369
final SubCommand command = parseCommand (args , 0 );
372
370
final String name = args [1 ];
373
-
371
+ final String lowerCaseName = name .toLowerCase ();
372
+
374
373
switch (command ) {
375
374
case ADD :
376
375
// Never overwrite an existing filter
377
- if ( findFilter ( name ) != null ) {
376
+ if ( filters . containsKey ( lowerCaseName ) ) {
378
377
sender .sendMessage (ChatColor .RED + "Filter " + name + " already exists. Remove it first." );
379
378
return true ;
380
379
}
@@ -402,8 +401,8 @@ public void conversationAbandoned(ConversationAbandonedEvent event) {
402
401
final Conversable whom = event .getContext ().getForWhom ();
403
402
404
403
if (event .gracefulExit ()) {
405
- String predicate = prompt .removeAccumulatedInput (event .getContext ());
406
- Filter filter = new Filter (name , predicate , packets );
404
+ final String predicate = prompt .removeAccumulatedInput (event .getContext ());
405
+ final Filter filter = new Filter (name , predicate , packets );
407
406
408
407
// Print the last line as well
409
408
whom .sendRawMessage (prompt .getPromptText (event .getContext ()));
@@ -412,7 +411,7 @@ public void conversationAbandoned(ConversationAbandonedEvent event) {
412
411
// Force early compilation
413
412
filter .compile (engine );
414
413
415
- filters .add ( filter );
414
+ filters .put ( lowerCaseName , filter );
416
415
whom .sendRawMessage (ChatColor .GOLD + "Added filter " + name );
417
416
} catch (ScriptException e ) {
418
417
e .printStackTrace ();
@@ -438,12 +437,12 @@ public void conversationAbandoned(ConversationAbandonedEvent event) {
438
437
break ;
439
438
440
439
case REMOVE :
441
- Filter filter = findFilter ( name );
440
+ final Filter filter = filters . get ( lowerCaseName );
442
441
443
442
// See if it exists before we remove it
444
443
if (filter != null ) {
445
444
filter .close (engine );
446
- filters .remove (filter );
445
+ filters .remove (lowerCaseName );
447
446
sender .sendMessage (ChatColor .GOLD + "Removed filter " + name );
448
447
} else {
449
448
sender .sendMessage (ChatColor .RED + "Unable to find a filter by the name " + name );
@@ -453,22 +452,7 @@ public void conversationAbandoned(ConversationAbandonedEvent event) {
453
452
454
453
return true ;
455
454
}
456
-
457
- /**
458
- * Lookup a filter by its name.
459
- * @param name - the filter name.
460
- * @return The filter, or NULL if not found.
461
- */
462
- private Filter findFilter (String name ) {
463
- // We'll just use a linear scan for now - we don't expect that many filters
464
- for (Filter filter : filters ) {
465
- if (filter .getName ().equalsIgnoreCase (name )) {
466
- return filter ;
467
- }
468
- }
469
- return null ;
470
- }
471
-
455
+
472
456
private SubCommand parseCommand (String [] args , int index ) {
473
457
String text = args [index ].toUpperCase ();
474
458
0 commit comments