Skip to content

Commit

Permalink
Add a method to data generator entrypoints to register custom keys wi…
Browse files Browse the repository at this point in the history
…th priorities (#3087)

* Add JsonKeySortOrderCallback

* Use an entry point rather than an event for sort keys and priorities

* Resolve static imports

* Add a bit of javadoc

* Check if a key is null and modify the javadoc

* Add a field reference in the javadoc

* Rename JsonKeySortOrderAdder to JsonKeySortOrderCallback
  • Loading branch information
ErrorCraft authored Sep 18, 2023
1 parent a51b242 commit 0883a8d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,11 @@ default String getEffectiveModId() {
*/
default void buildRegistry(RegistryBuilder registryBuilder) {
}

/**
* Provides a callback for setting the sort priority of object keys in generated JSON files.
* @param callback a callback for setting the sort priority for a given key
*/
default void addJsonKeySortOrders(JsonKeySortOrderCallback callback) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.datagen.v1;

/**
* Provides a callback for setting the sort priority of object keys in generated JSON files.
*/
@FunctionalInterface
public interface JsonKeySortOrderCallback {
/**
* Sets the sort priority for a given object key within generated JSON files.
* @param key the key to set priority for
* @param priority the priority for the key, where keys with lower priority are sorted before keys with higher priority
* @implNote The default priority is 2.
* @see net.minecraft.data.DataProvider#JSON_KEY_SORT_ORDER
*/
void add(String key, int priority);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -28,11 +29,13 @@

import com.mojang.logging.LogUtils;
import com.mojang.serialization.Lifecycle;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.minecraft.data.DataProvider;
import net.minecraft.registry.BuiltinRegistries;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.Registerable;
Expand Down Expand Up @@ -107,6 +110,9 @@ private static void runInternal() {
final List<DataGeneratorEntrypoint> entrypoints = dataGeneratorInitializers.stream().map(EntrypointContainer::getEntrypoint).toList();
CompletableFuture<RegistryWrapper.WrapperLookup> registriesFuture = CompletableFuture.supplyAsync(() -> createRegistryWrapper(entrypoints), Util.getMainWorkerExecutor());

Object2IntOpenHashMap<String> jsonKeySortOrders = (Object2IntOpenHashMap<String>) DataProvider.JSON_KEY_SORT_ORDER;
Object2IntOpenHashMap<String> defaultJsonKeySortOrders = new Object2IntOpenHashMap<>(jsonKeySortOrders);

for (EntrypointContainer<DataGeneratorEntrypoint> entrypointContainer : dataGeneratorInitializers) {
final String id = entrypointContainer.getProvider().getMetadata().getId();

Expand All @@ -123,13 +129,23 @@ private static void runInternal() {
final String effectiveModId = entrypoint.getEffectiveModId();
ModContainer modContainer = entrypointContainer.getProvider();

HashSet<String> keys = new HashSet<>();
entrypoint.addJsonKeySortOrders((key, value) -> {
Objects.requireNonNull(key, "Tried to register a priority for a null key");
jsonKeySortOrders.put(key, value);
keys.add(key);
});

if (effectiveModId != null) {
modContainer = FabricLoader.getInstance().getModContainer(effectiveModId).orElseThrow(() -> new RuntimeException("Failed to find effective mod container for mod id (%s)".formatted(effectiveModId)));
}

FabricDataGenerator dataGenerator = new FabricDataGenerator(outputDir, modContainer, STRICT_VALIDATION, registriesFuture);
entrypoint.onInitializeDataGenerator(dataGenerator);
dataGenerator.run();

jsonKeySortOrders.keySet().removeAll(keys);
jsonKeySortOrders.putAll(defaultJsonKeySortOrders);
} catch (Throwable t) {
throw new RuntimeException("Failed to run data generator from mod (%s)".formatted(id), t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.JsonKeySortOrderCallback;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricAdvancementProvider;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricBlockLootTableProvider;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricDynamicRegistryProvider;
Expand All @@ -91,6 +92,11 @@ public class DataGeneratorTestEntrypoint implements DataGeneratorEntrypoint {
private static final ConditionJsonProvider NEVER_LOADED = DefaultResourceConditions.allModsLoaded("a");
private static final ConditionJsonProvider ALWAYS_LOADED = DefaultResourceConditions.not(NEVER_LOADED);

@Override
public void addJsonKeySortOrders(JsonKeySortOrderCallback callback) {
callback.add("trigger", 0);
}

@Override
public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) {
final FabricDataGenerator.Pack pack = dataGenerator.createPack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@
import net.fabricmc.fabric.api.datagen.v1.DataGeneratorEntrypoint;
import net.fabricmc.fabric.api.datagen.v1.FabricDataGenerator;
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
import net.fabricmc.fabric.api.datagen.v1.JsonKeySortOrderCallback;
import net.fabricmc.fabric.api.datagen.v1.provider.FabricCodecDataProvider;

@SuppressWarnings("unused")
public class DataGeneratorClientTestEntrypoint implements DataGeneratorEntrypoint {
@Override
public void addJsonKeySortOrders(JsonKeySortOrderCallback callback) {
callback.add("type", 100); // Force 'type' at the end
}

@Override
public void onInitializeDataGenerator(FabricDataGenerator dataGenerator) {
final FabricDataGenerator.Pack pack = dataGenerator.createBuiltinResourcePack(new Identifier(MOD_ID, "example_builtin"));
Expand Down

0 comments on commit 0883a8d

Please sign in to comment.