[datagen] add translation generator
Some checks failed
Publish to snapshot maven / build (push) Failing after 2m6s

This commit is contained in:
moehreag 2024-09-29 12:52:02 +02:00
parent cb8958877b
commit 069f780da6
9 changed files with 109 additions and 20 deletions

View file

@ -14,12 +14,15 @@ plugins {
group = "dev.frogmc"
version =
"0.0.1-alpha.3+" + libs.versions.minecraft.get() +
"0.0.1-alpha.4+" + libs.versions.minecraft.get() +
("-local".takeUnless { project.hasProperty("FrogMCSnapshotsMavenPassword") } ?: "")
phytotelma {
minecraft {
version = libs.versions.minecraft
mappings = mojmapParchment {
gameVersion = "1.21"
}
}
loader {
version = libs.versions.frogloader

View file

@ -1,7 +1,7 @@
[versions]
minecraft = "1.21"
frogloader = "0.0.1-alpha.15"
phytotelma = "0.0.1-alpha.16"
minecraft = "1.21.1"
frogloader = "0.0.1-alpha.30"
phytotelma = "0.0.1-alpha.25"
lombok = "8.6"

View file

@ -3,7 +3,9 @@ package dev.frogmc.froglib.datagen.api;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import dev.frogmc.froglib.datagen.api.generators.TranslationGenerator;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistrySetBuilder;
import net.minecraft.data.DataGenerator;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
@ -18,6 +20,12 @@ public interface DatagenExtension {
default void generateItemModels(ItemModelGenerators generators) {}
default void generateTranslations(TranslationGenerator generator) {}
default void generateRegistries(RegistrySetBuilder builder) {
}
static <T extends DataProvider> DataProvider.Factory<T> bindRegistries(
BiFunction<PackOutput, CompletableFuture<HolderLookup.Provider>, T> factory,
CompletableFuture<HolderLookup.Provider> lookupProviderFuture

View file

@ -0,0 +1,26 @@
package dev.frogmc.froglib.datagen.api.generators;
import java.util.function.Function;
import com.google.gson.JsonObject;
public class TranslationGenerator {
private final Function<String, JsonObject> translation;
private String currentLang;
public TranslationGenerator(Function<String, JsonObject> translation) {
this.translation = translation;
}
public void setLanguage(String langCode) {
currentLang = langCode;
}
public void add(String key, String translation) {
if (currentLang == null) {
throw new IllegalArgumentException("No language has been set!");
}
this.translation.apply(currentLang).addProperty(key, translation);
}
}

View file

@ -0,0 +1,45 @@
package dev.frogmc.froglib.datagen.api.providers;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import com.google.gson.JsonObject;
import dev.frogmc.froglib.datagen.api.generators.TranslationGenerator;
import dev.frogmc.froglib.datagen.impl.FrogDatagen;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
public class TranslationProvider implements DataProvider {
private final PackOutput.PathProvider langProvider;
public TranslationProvider(PackOutput output) {
langProvider = output.createPathProvider(PackOutput.Target.RESOURCE_PACK, "lang");
}
@Override
public @NotNull CompletableFuture<?> run(@NotNull CachedOutput cachedOutput) {
List<CompletableFuture<?>> futures = new ArrayList<>();
Map<String, JsonObject> translations = new HashMap<>();
TranslationGenerator generator = new TranslationGenerator(s -> translations.computeIfAbsent(s, a -> new JsonObject()));
FrogDatagen.extension.generateTranslations(generator);
translations.forEach((lang, object) -> {
futures.add(DataProvider.saveStable(cachedOutput,
object,
langProvider.json(ResourceLocation.fromNamespaceAndPath(FrogDatagen.MOD_ID, lang))
));
});
return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new));
}
@Override
public String getName() {
return "FrogMC:Translations";
}
}

View file

@ -7,6 +7,7 @@ import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import dev.frogmc.froglib.datagen.api.DatagenExtension;
import dev.frogmc.froglib.datagen.api.providers.TranslationProvider;
import lombok.extern.slf4j.Slf4j;
import net.minecraft.SharedConstants;
import net.minecraft.Util;
@ -41,6 +42,7 @@ public final class FrogDatagen {
extension = getExtension();
pack.addProvider(ModelProvider::new);
pack.addProvider(TranslationProvider::new);
extension.addProviders(pack, providerFuture);
generator.run();

View file

@ -10,10 +10,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(BlockModelGenerators.class)
public class BlockModelGeneratorsMixin {
@Inject(method = "run", at = @At("HEAD"), cancellable = true)
@Inject(method = "run", at = @At("HEAD"))
private void generateModBlockModels(CallbackInfo ci){
FrogDatagen.extension.generateBlockModels((BlockModelGenerators)(Object)this);
ci.cancel();
}
}

View file

@ -10,10 +10,8 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ItemModelGenerators.class)
public class ItemModelGeneratorsMixin {
@Inject(method = "run", at = @At("HEAD"), cancellable = true)
@Inject(method = "run", at = @At("HEAD"))
private void generateItemModels(CallbackInfo ci) {
FrogDatagen.extension.generateItemModels((ItemModelGenerators) (Object) this);
ci.cancel();
}
}

View file

@ -1,37 +1,46 @@
package dev.frogmc.froglib.datagen.impl.mixin;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import com.google.gson.JsonElement;
import com.llamalad7.mixinextras.injector.v2.WrapWithCondition;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import dev.frogmc.froglib.datagen.impl.FrogDatagen;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.models.ModelProvider;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ModelProvider.class)
public class ModelProviderMixin {
@WrapOperation(method = "run", at = @At(value = "INVOKE", target = "Ljava/util/stream/Stream;map(Ljava/util/function/Function;)Ljava/util/stream/Stream;"))
private <R extends Block, T extends Map.Entry<net.minecraft.resources.ResourceKey<R>, R>> Stream<T> filterModOnly(Stream<T> instance, Function<? super T, ? extends T> function, Operation<Stream<T>> original){
return original.call(instance.filter(b -> b.getKey().location().getNamespace().equals(FrogDatagen.MOD_ID)), function);
/*@Unique
private static final ThreadLocal<List<Block>> froglib$list = new ThreadLocal<>();
@Redirect(method = "run", at = @At(value = "INVOKE", target = "Ljava/util/List;isEmpty()Z"))
private boolean wrapCheck(List<Block> instance) {
froglib$list.set(instance);
return true;
}
/*@WrapOperation(method = "run", at = @At(value = "INVOKE", target = "Ljava/util/stream/Stream;map(Ljava/util/function/Function;)Ljava/util/stream/Stream;"))
private <R extends Block, T extends Map.Entry<net.minecraft.resources.ResourceKey<R>, R>> Stream<T> filterModOnly(Stream<T> instance, Function<? super T, ? extends T> function, Operation<Stream<T>> original) {
return original.call(instance.filter(b -> b.getKey().location().getNamespace().equals(FrogDatagen.MOD_ID)), function);
}*
@Inject(method = "lambda$run$4", at = @At("HEAD"), cancellable = true)
private static void filterModOnlyBlocks(Set<Item> set, Map<ResourceLocation, Supplier<JsonElement>> map, Block block, CallbackInfo ci){
if (BuiltInRegistries.BLOCK.getKey(block).getNamespace().equals(FrogDatagen.MOD_ID)) {
private static void filterModOnlyBlocks(Set<Item> set, Map<ResourceLocation, Supplier<JsonElement>> map, Block block, CallbackInfo ci) {
if (froglib$list.get().contains(block)) {
ci.cancel();
}
}
}*/
}