-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add component type registration
- Loading branch information
1 parent
4ce3823
commit 132cd11
Showing
10 changed files
with
129 additions
and
1 deletion.
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
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
11 changes: 11 additions & 0 deletions
11
common/src/main/java/net/blay09/mods/balm/api/component/BalmComponents.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,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); | ||
} |
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
17 changes: 17 additions & 0 deletions
17
fabric/src/main/java/net/blay09/mods/balm/fabric/component/FabricBalmComponents.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,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(); | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
forge/src/main/java/net/blay09/mods/balm/forge/component/ForgeBalmComponents.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,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()); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
forge/src/main/java/net/blay09/mods/balm/forge/stats/ForgeBalmStats.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
19 changes: 19 additions & 0 deletions
19
neoforge/src/main/java/net/blay09/mods/balm/neoforge/component/NeoForgeBalmComponents.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,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); | ||
} | ||
} |