Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21.x] Topologically sort reload listeners based on dependency ordering #1915

Open
wants to merge 12 commits into
base: 1.21.x
Choose a base branch
from

Conversation

Shadows-of-Fire
Copy link
Contributor

@Shadows-of-Fire Shadows-of-Fire commented Jan 25, 2025

Overview

This PR introduces dependency sorting for both client and server reload listeners. The sort is topological, using FML's TopologicalSort class as the backbone. Reload listeners are added to a DAG where an edge from a->b means that a must run before b.

The sorting logic, and the modder-facing API, is in SortedReloadListenerEvent, the parent class of both AddReloadListenerEvent and AddClientReloadListenerEvent (formerly RegisterClientReloadListenersEvent).

Requirements

As a prerequisite, this change enforces that all reload listeners are named. The name must be provided via SortedReloadListenerEvent#addListener at the time of registration. Vanilla listeners are preemptively named, via VanillaClientListeners and VanillaServerListeners, which hold the class->RL maps for all known vanilla listener types.

Note: Mods that are currently using mixins or other non-event means to inject reload listeners will crash after this change.

Sorting

Each SortedReloadListenerEvent maintains a Graph<PreparableReloadListener> which holds the directed edges that dictate the dependency information. An edge from a->b means that a must run before b. New edges are added to the graph via #addDependency(first, second), which creates a new edge from first->second.

When the vanilla listeners are added in the event's constructor, edges are added such that each vanilla listener depends on the previous one in-order. This guarantees that the vanilla order is maintained without relying on implicit behavior from the toposort.

Finally, after the event has concluded, but before the sort, a search is performed to find dangling nodes and attach them to the last vanilla node, such that all mod-added listeners run after vanilla if they do not otherwise specify dependency ordering.

Error Handling

A dependency cycle in the toposort is an unrecoverable error and will cause the game to crash. If any cycles are detected, an IllegalArgumentException will be thrown containing a string representation of the cycle(s).

For example:

java.lang.IllegalArgumentException: Cycles were detected during reload listener sorting:
0: neoforge:branding->neoforge:client_mod_loading->minecraft:language_manager->neoforge:branding

Notes

This PR supersedes #1289.

This class injects a ModLoader.hasErrors() check into code that would not be reached if the mod loader errors.
Also adds static keys for the neo-added listeners.
@Shadows-of-Fire Shadows-of-Fire added enhancement New (or improvement to existing) feature or request 1.21.4 Targeted at Minecraft 1.21.4 labels Jan 25, 2025
@Shadows-of-Fire Shadows-of-Fire self-assigned this Jan 25, 2025
@neoforged-pr-publishing
Copy link

neoforged-pr-publishing bot commented Jan 25, 2025

  • Publish PR to GitHub Packages

Last commit published: 2601dc47a353d69a67627b17b8cba5638f481e6d.

PR Publishing

The artifacts published by this PR:

Repository Declaration

In order to use the artifacts published by the PR, add the following repository to your buildscript:

repositories {
    maven {
        name 'Maven for PR #1915' // https://github.com/neoforged/NeoForge/pull/1915
        url 'https://prmaven.neoforged.net/NeoForge/pr1915'
        content {
            includeModule('net.neoforged', 'neoforge')
            includeModule('net.neoforged', 'testframework')
        }
    }
}

MDK installation

In order to setup a MDK using the latest PR version, run the following commands in a terminal.
The script works on both *nix and Windows as long as you have the JDK bin folder on the path.
The script will clone the MDK in a folder named NeoForge-pr1915.
On Powershell you will need to remove the -L flag from the curl invocation.

mkdir NeoForge-pr1915
cd NeoForge-pr1915
curl -L https://prmaven.neoforged.net/NeoForge/pr1915/net/neoforged/neoforge/21.4.89-beta-pr-1915-listener-sort/mdk-pr1915.zip -o mdk.zip
jar xf mdk.zip
rm mdk.zip || del mdk.zip

To test a production environment, you can download the installer from here.

@Shadows-of-Fire Shadows-of-Fire marked this pull request as draft January 25, 2025 09:27
@Shadows-of-Fire Shadows-of-Fire marked this pull request as ready for review January 25, 2025 21:08
@neoforged-compatibility-checks
Copy link

neoforged-compatibility-checks bot commented Jan 25, 2025

@Shadows-of-Fire, this PR introduces breaking changes.
Fortunately, this project is currently accepting breaking changes, but if they are not intentional, please revert them.
Last checked commit: 2601dc47a353d69a67627b17b8cba5638f481e6d.

neoforge (:neoforge)

  • net/neoforged/neoforge/event/AddReloadListenerEvent
    • getListeners()Ljava/util/List;: ❗ API method was removed
    • addListener(Lnet/minecraft/server/packs/resources/PreparableReloadListener;)V: ❗ API method was removed
  • net/neoforged/neoforge/common/NeoForgeEventHandler
    • resourceReloadListeners(Lnet/neoforged/neoforge/event/AddReloadListenerEvent;)V: ⚠ API method was removed
  • net/neoforged/neoforge/client/event/RegisterClientReloadListenersEvent
    • ❗ API class no longer exists
  • net/minecraft/server/packs/resources/ReloadableResourceManager
    • registerReloadListenerIfNotPresent(Lnet/minecraft/server/packs/resources/PreparableReloadListener;)V: ❗ API method was removed

Copy link
Member

@Matyrobbrt Matyrobbrt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me, but someone else should review it before merging. When it is merged, do remember to make an announcement as this is a breaking change.

event.addListener(NeoForgeReloadListeners.CLIENT_MOD_LOADING, ClientModLoader::onResourceReload);
event.addListener(NeoForgeReloadListeners.BRANDING, BrandingControl.resourceManagerReloadListener());

// These run before vanilla reload listeners, so we add them before LANGUAGE, the first vanilla one.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth having a special FIRST field in VanillaClientListeners? And maybe LAST?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? The field indices are already ordered by their vanilla order, but sentinel fields that always point to the current first/last values might be useful (or a liability, if we forget to update them...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're even more of a liability for modders I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that we should prefer to not add them? Or in that relying on non-sentinel fields would be dangerous for modders?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I mean that it's a liability for every modder if we don't provide these fields.

*
* @see {@link AddReloadListenerEvent} for registering server-side reload listeners.
*/
public class AddClientReloadListenerEvent extends SortedReloadListenerEvent implements IModBusEvent {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add is weird naming compared to Register, as it implies that things can be added multiple times somehow. Also, anyone searching for the event will probably look for "RegisterReloadListener" or similar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had this discussion a bit earlier:

As for the names, I used Add because we aren't registering anything. There is no registry for these objects, they're being added to a List.

As for user discoverability, I've found that people are usually aware of the AddReloadListenerEvent but are not aware of the RegisterClientReloadListenersEvent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The list counts as the "registry" in this case. See also the registration of color providers which is just adding stuff to a map.


/**
* The main ResourceManager is recreated on each reload, just after {@link ReloadableServerResources}'s creation.
*
* The event is fired on each reload and lets modders add their own ReloadListeners, for server-side resources.
* The event is fired on the {@link NeoForge#EVENT_BUS}
*/
public class AddReloadListenerEvent extends Event {
private final List<PreparableReloadListener> listeners = new ArrayList<>();
public class AddReloadListenerEvent extends SortedReloadListenerEvent {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the name not contain Server somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, but I'm okay with the current name. I prefer to imply Server as a default state that doesn't need to be present in the class name, while client-only is non-default and must be reflected (hence AddClientReloadListenerEvent).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A silly example: we have ServerStartedEvent not StartedEvent. The event is limited to server reload listeners and the naming should make it clear. Even the documentation mentions server-side resources.

* @see {@link VanillaClientListeners} for vanilla client listener names.
* @see {@link VanillaServerListeners} for vanilla server listener names.
*/
public class NeoForgeReloadListeners {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make class final and add a private noarg constructor.

* @see {@link VanillaServerListeners} for vanilla server listener names.
* @see {@link NeoForgeReloadListeners} for Neo-added listener names.
*/
public class VanillaClientListeners {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final and private constructor.

* @see {@link VanillaServerListeners} for vanilla server listener names.
* @see {@link NeoForgeReloadListeners} for Neo-added listener names.
*/
public class VanillaClientListeners {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be moved to a package under net.neoforged.neoforge.client.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guessing that needs to be moved for #1920

Sucks for discoverability, but not much else to do about it. Links from the other classes will suffice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. And the links in the other classes will break with split sources but that's no big deal.

* @see {@link VanillaClientListeners} for vanilla client listener names.
* @see {@link NeoForgeReloadListeners} for Neo-added listener names.
*/
public class VanillaServerListeners {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final + private ctor

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly both of those things are pointless. Extensions of this class are meaningless, and so would be instantiations.

I'll stand by not bothering with either unless there is a necessity for them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just good practice, and if we ever get around to generating javadoc it will at least stop generating the constructor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.21.4 Targeted at Minecraft 1.21.4 enhancement New (or improvement to existing) feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants