Skip to content

Commit

Permalink
fix the datagen helper to properly work with addons
Browse files Browse the repository at this point in the history
  • Loading branch information
IchHabeHunger54 committed Oct 16, 2024
1 parent 4723c43 commit b1c421b
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 55 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.configuration-cache=true

mod_group=com.github.minecraftschurlimods
mod_id=bibliocraft
mod_version=1.0.0
mod_version=1.1.0
mod_name=Bibliocraft Legacy
mod_vendor=MinecraftschurliMods
mod_authors=IchHabeHunger54, Minecraftschurli
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.minecraftschurlimods.bibliocraft.api;

import com.github.minecraftschurlimods.bibliocraft.api.datagen.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.lockandkey.LockAndKeyBehaviors;
import com.github.minecraftschurlimods.bibliocraft.api.woodtype.BibliocraftWoodTypeRegistry;
import net.neoforged.fml.loading.FMLEnvironment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.minecraftschurlimods.bibliocraft.api;
package com.github.minecraftschurlimods.bibliocraft.api.datagen;

import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.api.woodtype.BibliocraftWoodType;
import net.minecraft.data.loot.BlockLootSubProvider;
import net.minecraft.data.recipes.RecipeOutput;
Expand Down Expand Up @@ -95,14 +96,16 @@ public interface BibliocraftDatagenHelper {
void generateRecipesFor(RecipeOutput output, BibliocraftWoodType woodType, String modId);

/**
* Generates block and item models, block and item tags, loot tables, and recipes for Bibliocraft blocks with a {@link BibliocraftWoodType}. Call this directly from a {@link GatherDataEvent} handler!
* Note: Language files are not created by this method because they would overwrite each other (since they all operate on the same file). Call the language helpers from your own language provider instead.
* Generates language files, block and item models, block and item tags, loot tables, and recipes for Bibliocraft blocks with a {@link BibliocraftWoodType}. Call this directly from a {@link GatherDataEvent} handler!
*
* @param woodType The {@link BibliocraftWoodType} to generate the files for.
* @param modId The namespace to store the files under, where applicable.
* @param event The {@link GatherDataEvent} whose handler this is called from.
* @param woodType The {@link BibliocraftWoodType} to generate the files for.
* @param modId The namespace to store the files under, where applicable.
* @param event The {@link GatherDataEvent} whose handler this is called from.
* @param englishLanguageProvider The {@link LanguageProvider} to use for generating english translations.
* @param blockTagsProvider The {@link BlockTagsProvider} to use for generating block tags. It is strongly suggested this be a {@link NonClearingBlockTagsProvider}.
* @param itemTagsProvider The {@link ItemTagsProvider} to use for generating item tags. It is strongly suggested this be a {@link NonClearingItemTagsProvider}.
*/
void generateAllFor(BibliocraftWoodType woodType, String modId, GatherDataEvent event);
void generateAllFor(BibliocraftWoodType woodType, String modId, GatherDataEvent event, LanguageProvider englishLanguageProvider, BlockTagsProvider blockTagsProvider, ItemTagsProvider itemTagsProvider);

/**
* Marks all {@link BibliocraftWoodType}s from the given mod as to-be-datagenned. This method is thread-safe.
Expand Down Expand Up @@ -180,13 +183,15 @@ default void generateRecipes(RecipeOutput output, String modId) {
}

/**
* Generates block and item models, block and item tags, loot tables, and recipes for Bibliocraft blocks with your mod's wood type(s). Call this directly from a {@link GatherDataEvent} handler!
* Note: Language files are not created by this method because they would overwrite each other (since they all operate on the same file). Call the language helpers from your own language provider instead.
* Generates language files, block and item models, block and item tags, loot tables, and recipes for Bibliocraft blocks with your mod's wood type(s). Call this directly from a {@link GatherDataEvent} handler!
*
* @param modId The namespace to store the files under, where applicable.
* @param event The {@link GatherDataEvent} whose handler this is called from.
* @param modId The namespace to store the files under, where applicable.
* @param event The {@link GatherDataEvent} whose handler this is called from.
* @param englishLanguageProvider The {@link LanguageProvider} to use for generating english translations.
* @param blockTagsProvider The {@link BlockTagsProvider} to use for generating block tags. It is strongly suggested this be a {@link NonClearingBlockTagsProvider}.
* @param itemTagsProvider The {@link ItemTagsProvider} to use for generating item tags. It is strongly suggested this be a {@link NonClearingItemTagsProvider}.
*/
default void generateAll(String modId, GatherDataEvent event) {
getWoodTypesToGenerate().forEach(woodType -> generateAllFor(woodType, modId, event));
default void generateAll(String modId, GatherDataEvent event, LanguageProvider englishLanguageProvider, BlockTagsProvider blockTagsProvider, ItemTagsProvider itemTagsProvider) {
getWoodTypesToGenerate().forEach(woodType -> generateAllFor(woodType, modId, event, englishLanguageProvider, blockTagsProvider, itemTagsProvider));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.minecraftschurlimods.bibliocraft.api.datagen;

import net.minecraft.core.HolderLookup;
import net.minecraft.data.PackOutput;
import net.neoforged.neoforge.common.data.BlockTagsProvider;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.CompletableFuture;

/**
* The default {@link BlockTagsProvider} implementation clears the builders before calling {@link BlockTagsProvider#addTags(HolderLookup.Provider)}.
* We don't want that, so we override {@link BlockTagsProvider#addTags(HolderLookup.Provider)} to not do that.
*/
public abstract class NonClearingBlockTagsProvider extends BlockTagsProvider {
// Store the provider here because while the superclass has it, it is private there.
private final CompletableFuture<HolderLookup.Provider> lookupProvider;

/**
* See super constructor for information.
*/
public NonClearingBlockTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> lookupProvider, String modId, @Nullable ExistingFileHelper existingFileHelper) {
super(output, lookupProvider, modId, existingFileHelper);
this.lookupProvider = lookupProvider;
}

@Override
protected CompletableFuture<HolderLookup.Provider> createContentsProvider() {
return lookupProvider.thenApply(provider -> {
addTags(provider);
return provider;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.github.minecraftschurlimods.bibliocraft.api.datagen;

import net.minecraft.core.HolderLookup;
import net.minecraft.data.PackOutput;
import net.minecraft.data.tags.ItemTagsProvider;
import net.minecraft.tags.TagBuilder;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
import java.util.concurrent.CompletableFuture;

/**
* The default {@link ItemTagsProvider} implementation clears the builders before calling {@link ItemTagsProvider#addTags(HolderLookup.Provider)}.
* We don't want that, so we override {@link ItemTagsProvider#addTags(HolderLookup.Provider)} to not do that.
*/
public abstract class NonClearingItemTagsProvider extends ItemTagsProvider {
// Store the provider here because while the superclass has it, it is private there.
private final CompletableFuture<HolderLookup.Provider> lookupProvider;

/**
* See super constructor for information.
*/
public NonClearingItemTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> lookupProvider, CompletableFuture<TagLookup<Block>> blockTags) {
super(output, lookupProvider, blockTags);
this.lookupProvider = lookupProvider;
}

/**
* See super constructor for information.
*/
@Deprecated
public NonClearingItemTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> lookupProvider, CompletableFuture<TagLookup<Item>> parentProvider, CompletableFuture<TagLookup<Block>> blockTags) {
super(output, lookupProvider, parentProvider, blockTags);
this.lookupProvider = lookupProvider;
}

/**
* See super constructor for information.
*/
public NonClearingItemTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> lookupProvider, CompletableFuture<TagLookup<Block>> blockTags, String modId, @Nullable ExistingFileHelper existingFileHelper) {
super(output, lookupProvider, blockTags, modId, existingFileHelper);
this.lookupProvider = lookupProvider;
}

/**
* See super constructor for information.
*/
public NonClearingItemTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> lookupProvider, CompletableFuture<TagLookup<Item>> parentProvider, CompletableFuture<TagLookup<Block>> blockTags, String modId, @Nullable ExistingFileHelper existingFileHelper) {
super(output, lookupProvider, parentProvider, blockTags, modId, existingFileHelper);
this.lookupProvider = lookupProvider;
}

@Override
protected CompletableFuture<HolderLookup.Provider> createContentsProvider() {
return lookupProvider.thenApply(provider -> {
addTags(provider);
return provider;
}).thenCombine(blockTags, (provider, tagLookup) -> {
tagsToCopy.forEach((block, item) -> {
TagBuilder tagBuilder = getOrCreateRawBuilder(item);
tagLookup.apply(block).orElseThrow(() -> new IllegalStateException("Missing block tag " + item.location())).build().forEach(tagBuilder::add);
});
return provider;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.minecraftschurlimods.bibliocraft.datagen;

import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.datagen.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.datagen.assets.BCBlockStateProvider;
import com.github.minecraftschurlimods.bibliocraft.datagen.assets.BCEnglishLanguageProvider;
import com.github.minecraftschurlimods.bibliocraft.datagen.assets.BCItemModelProvider;
Expand Down Expand Up @@ -29,18 +29,18 @@ private static void gatherData(GatherDataEvent event) {
ExistingFileHelper existingFileHelper = event.getExistingFileHelper();
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();

BibliocraftDatagenHelper helper = BibliocraftApi.getDatagenHelper();
helper.addWoodTypesToGenerateByModid("minecraft");
helper.generateAll(BibliocraftApi.MOD_ID, event);

generator.addProvider(event.includeClient(), new BCEnglishLanguageProvider(output));
BCEnglishLanguageProvider language = generator.addProvider(event.includeClient(), new BCEnglishLanguageProvider(output));
generator.addProvider(event.includeClient(), new BCBlockStateProvider(output, existingFileHelper));
generator.addProvider(event.includeClient(), new BCItemModelProvider(output, existingFileHelper));
generator.addProvider(event.includeClient(), new BCSoundDefinitionsProvider(output, existingFileHelper));

generator.addProvider(event.includeServer(), new BCLootTableProvider(output, lookupProvider));
generator.addProvider(event.includeServer(), new BCRecipeProvider(output, lookupProvider));
BCBlockTagsProvider blockTags = generator.addProvider(event.includeServer(), new BCBlockTagsProvider(output, lookupProvider, existingFileHelper));
generator.addProvider(event.includeServer(), new BCItemTagsProvider(output, lookupProvider, blockTags.contentsGetter(), existingFileHelper));
BCItemTagsProvider itemTags = generator.addProvider(event.includeServer(), new BCItemTagsProvider(output, lookupProvider, blockTags.contentsGetter(), existingFileHelper));

BibliocraftDatagenHelper helper = BibliocraftApi.getDatagenHelper();
helper.addWoodTypesToGenerateByModid("minecraft");
helper.generateAll(BibliocraftApi.MOD_ID, event, language, blockTags, itemTags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public BCEnglishLanguageProvider(PackOutput output) {

@Override
protected void addTranslations() {
BibliocraftApi.getDatagenHelper().generateEnglishTranslations(this);
add(BCBlocks.CLEAR_FANCY_GOLD_LAMP.get(), "Fancy Gold Lamp");
add(BCBlocks.CLEAR_FANCY_IRON_LAMP.get(), "Fancy Iron Lamp");
add(BCBlocks.CLEAR_FANCY_GOLD_LANTERN.get(), "Fancy Gold Lantern");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.github.minecraftschurlimods.bibliocraft.datagen.data;

import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.api.datagen.NonClearingBlockTagsProvider;
import com.github.minecraftschurlimods.bibliocraft.init.BCBlocks;
import com.github.minecraftschurlimods.bibliocraft.init.BCTags;
import com.github.minecraftschurlimods.bibliocraft.util.DatagenUtil;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.registries.Registries;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.BlockTags;
import net.minecraft.tags.TagBuilder;
import net.minecraft.tags.TagKey;
import net.neoforged.neoforge.common.data.BlockTagsProvider;
import net.neoforged.neoforge.common.data.ExistingFileHelper;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

public final class BCBlockTagsProvider extends BlockTagsProvider {
public final class BCBlockTagsProvider extends NonClearingBlockTagsProvider {
public BCBlockTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> lookupProvider, ExistingFileHelper existingFileHelper) {
super(output, lookupProvider, BibliocraftApi.MOD_ID, existingFileHelper);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.minecraftschurlimods.bibliocraft.datagen.data;

import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.api.datagen.NonClearingItemTagsProvider;
import com.github.minecraftschurlimods.bibliocraft.init.BCItems;
import com.github.minecraftschurlimods.bibliocraft.init.BCTags;
import net.minecraft.core.HolderLookup;
Expand All @@ -15,7 +16,7 @@

import java.util.concurrent.CompletableFuture;

public final class BCItemTagsProvider extends ItemTagsProvider {
public final class BCItemTagsProvider extends NonClearingItemTagsProvider {
public BCItemTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> lookupProvider, CompletableFuture<TagLookup<Block>> lookup, ExistingFileHelper existingFileHelper) {
super(output, lookupProvider, lookup, BibliocraftApi.MOD_ID, existingFileHelper);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.minecraftschurlimods.bibliocraft.apiimpl;

import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.datagen.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.woodtype.BibliocraftWoodType;
import com.github.minecraftschurlimods.bibliocraft.client.model.TableModel;
import com.github.minecraftschurlimods.bibliocraft.content.seat.SeatBackBlock;
Expand Down Expand Up @@ -87,16 +87,17 @@ public List<BibliocraftWoodType> getWoodTypesToGenerate() {
}

@Override
public void generateAllFor(BibliocraftWoodType woodType, String modId, GatherDataEvent event) {
public void generateAllFor(BibliocraftWoodType woodType, String modId, GatherDataEvent event, LanguageProvider englishLanguageProvider, BlockTagsProvider blockTagsProvider, ItemTagsProvider itemTagsProvider) {
DataGenerator generator = event.getGenerator();
PackOutput output = generator.getPackOutput();
ExistingFileHelper existingFileHelper = event.getExistingFileHelper();
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();

generateEnglishTranslationsFor(englishLanguageProvider, woodType);
generator.addProvider(event.includeClient(), new BlockStateProvider(output, BibliocraftApi.MOD_ID, existingFileHelper) {
@Override
protected void registerStatesAndModels() {
BibliocraftApi.getDatagenHelper().generateBlockStates(this);
generateBlockStatesFor(this, woodType);
}

@Override
Expand All @@ -107,7 +108,7 @@ public String getName() {
generator.addProvider(event.includeClient(), new ItemModelProvider(output, BibliocraftApi.MOD_ID, existingFileHelper) {
@Override
protected void registerModels() {
BibliocraftApi.getDatagenHelper().generateItemModels(this);
generateItemModelsFor(this, woodType);
}

@Override
Expand All @@ -120,7 +121,7 @@ public String getName() {

@Override
protected void generate() {
BibliocraftApi.getDatagenHelper().generateLootTables(this::add);
generateLootTablesFor(this::add, woodType);
}

@Override
Expand All @@ -142,36 +143,16 @@ public String getName() {
generator.addProvider(event.includeServer(), new RecipeProvider(output, lookupProvider) {
@Override
protected void buildRecipes(RecipeOutput output) {
BibliocraftApi.getDatagenHelper().generateRecipes(output, modId);
}

@Override
public String getName() {
return super.getName() + " (Bibliocraft datagen helper for wood type " + woodType.id() + ")";
}
});
var blockTags = generator.addProvider(event.includeServer(), new BlockTagsProvider(output, lookupProvider, modId, existingFileHelper) {
@Override
protected void addTags(HolderLookup.Provider provider) {
BibliocraftApi.getDatagenHelper().generateBlockTags(this::tag);
}

@Override
public String getName() {
return super.getName() + " (Bibliocraft datagen helper for wood type " + woodType.id() + ")";
}
});
generator.addProvider(event.includeServer(), new ItemTagsProvider(output, lookupProvider, blockTags.contentsGetter(), modId, existingFileHelper) {
@Override
protected void addTags(HolderLookup.Provider provider) {
BibliocraftApi.getDatagenHelper().generateItemTags(this::tag);
generateRecipesFor(output, woodType, modId);
}

@Override
public String getName() {
return super.getName() + " (Bibliocraft datagen helper for wood type " + woodType.id() + ")";
}
});
generateBlockTagsFor(blockTagsProvider::tag, woodType);
generateItemTagsFor(itemTagsProvider::tag, woodType);
}

@Override
Expand Down
Loading

0 comments on commit b1c421b

Please sign in to comment.