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 { Log.Debug($"Nbt: " + extraData.ToString()); 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(); }
public Entity(NbtCompound c) { orig = c; id = (orig["id"] as NbtString).Value; SetBaseStuff(c); #if DEBUG Console.WriteLine("*** BUG: Unknown entity (ID: {0})",id); Console.WriteLine(orig); #endif File.WriteAllText("UnknownEntity." + id + ".txt", orig.ToString().Replace("\n","\r\n")); }
// This tosses together a cheapass entity class for faster updates. // Primarily because I'm a lazy f**k. private static void GenTemplate(NbtCompound c, string tpl) { string ID = c.Get <NbtString>("id").Value; string Name = ID.Substring(0, 1).ToUpper() + ID.Substring(1); string newthing = File.ReadAllText("TileEntities/" + tpl); // Construct variables string vardec = ""; string dectmpl = "\n\t\t[Category(\"{0}\"), Description(\"(WIP)\")]\n\t\tpublic {1} {2} {{get;set;}}\n"; string varassn = ""; string assntpl = "\n\t\t\t{0} = c.Get<{1}>(\"{2}\").Value;"; string nbtassn = ""; string nbttpl = "\n\t\t\tc.Add(new {0}(\"{1}\", {2}));"; // Figure out if there are any new fields that we should be concerned about... foreach (NbtTag t in c) { if (CommonTileEntityVars.Contains(t.Name)) { continue; } string vname = t.Name.Substring(0, 1).ToUpper() + t.Name.Substring(1); string tagname = t.Name; string type = GetNativeType(t); string nbtTag = t.GetType().Name; vardec += string.Format(dectmpl, Name, type, vname); varassn += string.Format(assntpl, vname, nbtTag, tagname); nbtassn += string.Format(nbttpl, nbtTag, tagname, vname); } // {DATA_DUMP} - Crap out a dump of the entity. // {ENTITY_NAME} - Entity name, but capitalized (CamelCase) // {NEW_VARS} - New var declarations. // {VAR_ASSIGNMENT} - Set the vars from the Compound. // {TO_NBT} - Set the appropriate stuff in the Compound. // {ENTITY_ID} - Raw entity ID newthing = newthing.Replace("{DATA_DUMP}", c.ToString()); newthing = newthing.Replace("{TILEENTITY_NAME}", Name); newthing = newthing.Replace("{TILEENTITY_ID}", ID); newthing = newthing.Replace("{VAR_DECL}", vardec); newthing = newthing.Replace("{VAR_ASSIGNMENT}", varassn); newthing = newthing.Replace("{TO_NBT}", nbtassn); File.WriteAllText("TileEntities/" + Name + ".cs", newthing); // TODO: Compile? }
public static Entity GetEntity(NbtCompound c) { if (!c.Has("id")) { Console.WriteLine(c.ToString()); } if (c.Has("x") && c.Has("y") && c.Has("z")) { throw new Exception("TileEntity is in Entities compound!?"); } string entID = c.Get <NbtString>("id").Value; if (entID == "NULL") { return(null); } if (EntityTypes.ContainsKey(entID)) { try { return((Entity)EntityTypes[entID].GetConstructor(new Type[] { typeof(NbtCompound) }).Invoke(new Object[] { c })); } catch (TargetInvocationException e) { Console.WriteLine("Failed to load " + entID + ": \n" + e.InnerException.ToString()); throw e.InnerException; } } // Try to figure out what the hell this is. if (!Directory.Exists("Entities")) { Directory.CreateDirectory("Entities"); } // Do we have a LivingEntity or just an Entity? // Quick and simple test: health. if (c.Has("Health") && entID != "Item") { GenTemplate(c, "livingentity.template"); // Goodie, just whip up a new LivingEntity and we're relatively home free. return(new LivingEntity(c)); } else { GenTemplate(c, "entity.template"); return(new Entity(c)); } }
public override string ToString() { if (Empty) { return("(Empty)"); } string result = "ID: " + Id; if (Count != 1) { result += "; Count: " + Count; } if (Metadata != 0) { result += "; Metadata: " + Metadata; } if (Nbt != null) { result += Environment.NewLine + Nbt.ToString(); } return("(" + result + ")"); }
/// <summary> /// Returns the string representation of this item stack. /// </summary> /// <returns></returns> public override string ToString() { if (Empty) { return("(Empty)"); } StringBuilder resultBuilder = new StringBuilder("ID: " + ID); if (Count != 1) { resultBuilder.Append("; Count: " + Count); } if (Metadata != 0) { resultBuilder.Append("; Metadata: " + Metadata); } if (Nbt != null) { resultBuilder.Append(Environment.NewLine + Nbt.ToString()); } return("(" + resultBuilder.ToString() + ")"); }
// This tosses together a cheapass entity class for faster updates. // Primarily because I'm a lazy f**k. private static void GenTemplate(NbtCompound c, string tpl) { string ID = c.Get<NbtString>("id").Value; string Name = ID.Substring(0, 1).ToUpper() + ID.Substring(1); string newthing = File.ReadAllText("TileEntities/"+tpl); // Construct variables string vardec = ""; string dectmpl = "\n\t\t[Category(\"{0}\"), Description(\"(WIP)\")]\n\t\tpublic {1} {2} {{get;set;}}\n"; string varassn = ""; string assntpl = "\n\t\t\t{0} = c.Get<{1}>(\"{2}\").Value;"; string nbtassn = ""; string nbttpl = "\n\t\t\tc.Add(new {0}(\"{1}\", {2}));"; // Figure out if there are any new fields that we should be concerned about... foreach (NbtTag t in c) { if (CommonTileEntityVars.Contains(t.Name)) continue; string vname = t.Name.Substring(0, 1).ToUpper() + t.Name.Substring(1); string tagname = t.Name; string type = GetNativeType(t); string nbtTag = t.GetType().Name; vardec += string.Format(dectmpl, Name, type, vname); varassn += string.Format(assntpl, vname, nbtTag, tagname); nbtassn += string.Format(nbttpl, nbtTag, tagname, vname); } // {DATA_DUMP} - Crap out a dump of the entity. // {ENTITY_NAME} - Entity name, but capitalized (CamelCase) // {NEW_VARS} - New var declarations. // {VAR_ASSIGNMENT} - Set the vars from the Compound. // {TO_NBT} - Set the appropriate stuff in the Compound. // {ENTITY_ID} - Raw entity ID newthing = newthing.Replace("{DATA_DUMP}", c.ToString()); newthing = newthing.Replace("{TILEENTITY_NAME}", Name); newthing = newthing.Replace("{TILEENTITY_ID}", ID); newthing = newthing.Replace("{VAR_DECL}", vardec); newthing = newthing.Replace("{VAR_ASSIGNMENT}", varassn); newthing = newthing.Replace("{TO_NBT}", nbtassn); File.WriteAllText("TileEntities/" + Name + ".cs", newthing); // TODO: Compile? }
public static Entity GetEntity(NbtCompound c) { if (!c.Has("id")) { Console.WriteLine(c.ToString()); } if (c.Has("x") && c.Has("y") && c.Has("z")) throw new Exception("TileEntity is in Entities compound!?"); string entID = c.Get<NbtString>("id").Value; if (entID == "NULL") return null; if (EntityTypes.ContainsKey(entID)) { try { return (Entity)EntityTypes[entID].GetConstructor(new Type[] { typeof(NbtCompound) }).Invoke(new Object[] { c }); } catch (TargetInvocationException e) { Console.WriteLine("Failed to load " + entID + ": \n" + e.InnerException.ToString()); throw e.InnerException; } } // Try to figure out what the hell this is. if (!Directory.Exists("Entities")) Directory.CreateDirectory("Entities"); // Do we have a LivingEntity or just an Entity? // Quick and simple test: health. if (c.Has("Health") && entID!="Item") { GenTemplate(c, "livingentity.template"); // Goodie, just whip up a new LivingEntity and we're relatively home free. return new LivingEntity(c); } else { GenTemplate(c, "entity.template"); return new Entity(c); } }
/// <summary> /// Load a TileEntity from an NbtCompound. /// </summary> /// <param name="CX">Chunk X Coordinate.</param> /// <param name="CY">Chunk Y Coordinate.</param> /// <param name="CS">Chunk horizontal scale (16 in /game/)</param> /// <param name="c"></param> /// <returns>TileEntity.</returns> public static TileEntity GetEntity(int CX, int CY, int CS, NbtCompound c) { TileEntity e; switch ((c["id"] as NbtString).Value) { case "Chest": e = new Chest(CX,CY,CS,c); break; case "MobSpawner": e = new MobSpawner(CX, CY, CS, c); break; case "Furnace": e = new Furnace(CX, CY, CS, c); break; case "Sign": e = new Sign(CX, CY, CS, c); break; case "NULL": // Ignore it :| return new TileEntity(CX, CY, CS, c); default: #if DEBUG Console.WriteLine("*** Unknown TileEntity: {0}", (c["id"] as NbtString).Value); Console.WriteLine(c); #endif File.WriteAllText(string.Format("UnknownTileEntity.{0}.txt", (c["id"] as NbtString).Value),c.ToString().Replace("\n","\r\n")); return new TileEntity(CX, CY, CS, c); } #if DEBUG Console.WriteLine("Loaded {1} @ {0}", e,e.Pos); #endif return e; }