Skip to content

Commit a3768f6

Browse files
Merge pull request #389 from ut7/drop-rhino
Remove Rhino engine (Java < 8) support
2 parents 8d3572d + 34abca6 commit a3768f6

11 files changed

Lines changed: 77 additions & 646 deletions

File tree

docs/API-Reference.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -745,14 +745,13 @@ scload() will return the result of the last statement evaluated in the file.
745745

746746
### scsave() function
747747

748-
The scsave() function saves an in-memory javascript object to a
749-
specified file. Under the hood, scsave() uses JSON (specifically
750-
json2.js) to save the object. There will usually be no need to call
751-
this function directly - If you want to have a javascript object
752-
automatically loaded at startup and saved on shutdown then use the
753-
`persist()` module. The `persist()` module uses scsave and scload
754-
under the hood. Any in-memory object saved using the `scsave()`
755-
function can later be restored using the `scload()` function.
748+
The scsave() function saves an in-memory javascript object to a specified file.
749+
Under the hood, scsave() uses JSON to save the object. There will usually be no
750+
need to call this function directly - If you want to have a javascript object
751+
automatically loaded at startup and saved on shutdown then use the `persist()`
752+
module. The `persist()` module uses scsave and scload under the hood. Any
753+
in-memory object saved using the `scsave()` function can later be restored
754+
using the `scload()` function.
756755

757756
#### Parameters
758757

@@ -985,8 +984,8 @@ others.
985984
### Important
986985

987986
Although ScriptCraft now supports Node.js style modules, it does not
988-
support node modules. Node.js and Rhino are two very different
989-
Javascript environments. ScriptCraft uses Rhino Javascript, not
987+
support node modules. Node.js and Nashorn are two very different
988+
Javascript environments. ScriptCraft uses Nashorn Javascript, not
990989
Node.js. Standard Node.js modules such as `'fs'` are not available in ScriptCraft.
991990

992991
Modules can be loaded using relative or absolute paths. Per the CommonJS

src/main/java/bukkit/org/scriptcraftjs/bukkit/ScriptCraftPlugin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.bukkit.command.Command;
44
import org.bukkit.command.CommandSender;
5-
import org.bukkit.event.Listener;
65
import org.bukkit.plugin.java.JavaPlugin;
76

87
import javax.script.Invocable;
@@ -12,7 +11,7 @@
1211
import java.util.ArrayList;
1312
import java.util.List;
1413

15-
public class ScriptCraftPlugin extends JavaPlugin implements Listener
14+
public class ScriptCraftPlugin extends JavaPlugin
1615
{
1716
public boolean canary = false;
1817
public boolean bukkit = true;

src/main/js/lib/events-bukkit.js

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
/*global Java, exports, org, __plugin */
22
var bkEventPriority = org.bukkit.event.EventPriority,
3-
bkEventExecutor = org.bukkit.plugin.EventExecutor,
4-
bkRegisteredListener = org.bukkit.plugin.RegisteredListener;
3+
bkHandlerList = org.bukkit.event.HandlerList,
4+
bkPluginManager = org.bukkit.Bukkit.pluginManager;
55

6-
var nashorn = typeof Java != 'undefined';
6+
// Ask Nashorn to generate a class implementing the Listener
7+
// interface, so that we may instantiate it to tag our event
8+
// handlers.
9+
var ScriptCraftListener = Java.extend(org.bukkit.event.Listener, {});
710

8-
function getHandlerListForEventType(eventType) {
9-
var result = null;
10-
var clazz = null;
11-
if (nashorn) {
12-
//Nashorn doesn't like when getHandlerList is in a superclass of your event
13-
//so to avoid this problem, call getHandlerList using java.lang.reflect
14-
//methods
15-
clazz = eventType['class'];
16-
result = clazz.getMethod('getHandlerList').invoke(null);
17-
} else {
18-
result = eventType.getHandlerList();
19-
}
20-
21-
return result;
22-
}
2311
exports.on = function(
2412
/* Java Class */
2513
eventType,
@@ -29,53 +17,45 @@ exports.on = function(
2917
/* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */
3018
priority
3119
) {
32-
var handlerList, regd, eventExecutor;
33-
3420
if (typeof priority == 'undefined') {
3521
priority = bkEventPriority.HIGHEST;
3622
} else {
3723
priority = bkEventPriority[priority.toUpperCase().trim()];
3824
}
39-
handlerList = getHandlerListForEventType(eventType);
4025

4126
var result = {};
42-
eventExecutor = new bkEventExecutor({
43-
execute: function(l, evt) {
44-
function cancel() {
45-
if (evt instanceof org.bukkit.event.Cancellable) {
46-
evt.setCancelled(true);
47-
}
48-
}
49-
/*
50-
let handlers use this.cancel() to cancel the current event
51-
or this.unregister() to unregister from future events.
52-
*/
53-
var bound = {};
54-
for (var i in result) {
55-
bound[i] = result[i];
27+
var eventExecutor = function(l, evt) {
28+
function cancel() {
29+
if (evt instanceof org.bukkit.event.Cancellable) {
30+
evt.setCancelled(true);
5631
}
57-
bound.cancel = cancel;
58-
handler.call(bound, evt, cancel);
5932
}
60-
});
61-
/*
62-
wph 20130222 issue #64 bad interaction with Essentials plugin
63-
if another plugin tries to unregister a Listener (not a Plugin or a RegisteredListener)
64-
then BOOM! the other plugin will throw an error because Rhino can't coerce an
65-
equals() method from an Interface.
66-
The workaround is to make the ScriptCraftPlugin java class a Listener.
67-
Should only unregister() registered plugins in ScriptCraft js code.
68-
*/
69-
regd = new bkRegisteredListener(
70-
__plugin,
71-
eventExecutor,
33+
/*
34+
let handlers use this.cancel() to cancel the current event
35+
or this.unregister() to unregister from future events.
36+
*/
37+
var bound = {};
38+
for (var i in result) {
39+
bound[i] = result[i];
40+
}
41+
bound.cancel = cancel;
42+
handler.call(bound, evt, cancel);
43+
};
44+
45+
// Create an instance of our empty Listener implementation to track the handler
46+
var listener = new ScriptCraftListener();
47+
48+
bkPluginManager.registerEvent(
49+
eventType.class,
50+
listener,
7251
priority,
73-
__plugin,
74-
false
52+
eventExecutor,
53+
__plugin
7554
);
76-
handlerList.register(regd);
55+
7756
result.unregister = function() {
78-
handlerList.unregister(regd);
57+
bkHandlerList.unregisterAll(listener);
7958
};
59+
8060
return result;
8161
};

src/main/js/lib/events-canary.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*global nashorn, exports, require, Packages, __plugin*/
1+
/*global exports, require, Packages, __plugin*/
22
var cmPriority = Packages.net.canarymod.plugin.Priority,
33
cmCanary = Packages.net.canarymod.Canary,
44
cmPluginListener = Packages.net.canarymod.plugin.PluginListener;
@@ -58,10 +58,7 @@ exports.on = function(
5858
The workaround is to make the ScriptCraftPlugin java class a Listener.
5959
Should only unregister() registered plugins in ScriptCraft js code.
6060
*/
61-
if (nashorn) {
62-
// nashorn
63-
eventType = require('nashorn-type')(eventType);
64-
}
61+
eventType = eventType.class;
6562
regd = new cmPluginListener({});
6663
cmHookExecutor.registerHook(
6764
regd,

0 commit comments

Comments
 (0)