I'm trying to figure out how to pass my Javascript function to my Java method so that I can call it at runtime.
I have the following Java Method Signature:
public static void createBlock(String domain, String name, String eventType, EventFunction eventHandler)
I am calling this from Javascript like this:
function main() {
BlockManager.createBlock('sarah-egg', 'enderBlock', 'breakBlock', func);
}
function func (event) {
event.world.createExplosion(null, event.pos.getX(), event.pos.getY(), event.pos.getZ, 10, true);
}
And I have made my Java Functional Interface, EventFunction, like this:
package com.learntomod.event;
@FunctionalInterface
public interface EventFunction<T> {
void fun(T t);
}
When I run it, I'm getting this error:
Cannot cast jdk.nashorn.internal.objects.ScriptFunctionImpl to com.learntomod.event.EventFunction
I tried changing EventFunction to Runnable and getting rid of the parameter, and it worked.
Any ideas on what I might be doing wrong?
Invocableto your Java method.EventFunctioninterface in JavaScript asfunction func(event). That's possible (compare ideone.com/amHsPp ) but I think you'll have to get rid of the generic<T>.