Skip to content

Commit

Permalink
add byNameCodecs
Browse files Browse the repository at this point in the history
  • Loading branch information
DerToaster98 committed Mar 11, 2024
1 parent 807bc91 commit a082a52
Showing 1 changed file with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.mojang.serialization.Codec;

import de.dertoaster.multihitboxlib.util.LazyLoadField;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
Expand All @@ -17,19 +18,28 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.registries.DataPackRegistryEvent.NewRegistry;

public record DatapackRegistry<T>(
ResourceKey<Registry<T>> registryKey,
Codec<T> objectCodec,
Codec<Holder<T>> registryCodec
) {
public class DatapackRegistry<T> {

protected final ResourceKey<Registry<T>> registryKey;
protected final Codec<T> objectCodec;
protected final Codec<Holder<T>> registryCodec;

protected final LazyLoadField<Codec<T>> lazyLoadByNameCodec = new LazyLoadField<>(this::createByNameCodec);

public DatapackRegistry(final ResourceLocation id, final Codec<T> codec) {
this(ResourceKey.createRegistryKey(id), codec);
}

public DatapackRegistry(final ResourceKey<Registry<T>> resourceKey, final Codec<T> codec) {
this(resourceKey, codec, RegistryFileCodec.create(resourceKey, codec));
}

public DatapackRegistry(final ResourceKey<Registry<T>> resourceKey, final Codec<T> codec, final Codec<Holder<T>> registryCodec) {
super();
this.registryCodec = registryCodec;
this.objectCodec = codec;
this.registryKey = resourceKey;
}

public void registerSynchable(NewRegistry registryEvent) {
register(registryEvent, true);
Expand Down Expand Up @@ -68,4 +78,28 @@ public List<T> values(RegistryAccess registryAccess) {
return optRegistry.get().stream().collect(Collectors.toList());
}

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

public Codec<T> objectCodec() {
return this.objectCodec;
}

public Codec<T> byNameCodec() {
return this.lazyLoadByNameCodec.get();
}

public Codec<Holder<T>> registryCodec() {
return this.registryCodec;
}

protected Codec<T> createByNameCodec() {
return createByNameCodec(this);
}

protected static <V> Codec<V> createByNameCodec(DatapackRegistry<V> registry) {
return registry.registryCodec().xmap(Holder::get, Holder::direct);
}

}

0 comments on commit a082a52

Please sign in to comment.