-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (50 loc) · 1.65 KB
/
index.js
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
var events = require('events');
var EventDispatcher = function() {
var that = this;
that.queue = {};
that.dispatched = {};
try {
var ScriptCraftEvent = Java.type('org.eventdispatcher.events.scriptcraft.ScriptCraftEventWrapper');
that.nativeEvent = function(name) { return new ScriptCraftEvent(name); }
that.nativeEventClass = ScriptCraftEvent;
} catch(error) {
// no native event support
console.log("Warning: EventDispatcher constructed without native support. To enable, move event-dispatcher.jar to SpigotMC's plugins folder");
that.nativeEvent = null;
that.nativeEventClass = null;
// TODO add ability to unregister
}
return {
nativeEvent: that.nativeEvent,
nativeEventClass: that.nativeEventClass,
on: function(eventType, callback) {
if (events[eventType] !== undefined) {
events[eventType](callback);
} else {
if (that.dispatched[eventType] === true) {
return callback();
}
if (that.queue[eventType] === undefined) {
that.queue[eventType] = [];
}
that.queue[eventType].push(callback);
}
},
dispatch: function(eventType, eventData) {
if (that.queue[eventType] === undefined) {
return;
}
that.queue[eventType].forEach(function(callback) {
callback(eventData);
if (that.nativeEvent !== null) {
var ne = that.nativeEvent(eventType).getEvent();
ne.data = eventData;
server.getPluginManager().callEvent(ne);
}
}.bind(that));
that.queue[eventType] = [];
that.dispatched[eventType] = true;
}
}
}();
module.exports = EventDispatcher;