Skip to content

Commit

Permalink
feat: Add component type registration
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed May 22, 2024
1 parent 4ce3823 commit 132cd11
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 1 deletion.
5 changes: 5 additions & 0 deletions common/src/main/java/net/blay09/mods/balm/api/Balm.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,4 +68,6 @@ public interface BalmRuntime<TLoadContext extends BalmRuntimeLoadContext> {

void addServerReloadListener(ResourceLocation identifier, PreparableReloadListener reloadListener);
void addServerReloadListener(ResourceLocation identifier, Consumer<ResourceManager> reloadListener);

BalmComponents getComponents();
}
Original file line number Diff line number Diff line change
@@ -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 {
<TComponent> DeferredObject<DataComponentType<TComponent>> registerComponent(Supplier<DataComponentType<TComponent>> supplier, ResourceLocation identifier);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,6 +73,7 @@ public class FabricBalmRuntime implements BalmRuntime<EmptyLoadContext> {
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);
Expand Down Expand Up @@ -232,4 +235,9 @@ public ResourceLocation getFabricId() {
}
});
}

@Override
public BalmComponents getComponents() {
return components;
}
}
Original file line number Diff line number Diff line change
@@ -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 <TComponent> DeferredObject<DataComponentType<TComponent>> registerComponent(Supplier<DataComponentType<TComponent>> supplier, ResourceLocation identifier) {
return new DeferredObject<>(identifier, () -> Registry.register(BuiltInRegistries.DATA_COMPONENT_TYPE, identifier, supplier.get())).resolveImmediately();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -70,6 +72,7 @@ public class ForgeBalmRuntime implements BalmRuntime<EmptyLoadContext> {
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<String> addonClasses = new ArrayList<>();

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -230,4 +234,8 @@ public void addServerReloadListener(ResourceLocation identifier, Consumer<Resour
MinecraftForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> event.addListener((ResourceManagerReloadListener) reloadListener::accept));
}

@Override
public BalmComponents getComponents() {
return components;
}
}
Original file line number Diff line number Diff line change
@@ -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<DeferredObject<?>> dataComponentTypes = new ArrayList<>();

@SubscribeEvent
public void commonSetup(FMLCommonSetupEvent event) {
event.enqueueWork(() -> dataComponentTypes.forEach(DeferredObject::resolve));
}
}

private final Map<String, ForgeBalmComponents.Registrations> registrations = new ConcurrentHashMap<>();

@Override
public <TComponent> DeferredObject<DataComponentType<TComponent>> registerComponent(Supplier<DataComponentType<TComponent>> supplier, ResourceLocation identifier) {
DeferredObject<DataComponentType<TComponent>> deferredObject = new DeferredObject<>(identifier, () -> {
DataComponentType<TComponent> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -69,6 +71,7 @@ public class NeoForgeBalmRuntime implements BalmRuntime<NeoForgeLoadContext> {
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<String> addonClasses = new ArrayList<>();

Expand Down Expand Up @@ -228,4 +231,8 @@ public void addServerReloadListener(ResourceLocation identifier, Consumer<Resour
NeoForge.EVENT_BUS.addListener((AddReloadListenerEvent event) -> event.addListener((ResourceManagerReloadListener) reloadListener::accept));
}

@Override
public BalmComponents getComponents() {
return components;
}
}
Original file line number Diff line number Diff line change
@@ -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 <TComponent> DeferredObject<DataComponentType<TComponent>> registerComponent(Supplier<DataComponentType<TComponent>> 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);
}
}

0 comments on commit 132cd11

Please sign in to comment.