Skip to content

Commit

Permalink
Rename MapKeyResolver to KeyResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyces committed May 29, 2024
1 parent d0c7e36 commit 9674cb0
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 303 deletions.
7 changes: 3 additions & 4 deletions common/src/main/java/dev/dhyces/trimmed/TrimmedClient.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package dev.dhyces.trimmed;

import dev.dhyces.trimmed.api.client.TrimmedClientApiEntrypoint;
import dev.dhyces.trimmed.api.client.map.ClientMapKeyResolvers;
import dev.dhyces.trimmed.api.client.map.ClientKeyResolvers;
import dev.dhyces.trimmed.api.client.map.ClientMapKeys;
import dev.dhyces.trimmed.api.client.map.ClientMapTypes;
import dev.dhyces.trimmed.impl.ModApiConsumer;
import dev.dhyces.trimmed.impl.client.TrimmedClientRegistrationImpl;
import dev.dhyces.trimmed.impl.client.atlas.TrimmedSpriteSourceTypes;
import dev.dhyces.trimmed.impl.client.maps.MapKeyResolvers;
import dev.dhyces.trimmed.impl.client.maps.KeyResolvers;
import dev.dhyces.trimmed.impl.client.models.override.ItemOverrideReloadListener;
import dev.dhyces.trimmed.impl.client.models.override.provider.ItemOverrideProviderRegistry;
import dev.dhyces.trimmed.impl.client.models.source.ModelSourceLoader;
Expand All @@ -24,15 +24,14 @@
import net.minecraft.server.packs.resources.ResourceManager;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;

public class TrimmedClient {

public static void init() {
MapKeyResolvers.register(Trimmed.id("texture"), ClientMapKeyResolvers.TEXTURE);
KeyResolvers.register(Trimmed.id("texture"), ClientKeyResolvers.TEXTURE);
ClientMapTypes.init();
ClientMapManager.registerBaseKey(ClientMapKeys.MATERIAL_SUFFIXES);
ClientMapManager.registerBaseKey(ClientMapKeys.TRIM_MATERIAL_OVERRIDES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProvider;
import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProviderType;
import dev.dhyces.trimmed.api.maps.MapKey;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.api.maps.KeyResolver;
import net.minecraft.resources.ResourceLocation;

import java.util.function.Supplier;
Expand All @@ -30,9 +30,9 @@ interface TrimmedClientRegistration {
* @return
* @param <T>
*/
<T> MapKeyResolver<T> getOrRegisterMapKeyResolver(ResourceLocation id, Supplier<MapKeyResolver<T>> resolverSupplier);
<T> KeyResolver<T> getOrRegisterKeyResolver(ResourceLocation id, Supplier<KeyResolver<T>> resolverSupplier);

<T> MapKeyResolver<T> registerMapKeyResolver(ResourceLocation id, MapKeyResolver<T> resolver);
<T> KeyResolver<T> registerKeyResolver(ResourceLocation id, KeyResolver<T> resolver);

<K, V> MapKey<K, V> registerBaseMapKey(MapKey<K, V> mapKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import com.mojang.serialization.Codec;
import dev.dhyces.trimmed.api.maps.MapHolder;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.api.maps.KeyResolver;
import dev.dhyces.trimmed.api.maps.types.AdvancedMapType;
import dev.dhyces.trimmed.api.maps.types.MapType;
import dev.dhyces.trimmed.impl.client.TrimmedClientMapApiImpl;
import dev.dhyces.trimmed.api.maps.MapKey;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
Expand All @@ -28,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> MapKeyResolver<T> getRegistryMapKeyResolver(ResourceKey<? extends Registry<T>> id);
<T> KeyResolver<T> getRegistryKeyResolver(ResourceKey<? extends Registry<T>> id);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package dev.dhyces.trimmed.api.client.map;

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

public record ClientMapKeyResolver<T>(Codec<T> codec) implements MapKeyResolver<T> {
public record ClientKeyResolver<T>(Codec<T> codec) implements KeyResolver<T> {

@Override
public Codec<T> getCodec() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.dhyces.trimmed.api.client.map;

import dev.dhyces.trimmed.api.maps.KeyResolver;
import net.minecraft.resources.ResourceLocation;

public final class ClientKeyResolvers {
private ClientKeyResolvers() {}

public static final KeyResolver<ResourceLocation> TEXTURE = new ClientKeyResolver<>(ResourceLocation.CODEC);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public static <K, V> MapType<K, V> registerType(ResourceLocation id, MapType<K,
return mapType;
}

public static final MapType<ResourceLocation, String> TEXTURE_SUFFIX = registerType(Trimmed.id("texture_suffix"), MapType.simpleBuilder(ClientMapKeyResolvers.TEXTURE, Codec.STRING).build());
public static final MapType<ResourceLocation, ResourceLocation> TEXTURE_MAPPING = registerType(Trimmed.id("texture_mapping"), MapType.simpleBuilder(ClientMapKeyResolvers.TEXTURE, ResourceLocation.CODEC).build());
public static final MapType<ResourceLocation, String> TEXTURE_SUFFIX = registerType(Trimmed.id("texture_suffix"), MapType.simpleBuilder(ClientKeyResolvers.TEXTURE, Codec.STRING).build());
public static final MapType<ResourceLocation, ResourceLocation> TEXTURE_MAPPING = registerType(Trimmed.id("texture_mapping"), MapType.simpleBuilder(ClientKeyResolvers.TEXTURE, ResourceLocation.CODEC).build());
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import net.minecraft.network.codec.StreamCodec;
import org.jetbrains.annotations.Nullable;

public interface MapKeyResolver<T> {
public interface KeyResolver<T> {
Codec<T> getCodec();
@Nullable
StreamCodec<RegistryFriendlyByteBuf, T> getStreamCodec();

record RegistryWrapper<T>(Registry<T> registry) implements MapKeyResolver<T> {
record RegistryWrapper<T>(Registry<T> registry) implements KeyResolver<T> {

@Override
public Codec<T> getCodec() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package dev.dhyces.trimmed.api.maps.types;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.impl.client.maps.MapKeyResolvers;
import dev.dhyces.trimmed.api.maps.KeyResolver;
import dev.dhyces.trimmed.impl.client.maps.KeyResolvers;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -14,27 +13,27 @@
import java.util.function.Supplier;

public final class AdvancedMapType<K, V, M extends Map<K, V>> implements MapType<K, V> {
private final MapKeyResolver<K> keyResolver;
private final KeyResolver<K> keyResolver;
private final Codec<V> valueCodec;
@Nullable
private final StreamCodec<RegistryFriendlyByteBuf, V> valueStreamCodec;
private final boolean dataPackSynced;
private final Supplier<M> mapSupplier;

AdvancedMapType(MapKeyResolver<K> keyResolver, Codec<V> valueCodec, @Nullable StreamCodec<RegistryFriendlyByteBuf, V> valueStreamCodec, boolean dataPackSynced, @Nullable Supplier<M> mapSupplier) {
AdvancedMapType(KeyResolver<K> keyResolver, Codec<V> valueCodec, @Nullable StreamCodec<RegistryFriendlyByteBuf, V> valueStreamCodec, boolean dataPackSynced, @Nullable Supplier<M> mapSupplier) {
this.keyResolver = keyResolver;
this.valueCodec = valueCodec;
this.valueStreamCodec = valueStreamCodec;
this.dataPackSynced = dataPackSynced;
this.mapSupplier = mapSupplier;
}

public static <K, V, M extends Map<K, V>> Builder<K, V, M> builder(MapKeyResolver<K> keyResolver, Codec<V> valueCodec) {
public static <K, V, M extends Map<K, V>> Builder<K, V, M> builder(KeyResolver<K> keyResolver, Codec<V> valueCodec) {
return new Builder<>(keyResolver, valueCodec);
}

@Override
public MapKeyResolver<K> getKeyResolver() {
public KeyResolver<K> getKeyResolver() {
return keyResolver;
}

Expand Down Expand Up @@ -76,13 +75,13 @@ public int hashCode() {

@Override
public String toString() {
ResourceLocation resolverId = MapKeyResolvers.getId(keyResolver);
ResourceLocation resolverId = KeyResolvers.getId(keyResolver);
return "AdvancedMapType[map_key_resolver: " + (resolverId == null ? "unregistered" : resolverId) + "]";
}

public static class Builder<K, V, M extends Map<K, V>> extends BaseBuilder<K, V> {
private Supplier<M> mapSupplier;
protected Builder(MapKeyResolver<K> keyResolver, Codec<V> valueCodec) {
protected Builder(KeyResolver<K> keyResolver, Codec<V> valueCodec) {
super(keyResolver, valueCodec);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.dhyces.trimmed.api.maps.types;

import com.mojang.serialization.Codec;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.api.maps.KeyResolver;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Reference2ObjectLinkedOpenHashMap;
import net.minecraft.network.RegistryFriendlyByteBuf;
Expand All @@ -14,44 +14,44 @@

// TODO: Add optional validator
public sealed interface MapType<K, V> permits SimpleMapType, AdvancedMapType {
MapKeyResolver<K> getKeyResolver();
KeyResolver<K> getKeyResolver();
Codec<V> getValueCodec();
@Nullable
StreamCodec<RegistryFriendlyByteBuf, V> getValueStreamCodec();
boolean isDataPackSynced();

default Map<K, V> createMap() {
if (getKeyResolver() instanceof MapKeyResolver.RegistryWrapper<K>) {
if (getKeyResolver() instanceof KeyResolver.RegistryWrapper<K>) {
return new Reference2ObjectLinkedOpenHashMap<>();
} else {
return new Object2ObjectLinkedOpenHashMap<>();
}
}

static <K, V> SimpleMapType<K, V> simple(MapKeyResolver<K> keyResolver, Codec<V> valueCodec) {
static <K, V> SimpleMapType<K, V> simple(KeyResolver<K> keyResolver, Codec<V> valueCodec) {
return new SimpleMapType<>(keyResolver, valueCodec, null, false);
}

static <K, V> SimpleMapType.Builder<K, V> simpleBuilder(MapKeyResolver<K> keyResolver, Codec<V> valueCodec) {
static <K, V> SimpleMapType.Builder<K, V> simpleBuilder(KeyResolver<K> keyResolver, Codec<V> valueCodec) {
return SimpleMapType.builder(keyResolver, valueCodec);
}

static <K, V, M extends Map<K, V>> AdvancedMapType<K, V, M> advancedCollection(MapKeyResolver<K> keyResolver, Codec<V> valueCodec, Supplier<M> mapSupplier) {
static <K, V, M extends Map<K, V>> AdvancedMapType<K, V, M> advancedCollection(KeyResolver<K> keyResolver, Codec<V> valueCodec, Supplier<M> mapSupplier) {
return new AdvancedMapType<>(keyResolver, valueCodec, null, false, mapSupplier);
}

static <K, V, M extends Map<K, V>> AdvancedMapType.Builder<K, V, M> advancedBuilder(MapKeyResolver<K> keyResolver, Codec<V> valueCodec) {
static <K, V, M extends Map<K, V>> AdvancedMapType.Builder<K, V, M> advancedBuilder(KeyResolver<K> keyResolver, Codec<V> valueCodec) {
return AdvancedMapType.builder(keyResolver, valueCodec);
}

abstract class BaseBuilder<K, V> {
protected final MapKeyResolver<K> keyResolver;
protected final KeyResolver<K> keyResolver;
protected final Codec<V> valueCodec;
@Nullable
protected StreamCodec<RegistryFriendlyByteBuf, V> valueStreamCodec;
protected boolean dataPackSynced = false;

protected BaseBuilder(MapKeyResolver<K> keyResolver, Codec<V> valueCodec) {
protected BaseBuilder(KeyResolver<K> keyResolver, Codec<V> valueCodec) {
this.keyResolver = keyResolver;
this.valueCodec = valueCodec;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.dhyces.trimmed.api.maps.types;

import com.mojang.serialization.Codec;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.impl.client.maps.MapKeyResolvers;
import dev.dhyces.trimmed.api.maps.KeyResolver;
import dev.dhyces.trimmed.impl.client.maps.KeyResolvers;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -11,25 +11,25 @@
import java.util.Objects;

public final class SimpleMapType<K, V> implements MapType<K, V> {
private final MapKeyResolver<K> keyResolver;
private final KeyResolver<K> keyResolver;
private final Codec<V> valueCodec;
@Nullable
private final StreamCodec<RegistryFriendlyByteBuf, V> valueStreamCodec;
private final boolean dataPackSynced;

SimpleMapType(MapKeyResolver<K> keyResolver, Codec<V> valueCodec, @Nullable StreamCodec<RegistryFriendlyByteBuf, V> valueStreamCodec, boolean dataPackSynced) {
SimpleMapType(KeyResolver<K> keyResolver, Codec<V> valueCodec, @Nullable StreamCodec<RegistryFriendlyByteBuf, V> valueStreamCodec, boolean dataPackSynced) {
this.keyResolver = keyResolver;
this.valueCodec = valueCodec;
this.valueStreamCodec = valueStreamCodec;
this.dataPackSynced = dataPackSynced;
}

public static <K, V> SimpleMapType.Builder<K, V> builder(MapKeyResolver<K> keyResolver, Codec<V> valueCodec) {
public static <K, V> SimpleMapType.Builder<K, V> builder(KeyResolver<K> keyResolver, Codec<V> valueCodec) {
return new SimpleMapType.Builder<>(keyResolver, valueCodec);
}

@Override
public MapKeyResolver<K> getKeyResolver() {
public KeyResolver<K> getKeyResolver() {
return keyResolver;
}

Expand Down Expand Up @@ -63,12 +63,12 @@ public int hashCode() {

@Override
public String toString() {
ResourceLocation resolverId = MapKeyResolvers.getId(keyResolver);
ResourceLocation resolverId = KeyResolvers.getId(keyResolver);
return "SimpleMapType[map_key_resolver: " + (resolverId == null ? "unregistered" : resolverId) + "]";
}

public static class Builder<K, V> extends BaseBuilder<K, V> {
protected Builder(MapKeyResolver<K> keyResolver, Codec<V> valueCodec) {
protected Builder(KeyResolver<K> keyResolver, Codec<V> valueCodec) {
super(keyResolver, valueCodec);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.mojang.serialization.Codec;
import dev.dhyces.trimmed.api.client.TrimmedClientMapApi;
import dev.dhyces.trimmed.api.maps.MapHolder;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.api.maps.KeyResolver;
import dev.dhyces.trimmed.api.maps.types.AdvancedMapType;
import dev.dhyces.trimmed.api.maps.types.MapType;
import dev.dhyces.trimmed.api.maps.MapKey;
import dev.dhyces.trimmed.impl.client.maps.MapKeyResolvers;
import dev.dhyces.trimmed.impl.client.maps.KeyResolvers;
import dev.dhyces.trimmed.impl.client.maps.manager.ClientMapManager;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
Expand Down Expand Up @@ -40,7 +40,7 @@ public <K, V, M extends Map<K, V>> Codec<MapHolder.Typed<K, V, M>> advancedCodec
}

@Override
public @Nullable <T> MapKeyResolver<T> getRegistryMapKeyResolver(ResourceKey<? extends Registry<T>> id) {
return MapKeyResolvers.getResolver(id.location());
public @Nullable <T> KeyResolver<T> getRegistryKeyResolver(ResourceKey<? extends Registry<T>> id) {
return KeyResolvers.getResolver(id.location());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProvider;
import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProviderType;
import dev.dhyces.trimmed.api.maps.MapKey;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.impl.client.maps.MapKeyResolvers;
import dev.dhyces.trimmed.api.maps.KeyResolver;
import dev.dhyces.trimmed.impl.client.maps.KeyResolvers;
import dev.dhyces.trimmed.impl.client.maps.manager.ClientMapManager;
import dev.dhyces.trimmed.impl.client.models.override.provider.ItemOverrideProviderRegistry;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -20,23 +20,23 @@ public <T extends ItemOverrideProvider> ItemOverrideProviderType<T> registerItem
}

@Override
public <T> MapKeyResolver<T> getOrRegisterMapKeyResolver(ResourceLocation id, Supplier<MapKeyResolver<T>> resolverSupplier) {
MapKeyResolver<T> resolver;
public <T> KeyResolver<T> getOrRegisterKeyResolver(ResourceLocation id, Supplier<KeyResolver<T>> resolverSupplier) {
KeyResolver<T> resolver;
try {
resolver = MapKeyResolvers.getResolver(id);
resolver = KeyResolvers.getResolver(id);
} catch (ClassCastException e) {
throw new IllegalArgumentException("Tried to cast " + MapKeyResolvers.getResolver(id) + " for the ID " + id);
throw new IllegalArgumentException("Tried to cast " + KeyResolvers.getResolver(id) + " for the ID " + id);
}
if (resolver == null) {
resolver = resolverSupplier.get();
MapKeyResolvers.register(id, resolver);
KeyResolvers.register(id, resolver);
}
return resolver;
}

@Override
public <T> MapKeyResolver<T> registerMapKeyResolver(ResourceLocation id, MapKeyResolver<T> resolver) {
MapKeyResolvers.register(id, resolver);
public <T> KeyResolver<T> registerKeyResolver(ResourceLocation id, KeyResolver<T> resolver) {
KeyResolvers.register(id, resolver);
return resolver;
}

Expand Down
Loading

0 comments on commit 9674cb0

Please sign in to comment.