Skip to content

Commit

Permalink
split sourcesets and move to event based registration
Browse files Browse the repository at this point in the history
  • Loading branch information
Minecraftschurli committed Mar 11, 2024
1 parent f272732 commit 62b7302
Show file tree
Hide file tree
Showing 41 changed files with 370 additions and 394 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ plugins {
id("com.github.minecraftschurlimods.helperplugin")
}

helper.withApiSourceSet()
helper.withDataGenSourceSet()

dependencies {
implementation(helper.neoforge())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.minecraftschurlimods.bibliocraft.api;

import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.fml.loading.FMLLoader;
import net.neoforged.neoforge.common.util.Lazy;
import org.jetbrains.annotations.ApiStatus;
import org.slf4j.LoggerFactory;

import java.util.Optional;
import java.util.ServiceLoader;
import java.util.function.Supplier;

@ApiStatus.NonExtendable
public interface BibliocraftApi {
String MOD_ID = "bibliocraft";

static BibliocraftDatagenHelper getDatagenHelper() {
return InstanceHolder.DATAGEN_HELPER.get();
}

static BibliocraftWoodTypeRegistry getWoodTypeRegistry() {
return InstanceHolder.WOOD_TYPE_REGISTRY.get();
}

@ApiStatus.Internal
final class InstanceHolder {
private InstanceHolder() {}

private static final Lazy<BibliocraftDatagenHelper> DATAGEN_HELPER = Lazy.concurrentOf(fromServiceLoader(BibliocraftDatagenHelper.class));
private static final Lazy<BibliocraftWoodTypeRegistry> WOOD_TYPE_REGISTRY = Lazy.concurrentOf(fromServiceLoader(BibliocraftWoodTypeRegistry.class));

private static <T> Supplier<T> fromServiceLoader(Class<T> clazz) {
return () -> {
Optional<T> impl = ServiceLoader.load(FMLLoader.getGameLayer(), clazz).findFirst();
String msg = "Unable to find implementation for " + clazz.getSimpleName() + "!";
if (!FMLEnvironment.production) {
return impl.orElseThrow(() -> {
IllegalStateException exception = new IllegalStateException(msg);
LoggerFactory.getLogger(MOD_ID).error(exception.getMessage(), exception);
return exception;
});
}
return impl.orElseGet(() -> {
LoggerFactory.getLogger(MOD_ID).error(msg);
return null;
});
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.minecraftschurlimods.bibliocraft.api;

import com.github.minecraftschurlimods.bibliocraft.apiimpl.BibliocraftDatagenHelperImpl;
import net.minecraft.data.loot.BlockLootSubProvider;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.data.tags.IntrinsicHolderTagsProvider;
Expand Down Expand Up @@ -93,20 +92,13 @@ public interface BibliocraftDatagenHelper {
*/
void generateRecipesFor(RecipeOutput output, BibliocraftWoodType woodType);

/**
* @return The only instance of this class.
*/
static BibliocraftDatagenHelper get() {
return BibliocraftDatagenHelperImpl.get();
}

/**
* Marks all {@link BibliocraftWoodType}s from the given mod as to-be-datagenned. This method is thread-safe.
*
* @param modid The id of the mod to mark the {@link BibliocraftWoodType}s of.
*/
default void addWoodTypesToGenerateByModid(String modid) {
BibliocraftWoodTypeRegistry.get().getAll().stream()
BibliocraftApi.getWoodTypeRegistry().getAll().stream()
.filter(e -> e.getNamespace().equals(modid))
.forEach(this::addWoodTypeToGenerate);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.minecraftschurlimods.bibliocraft.api;

import net.minecraft.data.BlockFamily;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.WoodType;

import java.util.function.Supplier;

public record BibliocraftWoodType(
ResourceLocation id,
WoodType woodType,
Supplier<BlockBehaviour.Properties> properties,
ResourceLocation texture,
BlockFamily family
) {
/**
* @return The namespace of the id of this wood type.
*/
public String getNamespace() {
return id().getNamespace();
}

/**
* @return The path of the id of this wood type.
*/
public String getPath() {
return id().getPath();
}

/**
* @return The wood type prefix used for registration. Keeps the mod id for cases when two mods add identically named wood types.
*/
public String getRegistrationPrefix() {
return getNamespace().equals("minecraft") ? getPath() : id().toString().replace(':', '_');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.minecraftschurlimods.bibliocraft.api;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.properties.WoodType;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

/**
* The registry for {@link BibliocraftWoodType}s. Register a new wood type by subscribing to {@link RegisterBibliocraftWoodTypesEvent} and calling {@code register(...)} on it.
* Make sure to put this call behind a {@code ModList.isLoaded("bibliocraft")} check, and in a separate class, to prevent accidental classloading (like you would with client classes).
*/
public interface BibliocraftWoodTypeRegistry {

/**
* @param id The id of the wood type to get.
* @return The wood type with the given id. May return null if no wood type with the given id exists.
*/
@Nullable
BibliocraftWoodType get(ResourceLocation id);

/**
* @param id The id of the wood type to get.
* @return The wood type with the given id. May return null if no wood type with the given id exists.
*/
@Nullable
default BibliocraftWoodType get(String id) {
return get(new ResourceLocation(id));
}

/**
* @param woodType The vanilla wood type to get the wood type for.
* @return The wood type for the given vanilla wood type. May return null if no wood type for it exists.
*/
@Nullable
default BibliocraftWoodType get(WoodType woodType) {
return get(woodType.name());
}

/**
* @return An unmodifiable list of all registered wood types. Must only be called after registration is finished.
*/
Collection<BibliocraftWoodType> getAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.minecraftschurlimods.bibliocraft.api;

import net.minecraft.data.BlockFamily;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.neoforged.bus.api.Event;
import net.neoforged.fml.event.IModBusEvent;
import org.jetbrains.annotations.ApiStatus;

import java.util.Map;
import java.util.function.Supplier;

public class RegisterBibliocraftWoodTypesEvent extends Event implements IModBusEvent {
private final Map<ResourceLocation, BibliocraftWoodType> woodTypes;

@ApiStatus.Internal
public RegisterBibliocraftWoodTypesEvent(Map<ResourceLocation, BibliocraftWoodType> woodTypes) {
this.woodTypes = woodTypes;
}

public void register(WoodType woodType, Supplier<BlockBehaviour.Properties> properties, ResourceLocation texture, BlockFamily family) {
register(new ResourceLocation(woodType.name()), woodType, properties, texture, family);
}

/**
* Registers a new wood type. Must be called during the mod constructor.
*
* @param id The id of the wood type. Should be the id of the mod the wood type comes from, and the name of the wood type.
* @param woodType The vanilla {@link WoodType} associated with this wood type.
* @param properties A supplier for the {@link BlockBehaviour.Properties} associated with this wood type. Typically, this is a copy of the wood type's planks' properties.
* @param texture The location of the wood type's planks texture, for use in datagen.
* @param family The {@link BlockFamily} for the associated wood type, for use in datagen.
*/
public void register(ResourceLocation id, WoodType woodType, Supplier<BlockBehaviour.Properties> properties, ResourceLocation texture, BlockFamily family) {
if (this.woodTypes.containsKey(id))
throw new IllegalStateException("Wood type " + id + " is already registered");
System.out.println("Registering wood type " + woodType.name());
this.woodTypes.put(id, new BibliocraftWoodType(id, woodType, properties, texture, family));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.minecraftschurlimods.bibliocraft.datagen;

import com.github.minecraftschurlimods.bibliocraft.Bibliocraft;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
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 All @@ -20,15 +19,15 @@

import java.util.concurrent.CompletableFuture;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, modid = Bibliocraft.MOD_ID)
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD, modid = BibliocraftApi.MOD_ID)
public final class BCDatagen {
@SubscribeEvent
private static void gatherData(GatherDataEvent event) {
DataGenerator generator = event.getGenerator();
PackOutput output = generator.getPackOutput();
ExistingFileHelper existingFileHelper = event.getExistingFileHelper();
CompletableFuture<HolderLookup.Provider> lookupProvider = event.getLookupProvider();
BibliocraftDatagenHelper.get().addWoodTypesToGenerateByModid("minecraft");
BibliocraftApi.getDatagenHelper().addWoodTypesToGenerateByModid("minecraft");

generator.addProvider(event.includeClient(), new BCEnglishLanguageProvider(output));
generator.addProvider(event.includeClient(), new BCBlockStateProvider(output, existingFileHelper));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.minecraftschurlimods.bibliocraft.datagen.assets;

import com.github.minecraftschurlimods.bibliocraft.Bibliocraft;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.content.cookiejar.CookieJarBlock;
import com.github.minecraftschurlimods.bibliocraft.content.table.TableBlock;
import com.github.minecraftschurlimods.bibliocraft.init.BCBlocks;
Expand All @@ -14,12 +13,12 @@

public class BCBlockStateProvider extends BlockStateProvider {
public BCBlockStateProvider(PackOutput output, ExistingFileHelper exFileHelper) {
super(output, Bibliocraft.MOD_ID, exFileHelper);
super(output, BibliocraftApi.MOD_ID, exFileHelper);
}

@Override
protected void registerStatesAndModels() {
BibliocraftDatagenHelper.get().generateBlockStates(this);
BibliocraftApi.getDatagenHelper().generateBlockStates(this);
getVariantBuilder(BCBlocks.COOKIE_JAR.get()).forAllStates(state -> ConfiguredModel.builder()
.modelFile(models().getExistingFile(modLoc("block/template/cookie_jar" + (state.getValue(CookieJarBlock.OPEN) ? "_open" : ""))))
.build());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.minecraftschurlimods.bibliocraft.datagen.assets;

import com.github.minecraftschurlimods.bibliocraft.Bibliocraft;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.init.BCBlocks;
import com.github.minecraftschurlimods.bibliocraft.init.BCItems;
import com.github.minecraftschurlimods.bibliocraft.util.Translations;
Expand All @@ -11,12 +10,12 @@
@SuppressWarnings("SameParameterValue")
public class BCEnglishLanguageProvider extends LanguageProvider {
public BCEnglishLanguageProvider(PackOutput output) {
super(output, Bibliocraft.MOD_ID, "en_us");
super(output, BibliocraftApi.MOD_ID, "en_us");
}

@Override
protected void addTranslations() {
BibliocraftDatagenHelper.get().generateEnglishTranslations(this);
BibliocraftApi.getDatagenHelper().generateEnglishTranslations(this);
add(BCBlocks.COOKIE_JAR.get(), "Cookie Jar");
add(BCBlocks.DESK_BELL.get(), "Desk Bell");
add(BCBlocks.IRON_FANCY_ARMOR_STAND.get(), "Iron Fancy Armor Stand");
Expand All @@ -29,7 +28,7 @@ protected void addTranslations() {
add("container", "potion_shelf", "Potion Shelf");
add("container", "shelf", "Shelf");
add("container", "tool_rack", "Tool Rack");
add("itemGroup." + Bibliocraft.MOD_ID, "Bibliocraft");
add("itemGroup." + BibliocraftApi.MOD_ID, "Bibliocraft");
add(Translations.REDSTONE_BOOK_TITLE, "Redstone: Volume 1");
add(Translations.REDSTONE_BOOK_TEXT, "When putting this book into a bookcase, the bookcase will emit a redstone signal. The strength of the signal depends on the slot it is placed in. Slot 1 (top left) yields no signal. Slot 2 yields a signal strength of 1. Each slot after that increases the signal strength by one, all the way to slot 16 (bottom right), which yields a signal strength of 15.");
}
Expand All @@ -42,6 +41,6 @@ protected void addTranslations() {
* @param translation The translated string.
*/
private void add(String type, String name, String translation) {
add(type + "." + Bibliocraft.MOD_ID + "." + name, translation);
add(type + "." + BibliocraftApi.MOD_ID + "." + name, translation);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package com.github.minecraftschurlimods.bibliocraft.datagen.assets;

import com.github.minecraftschurlimods.bibliocraft.Bibliocraft;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import net.minecraft.data.PackOutput;
import net.neoforged.neoforge.client.model.generators.ItemModelProvider;
import net.neoforged.neoforge.common.data.ExistingFileHelper;

public class BCItemModelProvider extends ItemModelProvider {
public BCItemModelProvider(PackOutput output, ExistingFileHelper existingFileHelper) {
super(output, Bibliocraft.MOD_ID, existingFileHelper);
super(output, BibliocraftApi.MOD_ID, existingFileHelper);
}

@Override
protected void registerModels() {
BibliocraftDatagenHelper.get().generateItemModels(this);
BibliocraftApi.getDatagenHelper().generateItemModels(this);
withExistingParent("cookie_jar", modLoc("block/template/cookie_jar"));
withExistingParent("desk_bell", modLoc("block/desk_bell"));
withExistingParent("iron_fancy_armor_stand", modLoc("block/template/fancy_armor_stand/iron_inventory"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.minecraftschurlimods.bibliocraft.datagen.assets;

import com.github.minecraftschurlimods.bibliocraft.Bibliocraft;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.init.BCSoundEvents;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -10,16 +10,16 @@

public class BCSoundDefinitionsProvider extends SoundDefinitionsProvider {
public BCSoundDefinitionsProvider(PackOutput output, ExistingFileHelper helper) {
super(output, Bibliocraft.MOD_ID, helper);
super(output, BibliocraftApi.MOD_ID, helper);
}

@Override
public void registerSounds() {
add(BCSoundEvents.DESK_BELL.get(), SoundDefinition.definition().with(
sound(new ResourceLocation(Bibliocraft.MOD_ID, "desk_bell_1")),
sound(new ResourceLocation(Bibliocraft.MOD_ID, "desk_bell_2")),
sound(new ResourceLocation(Bibliocraft.MOD_ID, "desk_bell_3")),
sound(new ResourceLocation(Bibliocraft.MOD_ID, "desk_bell_4"))
sound(new ResourceLocation(BibliocraftApi.MOD_ID, "desk_bell_1")),
sound(new ResourceLocation(BibliocraftApi.MOD_ID, "desk_bell_2")),
sound(new ResourceLocation(BibliocraftApi.MOD_ID, "desk_bell_3")),
sound(new ResourceLocation(BibliocraftApi.MOD_ID, "desk_bell_4"))
));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.minecraftschurlimods.bibliocraft.datagen.data;

import com.github.minecraftschurlimods.bibliocraft.Bibliocraft;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftDatagenHelper;
import com.github.minecraftschurlimods.bibliocraft.api.BibliocraftApi;
import com.github.minecraftschurlimods.bibliocraft.init.BCBlocks;
import com.github.minecraftschurlimods.bibliocraft.init.BCTags;
import net.minecraft.core.HolderLookup;
Expand All @@ -14,13 +13,13 @@

public class BCBlockTagsProvider extends BlockTagsProvider {
public BCBlockTagsProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> lookupProvider, ExistingFileHelper existingFileHelper) {
super(output, lookupProvider, Bibliocraft.MOD_ID, existingFileHelper);
super(output, lookupProvider, BibliocraftApi.MOD_ID, existingFileHelper);
}

@SuppressWarnings("unchecked")
@Override
protected void addTags(HolderLookup.Provider lookupProvider) {
BibliocraftDatagenHelper.get().generateBlockTags(this::tag);
BibliocraftApi.getDatagenHelper().generateBlockTags(this::tag);
tag(BCTags.Blocks.FANCY_ARMOR_STANDS).addTag(BCTags.Blocks.FANCY_ARMOR_STANDS_WOOD).add(BCBlocks.IRON_FANCY_ARMOR_STAND.get());
tag(BlockTags.MINEABLE_WITH_AXE).addTags(BCTags.Blocks.BOOKCASES, BCTags.Blocks.DISPLAY_CASES, BCTags.Blocks.FANCY_ARMOR_STANDS_WOOD, BCTags.Blocks.LABELS, BCTags.Blocks.POTION_SHELVES, BCTags.Blocks.SEATS, BCTags.Blocks.SHELVES, BCTags.Blocks.TOOL_RACKS);
tag(BlockTags.MINEABLE_WITH_PICKAXE).add(BCBlocks.COOKIE_JAR.get(), BCBlocks.DESK_BELL.get(), BCBlocks.IRON_FANCY_ARMOR_STAND.get(), BCBlocks.SWORD_PEDESTAL.get());
Expand Down
Loading

0 comments on commit 62b7302

Please sign in to comment.