neoforged / bus Goto Github PK
View Code? Open in Web Editor NEWEvent firing and listening framework, based on the event bus concept
License: GNU Lesser General Public License v2.1
Event firing and listening framework, based on the event bus concept
License: GNU Lesser General Public License v2.1
Hi!
I've been discussing event priorities with someone over on the Twilight Forest Discord server as they wanted to change the priority of our death event so their mod would properly work. While doing this, we had the realization that only having 5 different priorities that had no sorting order (at least I don't think it does?) is a rather flawed system.
My proposal: take a page out of how Mixin does it and allow people to specify an int for the priority. The default could just be 0, and mods that need a lower priority could just specify a negative number if needed. Mods could then be sorted based on this priority (perhaps by the actual mod loading order if multiple mods use the same value) and it would allow for finer control of when things fire.
Event listeners ordinarily do not receive cancelled events. However, they can specifically opt-in to receiving cancelled events through either the receiveCanceled
parameter of the IEventBus#addListener
methods or the receiveCanceled
field of the @SubscribeEvent
annotation.
However, code investigation and testing (prompted by @PlatinPython, thanks btw) shows that the receiveCanceled
field of the @SubscribeEvent
annotation is not respected, thus causing cancelled events to never reach such an annotated listener. (The addListener
methods are unaffected as far as I can tell.)
Sample output from the code below:
Sending non-canceled event
StaticListener: Received event; canceled: false
InstanceListener: Received event; canceled: false
Sending canceled event
Sample code:
import net.neoforged.bus.api.*;
class Scratch {
public static void main(String[] args) {
final IEventBus bus = BusBuilder.builder().build();
final TestEvent event = new TestEvent();
bus.register(StaticListener.class);
final InstanceListener listener = new InstanceListener();
bus.register(listener);
System.out.println("Sending non-canceled event");
bus.post(event);
event.setCanceled(true);
System.out.println("Sending canceled event");
bus.post(event);
}
static class StaticListener {
@SubscribeEvent(receiveCanceled = true)
public static void listen(TestEvent event) {
System.out.println("StaticListener: Received event; canceled: " + event.isCanceled());
}
}
static class InstanceListener {
@SubscribeEvent(receiveCanceled = true)
public void listen(TestEvent event) {
System.out.println("InstanceListener: Received event; canceled: " + event.isCanceled());
}
}
public static class TestEvent extends Event implements ICancellableEvent {
}
}
I don't see the use case, and it makes the implementation of ListenerList
a lot more painful.
They could just be regular subclass fields. We could move Event.Result
to forge as a general-purpose "boolean or default" enum.
This should use listeners[x].listenerName()
to have a more readable name (right now the method is unused!).
As far as I can tell it's just a matter of adding such a method to Event
:
protected void reset() {
isCanceled = false;
result = Result.DEFAULT;
phase = null;
}
(event.setCanceled(false).setResult(Result.DEFAULT).setPhase(null)
will not work due to setPhase
)
Of course, this should come with a nice test too.
Minecraft Version: 1.21.1
NeoForge Version: 21.1.22
Steps to Reproduce:
NeoForgeServerSparkPlugin
Description of issue:
When you unregister
from the event bus, it marks the ListenerList to be rebuilt
The rebuild process is triggered when you call getListeners, which happens the next time this event is posted.
For this Spark case this ServerStoppingEvent
will be only called again when you leave the server for a second time, making you always have at least 1 leaked object if you leave and join a world.
With that said, should Spark clear their object and do not rely on Event Bus to release their object or there is something that can be fixed on NF side?
It doesn't seem too useful and right now it isn't particularly performant - once capabilities get reworked Forge won't be making use of it anymore.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.