Skip to content

Commit

Permalink
Registered: Add key accessor
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Sep 10, 2024
1 parent 5ee6166 commit 4b802b7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package juuxel.adorn.lib.registry;

import net.minecraft.registry.RegistryKey;

import java.util.function.Supplier;

@FunctionalInterface
public interface Registered<T> extends Supplier<T> {
RegistryKey<? super T> key();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.function.Supplier;
import java.util.stream.Stream;

public final class RegisteredMap<K, V> implements Registered<Map<K, V>> {
public final class RegisteredMap<K, V> implements Supplier<Map<K, V>> {
private final Map<K, Registered<? extends V>> map;
private final Supplier<Map<K, V>> builtMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import juuxel.adorn.lib.registry.Registered;
import juuxel.adorn.lib.registry.Registrar;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;

import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -20,9 +21,20 @@ public RegistrarImpl(Registry<T> registry) {

@Override
public <U extends T> Registered<U> register(String id, Supplier<? extends U> provider) {
var registered = Registry.register(registry, AdornCommon.id(id), provider.get());
var key = RegistryKey.of(registry.getKey(), AdornCommon.id(id));
var registered = Registry.register(registry, key, provider.get());
objects.add(registered);
return () -> registered;
return new Registered<>() {
@Override
public U get() {
return registered;
}

@Override
public RegistryKey<? super U> key() {
return key;
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.common.extensions.IHolderExtension;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;

Expand All @@ -31,7 +32,17 @@ public void hook(IEventBus modBus) {
public <U extends T> Registered<U> register(String id, Supplier<? extends U> provider) {
var registryObject = register.register(id, provider);
objects.add(registryObject);
return registryObject::get;
return new Registered<>() {
@Override
public RegistryKey<? super U> key() {
return ((IHolderExtension<T>) registryObject).getKey();
}

@Override
public U get() {
return registryObject.get();
}
};
}

@Override
Expand Down

0 comments on commit 4b802b7

Please sign in to comment.