improve reg
This commit is contained in:
@@ -18,7 +18,7 @@ loader_version_range=[2,)
|
||||
mod_id=l2core
|
||||
mod_name=L2Core
|
||||
mod_license=LGPL-2.1
|
||||
mod_version=3.0.2-pre6
|
||||
mod_version=3.0.2
|
||||
mod_group_id=dev.xkmc
|
||||
mod_authors=lcy0x1
|
||||
mod_description=Core Library mod for all L2 mods
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.mojang.serialization.Codec;
|
||||
import dev.xkmc.l2core.init.reg.datapack.DataMapReg;
|
||||
import dev.xkmc.l2core.init.reg.datapack.DatapackReg;
|
||||
import dev.xkmc.l2serial.serialization.codec.CodecAdaptor;
|
||||
import dev.xkmc.l2serial.util.Wrappers;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
@@ -13,58 +12,70 @@ import net.neoforged.neoforge.registries.DeferredRegister;
|
||||
import net.neoforged.neoforge.registries.datamaps.DataMapType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public final class Reg {
|
||||
|
||||
private final String modid;
|
||||
private final String modid;
|
||||
|
||||
private final Map<Registry<?>, DeferredRegister<?>> map = new LinkedHashMap<>();
|
||||
private final List<Consumer<IEventBus>> list = new ArrayList<>();
|
||||
public Reg(String modid) {
|
||||
this.modid = modid;
|
||||
}
|
||||
|
||||
public Reg(String modid) {
|
||||
this.modid = modid;
|
||||
}
|
||||
public <T> DeferredRegister<T> make(Registry<T> reg) {
|
||||
var ans = DeferredRegister.create(reg, modid);
|
||||
listen(ans::register);
|
||||
return ans;
|
||||
}
|
||||
|
||||
public <T> DeferredRegister<T> make(Registry<T> reg) {
|
||||
return Wrappers.cast(map.computeIfAbsent(reg, r -> DeferredRegister.create(r, modid)));
|
||||
}
|
||||
public <T> DeferredRegister<T> make(ResourceKey<Registry<T>> reg) {
|
||||
var ans = DeferredRegister.create(reg, modid);
|
||||
listen(ans::register);
|
||||
return ans;
|
||||
}
|
||||
|
||||
public <T> DatapackReg<T> dataReg(String id, Codec<T> codec) {
|
||||
var ans = new DatapackReg<>(ResourceKey.createRegistryKey(id(id)), codec);
|
||||
list.add(bus -> bus.addListener(ans::onRegister));
|
||||
return ans;
|
||||
}
|
||||
public <T> DatapackReg<T> dataReg(String id, Codec<T> codec) {
|
||||
var ans = new DatapackReg<>(ResourceKey.createRegistryKey(loc(id)), codec);
|
||||
listen(bus -> bus.addListener(ans::onRegister));
|
||||
return ans;
|
||||
}
|
||||
|
||||
public <T> DatapackReg<T> dataReg(String id, Class<T> cls) {
|
||||
return dataReg(id, new CodecAdaptor<>(cls));
|
||||
}
|
||||
public <T> DatapackReg<T> dataReg(String id, Class<T> cls) {
|
||||
return dataReg(id, new CodecAdaptor<>(cls));
|
||||
}
|
||||
|
||||
public <K, V> DataMapReg<K, V> dataMap(DataMapType<K, V> type) {
|
||||
var ans = new DataMapReg<>(type);
|
||||
list.add(bus -> bus.addListener(ans::register));
|
||||
return ans;
|
||||
}
|
||||
public <K, V> DataMapReg<K, V> dataMap(DataMapType<K, V> type) {
|
||||
var ans = new DataMapReg<>(type);
|
||||
listen(bus -> bus.addListener(ans::register));
|
||||
return ans;
|
||||
}
|
||||
|
||||
public <K, V> DataMapReg<K, V> dataMap(String id, ResourceKey<Registry<K>> k, Codec<V> codec, Codec<V> network) {
|
||||
return dataMap(DataMapType.builder(id(id), k, codec).synced(network, true).build());
|
||||
}
|
||||
public <K, V> DataMapReg<K, V> dataMap(String id, ResourceKey<Registry<K>> k, Codec<V> codec, Codec<V> network) {
|
||||
return dataMap(DataMapType.builder(loc(id), k, codec).synced(network, true).build());
|
||||
}
|
||||
|
||||
public <K, V> DataMapReg<K, V> dataMap(String id, ResourceKey<Registry<K>> k, Class<V> cls) {
|
||||
CodecAdaptor<V> codec = new CodecAdaptor<>(cls);
|
||||
return dataMap(id, k, codec, codec);
|
||||
}
|
||||
public <K, V> DataMapReg<K, V> dataMap(String id, ResourceKey<Registry<K>> k, Class<V> cls) {
|
||||
CodecAdaptor<V> codec = new CodecAdaptor<>(cls);
|
||||
return dataMap(id, k, codec, codec);
|
||||
}
|
||||
|
||||
public void register(IEventBus bus) {
|
||||
for (var e : map.values()) e.register(bus);
|
||||
for (var e : list) e.accept(bus);
|
||||
}
|
||||
public ResourceLocation loc(String id) {
|
||||
return ResourceLocation.fromNamespaceAndPath(modid, id);
|
||||
}
|
||||
|
||||
public ResourceLocation id(String id) {
|
||||
return ResourceLocation.fromNamespaceAndPath(modid, id);
|
||||
}
|
||||
private final List<Consumer<IEventBus>> list = new ArrayList<>();
|
||||
private IEventBus bus;
|
||||
|
||||
private void listen(Consumer<IEventBus> cons) {
|
||||
if (bus == null) list.add(cons);
|
||||
else cons.accept(bus);
|
||||
}
|
||||
|
||||
public void register(IEventBus bus) {
|
||||
for (var e : list) e.accept(bus);
|
||||
list.clear();
|
||||
this.bus = bus;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.xkmc.l2core.init.reg.simple;
|
||||
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.neoforged.neoforge.registries.DeferredHolder;
|
||||
import net.neoforged.neoforge.registries.DeferredRegister;
|
||||
|
||||
@@ -8,21 +9,25 @@ import java.util.function.Supplier;
|
||||
|
||||
public record SR<T>(DeferredRegister<T> reg) {
|
||||
|
||||
public static <T> SR<T> of(Reg parent, Registry<T> reg) {
|
||||
return new SR<>(parent.make(reg));
|
||||
}
|
||||
public static <T> SR<T> of(Reg parent, Registry<T> reg) {
|
||||
return new SR<>(parent.make(reg));
|
||||
}
|
||||
|
||||
public <H extends T> Val<H> reg(String id, Supplier<H> sup) {
|
||||
return new ValImpl<>(reg.register(id, sup));
|
||||
}
|
||||
public static <T> SR<T> of(Reg parent, ResourceKey<Registry<T>> reg) {
|
||||
return new SR<>(parent.make(reg));
|
||||
}
|
||||
|
||||
private record ValImpl<R, T extends R>(DeferredHolder<R, T> val) implements Val<T> {
|
||||
public <H extends T> Val<H> reg(String id, Supplier<H> sup) {
|
||||
return new ValImpl<>(reg.register(id, sup));
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
return val.get();
|
||||
}
|
||||
private record ValImpl<R, T extends R>(DeferredHolder<R, T> val) implements Val<T> {
|
||||
|
||||
}
|
||||
@Override
|
||||
public T get() {
|
||||
return val.get();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user