From d458771a49076a5e9058c16518a2d46b7e76c8c8 Mon Sep 17 00:00:00 2001 From: 90 Date: Sun, 10 Sep 2023 14:56:04 +0100 Subject: [PATCH] Move lang datagen to common --- .../datagen/CommonLanguageProvider.java | 76 +++++++++++++++++++ .../megacells/block/MEGAInterfaceBlock.java | 2 +- .../datagen/LocalisationProvider.java | 27 ------- .../megacells/datagen/MEGADataGenerators.java | 2 +- .../datagen/LocalisationProvider.java | 27 ------- .../megacells/datagen/MEGADataGenerators.java | 2 +- 6 files changed, 79 insertions(+), 57 deletions(-) create mode 100644 common/src/data/java/gripe/_90/megacells/datagen/CommonLanguageProvider.java delete mode 100644 fabric/src/data/java/gripe/_90/megacells/datagen/LocalisationProvider.java delete mode 100644 forge/src/data/java/gripe/_90/megacells/datagen/LocalisationProvider.java diff --git a/common/src/data/java/gripe/_90/megacells/datagen/CommonLanguageProvider.java b/common/src/data/java/gripe/_90/megacells/datagen/CommonLanguageProvider.java new file mode 100644 index 00000000..7b1a4caf --- /dev/null +++ b/common/src/data/java/gripe/_90/megacells/datagen/CommonLanguageProvider.java @@ -0,0 +1,76 @@ +package gripe._90.megacells.datagen; + +import java.util.Arrays; +import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.CompletableFuture; + +import com.google.gson.JsonObject; + +import org.jetbrains.annotations.NotNull; + +import net.minecraft.data.CachedOutput; +import net.minecraft.data.DataProvider; +import net.minecraft.data.PackOutput; + +import appeng.core.definitions.BlockDefinition; +import appeng.core.definitions.ItemDefinition; + +import gripe._90.megacells.MEGACells; +import gripe._90.megacells.definition.MEGABlocks; +import gripe._90.megacells.definition.MEGAItems; +import gripe._90.megacells.definition.MEGATranslations; + +public class CommonLanguageProvider implements DataProvider { + private final Map translationEntries = new TreeMap<>(); + private final PackOutput output; + + public CommonLanguageProvider(PackOutput output) { + this.output = output; + } + + private void addTranslations() { + MEGAItems.getItems().forEach(this::add); + MEGABlocks.getBlocks().forEach(this::add); + Arrays.stream(MEGATranslations.values()).forEach(this::add); + add("text.autoconfig.%s.title".formatted(MEGACells.MODID), MEGATranslations.ModName.getEnglishText()); + } + + private void add(ItemDefinition item) { + add(item.asItem().getDescriptionId(), item.getEnglishName()); + } + + private void add(BlockDefinition block) { + add(block.block().getDescriptionId(), block.getEnglishName()); + } + + private void add(MEGATranslations translation) { + add(translation.getTranslationKey(), translation.getEnglishText()); + } + + private void add(String translationKey, String englishText) { + if (translationEntries.containsKey(translationKey)) { + throw new RuntimeException("Duplicate translation key: " + translationKey); + } + + translationEntries.put(translationKey, englishText); + } + + @NotNull + @Override + public CompletableFuture run(CachedOutput writer) { + var translationJson = new JsonObject(); + var translationFile = output.createPathProvider(PackOutput.Target.RESOURCE_PACK, "lang") + .json(MEGACells.makeId("en_us")); + + addTranslations(); + translationEntries.forEach(translationJson::addProperty); + return DataProvider.saveStable(writer, translationJson, translationFile); + } + + @NotNull + @Override + public String getName() { + return "Language"; + } +} diff --git a/common/src/main/java/gripe/_90/megacells/block/MEGAInterfaceBlock.java b/common/src/main/java/gripe/_90/megacells/block/MEGAInterfaceBlock.java index 0821d331..33c53f2e 100644 --- a/common/src/main/java/gripe/_90/megacells/block/MEGAInterfaceBlock.java +++ b/common/src/main/java/gripe/_90/megacells/block/MEGAInterfaceBlock.java @@ -33,7 +33,7 @@ public InteractionResult onActivated( return InteractionResult.PASS; } - var be = this.getBlockEntity(level, pos); + var be = getBlockEntity(level, pos); if (be != null) { if (!level.isClientSide()) { diff --git a/fabric/src/data/java/gripe/_90/megacells/datagen/LocalisationProvider.java b/fabric/src/data/java/gripe/_90/megacells/datagen/LocalisationProvider.java deleted file mode 100644 index 62e726ab..00000000 --- a/fabric/src/data/java/gripe/_90/megacells/datagen/LocalisationProvider.java +++ /dev/null @@ -1,27 +0,0 @@ -package gripe._90.megacells.datagen; - -import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput; -import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider; - -import gripe._90.megacells.MEGACells; -import gripe._90.megacells.definition.MEGABlocks; -import gripe._90.megacells.definition.MEGAItems; -import gripe._90.megacells.definition.MEGATranslations; - -class LocalisationProvider extends FabricLanguageProvider { - protected LocalisationProvider(FabricDataOutput output) { - super(output, "en_us"); - } - - @Override - public void generateTranslations(TranslationBuilder builder) { - MEGAItems.getItems().forEach(item -> builder.add(item.asItem(), item.getEnglishName())); - MEGABlocks.getBlocks().forEach(block -> builder.add(block.block(), block.getEnglishName())); - - for (var translation : MEGATranslations.values()) { - builder.add(translation.getTranslationKey(), translation.getEnglishText()); - } - - builder.add("text.autoconfig.%s.title".formatted(MEGACells.MODID), MEGATranslations.ModName.getEnglishText()); - } -} diff --git a/fabric/src/data/java/gripe/_90/megacells/datagen/MEGADataGenerators.java b/fabric/src/data/java/gripe/_90/megacells/datagen/MEGADataGenerators.java index 949269b6..f86ad7f8 100644 --- a/fabric/src/data/java/gripe/_90/megacells/datagen/MEGADataGenerators.java +++ b/fabric/src/data/java/gripe/_90/megacells/datagen/MEGADataGenerators.java @@ -18,9 +18,9 @@ public void onInitializeDataGenerator(FabricDataGenerator generator) { pack.addProvider((FabricDataOutput output) -> new CommonTagProvider.ItemTags(output, registries, blocks.contentsGetter())); + pack.addProvider((FabricDataOutput output) -> new CommonLanguageProvider(output)); pack.addProvider(ModelProvider::new); pack.addProvider(RecipeProvider::new); pack.addProvider(LootTableProvider::new); - pack.addProvider(LocalisationProvider::new); } } diff --git a/forge/src/data/java/gripe/_90/megacells/datagen/LocalisationProvider.java b/forge/src/data/java/gripe/_90/megacells/datagen/LocalisationProvider.java deleted file mode 100644 index 3f64390f..00000000 --- a/forge/src/data/java/gripe/_90/megacells/datagen/LocalisationProvider.java +++ /dev/null @@ -1,27 +0,0 @@ -package gripe._90.megacells.datagen; - -import net.minecraft.data.PackOutput; -import net.minecraftforge.common.data.LanguageProvider; - -import gripe._90.megacells.MEGACells; -import gripe._90.megacells.definition.MEGABlocks; -import gripe._90.megacells.definition.MEGAItems; -import gripe._90.megacells.definition.MEGATranslations; - -class LocalisationProvider extends LanguageProvider { - public LocalisationProvider(PackOutput output) { - super(output, MEGACells.MODID, "en_us"); - } - - @Override - protected void addTranslations() { - MEGAItems.getItems().forEach(item -> add(item.asItem(), item.getEnglishName())); - MEGABlocks.getBlocks().forEach(block -> add(block.block(), block.getEnglishName())); - - for (var translation : MEGATranslations.values()) { - add(translation.getTranslationKey(), translation.getEnglishText()); - } - - add("text.autoconfig.%s.title".formatted(MEGACells.MODID), MEGATranslations.ModName.getEnglishText()); - } -} diff --git a/forge/src/data/java/gripe/_90/megacells/datagen/MEGADataGenerators.java b/forge/src/data/java/gripe/_90/megacells/datagen/MEGADataGenerators.java index 32fd4e2d..b2149293 100644 --- a/forge/src/data/java/gripe/_90/megacells/datagen/MEGADataGenerators.java +++ b/forge/src/data/java/gripe/_90/megacells/datagen/MEGADataGenerators.java @@ -28,6 +28,6 @@ public static void onGatherData(GatherDataEvent event) { pack.addProvider(RecipeProvider::new); pack.addProvider(LootTableProvider::new); - pack.addProvider(LocalisationProvider::new); + pack.addProvider(CommonLanguageProvider::new); } }