From 99c57f826b9439bcd11aaabc9544bee60e1f4886 Mon Sep 17 00:00:00 2001 From: dhyces <10985914+dhyces@users.noreply.github.com> Date: Tue, 14 May 2024 16:37:55 -0700 Subject: [PATCH] Update work Still not functional though, hence the dev branch --- .../main/java/dev/dhyces/trimmed/Trimmed.java | 4 +- .../dev/dhyces/trimmed/TrimmedClient.java | 9 +- .../dhyces/trimmed/api/TrimmedClientApi.java | 7 - .../trimmed/api/TrimmedClientMapApi.java | 27 +--- .../api/client/ClientMapKeyResolver.java | 20 +++ .../api/client/ClientMapKeyResolvers.java | 10 ++ .../trimmed/api/client/ClientMapKeys.java | 6 +- .../trimmed/api/client/ClientMapTypes.java | 13 +- .../TrimmedClientRegistration.java} | 6 +- .../provider/ItemOverrideProviderType.java | 9 +- .../AnyTrimItemOverrideProvider.java | 59 +++---- .../ComponentItemOverrideProvider.java | 67 ++++++++ .../providers/NbtItemOverrideProvider.java | 57 ------- .../api/data/BaseTrimDatagenSuite.java | 46 +----- .../api/data/ItemOverrideDataProvider.java | 14 +- .../trimmed/api/data/maps/MapBuilder.java | 25 ++- .../dhyces/trimmed/api/data/maps/MapFile.java | 50 +++--- .../trimmed/api/data/maps/MapValue.java | 30 ++-- .../data/maps/appenders/BaseMapAppender.java | 33 ++-- .../maps/appenders/ClientMapAppender.java | 15 +- .../appenders/ClientRegistryMapAppender.java | 60 +++---- .../dhyces/trimmed/api/maps/BiMapAccess.java | 5 - .../dhyces/trimmed/api/maps/MapAccess.java | 2 +- .../dhyces/trimmed/api/maps/MapHolder.java | 11 ++ .../trimmed/api/maps/MapKeyResolver.java | 27 ++++ .../dhyces/trimmed/api/maps/MapObserver.java | 7 - .../dev/dhyces/trimmed/api/maps/MapType.java | 47 ------ .../api/maps/types/AdvancedMapType.java | 98 ++++++++++++ .../trimmed/api/maps/types/MapType.java | 93 +++++++++++ .../trimmed/api/maps/types/SimpleMapType.java | 82 ++++++++++ .../dhyces/trimmed/api/util/CodecUtil.java | 12 +- .../api/util/LenientUnboundedMapCodec.java | 77 +++++++++ .../trimmed/impl/TrimmedClientApiImpl.java | 50 ------ .../trimmed/impl/TrimmedClientMapApiImpl.java | 34 +--- .../client/TrimmedClientRegistrationImpl.java | 18 +++ .../atlas/OpenPalettedPermutations.java | 23 +-- .../impl/client/maps/ClientMapKey.java | 54 ------- .../client/maps/ClientRegistryMapKey.java | 56 ------- .../trimmed/impl/client/maps/MapKey.java | 64 ++++++++ .../impl/client/maps/MapKeyResolvers.java | 37 +++++ .../client/maps/manager/ClientMapManager.java | 106 ++++--------- .../impl/client/maps/manager/MapHandler.java | 148 ++++++++++++++++++ .../client/models/source/ModelSource.java | 4 +- .../models/source/ModelSourceRegistry.java | 2 +- .../client/models/source/TrimModelSource.java | 20 ++- ...plate.java => GroovyTemplateResolver.java} | 2 +- .../models/template/ModelTemplateManager.java | 89 ----------- .../impl/client/models/template/Template.java | 14 -- .../models/template/TemplateResolver.java | 7 + .../impl/mixin/client/ArmorTrimMixin.java | 34 ---- .../client/AtlasSourceManagerAccessor.java | 4 +- .../impl/util/BakedModelManagerMixinUtil.java | 2 +- .../assets/minecraft/atlases/armor_trims.json | 16 +- .../assets/minecraft/atlases/blocks.json | 16 +- .../material_suffixes.json} | 2 +- .../darker_material_suffixes.json} | 0 Common/src/main/resources/mods.groovy | 24 ++- Common/src/main/resources/trimmed.mixins.json | 1 - .../dhyces/trimmed/FabricTrimmedClient.java | 2 +- .../trimmed/api/data/TrimDatagenSuite.java | 33 ++-- .../api/data/maps/BaseMapDataProvider.java | 7 +- .../api/data/maps/ClientMapDataProvider.java | 22 +-- .../maps/ClientRegistryMapDataProvider.java | 42 +++-- .../tags/ClientRegistryTagDataProvider.java | 2 +- .../api/data/tags/ClientTagDataProvider.java | 2 +- .../helpers/FabricPlatformHelper.java | 15 +- Neo/build.gradle | 11 +- .../java/dev/dhyces/trimmed/NeoTrimmed.java | 4 +- .../trimmed/api/data/TrimDatagenSuite.java | 10 +- .../api/data/maps/BaseMapDataProvider.java | 15 +- .../api/data/maps/ClientMapDataProvider.java | 24 +-- .../maps/ClientRegistryMapDataProvider.java | 41 ++--- .../tags/ClientRegistryTagDataProvider.java | 2 +- .../api/data/tags/ClientTagDataProvider.java | 2 +- .../impl/mixin/client/ModelLoaderMixin.java | 18 +-- Neo/src/main/resources/META-INF/mods.toml | 40 ----- 76 files changed, 1161 insertions(+), 986 deletions(-) delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/TrimmedClientApi.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeyResolver.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeyResolvers.java rename Common/src/main/java/dev/dhyces/trimmed/api/{TrimmedRegistration.java => client/TrimmedClientRegistration.java} (77%) create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/ComponentItemOverrideProvider.java delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/NbtItemOverrideProvider.java delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/maps/BiMapAccess.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/maps/MapHolder.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/maps/MapKeyResolver.java delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/maps/MapObserver.java delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/maps/MapType.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/maps/types/AdvancedMapType.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/maps/types/MapType.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/maps/types/SimpleMapType.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/api/util/LenientUnboundedMapCodec.java delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/TrimmedClientApiImpl.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/client/TrimmedClientRegistrationImpl.java delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/client/maps/ClientMapKey.java delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/client/maps/ClientRegistryMapKey.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/client/maps/MapKey.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/client/maps/MapKeyResolvers.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/client/maps/manager/MapHandler.java rename Common/src/main/java/dev/dhyces/trimmed/impl/client/models/template/{GroovyTemplate.java => GroovyTemplateResolver.java} (89%) delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/client/models/template/Template.java create mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/client/models/template/TemplateResolver.java delete mode 100644 Common/src/main/java/dev/dhyces/trimmed/impl/mixin/client/ArmorTrimMixin.java rename Common/src/main/resources/assets/trimmed/trimmed/maps/{unchecked/trim_materials.json => trim_textures/material_suffixes.json} (91%) rename Common/src/main/resources/assets/trimmed/trimmed/maps/{unchecked/darker_trim_materials.json => trim_textures/material_suffixes/darker_material_suffixes.json} (100%) delete mode 100644 Neo/src/main/resources/META-INF/mods.toml diff --git a/Common/src/main/java/dev/dhyces/trimmed/Trimmed.java b/Common/src/main/java/dev/dhyces/trimmed/Trimmed.java index a1f1d470..ed78b3af 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/Trimmed.java +++ b/Common/src/main/java/dev/dhyces/trimmed/Trimmed.java @@ -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()) { diff --git a/Common/src/main/java/dev/dhyces/trimmed/TrimmedClient.java b/Common/src/main/java/dev/dhyces/trimmed/TrimmedClient.java index e1640dcb..2e9141a7 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/TrimmedClient.java +++ b/Common/src/main/java/dev/dhyces/trimmed/TrimmedClient.java @@ -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; @@ -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; @@ -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 eventConsumer) { diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedClientApi.java b/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedClientApi.java deleted file mode 100644 index a127706f..00000000 --- a/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedClientApi.java +++ /dev/null @@ -1,7 +0,0 @@ -package dev.dhyces.trimmed.api; - -import dev.dhyces.trimmed.impl.TrimmedClientApiImpl; - -public interface TrimmedClientApi { - TrimmedClientApi INSTANCE = new TrimmedClientApiImpl(); -} diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedClientMapApi.java b/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedClientMapApi.java index 77e6a582..574b607b 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedClientMapApi.java +++ b/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedClientMapApi.java @@ -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 map(ClientMapKey clientMapKey); - - @ApiStatus.Experimental - Stream> mapStream(ClientMapKey clientMapKey); - - @ApiStatus.Experimental - MapAccess map(ClientRegistryMapKey clientRegistryMapKey); - - @Nullable - String getUncheckedClientValue(ClientMapKey clientMapKey, ResourceLocation key); + MapHolder> getSimpleMap(MapKey key); - @Nullable - String getRegistryClientValue(ClientRegistryMapKey clientRegistryMapKey, K key); - ClientMapKeyType registerKeyLoader(ResourceLocation key, Codec keyCodec); + > MapHolder getAdvancedMap(MapKey key, AdvancedMapType mapType); } diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeyResolver.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeyResolver.java new file mode 100644 index 00000000..b54dce88 --- /dev/null +++ b/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeyResolver.java @@ -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(Codec codec) implements MapKeyResolver { + + @Override + public Codec getCodec() { + return codec; + } + + @Override + public @Nullable StreamCodec getStreamCodec() { + return null; + } +} diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeyResolvers.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeyResolvers.java new file mode 100644 index 00000000..1218ca83 --- /dev/null +++ b/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeyResolvers.java @@ -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 TRIM_MATERIAL_TEXTURES = new ClientMapKeyResolver<>(ResourceLocation.CODEC); +} diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeys.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeys.java index d0f8d975..bb85b296 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeys.java +++ b/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapKeys.java @@ -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 DARKER_TRIM_MATERIALS = ClientMapKey.of(ClientMapTypes.TRIM_MATERIAL_PERMUTATIONS, Trimmed.id("darker_trim_materials")); - public static final ClientMapKey TRIM_MATERIALS = ClientMapKey.of(ClientMapTypes.TRIM_MATERIAL_PERMUTATIONS, Trimmed.id("trim_materials")); + public static final MapKey TRIM_MATERIALS = MapKey.of(ClientMapTypes.TRIM_MATERIALS, Trimmed.id("material_suffixes")); + public static final MapKey DARKER_TRIM_MATERIALS = TRIM_MATERIALS.makeSubKey("darker_material_suffixes"); } diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapTypes.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapTypes.java index b71ae808..b779dd2b 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapTypes.java +++ b/Common/src/main/java/dev/dhyces/trimmed/api/client/ClientMapTypes.java @@ -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 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 TRIM_MATERIALS = MapType.simpleBuilder(ClientMapKeyResolvers.TRIM_MATERIAL_TEXTURES, ResourceLocation.CODEC.fieldOf("texture")).build(); } diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedRegistration.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/TrimmedClientRegistration.java similarity index 77% rename from Common/src/main/java/dev/dhyces/trimmed/api/TrimmedRegistration.java rename to Common/src/main/java/dev/dhyces/trimmed/api/client/TrimmedClientRegistration.java index 4bf16baa..a54e908c 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/api/TrimmedRegistration.java +++ b/Common/src/main/java/dev/dhyces/trimmed/api/client/TrimmedClientRegistration.java @@ -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" diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/ItemOverrideProviderType.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/ItemOverrideProviderType.java index 07ebbe7b..10171bd7 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/ItemOverrideProviderType.java +++ b/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/ItemOverrideProviderType.java @@ -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 { - ItemOverrideProviderType NBT = register("nbt", NbtItemOverrideProvider.CODEC); + ItemOverrideProviderType NBT = register("nbt", ComponentItemOverrideProvider.CODEC); ItemOverrideProviderType ANY_TRIM = register("any_trim", AnyTrimItemOverrideProvider.CODEC); - Codec getCodec(); + MapCodec getCodec(); static void bootstrap() {} - private static ItemOverrideProviderType register(String id, Codec codec) { + private static ItemOverrideProviderType register(String id, MapCodec codec) { ItemOverrideProviderType type = () -> codec; ItemOverrideProviderRegistry.register(Trimmed.id(id), type); return type; diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/AnyTrimItemOverrideProvider.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/AnyTrimItemOverrideProvider.java index e4bd5d82..bae45ad8 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/AnyTrimItemOverrideProvider.java +++ b/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/AnyTrimItemOverrideProvider.java @@ -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; @@ -32,7 +28,7 @@ import java.util.stream.Collectors; public class AnyTrimItemOverrideProvider extends SimpleItemOverrideProvider { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> + public static final MapCodec CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( ResourceLocation.CODEC.fieldOf("template").forGetter(anyTrimItemOverrideProvider -> anyTrimItemOverrideProvider.templateId), ResourceLocation.CODEC.fieldOf("trim_texture").forGetter(anyTrimItemOverrideProvider -> anyTrimItemOverrideProvider.trimTexture), @@ -45,8 +41,6 @@ public class AnyTrimItemOverrideProvider extends SimpleItemOverrideProvider { private final Set excludedTextures; private ResourceLocation id; - private static final MapAccess PERMUTATIONS = ClientMapManager.getUncheckedHandler().getMap(ClientMapTypes.TRIM_MATERIAL_PERMUTATIONS); - public AnyTrimItemOverrideProvider(ResourceLocation templateId, ResourceLocation trimTexture, Set excludedTextures) { this.templateId = templateId; this.trimTexture = trimTexture; @@ -61,19 +55,16 @@ public Optional getModel(ItemStack itemStack, @Nullable ClientLevel @Override public Optional getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) { - if (world == null) { + if (!(itemStack.getItem() instanceof ArmorItem armorItem)) { return Optional.empty(); } - Optional materialIdOptional = ArmorTrim.getTrim(world.registryAccess(), itemStack, true) + Optional 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 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(); } @@ -81,23 +72,23 @@ public Optional getModelLocation(ItemStack itemStack, @Nu @Override public void finish(ResourceLocation id) { this.id = id; - ModelTemplateManager.addTemplate(templateId, (reader, modelConsumer) -> { - String rawData = reader.lines().collect(Collectors.joining()); - Pair item = Pair.of("item_texture", id.withPrefix("item/").toString()); - - for (ImmutableEntry material : PERMUTATIONS) { - if (excludedTextures.contains(material.getKey())) { - continue; - } - - List> 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 item = Pair.of("item_texture", id.withPrefix("item/").toString()); +// +// for (ImmutableEntry material : PERMUTATIONS) { +// if (excludedTextures.contains(material.getKey())) { +// continue; +// } +// +// List> 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 diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/ComponentItemOverrideProvider.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/ComponentItemOverrideProvider.java new file mode 100644 index 00000000..21d1704b --- /dev/null +++ b/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/ComponentItemOverrideProvider.java @@ -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 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 getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) { + if (!itemStack.getComponentsPatch().isEmpty()) { + DataComponentPatch stackPatch = itemStack.getComponentsPatch(); + for (Map.Entry, 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 getModelsToBake() { + return Stream.of(model); + } + + @Override + public ItemOverrideProviderType getType() { + return ItemOverrideProviderType.NBT; + } +} diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/NbtItemOverrideProvider.java b/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/NbtItemOverrideProvider.java deleted file mode 100644 index 3378c71f..00000000 --- a/Common/src/main/java/dev/dhyces/trimmed/api/client/override/provider/providers/NbtItemOverrideProvider.java +++ /dev/null @@ -1,57 +0,0 @@ -package dev.dhyces.trimmed.api.client.override.provider.providers; - -import com.mojang.serialization.Codec; -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.nbt.CompoundTag; -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.item.ItemStack; -import org.jetbrains.annotations.Nullable; - -import java.util.Optional; -import java.util.stream.Stream; - -public final class NbtItemOverrideProvider extends SimpleItemOverrideProvider { - public static final Codec CODEC = RecordCodecBuilder.create(instance -> - instance.group( - CompoundTag.CODEC.fieldOf("nbt").forGetter(nbtItemOverrideProvider -> nbtItemOverrideProvider.nbt), - CodecUtil.MODEL_IDENTIFIER_CODEC.fieldOf("model").forGetter(nbtItemOverrideProvider -> nbtItemOverrideProvider.model) - ).apply(instance, NbtItemOverrideProvider::new) - ); - - private final CompoundTag nbt; - private final ModelResourceLocation model; - - public NbtItemOverrideProvider(CompoundTag nbt, ModelResourceLocation modelId) { - this.nbt = nbt; - this.model = modelId; - } - - @Override - public Optional getModelLocation(ItemStack itemStack, @Nullable ClientLevel world, @Nullable LivingEntity entity, int seed) { - if (itemStack.hasTag()) { - CompoundTag stackNbt = itemStack.getTag(); - for (String key : nbt.getAllKeys()) { - if (!stackNbt.contains(key) || !stackNbt.get(key).equals(nbt.get(key))) { - return Optional.empty(); - } - } - return Optional.of(model); - } - return Optional.empty(); - } - - @Override - public Stream getModelsToBake() { - return Stream.of(model); - } - - @Override - public ItemOverrideProviderType getType() { - return ItemOverrideProviderType.NBT; - } -} diff --git a/Common/src/main/java/dev/dhyces/trimmed/api/data/BaseTrimDatagenSuite.java b/Common/src/main/java/dev/dhyces/trimmed/api/data/BaseTrimDatagenSuite.java index cb6743aa..db020618 100644 --- a/Common/src/main/java/dev/dhyces/trimmed/api/data/BaseTrimDatagenSuite.java +++ b/Common/src/main/java/dev/dhyces/trimmed/api/data/BaseTrimDatagenSuite.java @@ -1,7 +1,5 @@ package dev.dhyces.trimmed.api.data; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; import com.mojang.datafixers.util.Pair; import net.minecraft.Util; import net.minecraft.advancements.critereon.InventoryChangeTrigger; @@ -14,7 +12,6 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.tags.ItemTags; -import net.minecraft.world.item.ArmorMaterial; import net.minecraft.world.item.Items; import net.minecraft.world.item.armortrim.TrimMaterial; import net.minecraft.world.item.armortrim.TrimPattern; @@ -43,8 +40,7 @@ public abstract class BaseTrimDatagenSuite { protected List, TrimMaterial>> materials = new ArrayList<>(); protected List patternTextures = new ArrayList<>(); - protected Map materialTexturePermutations = new LinkedHashMap<>(); - protected Multimap, ArmorMaterialOverride> armorMaterialOverrides = HashMultimap.create(); + protected Map materialTexturePermutations = new LinkedHashMap<>(); public BaseTrimDatagenSuite(String modid, @Nullable BiConsumer translationConsumer) { this.modid = modid; @@ -203,7 +199,7 @@ public BaseTrimDatagenSuite makeMaterial(ResourceKey materialKey, materialConfigConsumer.accept(config); String translationKey = Util.makeDescriptionId("trim_pattern", materialKey.location()); - materials.add(Pair.of(materialKey, new TrimMaterial(config.assetName, materialItem.asItem().builtInRegistryHolder(), -1.0f, Map.of(), Component.translatable(translationKey).withStyle(config.materialStyle)))); + materials.add(Pair.of(materialKey, new TrimMaterial(config.assetName.toString(), materialItem.asItem().builtInRegistryHolder(), -1.0f, Map.of(), Component.translatable(translationKey).withStyle(config.materialStyle)))); if (mainTranslationConsumer != null) { String translation; @@ -227,13 +223,6 @@ public BaseTrimDatagenSuite makeMaterial(ResourceKey materialKey, materialTexturePermutations.put(key, config.assetName); } - if (!config.armorOverrides.isEmpty()) { - config.armorOverrides.forEach(armorMaterialOverride -> { - armorMaterialOverrides.put(materialKey, armorMaterialOverride); - materialTexturePermutations.put(armorMaterialOverride.textureLocation, armorMaterialOverride.overrideSuffix); - }); - } - return this; } @@ -313,12 +302,11 @@ public static class MaterialConfig { protected String mainTranslation; protected Set altTranslations = new HashSet<>(); protected ResourceLocation paletteTexture; - protected List armorOverrides = new ArrayList<>(); - protected String assetName; + protected ResourceLocation assetName; private MaterialConfig(ResourceKey materialKey, Style materialStyle) { this.materialKey = materialKey; - this.assetName = materialKey.location().toString().replace(':', '-'); + this.assetName = materialKey.location(); this.materialStyle = materialStyle; } @@ -348,28 +336,6 @@ public MaterialConfig colorPaletteTexture(ResourceLocation paletteTexture) { return this; } - /** - * Override for {@link MaterialConfig#armorOverride(ArmorMaterial, ResourceLocation, String)}, but it assumes - * the texture location is "{modid}:trims/color_palettes/{material}_darker". - * @param overrideSuffix Suffix for the override. It's encouraged to use the "modid-suffix" format. - */ - public MaterialConfig armorOverride(ArmorMaterial armorMaterial, String overrideSuffix) { - return armorOverride(armorMaterial, materialKey.location().withPath(s -> "trims/color_palettes/" + s + "_darker"), overrideSuffix); - } - - /** - * Allows users to specify texture overrides for armor materials, for example, using a darker texture when the - * material is applied to an armor of the same color/crafting material. Vanilla hardcodes an enum into their - * TrimMaterial class, which means that enum extension would be mandatory for modders and everyone would have - * to migrate their 'ArmorMaterial's to it. Trimmed provides an alternate system outside the material class - * as a resource-pack element. - * @param overrideSuffix Suffix for the override. It's encouraged to use the "modid-suffix" format. - */ - public MaterialConfig armorOverride(ArmorMaterial armorMaterial, ResourceLocation textureLocation, String overrideSuffix) { - armorOverrides.add(new ArmorMaterialOverride(new ResourceLocation(armorMaterial.getName()), textureLocation, overrideSuffix)); - return this; - } - /** * Allows modification of the style used in the material's component. This style is already colored with what * was passed into the {@link BaseTrimDatagenSuite#makeMaterial(ResourceKey, ItemLike, int, Consumer)} method. @@ -387,7 +353,7 @@ public MaterialConfig style(UnaryOperator