public static string MetadataToCode(MetadataDictionary metadata) { StringBuilder sb = new StringBuilder(); sb.AppendLine(); sb.AppendLine("MetadataDictionary metadata = new MetadataDictionary();"); foreach (var kvp in metadata._entries) { int idx = kvp.Key; MetadataEntry entry = kvp.Value; sb.Append($"metadata[{idx}] = new "); switch (entry.Identifier) { case 0: { var e = (MetadataByte)entry; sb.Append($"{e.GetType().Name}({e.Value});"); break; } case 1: { var e = (MetadataShort)entry; sb.Append($"{e.GetType().Name}({e.Value});"); break; } case 2: { var e = (MetadataInt)entry; sb.Append($"{e.GetType().Name}({e.Value});"); break; } case 3: { var e = (MetadataFloat)entry; sb.Append($"{e.GetType().Name}({e.Value.ToString(NumberFormatInfo.InvariantInfo)}f);"); break; } case 4: { var e = (MetadataString)entry; sb.Append($"{e.GetType().Name}(\"{e.Value}\");"); break; } case 5: { var e = (MetadataSlot)entry; sb.Append($"{e.GetType().Name}({e.Value});"); break; } case 6: { var e = (MetadataIntCoordinates)entry; sb.Append($"{e.GetType().Name}({e.Value});"); break; } case 7: { var e = (MetadataLong)entry; sb.Append($"{e.GetType().Name}({e.Value});"); if (idx == 0) { sb.Append($" // {Convert.ToString(e.Value, 2)}"); } break; } case 8: { var e = (MetadataVector3)entry; sb.Append($"{e.GetType().Name}({e.Value});"); break; } } sb.AppendLine(); } return(sb.ToString()); }
private static void WriteInventoryToFile(string fileName, MetadataEntry[] slots) { Log.Info($"Writing inventory to filename: {fileName}"); FileStream file = File.OpenWrite(fileName); IndentedTextWriter writer = new IndentedTextWriter(new StreamWriter(file)); writer.WriteLine("// GENERATED CODE. DON'T EDIT BY HAND"); writer.Indent++; writer.Indent++; writer.WriteLine("public static List<ItemStack> CreativeInventoryItems = new List<ItemStack>()"); writer.WriteLine("{"); writer.Indent++; foreach (var entry in slots) { MetadataSlot slot = (MetadataSlot) entry; NbtCompound extraData = slot.Value.ExtraData; if (extraData == null) { writer.WriteLine($"new ItemStack({slot.Value.Id}, {slot.Value.Count}, {slot.Value.Metadata}),"); } else { NbtList ench = (NbtList) extraData["ench"]; NbtCompound enchComp = (NbtCompound) ench[0]; var id = enchComp["id"].ShortValue; var lvl = enchComp["lvl"].ShortValue; writer.WriteLine($"new ItemStack({slot.Value.Id}, {slot.Value.Count}, {slot.Value.Metadata}){{ExtraData = new NbtCompound {{new NbtList(\"ench\") {{new NbtCompound {{new NbtShort(\"id\", {id}), new NbtShort(\"lvl\", {lvl}) }} }} }} }},"); } } new ItemStack(0, 0, 0) {ExtraData = new NbtCompound {new NbtList("ench") {new NbtCompound {new NbtShort("id", 0), new NbtShort("lvl", 0)}}}}; //var compound = new NbtCompound(string.Empty) { new NbtList("ench", new NbtCompound()) {new NbtShort("id", 0),new NbtShort("lvl", 0),}, }; writer.Indent--; writer.WriteLine("};"); writer.Indent--; writer.Indent--; writer.Flush(); file.Close(); }