Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions docs/API-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -745,14 +745,13 @@ scload() will return the result of the last statement evaluated in the file.

### scsave() function

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

#### Parameters

Expand Down Expand Up @@ -985,8 +984,8 @@ others.
### Important

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

Modules can be loaded using relative or absolute paths. Per the CommonJS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import javax.script.Invocable;
Expand All @@ -12,7 +11,7 @@
import java.util.ArrayList;
import java.util.List;

public class ScriptCraftPlugin extends JavaPlugin implements Listener
public class ScriptCraftPlugin extends JavaPlugin
{
public boolean canary = false;
public boolean bukkit = true;
Expand Down
86 changes: 33 additions & 53 deletions src/main/js/lib/events-bukkit.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
/*global Java, exports, org, __plugin */
var bkEventPriority = org.bukkit.event.EventPriority,
bkEventExecutor = org.bukkit.plugin.EventExecutor,
bkRegisteredListener = org.bukkit.plugin.RegisteredListener;
bkHandlerList = org.bukkit.event.HandlerList,
bkPluginManager = org.bukkit.Bukkit.pluginManager;

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

function getHandlerListForEventType(eventType) {
var result = null;
var clazz = null;
if (nashorn) {
//Nashorn doesn't like when getHandlerList is in a superclass of your event
//so to avoid this problem, call getHandlerList using java.lang.reflect
//methods
clazz = eventType['class'];
result = clazz.getMethod('getHandlerList').invoke(null);
} else {
result = eventType.getHandlerList();
}

return result;
}
exports.on = function(
/* Java Class */
eventType,
Expand All @@ -29,53 +17,45 @@ exports.on = function(
/* (optional) String (HIGH, HIGHEST, LOW, LOWEST, NORMAL, MONITOR), */
priority
) {
var handlerList, regd, eventExecutor;

if (typeof priority == 'undefined') {
priority = bkEventPriority.HIGHEST;
} else {
priority = bkEventPriority[priority.toUpperCase().trim()];
}
handlerList = getHandlerListForEventType(eventType);

var result = {};
eventExecutor = new bkEventExecutor({
execute: function(l, evt) {
function cancel() {
if (evt instanceof org.bukkit.event.Cancellable) {
evt.setCancelled(true);
}
}
/*
let handlers use this.cancel() to cancel the current event
or this.unregister() to unregister from future events.
*/
var bound = {};
for (var i in result) {
bound[i] = result[i];
var eventExecutor = function(l, evt) {
function cancel() {
if (evt instanceof org.bukkit.event.Cancellable) {
evt.setCancelled(true);
}
bound.cancel = cancel;
handler.call(bound, evt, cancel);
}
});
/*
wph 20130222 issue #64 bad interaction with Essentials plugin
if another plugin tries to unregister a Listener (not a Plugin or a RegisteredListener)
then BOOM! the other plugin will throw an error because Rhino can't coerce an
equals() method from an Interface.
The workaround is to make the ScriptCraftPlugin java class a Listener.
Should only unregister() registered plugins in ScriptCraft js code.
*/
regd = new bkRegisteredListener(
__plugin,
eventExecutor,
/*
let handlers use this.cancel() to cancel the current event
or this.unregister() to unregister from future events.
*/
var bound = {};
for (var i in result) {
bound[i] = result[i];
}
bound.cancel = cancel;
handler.call(bound, evt, cancel);
};

// Create an instance of our empty Listener implementation to track the handler
var listener = new ScriptCraftListener();

bkPluginManager.registerEvent(
eventType.class,
listener,
priority,
__plugin,
false
eventExecutor,
__plugin
);
handlerList.register(regd);

result.unregister = function() {
handlerList.unregister(regd);
bkHandlerList.unregisterAll(listener);
};

return result;
};
7 changes: 2 additions & 5 deletions src/main/js/lib/events-canary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global nashorn, exports, require, Packages, __plugin*/
/*global exports, require, Packages, __plugin*/
var cmPriority = Packages.net.canarymod.plugin.Priority,
cmCanary = Packages.net.canarymod.Canary,
cmPluginListener = Packages.net.canarymod.plugin.PluginListener;
Expand Down Expand Up @@ -58,10 +58,7 @@ exports.on = function(
The workaround is to make the ScriptCraftPlugin java class a Listener.
Should only unregister() registered plugins in ScriptCraft js code.
*/
if (nashorn) {
// nashorn
eventType = require('nashorn-type')(eventType);
}
eventType = eventType.class;
regd = new cmPluginListener({});
cmHookExecutor.registerHook(
regd,
Expand Down
Loading