Skip to content

Commit

Permalink
Start working on revising datagen
Browse files Browse the repository at this point in the history
  • Loading branch information
dhyces committed May 29, 2024
1 parent 187d422 commit acc17ef
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.dhyces.trimmed.api.data;

import com.mojang.datafixers.util.Pair;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap;
import net.minecraft.Util;
import net.minecraft.advancements.critereon.InventoryChangeTrigger;
import net.minecraft.core.registries.BuiltInRegistries;
Expand Down Expand Up @@ -34,13 +36,13 @@ public abstract class BaseTrimDatagenSuite {
private final BiConsumer<String, String> mainTranslationConsumer;
protected final String modid;

protected List<Pair<ResourceKey<TrimPattern>, TrimPattern>> patterns = new ArrayList<>();
protected List<Pair<ResourceLocation, ShapedRecipeBuilder>> copyRecipes = new ArrayList<>();
protected List<Pair<ResourceLocation, SmithingTrimRecipeBuilder>> trimRecipes = new ArrayList<>();
protected List<Pair<ResourceKey<TrimMaterial>, TrimMaterial>> materials = new ArrayList<>();
protected Map<ResourceKey<TrimPattern>, TrimPattern> patterns = new Reference2ObjectArrayMap<>();
protected Map<ResourceLocation, ShapedRecipeBuilder> copyRecipes = new Object2ObjectArrayMap<>();
protected Map<ResourceLocation, SmithingTrimRecipeBuilder> trimRecipes = new Object2ObjectArrayMap<>();
protected Map<ResourceKey<TrimMaterial>, TrimMaterial> materials = new Reference2ObjectArrayMap<>();

protected List<ResourceLocation> patternTextures = new ArrayList<>();
protected Map<ResourceLocation, String> materialTexturePermutations = new LinkedHashMap<>();
protected List<ResourceLocation> patternTextures = new ObjectArrayList<>();
protected Map<ResourceLocation, String> materialTexturePermutations = new Object2ObjectArrayMap<>();

public BaseTrimDatagenSuite(String modid, @Nullable BiConsumer<String, String> translationConsumer) {
this.modid = modid;
Expand All @@ -56,7 +58,7 @@ public void generate() {
}

/**
* Creates a new pattern, as well as add to the "trim_templates" tag, add an entry to the custom patterns
* Creates a new pattern with decal set to false, as well as add to the "trim_templates" tag, add an entry to the custom patterns
* client-tag, create a smithing trim recipe, and optionally add a lang entry (if a lang provider is provided)
* and a "copy" recipe (used for duplicating the template).
* @param patternKey The pattern key
Expand All @@ -74,8 +76,8 @@ public BaseTrimDatagenSuite makePattern(ResourceKey<TrimPattern> patternKey, Sup
* @param templateItem The item that is used to obtain this pattern on armor
* @param patternConfigConsumer An optional element allowing modification of certain elements
*/
public BaseTrimDatagenSuite makePattern(ResourceKey<TrimPattern> patternKey, Supplier<? extends ItemLike> templateItem, Consumer<PatternConfig> patternConfigConsumer) {
return makePattern(patternKey, templateItem.get(), patternConfigConsumer);
public BaseTrimDatagenSuite makePattern(ResourceKey<TrimPattern> patternKey, Supplier<? extends ItemLike> templateItem, boolean isDecal, Consumer<PatternConfig> patternConfigConsumer) {
return makePattern(patternKey, templateItem.get(), isDecal, patternConfigConsumer);
}

/**
Expand All @@ -86,21 +88,22 @@ public BaseTrimDatagenSuite makePattern(ResourceKey<TrimPattern> patternKey, Sup
* @param templateItem The item that is used to obtain this pattern on armor
*/
public BaseTrimDatagenSuite makePattern(ResourceKey<TrimPattern> patternKey, ItemLike templateItem) {
return makePattern(patternKey, templateItem, patternConfig -> {});
return makePattern(patternKey, templateItem, false, patternConfig -> {});
}

/**
* Creates a new pattern, as well as add to the "trim_templates" tag, add an entry to the custom patterns
* client-tag, create a smithing trim recipe, and optionally add a lang entry (if a lang provider is provided)
* and a "copy" recipe (used for duplicating the template).
* @param patternKey The pattern key
* @param templateItem The item that is used to obtain this pattern on armor
* @param patternConfigConsumer An optional element allowing modification of certain elements
* @param patternKey The pattern key.
* @param templateItem The item that is used to obtain this pattern on armor.
* @param isDecal True will make the pattern only render where the armor texture is. False will allow the trim to always render.
* @param patternConfigConsumer An optional element allowing modification of certain elements.
*/
public BaseTrimDatagenSuite makePattern(ResourceKey<TrimPattern> patternKey, ItemLike templateItem, Consumer<PatternConfig> patternConfigConsumer) {
public BaseTrimDatagenSuite makePattern(ResourceKey<TrimPattern> patternKey, ItemLike templateItem, boolean isDecal, Consumer<PatternConfig> patternConfigConsumer) {

String translationKey = Util.makeDescriptionId("trim_pattern", patternKey.location());
patterns.add(Pair.of(patternKey, new TrimPattern(patternKey.location(), templateItem.asItem().builtInRegistryHolder(), Component.translatable(translationKey), false)));
patterns.put(patternKey, new TrimPattern(patternKey.location(), templateItem.asItem().builtInRegistryHolder(), Component.translatable(translationKey), isDecal));

PatternConfig config = new PatternConfig(templateItem);
patternConfigConsumer.accept(config);
Expand Down Expand Up @@ -131,11 +134,11 @@ public BaseTrimDatagenSuite makePattern(ResourceKey<TrimPattern> patternKey, Ite
ResourceLocation id = BuiltInRegistries.ITEM.getKey(templateItem.asItem());

if (!config.omitTrimRecipe) {
trimRecipes.add(Pair.of(id.withSuffix("_smithing_trim"), makeTrimRecipe(templateItem)));
trimRecipes.put(id.withSuffix("_smithing_trim"), makeTrimRecipe(templateItem));
}

if (config.copyRecipe != null) {
copyRecipes.add(Pair.of(id, config.copyRecipe));
copyRecipes.put(id, config.copyRecipe);
}

return this;
Expand Down Expand Up @@ -199,7 +202,7 @@ public BaseTrimDatagenSuite makeMaterial(ResourceKey<TrimMaterial> materialKey,
materialConfigConsumer.accept(config);

String translationKey = Util.makeDescriptionId("trim_pattern", materialKey.location());
materials.add(Pair.of(materialKey, new TrimMaterial(config.assetName.toString(), materialItem.asItem().builtInRegistryHolder(), -1.0f, Map.of(), Component.translatable(translationKey).withStyle(config.materialStyle))));
materials.put(materialKey, new TrimMaterial(config.assetName, materialItem.asItem().builtInRegistryHolder(), -1.0f, Map.of(), Component.translatable(translationKey).withStyle(config.materialStyle)));

if (mainTranslationConsumer != null) {
String translation;
Expand Down Expand Up @@ -353,13 +356,13 @@ public MaterialConfig style(UnaryOperator<Style> styleOperator) {
* @param name The new asset name
* @return This instance for chaining method calls
*/
public MaterialConfig assetName(ResourceLocation name) {
assetName = name.toString().replace(":", "_");
public MaterialConfig assetName(String name) {
assetName = name;
return this;
}
}

record AltTranslation(BiConsumer<String, String> consumer, String translation) {
protected record AltTranslation(BiConsumer<String, String> consumer, String translation) {
public void finish(String key) {
consumer.accept(key, translation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public TrimDatagenSuite(FabricDataGenerator.Pack pack, String modid, @Nullable S
new FabricDynamicRegistryProvider(output, registriesFuture) {
@Override
protected void configure(HolderLookup.Provider registries, Entries entries) {
patterns.forEach(pair -> entries.add(pair.getFirst(), pair.getSecond()));
materials.forEach(pair -> entries.add(pair.getFirst(), pair.getSecond()));
patterns.forEach(entries::add);
materials.forEach(entries::add);
}

@Override
Expand All @@ -51,8 +51,8 @@ public String getName() {
pack.addProvider((FabricDataOutput output, CompletableFuture<HolderLookup.Provider> registriesFuture) -> new FabricRecipeProvider(output, registriesFuture) {
@Override
public void buildRecipes(RecipeOutput output) {
trimRecipes.forEach(pair -> pair.getSecond().save(output, pair.getFirst()));
copyRecipes.forEach(pair -> pair.getSecond().save(output));
trimRecipes.forEach((id, smithingTrimRecipeBuilder) -> smithingTrimRecipeBuilder.save(output, id));
copyRecipes.forEach((id, smithingTrimRecipeBuilder) -> smithingTrimRecipeBuilder.save(output));
}

@Override
Expand All @@ -66,10 +66,10 @@ public String getName() {
@Override
protected void addTags(HolderLookup.Provider provider) {
if (!patterns.isEmpty()) {
tag(ItemTags.TRIM_TEMPLATES).add(patterns.stream().map(pair -> pair.getSecond().templateItem().value()).toArray(Item[]::new));
tag(ItemTags.TRIM_TEMPLATES).add(patterns.values().stream().map(trimPattern -> trimPattern.templateItem().value()).toArray(Item[]::new));
}
if (!materials.isEmpty()) {
tag(ItemTags.TRIM_MATERIALS).add(materials.stream().map(pair -> pair.getSecond().ingredient().value()).toArray(Item[]::new));
tag(ItemTags.TRIM_MATERIALS).add(materials.values().stream().map(trimMaterial -> trimMaterial.ingredient().value()).toArray(Item[]::new));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public TrimDatagenSuite(GatherDataEvent event, String modid, @Nullable BiConsume
PackOutput packOutput = event.getGenerator().getPackOutput();
RegistrySetBuilder builder = new RegistrySetBuilder()
.add(Registries.TRIM_PATTERN, pContext -> {
patterns.forEach(pair -> pContext.register(pair.getFirst(), pair.getSecond()));
patterns.forEach(pContext::register);
})
.add(Registries.TRIM_MATERIAL, pContext -> {
materials.forEach(pair -> pContext.register(pair.getFirst(), pair.getSecond()));
materials.forEach(pContext::register);
});
generator.addProvider(event.includeServer(), (DataProvider.Factory<? extends DataProvider>) pOutput -> new DatapackBuiltinEntriesProvider(packOutput, event.getLookupProvider(), builder, Set.of(modid)) {
@Override
Expand All @@ -51,8 +51,8 @@ public String getName() {
generator.addProvider(event.includeServer(), new RecipeProvider(packOutput, event.getLookupProvider()) {
@Override
protected void buildRecipes(RecipeOutput output) {
trimRecipes.forEach(pair -> pair.getSecond().save(output, pair.getFirst()));
copyRecipes.forEach(pair -> pair.getSecond().save(output));
trimRecipes.forEach((id, smithingTrimRecipeBuilder) -> smithingTrimRecipeBuilder.save(output, id));
copyRecipes.forEach((id, smithingTrimRecipeBuilder) -> smithingTrimRecipeBuilder.save(output));
}

public String getName() {
Expand All @@ -63,10 +63,10 @@ public String getName() {
@Override
protected void addTags(HolderLookup.Provider pProvider) {
if (!patterns.isEmpty()) {
tag(ItemTags.TRIM_TEMPLATES).add(patterns.stream().map(pair -> pair.getSecond().templateItem().value()).toArray(Item[]::new));
tag(ItemTags.TRIM_TEMPLATES).add(patterns.values().stream().map(trimPattern -> trimPattern.templateItem().value()).toArray(Item[]::new));
}
if (!materials.isEmpty()) {
tag(ItemTags.TRIM_MATERIALS).add(materials.stream().map(pair -> pair.getSecond().ingredient().value()).toArray(Item[]::new));
tag(ItemTags.TRIM_MATERIALS).add(materials.values().stream().map(trimMaterial -> trimMaterial.ingredient().value()).toArray(Item[]::new));
}
}

Expand Down

0 comments on commit acc17ef

Please sign in to comment.