Guest User

Untitled

a guest
Jul 17th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package scripting.event;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import javax.script.Invocable;
  6. import javax.script.ScriptEngine;
  7. import javax.script.ScriptException;
  8. import net.channel.ChannelServer;
  9. import scripting.AbstractScriptManager;
  10.  
  11. public class EventScriptManager extends AbstractScriptManager {
  12. private Map<String,EventEntry> events = new LinkedHashMap<String,EventEntry>();
  13.  
  14. private class EventEntry {
  15. private EventEntry(String script, Invocable iv, EventManager em) {
  16. this.script = script;
  17. this.iv = iv;
  18. this.em = em;
  19. }
  20.  
  21. public String script;
  22. public Invocable iv;
  23. public EventManager em;
  24. }
  25.  
  26. public EventScriptManager(ChannelServer cserv, String[] scripts) {
  27. super();
  28. for (String script : scripts) {
  29. if (script.length() != 0) {
  30. Invocable iv = getInvocable("event/" + script + ".js", null);
  31. events.put(script, new EventEntry(script, iv, new EventManager(cserv, iv, script)));
  32. }
  33. }
  34. }
  35.  
  36. public EventManager getEventManager(String event) {
  37. EventEntry entry = events.get(event);
  38. if (entry == null) {
  39. return null;
  40. }
  41. return entry.em;
  42. }
  43.  
  44. public void init() {
  45. for (EventEntry entry : events.values()) {
  46. try {
  47. ((ScriptEngine) entry.iv).put("em", entry.em);
  48. entry.iv.invokeFunction("init", (Object) null);
  49. } catch (ScriptException ex) {
  50. System.out.println("Error " + ex);
  51. } catch (NoSuchMethodException ex) {
  52. System.out.println("Error " + ex);
  53. }
  54. }
  55. }
  56.  
  57. public void cancel() {
  58. for (EventEntry entry : events.values()) {
  59. if (entry.em != null) {
  60. entry.em.cancel();
  61. }
  62. }
  63. }
  64. }
Add Comment
Please, Sign In to add comment