Child: [r22] (diff)

Download this file

Hooks.java    79 lines (65 with data), 2.2 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
* Provides a hook system that allows end users to trigger custom actions
* whenever a log entry is made that matches a given pattern.
*/
package script;
import definitions.is;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import main.core;
/**
*
* @author Nuno Brito, 12th of January 2013 in Darmstadt, Germany.
*/
public class Hooks {
private HashMap<String, ArrayList<Hook>> hookList = new HashMap();
/**
* Removes all installed hooks
*/
public void removeAll(){
hookList.clear();
}
/**
* @param hook The log message that triggers this action
* @param script The file pointer to a script
* @param method The method that we want to run
*/
public void addAction(String hookText, File script, String method){
// preflight check
if(script == null){
System.err.println("HK01 - Hooks cannot accept null files as input");
return;
}
Hook hook = new Hook(script, method);
// if we already a hook of this kind, get the array and add this up
if(hookList.containsKey(hookText)){
hookList.get(hookText).add(hook);
return;
}
// we have nothing, create a new one from scratch
ArrayList<Hook> array = new ArrayList();
array.add(hook);
hookList.put(hookText, array);
}
public void process(String what){
//
if(hookList.containsKey(what) == false){
return;
}
// now it seems that we do have some hooks to run, let's do this
ArrayList<Hook> selectedHooks = hookList.get(what);
for(Hook hook : selectedHooks){
//System.err.println("HK00 - Running hook " + hook.script);
core.script.run(hook.script, hook.method, is.plugin);
}
}
class Hook {
File script;
String method;
public Hook(File script, String method){
this.script = script;
this.method = method;
}
}
}