Skip to content

Commit

Permalink
Add helpers for passing register Resource Key
Browse files Browse the repository at this point in the history
  • Loading branch information
UnRealDinnerbone committed Dec 10, 2024
1 parent 7eefb66 commit 561dd6e
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 18 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
### 21.4.1
### 21.4.2

- Fix registration on fabirc
- Pass ResourceKey for creation of new items
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.unrealdinnerbone.trenzalore.api.registry;

import com.google.common.base.Suppliers;
import com.unrealdinnerbone.trenzalore.lib.RLUtils;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

public class ItemRegistryObjects extends RegistryObjects<Item> {

public ItemRegistryObjects(String modID) {
super(modID, Registries.ITEM);
}

public <A extends Item> RegistryEntry<A> register(String name, Function<Item.Properties, A> object, UnaryOperator<Item.Properties> itemProperties) {
ResourceLocation rl = RLUtils.rl(modID, name);
ResourceKey<Item> key = ResourceKey.create(registryKey, rl);
Supplier<A> memoize = Suppliers.memoize(() -> object.apply(itemProperties.apply(new Item.Properties()
.setId(key))));
RegistryEntry<A> entry = new RegistryEntry<>(rl, memoize);
objects.add(entry);
return entry;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@

public class Regeneration {

public static <T> RegistryObjects<T> create(ResourceKey<Registry<T>> registry) {
return RegistryObjects.of(registry);
public static <T> RegistryObjects<T> create(String modID, ResourceKey<Registry<T>> registry) {
return RegistryObjects.of(modID, registry);
}

public static ItemRegistryObjects createItemRegistry(String modID) {
return new ItemRegistryObjects(modID);
}

public static void addItemToCreateTab(ResourceKey<CreativeModeTab> tabResourceKey, Supplier<? extends Item> item) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.unrealdinnerbone.trenzalore.api.registry;

import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.ApiStatus;

import java.util.function.Supplier;

public class RegistryEntry<T> implements Supplier<T> {

private final String name;
private final ResourceLocation key;
private final Supplier<T> entry;
private Holder<T> holder;

public RegistryEntry(String name, Supplier<T> entry) {
this.name = name;
public RegistryEntry(ResourceLocation key, Supplier<T> entry) {
this.key = key;
this.entry = entry;
}

Expand All @@ -33,8 +35,8 @@ public T get() {
return entry.get();
}

public String name() {
return name;
public ResourceLocation getKey() {
return key;
}

public Supplier<T> entry() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
package com.unrealdinnerbone.trenzalore.api.registry;

import com.google.common.base.Suppliers;
import com.unrealdinnerbone.trenzalore.lib.RLUtils;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

public record RegistryObjects<T>(ResourceKey<Registry<T>> registryKey, List<RegistryEntry<? extends T>> objects) {
public class RegistryObjects<T> {
protected final String modID;
protected final ResourceKey<Registry<T>> registryKey;
protected final List<RegistryEntry<? extends T>> objects;

public static <T> RegistryObjects<T> of(ResourceKey<Registry<T>> registryKey) {
return new RegistryObjects<>(registryKey, new ArrayList<>());
public RegistryObjects(String modID, ResourceKey<Registry<T>> registryKey) {
this.modID = modID;
this.registryKey = registryKey;
this.objects = new ArrayList<>();
}

public static <T> RegistryObjects<T> of(String modID, ResourceKey<Registry<T>> registryKey) {
return new RegistryObjects<>(modID, registryKey);
}

public <A extends T> RegistryEntry<A> register(String name, Supplier<A> object) {
RegistryEntry<A> entry = new RegistryEntry<>(name, Suppliers.memoize(object::get));
ResourceLocation rl = RLUtils.rl(modID, name);
RegistryEntry<A> entry = new RegistryEntry<>(rl, Suppliers.memoize(object::get));
objects.add(entry);
return entry;
}

public <A extends T> RegistryEntry<A> registerWithId(String name, Function<ResourceKey<T>, A> object) {
ResourceLocation rl = RLUtils.rl(modID, name);
ResourceKey<T> key = ResourceKey.create(registryKey, rl);
Supplier<A> memoize = Suppliers.memoize(() -> object.apply(key));
RegistryEntry<A> entry = new RegistryEntry<>(rl, memoize);
objects.add(entry);
return entry;
}

public ResourceKey<Registry<T>> registryKey() {
return registryKey;
}

public List<RegistryEntry<? extends T>> objects() {
return objects;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public <T> void registryRegistryObjects(String modId, RegistryObjects<T> registr
Registry<?> value = registry.value();
Registry<T> theRegistry = (Registry<T>) value;
for (RegistryEntry<? extends T> object : registryObjects.objects()) {
ResourceLocation id = RLUtils.rl(modId, object.name());
Holder.Reference<T> register = Registry.registerForHolder(theRegistry, id, object.get());
Holder.Reference<T> register = Registry.registerForHolder(theRegistry, object.getKey(), object.get());
object.setHolder(register);
}
}, () -> LOGGER.error("Failed to find registry: {}", registryKey.location()));
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project
mod_version=21.4.1
mod_version=21.4.2
maven_group=com.unrealdinnerbone
mod_name=Trenzalore
mod_author=UnRealDinnerbone
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

public class TrenzaloreNeoRegistry implements IRegistry {

private static final RegistryObjects<MapCodec<? extends IGlobalLootModifier>> GLOBAL_LOOT_MODIFIERS = Regeneration.create(NeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS);
private static final RegistryObjects<MapCodec<? extends IGlobalLootModifier>> GLOBAL_LOOT_MODIFIERS = Regeneration.create(Trenzalore.MOD_ID, NeoForgeRegistries.Keys.GLOBAL_LOOT_MODIFIER_SERIALIZERS);

public static final RegistryEntry<MapCodec<? extends IGlobalLootModifier>> SIMPLE_LOOT_MODIFIER = GLOBAL_LOOT_MODIFIERS.register("replace", () -> ReplaceItemModifier.CODEC);
public static final RegistryEntry<MapCodec<? extends IGlobalLootModifier>> ADD_ITEM_MODIFIER = GLOBAL_LOOT_MODIFIERS.register("add", () -> AddItemModifier.CODEC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public boolean isModLoaded(String modId) {
public <T> void registryRegistryObjects(String modId, RegistryObjects<T> registryObjects) {
DeferredRegister<T> deferredRegister = DeferredRegister.create(registryObjects.registryKey(), modId);
registryObjects.objects().forEach(registryEntry -> {
Holder<T> register = deferredRegister.register(registryEntry.name(), registryEntry.entry());
Holder<T> register = deferredRegister.register(registryEntry.getKey().getPath(), registryEntry.entry());
registryEntry.setHolder(register);
});
IEventBus modEventBus = ModList.get().getModContainerById(modId)
Expand Down

0 comments on commit 561dd6e

Please sign in to comment.