-
Notifications
You must be signed in to change notification settings - Fork 487
Allow registration of data resource reloader using a factory and provide a store to store data reload results. #4944
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
Merged
modmuss50
merged 4 commits into
FabricMC:1.21.10
from
LambdAurora:feature/resource_loader/improved_data_resource_reloaders
Nov 4, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d1ff6a0
Allow once again to register data resource reloader using a factory, …
LambdAurora e329c39
Remove dead code, add access to advancement and recipe loaders.
LambdAurora 9e8eda6
Fix import order of `DataResourceLoader`.
LambdAurora 58a9839
Fix interface injection of `DataResourceStore` into `MinecraftServer`.
LambdAurora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
87 changes: 87 additions & 0 deletions
87
...ource-loader-v1/src/main/java/net/fabricmc/fabric/api/resource/v1/DataResourceLoader.java
This file contains hidden or 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,87 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.api.resource.v1; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| import org.jetbrains.annotations.ApiStatus; | ||
|
|
||
| import net.minecraft.recipe.ServerRecipeManager; | ||
| import net.minecraft.registry.RegistryWrapper; | ||
| import net.minecraft.resource.ResourceReloader; | ||
| import net.minecraft.resource.ResourceType; | ||
| import net.minecraft.server.ServerAdvancementLoader; | ||
| import net.minecraft.util.Identifier; | ||
|
|
||
| import net.fabricmc.fabric.impl.resource.v1.DataResourceLoaderImpl; | ||
|
|
||
| /** | ||
| * Provides various hooks into the {@linkplain net.minecraft.resource.ResourceType#SERVER_DATA server data} resource loader. | ||
| */ | ||
| @ApiStatus.NonExtendable | ||
| public interface DataResourceLoader extends ResourceLoader { | ||
| /** | ||
| * The resource reloader store key for the recipe manager. | ||
| * | ||
| * @apiNote The recipe manager is only available in {@linkplain ResourceType#SERVER_DATA server data} resource reloaders. | ||
| * <br/> | ||
| * It should <b>only</b> be accessed in the application phase of the resource reloader, | ||
| * and you should depend on {@link net.fabricmc.fabric.api.resource.v1.reloader.ResourceReloaderKeys.Server#RECIPES}. | ||
| */ | ||
| ResourceReloader.Key<ServerRecipeManager> RECIPE_MANAGER_KEY = new ResourceReloader.Key<>(); | ||
| /** | ||
| * The resource reloader store key for the advancement loader. | ||
| * | ||
| * @apiNote The advancement loader is only available in {@linkplain ResourceType#SERVER_DATA server data} resource reloaders. | ||
| * <br/> | ||
| * It should <b>only</b> be accessed in the application phase of the resource reloader, | ||
| * and you should depend on {@link net.fabricmc.fabric.api.resource.v1.reloader.ResourceReloaderKeys.Server#ADVANCEMENTS}. | ||
| */ | ||
| ResourceReloader.Key<ServerAdvancementLoader> ADVANCEMENT_LOADER_KEY = new ResourceReloader.Key<>(); | ||
| /** | ||
| * The resource reloader store key for the data resource store. | ||
| * | ||
| * @apiNote The data resource store is only available in {@linkplain ResourceType#SERVER_DATA server data} resource reloaders. | ||
| * <br/> | ||
| * It should <b>only</b> be mutated in the application phase of the resource reloader. | ||
| */ | ||
| ResourceReloader.Key<DataResourceStore.Mutable> DATA_RESOURCE_STORE_KEY = new ResourceReloader.Key<>(); | ||
|
|
||
| static DataResourceLoader get() { | ||
| return DataResourceLoaderImpl.INSTANCE; | ||
| } | ||
|
|
||
| /** | ||
| * Registers a data resource reloader. | ||
| * | ||
| * @param id the identifier of the resource reloader | ||
| * @param factory the factory function of the resource reloader | ||
| * @see #registerReloader(Identifier, ResourceReloader) | ||
| * @see #addReloaderOrdering(Identifier, Identifier) | ||
| * | ||
| * @apiNote In most cases {@link #registerReloader(Identifier, ResourceReloader)} is sufficient and should be preferred, | ||
| * but for some resource reloaders like {@link net.minecraft.resource.JsonDataLoader} constructing the resource reloader | ||
| * with a known instance of the wrapper lookup is required. | ||
| * <br/> | ||
| * While this may encourage stateful resource reloaders, it is best to primarily use resource reloaders as stateless loaders, | ||
| * as storing a state may easily lead to incomplete or leaking data. | ||
| */ | ||
| void registerReloader( | ||
| Identifier id, | ||
| Function<RegistryWrapper.WrapperLookup, ResourceReloader> factory | ||
| ); | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
...source-loader-v1/src/main/java/net/fabricmc/fabric/api/resource/v1/DataResourceStore.java
This file contains hidden or 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,53 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.api.resource.v1; | ||
|
|
||
| /** | ||
| * Represents a resource store for data. | ||
| * | ||
| * <p>Such resource store can be filled in the application phase of data {@linkplain net.minecraft.resource.ResourceReloader resource reloaders}. | ||
| * And queried through an instance of {@link net.minecraft.server.MinecraftServer}. | ||
| */ | ||
| public interface DataResourceStore { | ||
| /** | ||
| * Represents a typed key for {@linkplain DataResourceStore the data resource store}. | ||
| * | ||
| * @param <T> the type of this key | ||
| */ | ||
| final class Key<T> { | ||
| } | ||
|
|
||
| /** | ||
| * Gets data stored at the given key, or throws if not found. | ||
| * | ||
| * @param key the key | ||
| * @return the data stored at the given key | ||
| * @param <T> the type of data | ||
| */ | ||
| <T> T getOrThrow(Key<T> key); | ||
|
|
||
| interface Mutable extends DataResourceStore { | ||
| /** | ||
| * Puts data at the given key. | ||
| * | ||
| * @param key the key to store at | ||
| * @param data the data to store | ||
| * @param <T> the type of data | ||
| */ | ||
| <T> void put(Key<T> key, T data); | ||
| } | ||
| } |
This file contains hidden or 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
88 changes: 88 additions & 0 deletions
88
...-loader-v1/src/main/java/net/fabricmc/fabric/impl/resource/v1/DataResourceLoaderImpl.java
This file contains hidden or 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,88 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.impl.resource.v1; | ||
|
|
||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.function.Function; | ||
|
|
||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import net.minecraft.registry.RegistryWrapper; | ||
| import net.minecraft.resource.ResourceReloader; | ||
| import net.minecraft.resource.ResourceType; | ||
| import net.minecraft.util.Identifier; | ||
|
|
||
| import net.fabricmc.fabric.api.resource.v1.DataResourceLoader; | ||
|
|
||
| public final class DataResourceLoaderImpl extends ResourceLoaderImpl implements DataResourceLoader { | ||
| public static final DataResourceLoaderImpl INSTANCE = new DataResourceLoaderImpl(); | ||
| private final Map<Identifier, Function<RegistryWrapper.WrapperLookup, ResourceReloader>> addedReloaderFactories | ||
| = new LinkedHashMap<>(); | ||
|
|
||
| private DataResourceLoaderImpl() { | ||
| super(ResourceType.SERVER_DATA); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean hasResourceReloader(Identifier id) { | ||
| return super.hasResourceReloader(id) || this.addedReloaderFactories.containsKey(id); | ||
| } | ||
|
|
||
| @Override | ||
| public void registerReloader(Identifier id, Function<RegistryWrapper.WrapperLookup, ResourceReloader> factory) { | ||
| Objects.requireNonNull(id, "The reloader identifier should not be null."); | ||
| Objects.requireNonNull(factory, "The reloader factory should not be null."); | ||
| this.checkUniqueResourceReloader(id); | ||
|
|
||
| for (Map.Entry<Identifier, Function<RegistryWrapper.WrapperLookup, ResourceReloader>> entry | ||
| : this.addedReloaderFactories.entrySet() | ||
| ) { | ||
| if (entry.getValue() == factory) { | ||
| throw new IllegalStateException( | ||
| "Resource reloader factory with ID %s already in resource reloader factory set with ID %s!" | ||
| .formatted(id, entry.getKey()) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| this.addedReloaderFactories.put(id, factory); | ||
| } | ||
|
|
||
| @Override | ||
| protected Set<Map.Entry<Identifier, ResourceReloader>> collectReloadersToAdd( | ||
| @Nullable SetupMarkerResourceReloader setupMarker | ||
| ) { | ||
| if (setupMarker == null) { | ||
| throw new IllegalStateException("The setup marker should not be null for data resource loading."); | ||
| } | ||
|
|
||
| RegistryWrapper.WrapperLookup registries = setupMarker.dataPackContents().getReloadableRegistries().createRegistryLookup(); | ||
| Set<Map.Entry<Identifier, ResourceReloader>> reloadersToAdd = super.collectReloadersToAdd(setupMarker); | ||
|
|
||
| for (Map.Entry<Identifier, Function<RegistryWrapper.WrapperLookup, ResourceReloader>> entry | ||
| : this.addedReloaderFactories.entrySet() | ||
| ) { | ||
| ResourceReloader reloader = entry.getValue().apply(registries); | ||
| reloadersToAdd.add(Map.entry(entry.getKey(), reloader)); | ||
| } | ||
|
|
||
| return reloadersToAdd; | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
...e-loader-v1/src/main/java/net/fabricmc/fabric/impl/resource/v1/DataResourceStoreImpl.java
This file contains hidden or 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,38 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.impl.resource.v1; | ||
|
|
||
| import java.util.IdentityHashMap; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| import net.fabricmc.fabric.api.resource.v1.DataResourceStore; | ||
|
|
||
| public final class DataResourceStoreImpl implements DataResourceStore.Mutable { | ||
| private final Map<Key<?>, Object> store = new IdentityHashMap<>(); | ||
|
|
||
| @Override | ||
| public <T> void put(Key<T> key, T data) { | ||
| this.store.put(key, data); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public <T> T getOrThrow(Key<T> key) { | ||
| return Objects.requireNonNull((T) this.store.get(key)); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused why we are using a
WrapperLookuphere, rather than theDataResourceStore. Is there a good reason?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, the main need for this overload is for
JsonDataLoaderand it only needsWrapperLookup, I'm a bit concerned about how confusing theDataResourceStoremight be here.Also the
DataResourceStoredoes not contain theWrapperLookup, the lookup and other reloaders are also available through the Vanilla ResourceReloader shared state store.