BlockBBEvent in scripts?
-
-
@idk-my-name
WhatJava.extend()
returns is something that behaves like a type (Forgot what it really is, probably a JSAdaptor or something).
Similar to what you will get from usingJava.type()
.ArrayList = Java.type("java.util.ArrayList") arrayList = new ArrayList()
BBConsumer = Java.extend(Listenable, Consumer, { handleEvents: function() { return true}, accept: function(e) { // onBlockBB here } }) consumer = new BBConsumer() registerListener(consumer) // not registerListener(BBConsumer) /* I am sure there would be a better name for BBConsumer And if you want to use it like a anonymous class in Java you could just read the BlockAnimations.js or better, nashorn document https://github.com/CzechHek/Core/blob/879f36d7e41ccc7868285b0a808866b360f30822/Scripts/BlockAnimations.js#L35 */
Not sure if the type from
Java.extend
would have other unexpected methods that only takes 1 parameters, you might want to check if the parameter's super class isEvent
.Or you could just explicitly specify the
accept
method in it.
method = consumer.class.getMethod("accept", java.lang.Object.class)
Edit:
Nvm, I forgot that this was scriptAPI, theConsumer
here is aConsumer<?>
, notConsumer<BlockBBEvent>
which means theaccepct()
takes anjava.lang.Object
, and it will register a Listener that listens to any event that is anjava.lang.Object
, which probably means all events.'Here is what I did
registry = getField(LiquidBounce.eventManager, "registry").get(LiquidBounce.eventManager) BBs = registry.getOrDefault(BlockBBEvent.class, new ArrayList()) BBs.add(new EventHook(consumer, consumer.class.getMethod("accept", java.lang.Object.class), LiquidBounce.moduleManager.class .getDeclaredMethod("onKey", KeyEvent.class) .getAnnotation(EventTarget.class))) registry[BlockBBEvent.class] = BBs
-
@commandblock2 Thank you
-
-
This is what I came up with without seeing @commandblock2's solution.
I had to reverse engineer a bunch of stuff and then put all together and it worked.I was wondering what the hell was annotation in Nashorn and it turns out that
print(annotation.class)
literally returns
I'll try to simplify it and automize it via Core.
///api_version=2 (script = registerScript({ name: "Test", authors: ["CzechHek"], version: "1.0" })).import("Core.lib"); module = { onClickGuiLoaded: function () { listener = new (Java.extend(Listenable, Consumer, { handleEvents: function() { return TestModule.state }, accept: function(e) { print(e) } })); method = getMethod(listener, "accept"); annotation = getMethod(TestModule, "onUpdate").getDeclaredAnnotations()[0]; AeventManager = new Reflector(LiquidBounce.eventManager) invocableEventTargets = AeventManager.registry.getOrDefault(BlockBBEvent.class, new ArrayList()); invocableEventTargets.add(new EventHook(listener, method, annotation)); AeventManager.registry[BlockBBEvent.class] = invocableEventTargets; } } Listenable = Java.type("net.ccbluex.liquidbounce.event.Listenable"); Consumer = Java.type("java.util.function.Consumer"); BlockBBEvent = Java.type("net.ccbluex.liquidbounce.event.BlockBBEvent"); EventHook = Java.type("net.ccbluex.liquidbounce.event.EventHook"); ArrayList = Java.type("java.util.ArrayList")
-
@idk-my-name you forgor
-
@fartcheese i forgor
-
@idk-my-name we forgor
-
@fartcheese Wait... I rember
-
@idk-my-name same
i rember
-
@czechhek said in BlockBBEvent in scripts?:
I'll try to simplify it and automize it via Core.
@idk-my-name https://forums.ccbluex.net/topic/94/script-base-core-3-28-simple-yet-powerful-scriptapi-base/30 and here it is
You can use all events if you import Core base, no need to do any of this stuff.
For more info, look at my comment on Core post.