Skip to content

Commit

Permalink
Write Armor trim codecs to registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Nov 14, 2023
1 parent b09be32 commit 92be879
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 1 deletion.
37 changes: 37 additions & 0 deletions Obsidian/Net/MinecraftStream.Writing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -721,11 +721,48 @@ public void WriteMixedCodec(MixedCodec _)
this.WriteBiomeCodec(writer);
this.WriteChatCodec(writer);
this.WriteDamageTypeCodec(writer);
this.WriteTrimPatternCodec(writer);
this.WriteTrimMaterialCodec(writer);

writer.EndCompound();
writer.TryFinish();
}

private void WriteTrimPatternCodec(NbtWriter writer)
{
var trimPatterns = new NbtList(NbtTagType.Compound, "value");

foreach (var (_, trimPattern) in CodecRegistry.TrimPatterns.All)
trimPattern.Write(trimPatterns);

var trimPatternsCompound = new NbtCompound(CodecRegistry.TrimPatterns.CodecKey)
{
new NbtTag<string>("type", CodecRegistry.TrimPatterns.CodecKey),

trimPatterns
};

writer.WriteTag(trimPatternsCompound);
}

private void WriteTrimMaterialCodec(NbtWriter writer)
{
var trimMaterials = new NbtList(NbtTagType.Compound, "value");

foreach (var (_, trimMaterial) in CodecRegistry.TrimMaterials.All)
trimMaterial.Write(trimMaterials);

var trimMaterialsCompound = new NbtCompound(CodecRegistry.TrimMaterials.CodecKey)
{
new NbtTag<string>("type", CodecRegistry.TrimMaterials.CodecKey),

trimMaterials
};

writer.WriteTag(trimMaterialsCompound);
}


private void WriteDamageTypeCodec(NbtWriter writer)
{
var damageTypes = new NbtList(NbtTagType.Compound, "value");
Expand Down
87 changes: 86 additions & 1 deletion Obsidian/Utilities/Extensions.Nbt.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Obsidian.API.Registry.Codecs.Biomes;
using Obsidian.API.Registry.Codecs.ArmorTrims.TrimMaterial;
using Obsidian.API.Registry.Codecs.ArmorTrims.TrimPattern;
using Obsidian.API.Registry.Codecs.Biomes;
using Obsidian.API.Registry.Codecs.Chat;
using Obsidian.API.Registry.Codecs.DamageTypes;
using Obsidian.API.Registry.Codecs.Dimensions;
Expand Down Expand Up @@ -506,4 +508,87 @@ public static void WriteParticle(this BiomeParticle value, NbtCompound compound)
compound.Add(particle);
}
#endregion

#region Trim Pattern Writing
public static void Write(this TrimPatternCodec value, NbtList list)
{
var compound = new NbtCompound
{
new NbtTag<int>("id", value.Id),

new NbtTag<string>("name", value.Name),

value.WriteElement()
};

list.Add(compound);
}

public static NbtCompound WriteElement(this TrimPatternCodec value)
{
var patternElement = value.Element;

var description = new NbtList(NbtTagType.String, "description")
{
new NbtTag<string>("translate", patternElement.Description.Translate)
};

var element = new NbtCompound("element")
{
new NbtTag<string>("template_item", patternElement.TemplateItem),
description,
new NbtTag<string>("asset_id", patternElement.AssetId),
new NbtTag<bool>("decal", patternElement.Decal)
};

return element;
}
#endregion

#region Trim Material Writing
public static void Write(this TrimMaterialCodec value, NbtList list)
{
var compound = new NbtCompound
{
new NbtTag<int>("id", value.Id),

new NbtTag<string>("name", value.Name),

value.WriteElement()
};

list.Add(compound);
}

public static NbtCompound WriteElement(this TrimMaterialCodec value)
{
var materialElement = value.Element;

var description = new NbtList(NbtTagType.String, "description")
{
new NbtTag<string>("translate", materialElement.Description.Translate),
new NbtTag<string>("color", materialElement.Description.Color!)
};

var element = new NbtCompound("element")
{
new NbtTag<string>("ingredient", materialElement.Ingredient),
description,
new NbtTag<string>("asset_name", materialElement.AssetName),
new NbtTag<double>("item_model_index", materialElement.ItemModelIndex)
};

if (materialElement.OverrideArmorMaterials is Dictionary<string, string> overrideArmorMats)
{
var overrideArmorMaterialsCompound = new NbtCompound("override_armor_materials");

foreach (var (type, replacement) in overrideArmorMats)
overrideArmorMaterialsCompound.Add(new NbtTag<string>(type, replacement));

element.Add(overrideArmorMaterialsCompound);
}

return element;
}
#endregion
}

0 comments on commit 92be879

Please sign in to comment.