generated from neoforged/MDK
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split sourcesets and move to event based registration
- Loading branch information
1 parent
f272732
commit 62b7302
Showing
41 changed files
with
370 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/api/java/com/github/minecraftschurlimods/bibliocraft/api/BibliocraftApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/api/java/com/github/minecraftschurlimods/bibliocraft/api/BibliocraftWoodType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(':', '_'); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...api/java/com/github/minecraftschurlimods/bibliocraft/api/BibliocraftWoodTypeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
41 changes: 41 additions & 0 deletions
41
...va/com/github/minecraftschurlimods/bibliocraft/api/RegisterBibliocraftWoodTypesEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 3 additions & 4 deletions
7
...t/datagen/assets/BCItemModelProvider.java → ...t/datagen/assets/BCItemModelProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.