Make events and items work with JRE 6, 7 and 8

This commit is contained in:
walterhiggins 2014-10-19 15:41:06 +01:00
parent c36cd97c99
commit fde20f6fa2
3 changed files with 84 additions and 38 deletions

View file

@ -19,6 +19,10 @@ import net.canarymod.Canary;
import net.canarymod.api.inventory.recipes.CraftingRecipe;
import net.canarymod.api.inventory.recipes.RecipeRow;
import net.canarymod.api.inventory.Item;
// event help stuff
import net.canarymod.hook.Dispatcher;
import net.canarymod.plugin.PluginListener;
import net.canarymod.hook.Hook;
public class ScriptCraftPlugin extends Plugin implements PluginListener, CommandListener
{
@ -60,6 +64,16 @@ public class ScriptCraftPlugin extends Plugin implements PluginListener, Command
}
return true;
}
public static interface IDispatcher {
public void execute(PluginListener listener, Hook hook);
}
public Dispatcher getDispatcher(final IDispatcher impl){
return new Dispatcher(){
public void execute(PluginListener listener, Hook hook){
impl.execute(listener, hook);
}
};
}
public CraftingRecipe makeShapedRecipe(Item resultingItem, RecipeRow... rows){
CraftingRecipe result = new CraftingRecipe(resultingItem, rows);
return result;
@ -116,7 +130,7 @@ public class ScriptCraftPlugin extends Plugin implements PluginListener, Command
/*
groupmod permission add visitors canary.jsp
groupmod permission add visitors canary.command.jsp
*/
*/
@Command(
aliases = { "jsp" },
description = "Run javascript-provided command",

View file

@ -23,9 +23,13 @@ exports.on = function(
}
var result = { };
eventExecutor = new cmDispatcher( {
execute: function (l, evt) {
handler.call(result, evt );
eventExecutor = __plugin.getDispatcher( function(l,e){
try {
handler.call(result, e);
} catch ( error ){
console.log('Error while executing handler:' + handler +
' for event type:' + eventType +
' error: ' + error);
}
});
/*
@ -36,8 +40,15 @@ exports.on = function(
The workaround is to make the ScriptCraftPlugin java class a Listener.
Should only unregister() registered plugins in ScriptCraft js code.
*/
try {
// nashorn
eventType = eventType.class;
} catch ( e ){
// non-nashorn
eventType = eventType;
}
regd = new cmPluginListener({});
cmHookExecutor.registerHook(regd, __plugin, eventType.class, eventExecutor, priority);
cmHookExecutor.registerHook(regd, __plugin, eventType, eventExecutor, priority);
result.unregister = function(){
cmHookExecutor.unregisterPluginListener(regd);
};

View file

@ -8,41 +8,62 @@ function items( material, amount ) {
result.amount = amount;
return result;
}
var materials = ItemType.class.getDeclaredFields();
for (var i = 0;i < materials.length; i++ ){
if (materials[i].type != ItemType.class) {
continue;
}
var materialField = materials[i];
var name = (''+materialField.name);
name = name.replace(/^(.)/,function(a){
return a.toLowerCase();
});
items[name] = (function(material){
return function(amount){
if (typeof amount == 'undefined'){
return material;
}
if (typeof amount == 'number'){
var itemStack = itemFactory["newItem(net.canarymod.api.inventory.ItemType)"](material);
itemStack.amount = amount;
return itemStack;
} else {
var result = (amount == material);
if (!result){
if (amount.getId && amount.getData){
var m2 = ItemType.fromIdAndData(amount.id, amount.data);
result = (m2 == material);
}
function getMaterialHandler( material ){
return function(amount){
if (typeof amount == 'undefined'){
return material;
}
if (typeof amount == 'number'){
var itemStack = itemFactory["newItem(net.canarymod.api.inventory.ItemType)"](material);
itemStack.amount = amount;
return itemStack;
} else {
var result = (amount == material);
if (!result){
if (amount.getId && amount.getData){
var m2 = ItemType.fromIdAndData(amount.id, amount.data);
result = (m2 == material);
}
return result;
}
};
})(materialField.get(ItemType));
return result;
}
};
}
try {
/*
nashorn
*/
var itemTypeClass = ItemType.class;
var materials = itemTypeClass.getDeclaredFields();
for (var i = 0;i < materials.length; i++ ){
if (materials[i].type != itemTypeClass) {
continue;
}
var materialField = materials[i];
var name = (''+materialField.name);
name = name.replace(/^(.)/,function(a){
return a.toLowerCase();
});
items[name] = getMaterialHandler(materialField.get(ItemType));
}
} catch ( e ){
// non-nashorn
for (var field in ItemType){
if (ItemType[field] === undefined){
continue;
}
if (ItemType[field].class != ItemType){
continue;
}
var name = (''+field).replace(/^(.)/,function(a){
return a.toLowerCase();
});
console.log(name);
items[name] = getMaterialHandler(ItemType[field]);
}
}
module.exports = items;