From 617553032af64011d3c80867c674a5da95ce736e Mon Sep 17 00:00:00 2001 From: Up Date: Sat, 14 Sep 2024 11:59:15 +0200 Subject: [PATCH] expose holder for registry suppliers --- .../api/registry/RegistrySupplier.java | 3 + .../sparkweave/testmod/SparkweaveTestmod.java | 2 + .../effect/ExperiencedStatusEffect.java | 12 ++++ .../testmod/init/TestStatusEffects.java | 16 ++++++ .../impl/registry/FabricRegistryHandler.java | 6 +- ...plier.java => FabricRegistrySupplier.java} | 54 ++++++++++++++---- .../mixin/internal/DeferredHolderMixin.java | 7 +++ .../impl/registry/QuiltRegistryHandler.java | 6 +- .../impl/registry/QuiltRegistrySupplier.java | 55 +++++++++++++++---- 9 files changed, 131 insertions(+), 30 deletions(-) create mode 100644 Common/src/testmod/java/dev/upcraft/sparkweave/testmod/effect/ExperiencedStatusEffect.java create mode 100644 Common/src/testmod/java/dev/upcraft/sparkweave/testmod/init/TestStatusEffects.java rename Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/{QuiltRegistrySupplier.java => FabricRegistrySupplier.java} (53%) diff --git a/Common/src/main/java/dev/upcraft/sparkweave/api/registry/RegistrySupplier.java b/Common/src/main/java/dev/upcraft/sparkweave/api/registry/RegistrySupplier.java index 1daf690..c66abd6 100644 --- a/Common/src/main/java/dev/upcraft/sparkweave/api/registry/RegistrySupplier.java +++ b/Common/src/main/java/dev/upcraft/sparkweave/api/registry/RegistrySupplier.java @@ -1,5 +1,6 @@ package dev.upcraft.sparkweave.api.registry; +import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -32,4 +33,6 @@ default boolean is(Supplier other) { ResourceKey getRegistryKey(); Registry getRegistry(); + + Holder holder(); } diff --git a/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/SparkweaveTestmod.java b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/SparkweaveTestmod.java index f1e9431..4c5e37a 100644 --- a/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/SparkweaveTestmod.java +++ b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/SparkweaveTestmod.java @@ -3,6 +3,7 @@ import dev.upcraft.sparkweave.api.platform.services.RegistryService; import dev.upcraft.sparkweave.testmod.init.TestCreativeTabs; import dev.upcraft.sparkweave.testmod.init.TestItems; +import dev.upcraft.sparkweave.testmod.init.TestStatusEffects; import net.minecraft.resources.ResourceLocation; public class SparkweaveTestmod { @@ -14,6 +15,7 @@ public static void init() { TestItems.ITEMS.accept(registryService); TestCreativeTabs.TABS.accept(registryService); + TestStatusEffects.STATUS_EFFECTS.accept(registryService); } public static ResourceLocation id(String path) { diff --git a/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/effect/ExperiencedStatusEffect.java b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/effect/ExperiencedStatusEffect.java new file mode 100644 index 0000000..868d48b --- /dev/null +++ b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/effect/ExperiencedStatusEffect.java @@ -0,0 +1,12 @@ +package dev.upcraft.sparkweave.testmod.effect; + +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectCategory; + +// TODO make this randomly give experience to all entities nearby +public class ExperiencedStatusEffect extends MobEffect { + + public ExperiencedStatusEffect(MobEffectCategory category, int particleColor) { + super(category, particleColor); + } +} diff --git a/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/init/TestStatusEffects.java b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/init/TestStatusEffects.java new file mode 100644 index 0000000..9e0c3a1 --- /dev/null +++ b/Common/src/testmod/java/dev/upcraft/sparkweave/testmod/init/TestStatusEffects.java @@ -0,0 +1,16 @@ +package dev.upcraft.sparkweave.testmod.init; + +import dev.upcraft.sparkweave.api.registry.RegistryHandler; +import dev.upcraft.sparkweave.api.registry.RegistrySupplier; +import dev.upcraft.sparkweave.testmod.SparkweaveTestmod; +import dev.upcraft.sparkweave.testmod.effect.ExperiencedStatusEffect; +import net.minecraft.core.registries.Registries; +import net.minecraft.world.effect.MobEffect; +import net.minecraft.world.effect.MobEffectCategory; + +public class TestStatusEffects { + + public static final RegistryHandler STATUS_EFFECTS = RegistryHandler.create(Registries.MOB_EFFECT, SparkweaveTestmod.MODID); + + public static final RegistrySupplier EXPERIENCED = STATUS_EFFECTS.register("experienced", () -> new ExperiencedStatusEffect(MobEffectCategory.BENEFICIAL, 0xFF005A)); +} diff --git a/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/FabricRegistryHandler.java b/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/FabricRegistryHandler.java index 35c1923..19db460 100644 --- a/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/FabricRegistryHandler.java +++ b/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/FabricRegistryHandler.java @@ -23,7 +23,7 @@ public class FabricRegistryHandler implements RegistryHandler { private final ResourceKey> registryKey; private final String namespace; - private final Supplier> registry; + private final Supplier> registry; private final Map> values = new Object2ObjectOpenHashMap<>(); private final List> orderedEntries = new LinkedList<>(); @@ -39,9 +39,9 @@ public static FabricRegistryHandler create(ResourceKey> regis } @Override - public QuiltRegistrySupplier register(String name, Supplier factory) { + public FabricRegistrySupplier register(String name, Supplier factory) { var id = ResourceLocation.fromNamespaceAndPath(namespace, name); - var supplier = new QuiltRegistrySupplier<>(ResourceKey.create(registryKey, id), factory); + FabricRegistrySupplier supplier = new FabricRegistrySupplier<>(ResourceKey.create(registryKey, id), factory); // immediately register entry supplier.register(registry.get()); diff --git a/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/QuiltRegistrySupplier.java b/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/FabricRegistrySupplier.java similarity index 53% rename from Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/QuiltRegistrySupplier.java rename to Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/FabricRegistrySupplier.java index f27d07e..b1cea39 100644 --- a/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/QuiltRegistrySupplier.java +++ b/Fabric/src/main/java/dev/upcraft/sparkweave/fabric/impl/registry/FabricRegistrySupplier.java @@ -2,6 +2,7 @@ import dev.upcraft.sparkweave.api.registry.RegistryHelper; import dev.upcraft.sparkweave.api.registry.RegistrySupplier; +import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -10,31 +11,34 @@ import java.util.Objects; import java.util.function.Supplier; +import java.util.stream.Stream; -public class QuiltRegistrySupplier implements RegistrySupplier { +public class FabricRegistrySupplier implements RegistrySupplier { private final ResourceKey id; private final Supplier factory; @Nullable + private LazyHolder holder; + @Nullable private T value; - private Registry registry; + private Registry registry; - public QuiltRegistrySupplier(ResourceKey id, Supplier factory) { + public FabricRegistrySupplier(ResourceKey id, Supplier factory) { this.id = id; this.factory = factory; } - public void register(Registry registry) { + public void register(Registry registry) { this.registry = registry; this.value = Registry.register(registry, this.getId(), Objects.requireNonNull(this.factory.get(), "factory returned null for " + this.id)); + ((LazyHolder) holder()).bindValue(value); } @SuppressWarnings("unchecked") @Override public T get() { if (value == null) { - Registry reg = (Registry) getRegistry(); - value = (T) Objects.requireNonNull(reg.get(this.id.location()), "Registry supplier called too early: " + this.getId()); + value = (T) Objects.requireNonNull(getRegistry().get(this.id.location()), "Registry supplier called too early: " + this.getId()); } return value; } @@ -47,8 +51,7 @@ public boolean is(T other) { @SuppressWarnings("unchecked") @Override public boolean matches(TagKey tag) { - Registry reg = (Registry) getRegistry(); - return reg.getHolderOrThrow((ResourceKey) getRegistryKey()).is((TagKey) tag); + return holder().is((TagKey) tag); } @Override @@ -56,22 +59,32 @@ public ResourceLocation getId() { return this.id.location(); } + @SuppressWarnings("unchecked") @Override - public ResourceKey getRegistryKey() { - return this.id; + public ResourceKey getRegistryKey() { + return (ResourceKey) this.id; } @Override - public Registry getRegistry() { + public Registry getRegistry() { if(registry == null) { registry = RegistryHelper.getBuiltinRegistry(ResourceKey.createRegistryKey(id.registry())); } return registry; } + @SuppressWarnings("unchecked") + @Override + public Holder holder() { + if (holder == null) { + holder = new LazyHolder(getRegistryKey()); + } + return holder; + } + @Override public boolean equals(Object obj) { - if (obj instanceof QuiltRegistrySupplier other) { + if (obj instanceof FabricRegistrySupplier other) { return this.getRegistryKey() == other.getRegistryKey(); } else { return false; @@ -82,4 +95,21 @@ public boolean equals(Object obj) { public int hashCode() { return Objects.hash(this.id.registry(), this.id.location()); } + + private class LazyHolder extends Holder.Reference { + + private LazyHolder(ResourceKey key) { + super(Type.STAND_ALONE, registry.holderOwner(), key, value); + } + + @Override + public void bindValue(R value) { + super.bindValue(value); + } + + @Override + public Stream> tags() { + return registry.getHolder(this.key()).map(Holder::tags).orElseGet(Stream::empty); + } + } } diff --git a/NeoForge/src/main/java/dev/upcraft/sparkweave/neoforge/mixin/internal/DeferredHolderMixin.java b/NeoForge/src/main/java/dev/upcraft/sparkweave/neoforge/mixin/internal/DeferredHolderMixin.java index 4ddf3f0..e782f17 100644 --- a/NeoForge/src/main/java/dev/upcraft/sparkweave/neoforge/mixin/internal/DeferredHolderMixin.java +++ b/NeoForge/src/main/java/dev/upcraft/sparkweave/neoforge/mixin/internal/DeferredHolderMixin.java @@ -1,6 +1,7 @@ package dev.upcraft.sparkweave.neoforge.mixin.internal; import dev.upcraft.sparkweave.api.registry.RegistrySupplier; +import net.minecraft.core.Holder; import net.minecraft.resources.ResourceKey; import net.minecraft.tags.TagKey; import net.neoforged.neoforge.registries.DeferredHolder; @@ -17,4 +18,10 @@ public abstract class DeferredHolderMixin implements RegistrySup @Invoker("getKey") @Override public abstract ResourceKey getRegistryKey(); + + @SuppressWarnings("unchecked") + @Override + public Holder holder() { + return (Holder) this; + } } diff --git a/Quilt/src/main/java/dev/upcraft/sparkweave/quilt/impl/registry/QuiltRegistryHandler.java b/Quilt/src/main/java/dev/upcraft/sparkweave/quilt/impl/registry/QuiltRegistryHandler.java index 825002d..d1fc626 100644 --- a/Quilt/src/main/java/dev/upcraft/sparkweave/quilt/impl/registry/QuiltRegistryHandler.java +++ b/Quilt/src/main/java/dev/upcraft/sparkweave/quilt/impl/registry/QuiltRegistryHandler.java @@ -23,7 +23,7 @@ public class QuiltRegistryHandler implements RegistryHandler { private final ResourceKey> registryKey; private final String namespace; - private final Supplier> registry; + private final Supplier> registry; private final Map> values = new Object2ObjectOpenHashMap<>(); private final List> orderedEntries = new LinkedList<>(); @@ -39,9 +39,9 @@ public static QuiltRegistryHandler create(ResourceKey> regist } @Override - public QuiltRegistrySupplier register(String name, Supplier factory) { + public QuiltRegistrySupplier register(String name, Supplier factory) { var id = ResourceLocation.fromNamespaceAndPath(namespace, name); - var supplier = new QuiltRegistrySupplier<>(ResourceKey.create(registryKey, id), factory); + QuiltRegistrySupplier supplier = new QuiltRegistrySupplier<>(ResourceKey.create(registryKey, id), factory); // immediately register entry supplier.register(registry.get()); diff --git a/Quilt/src/main/java/dev/upcraft/sparkweave/quilt/impl/registry/QuiltRegistrySupplier.java b/Quilt/src/main/java/dev/upcraft/sparkweave/quilt/impl/registry/QuiltRegistrySupplier.java index 9b56021..aec6572 100644 --- a/Quilt/src/main/java/dev/upcraft/sparkweave/quilt/impl/registry/QuiltRegistrySupplier.java +++ b/Quilt/src/main/java/dev/upcraft/sparkweave/quilt/impl/registry/QuiltRegistrySupplier.java @@ -2,6 +2,7 @@ import dev.upcraft.sparkweave.api.registry.RegistryHelper; import dev.upcraft.sparkweave.api.registry.RegistrySupplier; +import net.minecraft.core.Holder; import net.minecraft.core.Registry; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; @@ -10,31 +11,35 @@ import java.util.Objects; import java.util.function.Supplier; +import java.util.stream.Stream; -public class QuiltRegistrySupplier implements RegistrySupplier { +public class QuiltRegistrySupplier implements RegistrySupplier { private final ResourceKey id; private final Supplier factory; @Nullable + private LazyHolder holder; + @Nullable private T value; - private Registry registry; - public QuiltRegistrySupplier(ResourceKey id, Supplier factory) { + private Registry registry; + + public QuiltRegistrySupplier(ResourceKey id, Supplier factory) { this.id = id; this.factory = factory; } - public void register(Registry registry) { + public void register(Registry registry) { this.registry = registry; this.value = Registry.register(registry, this.getId(), Objects.requireNonNull(this.factory.get(), "factory returned null for " + this.id)); + ((LazyHolder) holder()).bindValue(value); } @SuppressWarnings("unchecked") @Override public T get() { if (value == null) { - Registry reg = (Registry) getRegistry(); - value = (T) Objects.requireNonNull(reg.get(this.id.location()), "Registry supplier called too early: " + this.getId()); + value = (T) Objects.requireNonNull(getRegistry().get(this.id.location()), "Registry supplier called too early: " + this.getId()); } return value; } @@ -47,8 +52,7 @@ public boolean is(T other) { @SuppressWarnings("unchecked") @Override public boolean matches(TagKey tag) { - Registry reg = (Registry) getRegistry(); - return reg.getHolderOrThrow((ResourceKey) getRegistryKey()).is((TagKey) tag); + return holder().is((TagKey) tag); } @Override @@ -56,22 +60,32 @@ public ResourceLocation getId() { return this.id.location(); } + @SuppressWarnings("unchecked") @Override - public ResourceKey getRegistryKey() { - return this.id; + public ResourceKey getRegistryKey() { + return (ResourceKey) this.id; } @Override - public Registry getRegistry() { + public Registry getRegistry() { if(registry == null) { registry = RegistryHelper.getBuiltinRegistry(ResourceKey.createRegistryKey(id.registry())); } return registry; } + @SuppressWarnings("unchecked") + @Override + public Holder holder() { + if(holder == null) { + holder = new LazyHolder(getRegistryKey()); + } + return holder; + } + @Override public boolean equals(Object obj) { - if (obj instanceof QuiltRegistrySupplier other) { + if (obj instanceof QuiltRegistrySupplier other) { return this.getRegistryKey() == other.getRegistryKey(); } else { return false; @@ -82,4 +96,21 @@ public boolean equals(Object obj) { public int hashCode() { return Objects.hash(this.id.registry(), this.id.location()); } + + private class LazyHolder extends Holder.Reference { + + private LazyHolder(ResourceKey key) { + super(Type.STAND_ALONE, registry.holderOwner(), key, value); + } + + @Override + public void bindValue(R value) { + super.bindValue(value); + } + + @Override + public Stream> tags() { + return registry.getHolder(this.key()).map(Holder::tags).orElseGet(Stream::empty); + } + } }