-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NativeEvents can now be used from any script type, can be added/remov…
…ed/reloaded at runtime. Also added optional priority as first param. Removed NativeModEvents, it now decides based on class automatically
- Loading branch information
1 parent
f98a525
commit 8387f54
Showing
8 changed files
with
78 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
src/main/java/dev/latvian/mods/kubejs/neoforge/NativeEventConsumer.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
src/main/java/dev/latvian/mods/kubejs/neoforge/NativeEventListeners.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dev.latvian.mods.kubejs.neoforge; | ||
|
||
import dev.latvian.mods.rhino.type.TypeInfo; | ||
import net.neoforged.bus.api.Event; | ||
import net.neoforged.bus.api.EventPriority; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
public class NativeEventListeners implements Consumer<Event> { | ||
public static final TypeInfo EVENT_CONSUMER_TYPE = TypeInfo.RAW_CONSUMER.withParams(TypeInfo.of(Event.class)); | ||
|
||
public record Key(Class<?> eventClass, EventPriority priority) { | ||
@Override | ||
public boolean equals(Object o) { | ||
return o == this || o instanceof Key k && k.eventClass == eventClass && k.priority == priority; | ||
} | ||
} | ||
|
||
public final List<Consumer<Event>> listeners; | ||
|
||
public NativeEventListeners() { | ||
this.listeners = new LinkedList<>(); | ||
} | ||
|
||
@Override | ||
public void accept(Event event) { | ||
for (var listener : listeners) { | ||
listener.accept(event); | ||
} | ||
} | ||
} |
49 changes: 33 additions & 16 deletions
49
src/main/java/dev/latvian/mods/kubejs/neoforge/NativeEventWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,46 @@ | ||
package dev.latvian.mods.kubejs.neoforge; | ||
|
||
import dev.latvian.mods.kubejs.script.ConsoleJS; | ||
import dev.latvian.mods.kubejs.KubeJS; | ||
import dev.latvian.mods.kubejs.script.KubeJSContext; | ||
import dev.latvian.mods.rhino.Context; | ||
import net.neoforged.bus.api.Event; | ||
import net.neoforged.bus.api.EventPriority; | ||
import net.neoforged.bus.api.IEventBus; | ||
import net.neoforged.fml.event.IModBusEvent; | ||
import net.neoforged.neoforge.common.NeoForge; | ||
|
||
public record NativeEventWrapper(String name, IEventBus eventBus) { | ||
public Object onEvent(Context cx, Object eventClass, NativeEventConsumer consumer) { | ||
if (!((KubeJSContext) cx).getType().isStartup()) { | ||
throw new RuntimeException("Native event wrappers are only allowed in startup scripts!"); | ||
} else if (!(eventClass instanceof CharSequence || eventClass instanceof Class)) { | ||
throw new RuntimeException("Invalid syntax! " + name + ".onEvent(eventType, function) requires event class and handler"); | ||
} else if (!((KubeJSContext) cx).kjsFactory.manager.firstLoad) { | ||
ConsoleJS.STARTUP.warn(name + ".onEvent() can't be reloaded! You will have to restart the game for changes to take effect."); | ||
return null; | ||
import java.util.function.Consumer; | ||
|
||
public interface NativeEventWrapper { | ||
static void onEvent(Context cx, Class<?> eventClass, Consumer<Event> consumer) { | ||
onEvent(cx, EventPriority.NORMAL, eventClass, consumer); | ||
} | ||
|
||
static void onEvent(Context cx, EventPriority priority, Class<?> eventClass, Consumer<Event> consumer) { | ||
if (!Event.class.isAssignableFrom(eventClass)) { | ||
throw new IllegalArgumentException("Event class must extend net.neoforged.bus.api.Event!"); | ||
} | ||
|
||
try { | ||
Class type = eventClass instanceof Class<?> c ? c : Class.forName(eventClass.toString()); | ||
eventBus.addListener(EventPriority.NORMAL, false, type, consumer); | ||
} catch (Exception ex) { | ||
throw new RuntimeException(ex); | ||
var scriptType = ((KubeJSContext) cx).kjsFactory.manager.scriptType; | ||
var key = new NativeEventListeners.Key(eventClass, priority == null ? EventPriority.NORMAL : priority); | ||
|
||
var listeners = scriptType.nativeEventListeners.get(key); | ||
|
||
if (listeners == null) { | ||
listeners = new NativeEventListeners(); | ||
scriptType.nativeEventListeners.put(key, listeners); | ||
|
||
IEventBus bus; | ||
|
||
if (IModBusEvent.class.isAssignableFrom(eventClass)) { | ||
bus = KubeJS.modEventBus; | ||
} else { | ||
bus = NeoForge.EVENT_BUS; | ||
} | ||
|
||
bus.addListener(priority, false, (Class) eventClass, listeners); | ||
} | ||
|
||
return null; | ||
listeners.listeners.add((Consumer<Event>) cx.jsToJava(consumer, NativeEventListeners.EVENT_CONSUMER_TYPE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters