Skip to content

Commit

Permalink
Lots of rewritten garbage to enforce RL keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyces committed Jun 11, 2024
1 parent 08e505b commit 8ad62a9
Show file tree
Hide file tree
Showing 78 changed files with 925 additions and 600 deletions.
5 changes: 3 additions & 2 deletions common/src/main/java/dev/dhyces/trimmed/TrimmedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public static void init() {

public static void initApi() {
TrimmedClientApiEntrypoint.TrimmedClientRegistration registration = new TrimmedClientRegistrationImpl();
for (ModApiConsumer<TrimmedClientApiEntrypoint> entrypoint : Services.CLIENT_HELPER.getClientApiConsumers()) {
entrypoint.entrypoint().registration(registration);
for (ModApiConsumer<TrimmedClientApiEntrypoint> consumer : Services.CLIENT_HELPER.getClientApiConsumers()) {
consumer.entrypoint().registration(registration);
}
}

Expand All @@ -63,6 +63,7 @@ public static void onTagsSynced(RegistryAccess registryAccess, boolean shouldUpd
// Minecraft.getInstance().getToasts().addToast(InfoToast.reloadClientInfo());
// }
ClientTagManager.updateDatapacksSynced(registryAccess);
ClientMapManager.updateDatapacksSynced(registryAccess);
}
}

Expand Down
48 changes: 39 additions & 9 deletions common/src/main/java/dev/dhyces/trimmed/api/KeyResolver.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package dev.dhyces.trimmed.api;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DynamicOps;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public interface KeyResolver<T> {
Codec<T> getCodec();
@Nullable
T decode(ResourceLocation resourceLocation, DynamicOps<?> ops);
@Nullable
StreamCodec<RegistryFriendlyByteBuf, T> getStreamCodec();
boolean requiresActiveWorld();
Expand All @@ -19,14 +22,23 @@ interface RegistryResolver<T> extends KeyResolver<T> {
ResourceKey<? extends Registry<T>> getKey();
}

record RegistryWrapper<T>(ResourceKey<? extends Registry<T>> registryKey, Codec<Holder<T>> holderByNameCodec, boolean requiresActiveWorld) implements RegistryResolver<T> {
public static <T> RegistryWrapper<T> createStatic(Registry<T> registry) {
return new RegistryWrapper<>(registry.key(), registry.holderByNameCodec(), false);
record Dynamic<T>(ResourceKey<? extends Registry<T>> registryKey) implements RegistryResolver<T> {

@Override
public ResourceKey<? extends Registry<T>> getKey() {
return registryKey;
}

@Nullable
@Override
public Codec<T> getCodec() {
return holderByNameCodec.xmap(Holder::value, t -> {throw new UnsupportedOperationException("Does not support encoding values");});
public T decode(ResourceLocation resourceLocation, DynamicOps<?> ops) {
if (!(ops instanceof RegistryOps<?> registryOps)) {
throw new IllegalArgumentException("Must have registry ops to decode dynamic content.");
}
return registryOps.getter(registryKey).orElseThrow(() -> new IllegalStateException("Registry {%s} is not available".formatted(registryKey.location())))
.get(ResourceKey.create(registryKey, resourceLocation))
.map(Holder.Reference::value)
.orElse(null);
}

@Override
Expand All @@ -36,12 +48,30 @@ public StreamCodec<RegistryFriendlyByteBuf, T> getStreamCodec() {

@Override
public boolean requiresActiveWorld() {
return requiresActiveWorld;
return true;
}
}

record Static<T>(Registry<T> registry) implements RegistryResolver<T> {

@Override
public ResourceKey<? extends Registry<T>> getKey() {
return registryKey;
return registry.key();
}

@Override
public @Nullable T decode(ResourceLocation resourceLocation, DynamicOps<?> ops) {
return registry.get(resourceLocation);
}

@Override
public StreamCodec<RegistryFriendlyByteBuf, T> getStreamCodec() {
return ByteBufCodecs.registry(registry.key());
}

@Override
public boolean requiresActiveWorld() {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package dev.dhyces.trimmed.api.client;

import com.mojang.serialization.Codec;
import com.mojang.serialization.DynamicOps;
import dev.dhyces.trimmed.api.KeyResolver;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

public record ClientKeyResolver<T>(Codec<T> codec) implements KeyResolver<T> {
import java.util.function.BiFunction;

public record ClientKeyResolver<T>(BiFunction<ResourceLocation, DynamicOps<?>, T> decoder) implements KeyResolver<T> {
@Override
public Codec<T> getCodec() {
return codec;
public @Nullable T decode(ResourceLocation resourceLocation, DynamicOps<?> ops) {
return decoder.apply(resourceLocation, ops);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
public final class ClientKeyResolvers {
private ClientKeyResolvers() {}

public static final KeyResolver<ResourceLocation> TEXTURE = new ClientKeyResolver<>(ResourceLocation.CODEC);
public static final KeyResolver<ResourceLocation> TEXTURE = new ClientKeyResolver<>((resourceLocation, dynamicOps) -> resourceLocation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static TrimmedClientApi getInstance() {
@Nullable
<T> KeyResolver<T> getKeyResolver(ResourceLocation id);
@Nullable
<T> KeyResolver.RegistryWrapper<T> getRegistryKeyResolver(ResourceKey<? extends Registry<T>> key);
<T> KeyResolver.RegistryResolver<T> getRegistryKeyResolver(ResourceKey<? extends Registry<T>> key);
@Nullable
<T> ResourceLocation getId(KeyResolver<T> keyResolver);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ static TrimmedClientMapApi getInstance() {
<K, V, M extends Map<K, V>> Codec<MapHolder.Typed<K, V, M>> advancedCodec(AdvancedMapType<K, V, M> mapType);

@Nullable
<T> KeyResolver.RegistryWrapper<T> getRegistryKeyResolver(ResourceKey<? extends Registry<T>> key);
<T> KeyResolver.RegistryResolver<T> getRegistryKeyResolver(ResourceKey<? extends Registry<T>> key);
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected ClientTagBuilder<T> getOrCreateBuilder(ClientTagKey<T> clientTagKey) {

protected void onBuilderCreation(ResourceLocation id) {}

protected boolean doesTagExist(ClientTagKey<T> clientTagKey) {
return builders.get(clientTagKey.getTagId()) != null;
protected boolean doesTagExist(ResourceLocation clientTagId) {
return builders.get(clientTagId) != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import dev.dhyces.trimmed.api.client.tag.ClientTagKey;
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
import net.minecraft.resources.ResourceLocation;

import java.util.Set;

public class ClientTagBuilder<T> {
private final Set<ClientTagEntry<T>> elements;
private final Set<ClientTagEntry> elements;
private boolean replaces;

public ClientTagBuilder() {
this.elements = new ObjectLinkedOpenHashSet<>();
}

public ClientTagBuilder<T> add(T element, boolean isRequired) {
public ClientTagBuilder<T> add(ResourceLocation element, boolean isRequired) {
this.elements.add(ClientTagEntry.element(element, isRequired));
return this;
}
Expand All @@ -28,7 +29,7 @@ public ClientTagBuilder<T> setReplaces(boolean doesReplace) {
return this;
}

public ClientTagFile<T> build() {
return new ClientTagFile<>(elements, replaces);
public ClientTagFile build() {
return new ClientTagFile(elements, replaces);
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,54 @@
package dev.dhyces.trimmed.api.data.client.tag;

import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.dhyces.trimmed.api.KeyResolver;
import dev.dhyces.trimmed.api.client.tag.ClientTagKey;
import org.jetbrains.annotations.Nullable;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.ExtraCodecs;

import java.util.function.Predicate;

public record ClientTagEntry<T>(Either<T, ClientTagKey<T>> element, boolean isRequired) {
public static <T> Codec<Either<T, ClientTagKey<T>>> elementOrTagCodec(KeyResolver<T> keyResolver) {
return Codec.either(keyResolver.getCodec(), ClientTagKey.tagCodec(keyResolver));
}
public static <T> Codec<ClientTagEntry<T>> fullCodec(KeyResolver<T> keyResolver) {
return RecordCodecBuilder.create(instance ->
instance.group(
elementOrTagCodec(keyResolver).fieldOf("value").forGetter(ClientTagEntry::element),
Codec.BOOL.fieldOf("required").forGetter(ClientTagEntry::isRequired)
).apply(instance, ClientTagEntry::new)
);
}
public static <T> Codec<ClientTagEntry<T>> codec(KeyResolver<T> keyResolver) {
return Codec.withAlternative(elementOrTagCodec(keyResolver).xmap(
either -> new ClientTagEntry<>(either, true),
ClientTagEntry::element), fullCodec(keyResolver)
);
}
public record ClientTagEntry(ExtraCodecs.TagOrElementLocation tagOrElementLocation, boolean isRequired) {
public static final Codec<ClientTagEntry> FULL_CODEC = RecordCodecBuilder.create(instance ->
instance.group(
ExtraCodecs.TAG_OR_ELEMENT_ID.fieldOf("id").forGetter(ClientTagEntry::tagOrElementLocation),
Codec.BOOL.fieldOf("required").forGetter(ClientTagEntry::isRequired)
).apply(instance, ClientTagEntry::new)
);

public static final Codec<ClientTagEntry> CODEC = Codec.withAlternative(
ExtraCodecs.TAG_OR_ELEMENT_ID.xmap(
either -> new ClientTagEntry(either, true),
ClientTagEntry::tagOrElementLocation
),
FULL_CODEC
);

public boolean isTag() {
return element.right().isPresent();
return tagOrElementLocation.tag();
}

@Nullable
public T getElement() {
return element.left().orElse(null);
public ResourceLocation getId() {
return tagOrElementLocation.id();
}

@Nullable
public ClientTagKey<T> getTag() {
return element.right().orElse(null);
public <T> ClientTagKey<T> getTag(KeyResolver<T> keyResolver) {
return ClientTagKey.of(keyResolver, tagOrElementLocation.id());
}

public boolean verifyExists(Predicate<T> elementPredicate, Predicate<ClientTagKey<T>> keyPredicate) {
return element.map(elementPredicate::test, keyPredicate::test);
public boolean verifyExists(Predicate<ResourceLocation> elementPredicate, Predicate<ResourceLocation> tagPredicate) {
if (tagOrElementLocation.tag()) {
return tagPredicate.test(tagOrElementLocation.id());
}
return elementPredicate.test(tagOrElementLocation.id());
}

public static <T> ClientTagEntry<T> element(T element, boolean isRequired) {
return new ClientTagEntry<>(Either.left(element), isRequired);
public static ClientTagEntry element(ResourceLocation element, boolean isRequired) {
return new ClientTagEntry(new ExtraCodecs.TagOrElementLocation(element, false), isRequired);
}

public static <T> ClientTagEntry<T> clientTagKey(ClientTagKey<T> clientTagKey, boolean isRequired) {
return new ClientTagEntry<>(Either.right(clientTagKey), isRequired);
public static <T> ClientTagEntry clientTagKey(ClientTagKey<T> clientTagKey, boolean isRequired) {
return new ClientTagEntry(new ExtraCodecs.TagOrElementLocation(clientTagKey.getTagId(), true), isRequired);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@

import java.util.Set;

public record ClientTagFile<T>(Set<ClientTagEntry<T>> entries, boolean replace) {
public static <T> Codec<ClientTagFile<T>> codec(KeyResolver<T> keyResolver) {
return RecordCodecBuilder.create(instance ->
instance.group(
CodecUtil.setOf(ClientTagEntry.codec(keyResolver)).fieldOf("values").forGetter(ClientTagFile::entries),
Codec.BOOL.optionalFieldOf("replace", false).forGetter(ClientTagFile::replace)
).apply(instance, ClientTagFile::new)
);
}
public record ClientTagFile(Set<ClientTagEntry> entries, boolean replace) {
public static final Codec<ClientTagFile> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
CodecUtil.setOf(ClientTagEntry.CODEC).fieldOf("values").forGetter(ClientTagFile::entries),
Codec.BOOL.optionalFieldOf("replace", false).forGetter(ClientTagFile::replace)
).apply(instance, ClientTagFile::new)
);
}
Loading

0 comments on commit 8ad62a9

Please sign in to comment.