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.CraftingRecipe;
import net.canarymod.api.inventory.recipes.RecipeRow; import net.canarymod.api.inventory.recipes.RecipeRow;
import net.canarymod.api.inventory.Item; 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 public class ScriptCraftPlugin extends Plugin implements PluginListener, CommandListener
{ {
@ -60,6 +64,16 @@ public class ScriptCraftPlugin extends Plugin implements PluginListener, Command
} }
return true; 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){ public CraftingRecipe makeShapedRecipe(Item resultingItem, RecipeRow... rows){
CraftingRecipe result = new CraftingRecipe(resultingItem, rows); CraftingRecipe result = new CraftingRecipe(resultingItem, rows);
return result; return result;

View file

@ -23,9 +23,13 @@ exports.on = function(
} }
var result = { }; var result = { };
eventExecutor = new cmDispatcher( { eventExecutor = __plugin.getDispatcher( function(l,e){
execute: function (l, evt) { try {
handler.call(result, evt ); 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. The workaround is to make the ScriptCraftPlugin java class a Listener.
Should only unregister() registered plugins in ScriptCraft js code. Should only unregister() registered plugins in ScriptCraft js code.
*/ */
try {
// nashorn
eventType = eventType.class;
} catch ( e ){
// non-nashorn
eventType = eventType;
}
regd = new cmPluginListener({}); regd = new cmPluginListener({});
cmHookExecutor.registerHook(regd, __plugin, eventType.class, eventExecutor, priority); cmHookExecutor.registerHook(regd, __plugin, eventType, eventExecutor, priority);
result.unregister = function(){ result.unregister = function(){
cmHookExecutor.unregisterPluginListener(regd); cmHookExecutor.unregisterPluginListener(regd);
}; };

View file

@ -8,21 +8,7 @@ function items( material, amount ) {
result.amount = amount; result.amount = amount;
return result; return result;
} }
function getMaterialHandler( material ){
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){ return function(amount){
if (typeof amount == 'undefined'){ if (typeof amount == 'undefined'){
return material; return material;
@ -42,7 +28,42 @@ for (var i = 0;i < materials.length; i++ ){
return result; return result;
} }
}; };
})(materialField.get(ItemType)); }
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; module.exports = items;