diff --git a/common/src/main/java/net/blay09/mods/balm/api/Balm.java b/common/src/main/java/net/blay09/mods/balm/api/Balm.java index 5a09e5bc..cce599a7 100644 --- a/common/src/main/java/net/blay09/mods/balm/api/Balm.java +++ b/common/src/main/java/net/blay09/mods/balm/api/Balm.java @@ -3,6 +3,7 @@ import net.blay09.mods.balm.api.block.BalmBlockEntities; import net.blay09.mods.balm.api.block.BalmBlocks; import net.blay09.mods.balm.api.command.BalmCommands; +import net.blay09.mods.balm.api.component.BalmComponents; import net.blay09.mods.balm.api.config.BalmConfig; import net.blay09.mods.balm.api.entity.BalmEntities; import net.blay09.mods.balm.api.event.BalmEvents; @@ -87,6 +88,10 @@ public static BalmItems getItems() { return runtime.getItems(); } + public static BalmComponents getComponents() { + return runtime.getComponents(); + } + public static BalmMenus getMenus() { return runtime.getMenus(); } diff --git a/common/src/main/java/net/blay09/mods/balm/api/BalmRuntime.java b/common/src/main/java/net/blay09/mods/balm/api/BalmRuntime.java index 054f5685..bc748453 100644 --- a/common/src/main/java/net/blay09/mods/balm/api/BalmRuntime.java +++ b/common/src/main/java/net/blay09/mods/balm/api/BalmRuntime.java @@ -3,6 +3,7 @@ import net.blay09.mods.balm.api.block.BalmBlockEntities; import net.blay09.mods.balm.api.block.BalmBlocks; import net.blay09.mods.balm.api.command.BalmCommands; +import net.blay09.mods.balm.api.component.BalmComponents; import net.blay09.mods.balm.api.config.BalmConfig; import net.blay09.mods.balm.api.entity.BalmEntities; import net.blay09.mods.balm.api.event.BalmEvents; @@ -67,4 +68,6 @@ public interface BalmRuntime { void addServerReloadListener(ResourceLocation identifier, PreparableReloadListener reloadListener); void addServerReloadListener(ResourceLocation identifier, Consumer reloadListener); + + BalmComponents getComponents(); } diff --git a/common/src/main/java/net/blay09/mods/balm/api/component/BalmComponents.java b/common/src/main/java/net/blay09/mods/balm/api/component/BalmComponents.java new file mode 100644 index 00000000..c999fda6 --- /dev/null +++ b/common/src/main/java/net/blay09/mods/balm/api/component/BalmComponents.java @@ -0,0 +1,11 @@ +package net.blay09.mods.balm.api.component; + +import net.blay09.mods.balm.api.DeferredObject; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.resources.ResourceLocation; + +import java.util.function.Supplier; + +public interface BalmComponents { + DeferredObject> registerComponent(Supplier> supplier, ResourceLocation identifier); +} diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalmRuntime.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalmRuntime.java index 1c5cd4d5..cd4893a4 100644 --- a/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalmRuntime.java +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalmRuntime.java @@ -7,11 +7,13 @@ import net.blay09.mods.balm.api.block.BalmBlockEntities; import net.blay09.mods.balm.api.block.BalmBlocks; import net.blay09.mods.balm.api.command.BalmCommands; +import net.blay09.mods.balm.api.component.BalmComponents; import net.blay09.mods.balm.api.config.BalmConfig; import net.blay09.mods.balm.api.entity.BalmEntities; import net.blay09.mods.balm.api.event.BalmEvents; import net.blay09.mods.balm.api.recipe.BalmRecipes; import net.blay09.mods.balm.api.stats.BalmStats; +import net.blay09.mods.balm.fabric.component.FabricBalmComponents; import net.blay09.mods.balm.fabric.event.FabricBalmCommonEvents; import net.blay09.mods.balm.fabric.event.FabricBalmEvents; import net.blay09.mods.balm.api.item.BalmItems; @@ -71,6 +73,7 @@ public class FabricBalmRuntime implements BalmRuntime { private final BalmLootTables lootTables = new CommonBalmLootTables(); private final BalmStats stats = new FabricBalmStats(); private final BalmRecipes recipes = new FabricBalmRecipes(); + private final BalmComponents components = new FabricBalmComponents(); public FabricBalmRuntime() { FabricBalmCommonEvents.registerEvents(events); @@ -232,4 +235,9 @@ public ResourceLocation getFabricId() { } }); } + + @Override + public BalmComponents getComponents() { + return components; + } } diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/component/FabricBalmComponents.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/component/FabricBalmComponents.java new file mode 100644 index 00000000..84a97454 --- /dev/null +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/component/FabricBalmComponents.java @@ -0,0 +1,17 @@ +package net.blay09.mods.balm.fabric.component; + +import net.blay09.mods.balm.api.DeferredObject; +import net.blay09.mods.balm.api.component.BalmComponents; +import net.minecraft.core.Registry; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; + +import java.util.function.Supplier; + +public class FabricBalmComponents implements BalmComponents { + @Override + public DeferredObject> registerComponent(Supplier> supplier, ResourceLocation identifier) { + return new DeferredObject<>(identifier, () -> Registry.register(BuiltInRegistries.DATA_COMPONENT_TYPE, identifier, supplier.get())).resolveImmediately(); + } +} diff --git a/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalmRuntime.java b/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalmRuntime.java index 8f121e9c..4f885c25 100644 --- a/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalmRuntime.java +++ b/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalmRuntime.java @@ -7,12 +7,14 @@ import net.blay09.mods.balm.api.block.BalmBlockEntities; import net.blay09.mods.balm.api.block.BalmBlocks; import net.blay09.mods.balm.api.command.BalmCommands; +import net.blay09.mods.balm.api.component.BalmComponents; import net.blay09.mods.balm.api.config.BalmConfig; import net.blay09.mods.balm.api.entity.BalmEntities; import net.blay09.mods.balm.api.event.BalmEvents; import net.blay09.mods.balm.api.recipe.BalmRecipes; import net.blay09.mods.balm.api.stats.BalmStats; import net.blay09.mods.balm.common.CommonBalmLootTables; +import net.blay09.mods.balm.forge.component.ForgeBalmComponents; import net.blay09.mods.balm.forge.event.ForgeBalmEvents; import net.blay09.mods.balm.api.item.BalmItems; import net.blay09.mods.balm.api.loot.BalmLootTables; @@ -70,6 +72,7 @@ public class ForgeBalmRuntime implements BalmRuntime { private final BalmLootTables lootTables = new CommonBalmLootTables(); private final BalmStats stats = new ForgeBalmStats(); private final BalmRecipes recipes = new ForgeBalmRecipes(); + private final BalmComponents components = new ForgeBalmComponents(); private final List addonClasses = new ArrayList<>(); @@ -177,6 +180,7 @@ public void initialize(String modId, EmptyLoadContext context, Runnable initiali ((ForgeBalmItems) items).register(); ((ForgeBalmEntities) entities).register(); ((ForgeBalmWorldGen) worldGen).register(); + ((ForgeBalmComponents) components).register(); ((ForgeBalmStats) stats).register(); initializer.run(); @@ -230,4 +234,8 @@ public void addServerReloadListener(ResourceLocation identifier, Consumer event.addListener((ResourceManagerReloadListener) reloadListener::accept)); } + @Override + public BalmComponents getComponents() { + return components; + } } \ No newline at end of file diff --git a/forge/src/main/java/net/blay09/mods/balm/forge/component/ForgeBalmComponents.java b/forge/src/main/java/net/blay09/mods/balm/forge/component/ForgeBalmComponents.java new file mode 100644 index 00000000..9c5d3258 --- /dev/null +++ b/forge/src/main/java/net/blay09/mods/balm/forge/component/ForgeBalmComponents.java @@ -0,0 +1,51 @@ +package net.blay09.mods.balm.forge.component; + +import net.blay09.mods.balm.api.DeferredObject; +import net.blay09.mods.balm.api.component.BalmComponents; +import net.minecraft.core.Registry; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.resources.ResourceLocation; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModLoadingContext; +import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; +import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; + +public class ForgeBalmComponents implements BalmComponents { + + private static class Registrations { + public final List> dataComponentTypes = new ArrayList<>(); + + @SubscribeEvent + public void commonSetup(FMLCommonSetupEvent event) { + event.enqueueWork(() -> dataComponentTypes.forEach(DeferredObject::resolve)); + } + } + + private final Map registrations = new ConcurrentHashMap<>(); + + @Override + public DeferredObject> registerComponent(Supplier> supplier, ResourceLocation identifier) { + DeferredObject> deferredObject = new DeferredObject<>(identifier, () -> { + DataComponentType dataComponentType = supplier.get(); + Registry.register(BuiltInRegistries.DATA_COMPONENT_TYPE, identifier, dataComponentType); + return dataComponentType; + }); + getActiveRegistrations().dataComponentTypes.add(deferredObject); + return deferredObject; + } + + public void register() { + FMLJavaModLoadingContext.get().getModEventBus().register(getActiveRegistrations()); + } + + private ForgeBalmComponents.Registrations getActiveRegistrations() { + return registrations.computeIfAbsent(ModLoadingContext.get().getActiveNamespace(), it -> new ForgeBalmComponents.Registrations()); + } +} diff --git a/forge/src/main/java/net/blay09/mods/balm/forge/stats/ForgeBalmStats.java b/forge/src/main/java/net/blay09/mods/balm/forge/stats/ForgeBalmStats.java index 71d4204d..0a603305 100644 --- a/forge/src/main/java/net/blay09/mods/balm/forge/stats/ForgeBalmStats.java +++ b/forge/src/main/java/net/blay09/mods/balm/forge/stats/ForgeBalmStats.java @@ -1,6 +1,5 @@ package net.blay09.mods.balm.forge.stats; -import net.blay09.mods.balm.api.BalmRegistries; import net.blay09.mods.balm.api.stats.BalmStats; import net.minecraft.core.Registry; import net.minecraft.core.registries.BuiltInRegistries; diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmRuntime.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmRuntime.java index 8423fb43..cc9718de 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmRuntime.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmRuntime.java @@ -6,6 +6,7 @@ import net.blay09.mods.balm.api.block.BalmBlockEntities; import net.blay09.mods.balm.api.block.BalmBlocks; import net.blay09.mods.balm.api.command.BalmCommands; +import net.blay09.mods.balm.api.component.BalmComponents; import net.blay09.mods.balm.api.config.BalmConfig; import net.blay09.mods.balm.api.entity.BalmEntities; import net.blay09.mods.balm.api.event.BalmEvents; @@ -24,6 +25,7 @@ import net.blay09.mods.balm.neoforge.block.NeoForgeBalmBlocks; import net.blay09.mods.balm.neoforge.block.entity.NeoForgeBalmBlockEntities; import net.blay09.mods.balm.neoforge.command.NeoForgeBalmCommands; +import net.blay09.mods.balm.neoforge.component.NeoForgeBalmComponents; import net.blay09.mods.balm.neoforge.config.NeoForgeBalmConfig; import net.blay09.mods.balm.neoforge.entity.NeoForgeBalmEntities; import net.blay09.mods.balm.neoforge.event.NeoForgeBalmCommonEvents; @@ -69,6 +71,7 @@ public class NeoForgeBalmRuntime implements BalmRuntime { private final BalmLootTables lootTables = new CommonBalmLootTables(); private final BalmStats stats = new NeoForgeBalmStats(); private final BalmRecipes recipes = new NeoForgeBalmRecipes(); + private final BalmComponents components = new NeoForgeBalmComponents(); private final List addonClasses = new ArrayList<>(); @@ -228,4 +231,8 @@ public void addServerReloadListener(ResourceLocation identifier, Consumer event.addListener((ResourceManagerReloadListener) reloadListener::accept)); } + @Override + public BalmComponents getComponents() { + return components; + } } \ No newline at end of file diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/component/NeoForgeBalmComponents.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/component/NeoForgeBalmComponents.java new file mode 100644 index 00000000..82e7fde2 --- /dev/null +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/component/NeoForgeBalmComponents.java @@ -0,0 +1,19 @@ +package net.blay09.mods.balm.neoforge.component; + +import net.blay09.mods.balm.api.DeferredObject; +import net.blay09.mods.balm.api.component.BalmComponents; +import net.blay09.mods.balm.neoforge.DeferredRegisters; +import net.minecraft.core.component.DataComponentType; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceLocation; + +import java.util.function.Supplier; + +public class NeoForgeBalmComponents implements BalmComponents { + @Override + public DeferredObject> registerComponent(Supplier> supplier, ResourceLocation identifier) { + final var register = DeferredRegisters.get(Registries.DATA_COMPONENT_TYPE, identifier.getNamespace()); + final var registryObject = register.register(identifier.getPath(), supplier); + return new DeferredObject<>(identifier, registryObject, registryObject::isBound); + } +}