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