Skip to content

Commit

Permalink
Update work
Browse files Browse the repository at this point in the history
Still not functional though, hence the dev branch
  • Loading branch information
dhyces committed May 14, 2024
1 parent fbd97e3 commit 99c57f8
Show file tree
Hide file tree
Showing 76 changed files with 1,161 additions and 986 deletions.
4 changes: 1 addition & 3 deletions Common/src/main/java/dev/dhyces/trimmed/Trimmed.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ public static ResourceLocation id(String id) {

public static final Logger LOGGER = LoggerFactory.getLogger("Trimmed");

public static void init() {
// Networking.init();
}
public static void init() {}

public static void logInDev(String str) {
if (!Services.PLATFORM_HELPER.isProduction()) {
Expand Down
9 changes: 8 additions & 1 deletion Common/src/main/java/dev/dhyces/trimmed/TrimmedClient.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package dev.dhyces.trimmed;

import dev.dhyces.trimmed.api.client.ClientMapKeyResolvers;
import dev.dhyces.trimmed.api.client.ClientMapKeys;
import dev.dhyces.trimmed.api.client.ClientMapTypes;
import dev.dhyces.trimmed.impl.client.atlas.TrimmedSpriteSourceTypes;
import dev.dhyces.trimmed.impl.client.maps.MapKeyResolvers;
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.tags.manager.ClientTagManager;
Expand All @@ -11,6 +15,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.PreparableReloadListener;

import java.util.function.BiConsumer;
Expand All @@ -26,8 +31,10 @@ public class TrimmedClient {

public static void init() {
TrimmedSpriteSourceTypes.bootstrap();
ModelTemplateManager.init();
// ModelTemplateManager.init();
ItemOverrideProviderRegistry.init();
MapKeyResolvers.register(Trimmed.id("trim_textures"), ClientMapKeyResolvers.TRIM_MATERIAL_TEXTURES);
ClientMapManager.registerBaseKey(ClientMapKeys.TRIM_MATERIALS);
}

public static void registerClientReloadListener(BiConsumer<String, PreparableReloadListener> eventConsumer) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
package dev.dhyces.trimmed.api;

import dev.dhyces.trimmed.api.maps.MapAccess;
import dev.dhyces.trimmed.api.maps.MapHolder;
import dev.dhyces.trimmed.api.maps.types.AdvancedMapType;
import dev.dhyces.trimmed.impl.TrimmedClientMapApiImpl;
import dev.dhyces.trimmed.api.maps.OptionalMapEntry;
import com.mojang.serialization.Codec;
import dev.dhyces.trimmed.impl.client.maps.ClientMapKey;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.ApiStatus;
import dev.dhyces.trimmed.impl.client.maps.MapKey;

import java.util.Map;

public interface TrimmedClientMapApi {
static TrimmedClientMapApi getInstance() {
return TrimmedClientMapApiImpl.INSTANCE;
}

@ApiStatus.Experimental
MapAccess<ResourceLocation, String> map(ClientMapKey clientMapKey);

@ApiStatus.Experimental
Stream<OptionalMapEntry<ResourceLocation, String>> mapStream(ClientMapKey clientMapKey);

@ApiStatus.Experimental
<K> MapAccess<K, String> map(ClientRegistryMapKey<K> clientRegistryMapKey);

@Nullable
String getUncheckedClientValue(ClientMapKey clientMapKey, ResourceLocation key);
<K, V> MapHolder<K, V, Map<K, V>> getSimpleMap(MapKey<K, V> key);

@Nullable
<K> String getRegistryClientValue(ClientRegistryMapKey<K> clientRegistryMapKey, K key);
<T> ClientMapKeyType<T> registerKeyLoader(ResourceLocation key, Codec<T> keyCodec);
<K, V, M extends Map<K, V>> MapHolder<K, V, M> getAdvancedMap(MapKey<K, V> key, AdvancedMapType<K, V, M> mapType);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.dhyces.trimmed.api.client;

import com.mojang.serialization.Codec;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
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> {

@Override
public Codec<T> getCodec() {
return codec;
}

@Override
public @Nullable StreamCodec<RegistryFriendlyByteBuf, T> getStreamCodec() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.dhyces.trimmed.api.client;

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

public final class ClientMapKeyResolvers {
private ClientMapKeyResolvers() {}

public static final MapKeyResolver<ResourceLocation> TRIM_MATERIAL_TEXTURES = new ClientMapKeyResolver<>(ResourceLocation.CODEC);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.dhyces.trimmed.api.client;

import dev.dhyces.trimmed.Trimmed;
import dev.dhyces.trimmed.impl.client.maps.ClientMapKey;
import dev.dhyces.trimmed.impl.client.maps.MapKey;
import net.minecraft.resources.ResourceLocation;

public class ClientMapKeys {
public static final ClientMapKey<ResourceLocation, ResourceLocation> DARKER_TRIM_MATERIALS = ClientMapKey.of(ClientMapTypes.TRIM_MATERIAL_PERMUTATIONS, Trimmed.id("darker_trim_materials"));
public static final ClientMapKey<ResourceLocation, ResourceLocation> TRIM_MATERIALS = ClientMapKey.of(ClientMapTypes.TRIM_MATERIAL_PERMUTATIONS, Trimmed.id("trim_materials"));
public static final MapKey<ResourceLocation, ResourceLocation> TRIM_MATERIALS = MapKey.of(ClientMapTypes.TRIM_MATERIALS, Trimmed.id("material_suffixes"));
public static final MapKey<ResourceLocation, ResourceLocation> DARKER_TRIM_MATERIALS = TRIM_MATERIALS.makeSubKey("darker_material_suffixes");
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package dev.dhyces.trimmed.api.client;

import dev.dhyces.trimmed.api.maps.MapType;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.api.maps.types.MapType;
import net.minecraft.resources.ResourceLocation;

public class ClientMapTypes {
// TODO: Figure out registration. Probably make it separate from instantiation.
public static final MapType<ResourceLocation, ResourceLocation> TRIM_MATERIAL_PERMUTATIONS = MapType.builder(ResourceLocation.CODEC, ResourceLocation.CODEC).build();
// Map resolvers resolve the key object and directory. Map types hold the resolver for the key type and the value codec.
// Map types can either be independent or "grouped" (under a named subdirectory, which then permits the "append" field).
//
public final class ClientMapTypes {
private ClientMapTypes() {}

public static final MapType<ResourceLocation, ResourceLocation> TRIM_MATERIALS = MapType.simpleBuilder(ClientMapKeyResolvers.TRIM_MATERIAL_TEXTURES, ResourceLocation.CODEC.fieldOf("texture")).build();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.dhyces.trimmed.api;
package dev.dhyces.trimmed.api.client;

import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProvider;
import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProviderType;
import dev.dhyces.trimmed.api.maps.MapKeyResolver;
import dev.dhyces.trimmed.api.maps.types.MapType;
import net.minecraft.resources.ResourceLocation;

public interface TrimmedRegistration {
public interface TrimmedClientRegistration {
/**
* This is a helper to register your own item override type. It's basically a codec supplier.
* @param id Id for this override type, ie "trimmed:nbt"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package dev.dhyces.trimmed.api.client.override.provider;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import dev.dhyces.trimmed.Trimmed;
import dev.dhyces.trimmed.api.client.override.provider.providers.AnyTrimItemOverrideProvider;
import dev.dhyces.trimmed.api.client.override.provider.providers.NbtItemOverrideProvider;
import dev.dhyces.trimmed.api.client.override.provider.providers.ComponentItemOverrideProvider;
import dev.dhyces.trimmed.impl.client.models.override.provider.ItemOverrideProviderRegistry;

public interface ItemOverrideProviderType<T extends ItemOverrideProvider> {
ItemOverrideProviderType<NbtItemOverrideProvider> NBT = register("nbt", NbtItemOverrideProvider.CODEC);
ItemOverrideProviderType<ComponentItemOverrideProvider> NBT = register("nbt", ComponentItemOverrideProvider.CODEC);
ItemOverrideProviderType<AnyTrimItemOverrideProvider> ANY_TRIM = register("any_trim", AnyTrimItemOverrideProvider.CODEC);

Codec<T> getCodec();
MapCodec<T> getCodec();

static void bootstrap() {}

private static <T extends ItemOverrideProvider> ItemOverrideProviderType<T> register(String id, Codec<T> codec) {
private static <T extends ItemOverrideProvider> ItemOverrideProviderType<T> register(String id, MapCodec<T> codec) {
ItemOverrideProviderType<T> type = () -> codec;
ItemOverrideProviderRegistry.register(Trimmed.id(id), type);
return type;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
package dev.dhyces.trimmed.api.client.override.provider.providers;

import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.dhyces.trimmed.api.TrimmedClientApi;
import dev.dhyces.trimmed.api.maps.MapAccess;
import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProviderType;
import dev.dhyces.trimmed.api.client.override.provider.SimpleItemOverrideProvider;
import dev.dhyces.trimmed.api.maps.ImmutableEntry;
import dev.dhyces.trimmed.api.client.ClientMapTypes;
import dev.dhyces.trimmed.api.util.CodecUtil;
import dev.dhyces.trimmed.impl.client.maps.manager.ClientMapManager;
import dev.dhyces.trimmed.impl.client.models.template.ModelTemplateManager;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.block.model.BlockModel;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
import net.minecraft.core.component.DataComponents;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.armortrim.ArmorTrim;
import org.jetbrains.annotations.Nullable;
Expand All @@ -32,7 +28,7 @@
import java.util.stream.Collectors;

public class AnyTrimItemOverrideProvider extends SimpleItemOverrideProvider {
public static final Codec<AnyTrimItemOverrideProvider> CODEC = RecordCodecBuilder.create(instance ->
public static final MapCodec<AnyTrimItemOverrideProvider> CODEC = RecordCodecBuilder.mapCodec(instance ->
instance.group(
ResourceLocation.CODEC.fieldOf("template").forGetter(anyTrimItemOverrideProvider -> anyTrimItemOverrideProvider.templateId),
ResourceLocation.CODEC.fieldOf("trim_texture").forGetter(anyTrimItemOverrideProvider -> anyTrimItemOverrideProvider.trimTexture),
Expand All @@ -45,8 +41,6 @@ public class AnyTrimItemOverrideProvider extends SimpleItemOverrideProvider {
private final Set<ResourceLocation> excludedTextures;
private ResourceLocation id;

private static final MapAccess<ResourceLocation, String> PERMUTATIONS = ClientMapManager.getUncheckedHandler().getMap(ClientMapTypes.TRIM_MATERIAL_PERMUTATIONS);

public AnyTrimItemOverrideProvider(ResourceLocation templateId, ResourceLocation trimTexture, Set<ResourceLocation> excludedTextures) {
this.templateId = templateId;
this.trimTexture = trimTexture;
Expand All @@ -61,43 +55,40 @@ public Optional<BakedModel> getModel(ItemStack itemStack, @Nullable ClientLevel

@Override
public Optional<ModelResourceLocation> getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) {
if (world == null) {
if (!(itemStack.getItem() instanceof ArmorItem armorItem)) {
return Optional.empty();
}

Optional<ResourceLocation> materialIdOptional = ArmorTrim.getTrim(world.registryAccess(), itemStack, true)
Optional<ResourceLocation> materialIdOptional = Optional.ofNullable(itemStack.get(DataComponents.TRIM))
.map(ArmorTrim::material)
.flatMap(Holder::unwrapKey)
.map(ResourceKey::location);
.flatMap(holder -> holder.unwrapKey().map(key -> key.location().withPath(holder.value().overrideArmorMaterials().getOrDefault(armorItem.getMaterial(), holder.value().assetName()))));
if (materialIdOptional.isPresent()) {
ResourceLocation materialId = materialIdOptional.get();
Optional<String> materialOverride = TrimmedClientApi.INSTANCE.getArmorTrimSuffix(world.registryAccess(), itemStack);
String trimMaterialSuffix = materialOverride.orElseGet(() -> PERMUTATIONS.get(materialId.withPrefix("trims/color_palettes/")));
return Optional.of(new ModelResourceLocation(id.withSuffix("_" + trimMaterialSuffix + "_trim"), "inventory"));
return Optional.of(new ModelResourceLocation(id.withSuffix("_" + materialId.getPath() + "_trim"), "inventory"));
}
return Optional.empty();
}

@Override
public void finish(ResourceLocation id) {
this.id = id;
ModelTemplateManager.addTemplate(templateId, (reader, modelConsumer) -> {
String rawData = reader.lines().collect(Collectors.joining());
Pair<String, String> item = Pair.of("item_texture", id.withPrefix("item/").toString());

for (ImmutableEntry<ResourceLocation, String> material : PERMUTATIONS) {
if (excludedTextures.contains(material.getKey())) {
continue;
}

List<Pair<String, String>> replacers = new ArrayList<>();
replacers.add(item);
replacers.add(Pair.of("material", material.getValue()));
replacers.add(Pair.of("trim_texture", trimTexture.toString()));

modelConsumer.accept(id.withSuffix("_" + material.getValue() + "_trim"), () -> BlockModel.fromString(GroovyReplacer.replace(rawData, replacers)));
}
});
// ModelTemplateManager.addTemplate(templateId, (reader, modelConsumer) -> {
// String rawData = reader.lines().collect(Collectors.joining());
// Pair<String, String> item = Pair.of("item_texture", id.withPrefix("item/").toString());
//
// for (ImmutableEntry<ResourceLocation, String> material : PERMUTATIONS) {
// if (excludedTextures.contains(material.getKey())) {
// continue;
// }
//
// List<Pair<String, String>> replacers = new ArrayList<>();
// replacers.add(item);
// replacers.add(Pair.of("material", material.getValue()));
// replacers.add(Pair.of("trim_texture", trimTexture.toString()));
//
// modelConsumer.accept(id.withSuffix("_" + material.getValue() + "_trim"), () -> BlockModel.fromString(GroovyReplacer.replace(rawData, replacers)));
// }
// });
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dev.dhyces.trimmed.api.client.override.provider.providers;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProviderType;
import dev.dhyces.trimmed.api.client.override.provider.SimpleItemOverrideProvider;
import dev.dhyces.trimmed.api.util.CodecUtil;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

public final class ComponentItemOverrideProvider extends SimpleItemOverrideProvider {
public static final MapCodec<ComponentItemOverrideProvider> CODEC = RecordCodecBuilder.mapCodec(instance ->
instance.group(
DataComponentPatch.CODEC.fieldOf("components").forGetter(componentItemOverrideProvider -> componentItemOverrideProvider.componentPatch),
CodecUtil.MODEL_IDENTIFIER_CODEC.fieldOf("model").forGetter(componentItemOverrideProvider -> componentItemOverrideProvider.model)
).apply(instance, ComponentItemOverrideProvider::new)
);

private final DataComponentPatch componentPatch;
private final ModelResourceLocation model;

public ComponentItemOverrideProvider(DataComponentPatch componentPatch, ModelResourceLocation modelId) {
this.componentPatch = componentPatch;
this.model = modelId;
}

@Override
public Optional<ModelResourceLocation> getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) {
if (!itemStack.getComponentsPatch().isEmpty()) {
DataComponentPatch stackPatch = itemStack.getComponentsPatch();
for (Map.Entry<DataComponentType<?>, Optional<?>> entry : componentPatch.entrySet()) {
Optional<?> stackData = stackPatch.get(entry.getKey());
Optional<?> testData = componentPatch.get(entry.getKey());
if ((stackData == null) != (testData == null)) {
return Optional.empty();
}
if (stackData.isEmpty() != testData.isEmpty()) {
return Optional.empty();
}
if (stackData.isPresent() && testData.isPresent() && !stackData.get().equals(testData.get())) {
return Optional.empty();
}
}
return Optional.of(model);
}
return Optional.empty();
}

@Override
public Stream<ModelResourceLocation> getModelsToBake() {
return Stream.of(model);
}

@Override
public ItemOverrideProviderType<?> getType() {
return ItemOverrideProviderType.NBT;
}
}
Loading

0 comments on commit 99c57f8

Please sign in to comment.