Skip to content

Commit

Permalink
Change any_trim overrides and provider
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyces committed May 27, 2024
1 parent 09c43d6 commit acb134a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package dev.dhyces.trimmed.api.client.override.provider.providers;

import com.mojang.datafixers.util.Pair;
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.maps.ImmutableEntry;
import dev.dhyces.trimmed.api.util.CodecUtil;
import dev.dhyces.trimmed.impl.client.models.template.ModelTemplateManager;
import dev.dhyces.trimmed.impl.client.models.template.StringTemplate;
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.component.DataComponents;
Expand All @@ -21,30 +17,19 @@
import net.minecraft.world.item.armortrim.ArmorTrim;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class AnyTrimItemOverrideProvider extends SimpleItemOverrideProvider {
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),
CodecUtil.setOf(ResourceLocation.CODEC).optionalFieldOf("exclude_palettes", Set.of()).forGetter(anyTrimItemOverrideProvider -> anyTrimItemOverrideProvider.excludedTextures)
StringTemplate.CODEC.fieldOf("model_id_template").forGetter(anyTrimItemOverrideProvider -> anyTrimItemOverrideProvider.modelIdTemplate)
).apply(instance, AnyTrimItemOverrideProvider::new)
);

private final ResourceLocation templateId;
private final ResourceLocation trimTexture;
private final Set<ResourceLocation> excludedTextures;
private ResourceLocation id;
private final StringTemplate modelIdTemplate;

public AnyTrimItemOverrideProvider(ResourceLocation templateId, ResourceLocation trimTexture, Set<ResourceLocation> excludedTextures) {
this.templateId = templateId;
this.trimTexture = trimTexture;
this.excludedTextures = excludedTextures;
public AnyTrimItemOverrideProvider(StringTemplate modelIdTemplate) {
this.modelIdTemplate = modelIdTemplate;
}

@Override
Expand All @@ -64,33 +49,17 @@ public Optional<ModelResourceLocation> getModelLocation(ItemStack itemStack, @Nu
.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();
return Optional.of(new ModelResourceLocation(id.withSuffix("_" + materialId.getPath() + "_trim"), "inventory"));
return Optional.of(new ModelResourceLocation(new ResourceLocation(modelIdTemplate.process(s -> {
if (s.equals("material_suffix")) {
return materialId.getPath();
} else {
return null;
}
})), "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)));
// }
// });
}

@Override
public ItemOverrideProviderType<?> getType() {
return ItemOverrideProviderType.ANY_TRIM;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package dev.dhyces.trimmed.api.data;

import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonElement;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.JsonOps;
import dev.dhyces.trimmed.Trimmed;
import dev.dhyces.trimmed.api.client.override.provider.ItemOverrideProvider;
import dev.dhyces.trimmed.api.client.override.provider.providers.AnyTrimItemOverrideProvider;
import dev.dhyces.trimmed.api.client.override.provider.providers.ComponentItemOverrideProvider;
import dev.dhyces.trimmed.impl.client.models.template.StringTemplate;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.core.component.DataComponentPatch;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.CachedOutput;
import net.minecraft.data.DataProvider;
import net.minecraft.data.PackOutput;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.ItemLike;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;

public abstract class ItemOverrideDataProvider implements DataProvider {
Expand Down Expand Up @@ -51,8 +47,20 @@ protected ArmorSetTrimBuilder anyTrimBuilder(ArmorSet armorSet) {
return new ArmorSetTrimBuilder(armorSet);
}

protected void addAnyTrimOverride(ItemLike item, ResourceLocation templateId, ResourceLocation trimTexture, List<ResourceLocation> excludedTextures) {
addItemOverrides(item, new AnyTrimItemOverrideProvider(templateId, trimTexture, new LinkedHashSet<>(excludedTextures)));
protected void defaultAnyTrim(ArmorSet armorSet) {
new ArmorSetTrimBuilder(armorSet).defaultTemplate().end();
}

protected void addAnyTrimOverride(ItemLike item, StringTemplate stringTemplate) {
addItemOverrides(item, new AnyTrimItemOverrideProvider(stringTemplate));
}

protected void addAnyTrimOverride(ItemLike item, String stringTemplate) {
addAnyTrimOverride(item, StringTemplate.of(stringTemplate));
}

protected void addAnyTrimOverride(ItemLike item) {
addAnyTrimOverride(item, defaultTemplateString(item));
}

protected void addItemOverrides(ItemLike item, ItemOverrideProvider... providers) {
Expand All @@ -61,6 +69,10 @@ protected void addItemOverrides(ItemLike item, ItemOverrideProvider... providers
}
}

protected String defaultTemplateString(ItemLike item) {
return BuiltInRegistries.ITEM.getKey(item.asItem()).withPrefix("item/") + "_${material_suffix}_trim";
}

@Override
public CompletableFuture<?> run(CachedOutput writer) {
addItemOverrides();
Expand All @@ -81,67 +93,65 @@ public record ArmorSet(ItemLike helmet, ItemLike chestplate, ItemLike leggings,

public class ArmorSetTrimBuilder {
private ArmorSet armorSet;
private ResourceLocation templateId;
private final Set<ResourceLocation> excludedTextures = new LinkedHashSet<>();

public static final Supplier<List<ResourceLocation>> VANILLA_DARKER_COLORS = Suppliers.memoize(() -> {
ImmutableList.Builder<ResourceLocation> builder = ImmutableList.builder();
builder.add(new ResourceLocation("minecraft:trims/color_palettes/iron_darker"));
builder.add(new ResourceLocation("minecraft:trims/color_palettes/gold_darker"));
builder.add(new ResourceLocation("minecraft:trims/color_palettes/diamond_darker"));
builder.add(new ResourceLocation("minecraft:trims/color_palettes/netherite_darker"));
return builder.build();
});
private StringTemplate helmetTemplate;
private StringTemplate chestplateTemplate;
private StringTemplate leggingsTemplate;
private StringTemplate bootsTemplate;

ArmorSetTrimBuilder(ArmorSet armorSet) {
this.armorSet = armorSet;
}

public ArmorSetTrimBuilder twoLayer() {
templateId = Trimmed.id("item/two_layer_trim");
public ArmorSetTrimBuilder defaultTemplate() {
helmetTemplate = StringTemplate.of(defaultTemplateString(armorSet.helmet));
chestplateTemplate = StringTemplate.of(defaultTemplateString(armorSet.chestplate));
leggingsTemplate = StringTemplate.of(defaultTemplateString(armorSet.leggings));
bootsTemplate = StringTemplate.of(defaultTemplateString(armorSet.boots));
return this;
}

public ArmorSetTrimBuilder threeLayer() {
templateId = Trimmed.id("item/three_layer_trim");
public ArmorSetTrimBuilder helmetTemplate(String string) {
this.helmetTemplate = StringTemplate.of(string);
if (!helmetTemplate.getVariables().contains("material_suffix") || helmetTemplate.getVariables().size() != 1) {
throw new IllegalStateException("Only \"material_suffix\" is a valid template variable for \"any_trim\" model overrides");
}
return this;
}

public ArmorSetTrimBuilder setTemplateId(ResourceLocation templateId) {
this.templateId = templateId;
public ArmorSetTrimBuilder chestplateTemplate(String string) {
this.chestplateTemplate = StringTemplate.of(string);
if (!chestplateTemplate.getVariables().contains("material_suffix") || chestplateTemplate.getVariables().size() != 1) {
throw new IllegalStateException("Only \"material_suffix\" is a valid template variable for \"any_trim\" model overrides");
}
return this;
}

public ArmorSetTrimBuilder excludeVanillaDarker() {
this.excludedTextures.add(new ResourceLocation("minecraft:trims/color_palettes/iron_darker"));
this.excludedTextures.add(new ResourceLocation("minecraft:trims/color_palettes/gold_darker"));
this.excludedTextures.add(new ResourceLocation("minecraft:trims/color_palettes/diamond_darker"));
this.excludedTextures.add(new ResourceLocation("minecraft:trims/color_palettes/netherite_darker"));
public ArmorSetTrimBuilder leggingsTemplate(String string) {
this.leggingsTemplate = StringTemplate.of(string);
if (!leggingsTemplate.getVariables().contains("material_suffix") || leggingsTemplate.getVariables().size() != 1) {
throw new IllegalStateException("Only \"material_suffix\" is a valid template variable for \"any_trim\" model overrides");
}
return this;
}

public ArmorSetTrimBuilder exclude(ResourceLocation resourceLocation) {
this.excludedTextures.add(resourceLocation);
return this;
}

public ArmorSetTrimBuilder exclude(ResourceLocation... resourceLocations) {
this.excludedTextures.addAll(Arrays.asList(resourceLocations));
return this;
}

public ArmorSetTrimBuilder include(ResourceLocation resourceLocation) {
this.excludedTextures.remove(resourceLocation);
public ArmorSetTrimBuilder bootsTemplate(String string) {
this.bootsTemplate = StringTemplate.of(string);
if (!bootsTemplate.getVariables().contains("material_suffix") || bootsTemplate.getVariables().size() != 1) {
throw new IllegalStateException("Only \"material_suffix\" is a valid template variable for \"any_trim\" model overrides");
}
return this;
}

public void end() {
Objects.requireNonNull(templateId);

addItemOverrides(armorSet.helmet, new AnyTrimItemOverrideProvider(templateId, new ResourceLocation("minecraft:trims/items/helmet_trim"), excludedTextures));
addItemOverrides(armorSet.chestplate, new AnyTrimItemOverrideProvider(templateId, new ResourceLocation("minecraft:trims/items/chestplate_trim"), excludedTextures));
addItemOverrides(armorSet.leggings, new AnyTrimItemOverrideProvider(templateId, new ResourceLocation("minecraft:trims/items/leggings_trim"), excludedTextures));
addItemOverrides(armorSet.boots, new AnyTrimItemOverrideProvider(templateId, new ResourceLocation("minecraft:trims/items/boots_trim"), excludedTextures));
Objects.requireNonNull(helmetTemplate);
Objects.requireNonNull(chestplateTemplate);
Objects.requireNonNull(leggingsTemplate);
Objects.requireNonNull(bootsTemplate);

addItemOverrides(armorSet.helmet, new AnyTrimItemOverrideProvider(helmetTemplate));
addItemOverrides(armorSet.chestplate, new AnyTrimItemOverrideProvider(chestplateTemplate));
addItemOverrides(armorSet.leggings, new AnyTrimItemOverrideProvider(leggingsTemplate));
addItemOverrides(armorSet.boots, new AnyTrimItemOverrideProvider(bootsTemplate));
}
}
}

0 comments on commit acb134a

Please sign in to comment.