Skip to content

Commit

Permalink
expose holder for registry suppliers
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Sep 14, 2024
1 parent c9d63db commit 6175530
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,4 +33,6 @@ default boolean is(Supplier<T> other) {
ResourceKey<? super T> getRegistryKey();

Registry<? super T> getRegistry();

<R> Holder<R> holder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<MobEffect> STATUS_EFFECTS = RegistryHandler.create(Registries.MOB_EFFECT, SparkweaveTestmod.MODID);

public static final RegistrySupplier<MobEffect> EXPERIENCED = STATUS_EFFECTS.register("experienced", () -> new ExperiencedStatusEffect(MobEffectCategory.BENEFICIAL, 0xFF005A));
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class FabricRegistryHandler<T> implements RegistryHandler<T> {

private final ResourceKey<Registry<T>> registryKey;
private final String namespace;
private final Supplier<Registry<? super T>> registry;
private final Supplier<Registry<T>> registry;

private final Map<ResourceLocation, RegistrySupplier<? extends T>> values = new Object2ObjectOpenHashMap<>();
private final List<RegistrySupplier<? extends T>> orderedEntries = new LinkedList<>();
Expand All @@ -39,9 +39,9 @@ public static <T> FabricRegistryHandler<T> create(ResourceKey<Registry<T>> regis
}

@Override
public <S extends T> QuiltRegistrySupplier<S> register(String name, Supplier<S> factory) {
public <S extends T> FabricRegistrySupplier<T, S> register(String name, Supplier<S> factory) {
var id = ResourceLocation.fromNamespaceAndPath(namespace, name);
var supplier = new QuiltRegistrySupplier<>(ResourceKey.create(registryKey, id), factory);
FabricRegistrySupplier<T, S> supplier = new FabricRegistrySupplier<>(ResourceKey.create(registryKey, id), factory);

// immediately register entry
supplier.register(registry.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,31 +11,34 @@

import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class QuiltRegistrySupplier<T> implements RegistrySupplier<T> {
public class FabricRegistrySupplier<R, T extends R> implements RegistrySupplier<T> {

private final ResourceKey<? super T> id;
private final Supplier<T> factory;
@Nullable
private LazyHolder holder;
@Nullable
private T value;
private Registry<? super T> registry;
private Registry<R> registry;

public QuiltRegistrySupplier(ResourceKey<? super T> id, Supplier<T> factory) {
public FabricRegistrySupplier(ResourceKey<? super T> id, Supplier<T> factory) {
this.id = id;
this.factory = factory;
}

public void register(Registry<? super T> registry) {
public void register(Registry<R> 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<Object> reg = (Registry<Object>) 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;
}
Expand All @@ -47,31 +51,40 @@ public boolean is(T other) {
@SuppressWarnings("unchecked")
@Override
public boolean matches(TagKey<? super T> tag) {
Registry<Object> reg = (Registry<Object>) getRegistry();
return reg.getHolderOrThrow((ResourceKey<Object>) getRegistryKey()).is((TagKey<Object>) tag);
return holder().is((TagKey<R>) tag);
}

@Override
public ResourceLocation getId() {
return this.id.location();
}

@SuppressWarnings("unchecked")
@Override
public ResourceKey<? super T> getRegistryKey() {
return this.id;
public ResourceKey<R> getRegistryKey() {
return (ResourceKey<R>) this.id;
}

@Override
public Registry<? super T> getRegistry() {
public Registry<R> getRegistry() {
if(registry == null) {
registry = RegistryHelper.getBuiltinRegistry(ResourceKey.createRegistryKey(id.registry()));
}
return registry;
}

@SuppressWarnings("unchecked")
@Override
public Holder<R> 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;
Expand All @@ -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<R> {

private LazyHolder(ResourceKey<R> key) {
super(Type.STAND_ALONE, registry.holderOwner(), key, value);
}

@Override
public void bindValue(R value) {
super.bindValue(value);
}

@Override
public Stream<TagKey<R>> tags() {
return registry.getHolder(this.key()).map(Holder::tags).orElseGet(Stream::empty);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,4 +18,10 @@ public abstract class DeferredHolderMixin<R, T extends R> implements RegistrySup
@Invoker("getKey")
@Override
public abstract ResourceKey<? super T> getRegistryKey();

@SuppressWarnings("unchecked")
@Override
public Holder<R> holder() {
return (Holder<R>) this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class QuiltRegistryHandler<T> implements RegistryHandler<T> {

private final ResourceKey<Registry<T>> registryKey;
private final String namespace;
private final Supplier<Registry<? super T>> registry;
private final Supplier<Registry<T>> registry;

private final Map<ResourceLocation, RegistrySupplier<? extends T>> values = new Object2ObjectOpenHashMap<>();
private final List<RegistrySupplier<? extends T>> orderedEntries = new LinkedList<>();
Expand All @@ -39,9 +39,9 @@ public static <T> QuiltRegistryHandler<T> create(ResourceKey<Registry<T>> regist
}

@Override
public <S extends T> QuiltRegistrySupplier<S> register(String name, Supplier<S> factory) {
public <S extends T> QuiltRegistrySupplier<T, S> register(String name, Supplier<S> factory) {
var id = ResourceLocation.fromNamespaceAndPath(namespace, name);
var supplier = new QuiltRegistrySupplier<>(ResourceKey.create(registryKey, id), factory);
QuiltRegistrySupplier<T, S> supplier = new QuiltRegistrySupplier<>(ResourceKey.create(registryKey, id), factory);

// immediately register entry
supplier.register(registry.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,31 +11,35 @@

import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class QuiltRegistrySupplier<T> implements RegistrySupplier<T> {
public class QuiltRegistrySupplier<R, T extends R> implements RegistrySupplier<T> {

private final ResourceKey<? super T> id;
private final Supplier<T> factory;
@Nullable
private LazyHolder holder;
@Nullable
private T value;
private Registry<? super T> registry;

public QuiltRegistrySupplier(ResourceKey<? super T> id, Supplier<T> factory) {
private Registry<R> registry;

public QuiltRegistrySupplier(ResourceKey<R> id, Supplier<T> factory) {
this.id = id;
this.factory = factory;
}

public void register(Registry<? super T> registry) {
public void register(Registry<R> 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<Object> reg = (Registry<Object>) 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;
}
Expand All @@ -47,31 +52,40 @@ public boolean is(T other) {
@SuppressWarnings("unchecked")
@Override
public boolean matches(TagKey<? super T> tag) {
Registry<Object> reg = (Registry<Object>) getRegistry();
return reg.getHolderOrThrow((ResourceKey<Object>) getRegistryKey()).is((TagKey<Object>) tag);
return holder().is((TagKey<R>) tag);
}

@Override
public ResourceLocation getId() {
return this.id.location();
}

@SuppressWarnings("unchecked")
@Override
public ResourceKey<? super T> getRegistryKey() {
return this.id;
public ResourceKey<R> getRegistryKey() {
return (ResourceKey<R>) this.id;
}

@Override
public Registry<? super T> getRegistry() {
public Registry<R> getRegistry() {
if(registry == null) {
registry = RegistryHelper.getBuiltinRegistry(ResourceKey.createRegistryKey(id.registry()));
}
return registry;
}

@SuppressWarnings("unchecked")
@Override
public Holder<R> 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;
Expand All @@ -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<R> {

private LazyHolder(ResourceKey<R> key) {
super(Type.STAND_ALONE, registry.holderOwner(), key, value);
}

@Override
public void bindValue(R value) {
super.bindValue(value);
}

@Override
public Stream<TagKey<R>> tags() {
return registry.getHolder(this.key()).map(Holder::tags).orElseGet(Stream::empty);
}
}
}

0 comments on commit 6175530

Please sign in to comment.