public void Write(Recipes recipes) { Write(recipes.Count); foreach (Recipe recipe in recipes) { if (recipe is ShapelessRecipe) { var memoryStream = new MemoryStream(); McpeWriter writer = new McpeWriter(memoryStream); ShapelessRecipe rec = (ShapelessRecipe)recipe; writer.Write(rec.Input.Count); foreach (ItemStack stack in rec.Input) { writer.Write((MetadataSlot)stack); } writer.Write(1); writer.Write(rec.Result); writer.Write(new UUID(Guid.NewGuid())); Write(0); // Type var bytes = memoryStream.ToArray(); Write(bytes.Length); Write(bytes); } else if (recipe is ShapedRecipe) { var memoryStream = new MemoryStream(); McpeWriter writer = new McpeWriter(memoryStream); ShapedRecipe rec = (ShapedRecipe)recipe; writer.Write(rec.Width); writer.Write(rec.Height); for (int w = 0; w < rec.Width; w++) { for (int h = 0; h < rec.Height; h++) { writer.Write(new ItemStack(rec.Input[(h * rec.Width) + w], 1)); } } writer.Write(1); writer.Write(rec.Result); writer.Write(new UUID(Guid.NewGuid())); Write(1); // Type var bytes = memoryStream.ToArray(); Write(bytes.Length); Write(bytes); } } }
public Recipes ReadRecipes() { Recipes recipes = new Recipes(); int count = (int)ReadUnsignedVarInt(); Log.Error($"Reading {count} recipes"); for (int i = 0; i < count; i++) { int recipeType = (int)ReadSignedVarInt(); //Log.Error($"Read recipe no={i} type={recipeType}"); if (recipeType < 0 /*|| len == 0*/) { Log.Error("Read void recipe"); break; } if (recipeType == 0) { //const ENTRY_SHAPELESS = 0; ShapelessRecipe recipe = new ShapelessRecipe(); int ingrediensCount = ReadVarInt(); // for (int j = 0; j < ingrediensCount; j++) { recipe.Input.Add(ReadItem()); } ReadVarInt(); // 1? recipe.Result = ReadItem(); recipe.Id = ReadUUID(); // Id recipes.Add(recipe); //Log.Error("Read shapeless recipe"); } else if (recipeType == 1) { //const ENTRY_SHAPED = 1; int width = ReadSignedVarInt(); // Width int height = ReadSignedVarInt(); // Height ShapedRecipe recipe = new ShapedRecipe(width, height); if (width > 3 || height > 3) { throw new Exception("Wrong number of ingredience. Width=" + width + ", height=" + height); } for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { recipe.Input[(h * width) + w] = ReadItem(); } } int resultCount = ReadVarInt(); // 1? for (int j = 0; j < resultCount; j++) { recipe.Result = ReadItem(); } recipe.Id = ReadUUID(); // Id recipes.Add(recipe); //Log.Error("Read shaped recipe"); } else if (recipeType == 2) { //const ENTRY_FURNACE = 2; SmeltingRecipe recipe = new SmeltingRecipe(); //short meta = (short) ReadVarInt(); // input (with metadata) short id = (short)ReadSignedVarInt(); // input (with metadata) Item result = ReadItem(); // Result recipe.Input = ItemFactory.GetItem(id, 0); recipe.Result = result; recipes.Add(recipe); //Log.Error("Read furnace recipe"); //Log.Error($"Input={id}, meta={""} Item={result.Id}, Meta={result.Metadata}"); } else if (recipeType == 3) { //const ENTRY_FURNACE_DATA = 3; SmeltingRecipe recipe = new SmeltingRecipe(); short id = (short)ReadSignedVarInt(); // input (with metadata) short meta = (short)ReadSignedVarInt(); // input (with metadata) Item result = ReadItem(); // Result recipe.Input = ItemFactory.GetItem(id, meta); recipe.Result = result; recipes.Add(recipe); //Log.Error("Read smelting recipe"); //Log.Error($"Input={id}, meta={meta} Item={result.Id}, Meta={result.Metadata}"); } else if (recipeType == 4) { //const ENTRY_ENCHANT_LIST = 4; Log.Error("Reading ENCHANT_LIST"); ReadUUID(); } else { Log.Error($"Read unknown recipe type: {recipeType}"); //ReadBytes(len); } } ReadByte(); // Clean (1) or update (0) return(recipes); }
public void Write(Recipes recipes) { WriteUnsignedVarInt((uint)recipes.Count); foreach (Recipe recipe in recipes) { if (recipe is ShapelessRecipe) { WriteSignedVarInt(0); // Type ShapelessRecipe rec = (ShapelessRecipe)recipe; WriteVarInt(rec.Input.Count); foreach (Item stack in rec.Input) { Write(stack); } WriteVarInt(1); Write(rec.Result); Write(new UUID(Guid.NewGuid())); } else if (recipe is ShapedRecipe) { WriteSignedVarInt(1); // Type ShapedRecipe rec = (ShapedRecipe)recipe; WriteSignedVarInt(rec.Width); WriteSignedVarInt(rec.Height); for (int w = 0; w < rec.Width; w++) { for (int h = 0; h < rec.Height; h++) { Write(rec.Input[(h * rec.Width) + w]); } } WriteVarInt(1); Write(rec.Result); Write(new UUID(Guid.NewGuid())); } else if (recipe is SmeltingRecipe) { SmeltingRecipe rec = (SmeltingRecipe)recipe; WriteSignedVarInt(rec.Input.Metadata == 0 ? 2 : 3); // Type WriteSignedVarInt(rec.Input.Id); if (rec.Input.Metadata != 0) { WriteSignedVarInt(rec.Input.Metadata); } Write(rec.Result); } else if (recipe is EnchantingRecipe) { //var memoryStream = MiNetServer.MemoryStreamManager.GetStream(); //McpeWriter writer = new McpeWriter(memoryStream); //writer.Write((byte) 3); // Count //{ // writer.Write((int) 1); // Cost // writer.Write((byte) 1); // Count // writer.Write((int) 9); // Id // writer.Write((int) 1); // Level // writer.Write("Test1"); // Level //} //Write(4); // Type //var bytes = memoryStream.ToArray(); //Write(bytes.Length); //Write(bytes); } } Write((byte)1); }
public void Write(Recipes recipes) { Write(recipes.Count); foreach (Recipe recipe in recipes) { if (recipe is ShapelessRecipe) { var memoryStream = MiNetServer.MemoryStreamManager.GetStream(); McpeWriter writer = new McpeWriter(memoryStream); ShapelessRecipe rec = (ShapelessRecipe)recipe; writer.Write(rec.Input.Count); foreach (Item stack in rec.Input) { writer.Write(stack); } writer.Write(1); writer.Write(rec.Result); writer.Write(new UUID(Guid.NewGuid())); Write(0); // Type var bytes = memoryStream.ToArray(); Write(bytes.Length); Write(bytes); } else if (recipe is ShapedRecipe) { var memoryStream = MiNetServer.MemoryStreamManager.GetStream(); McpeWriter writer = new McpeWriter(memoryStream); ShapedRecipe rec = (ShapedRecipe)recipe; writer.Write(rec.Width); writer.Write(rec.Height); for (int w = 0; w < rec.Width; w++) { for (int h = 0; h < rec.Height; h++) { writer.Write(rec.Input[(h * rec.Width) + w]); } } writer.Write(1); writer.Write(rec.Result); writer.Write(new UUID(Guid.NewGuid())); Write(1); // Type var bytes = memoryStream.ToArray(); Write(bytes.Length); Write(bytes); } else if (recipe is SmeltingRecipe) { ////const ENTRY_FURNACE = 2; //SmeltingRecipe recipe = new SmeltingRecipe(); //short meta = ReadShort(); // input (with metadata) //short id = ReadShort(); // input (with metadata) //Item result = ReadItem(); // Result //recipe.Input = ItemFactory.GetItem(id, meta); //recipe.Result = result; //recipes.Add(recipe); var memoryStream = MiNetServer.MemoryStreamManager.GetStream(); McpeWriter writer = new McpeWriter(memoryStream); SmeltingRecipe rec = (SmeltingRecipe)recipe; writer.Write(rec.Input.Metadata); writer.Write(rec.Input.Id); writer.Write(rec.Result); Write(rec.Input.Metadata == 0 ? 2 : 3); // Type var bytes = memoryStream.ToArray(); Write(bytes.Length); Write(bytes); } else if (recipe is EnchantingRecipe) { var memoryStream = MiNetServer.MemoryStreamManager.GetStream(); McpeWriter writer = new McpeWriter(memoryStream); writer.Write((byte)3); // Count { writer.Write((int)1); // Cost writer.Write((byte)1); // Count writer.Write((int)9); // Id writer.Write((int)1); // Level writer.Write("Test1"); // Level } { writer.Write((int)2); // Cost writer.Write((byte)1); // Count writer.Write((int)10); // Id writer.Write((int)2); // Level writer.Write("Test2"); // Level } { writer.Write((int)3); // Cost writer.Write((byte)1); // Count writer.Write((int)12); // Id writer.Write((int)3); // Level writer.Write("Test3"); // Level } Write(4); // Type var bytes = memoryStream.ToArray(); Write(bytes.Length); Write(bytes); } } Write((byte)1); }
public Recipes ReadRecipes() { Recipes recipes = new Recipes(); int count = ReadInt(); for (int i = 0; i < count; i++) { int recipeType = ReadInt(); int len = ReadInt(); if (recipeType < 0 || len == 0) { Log.Error("Read void recipe"); continue; } if (recipeType == 0) { //const ENTRY_SHAPELESS = 0; ShapelessRecipe recipe = new ShapelessRecipe(); int ingrediensCount = ReadInt(); // for (int j = 0; j < ingrediensCount; j++) { recipe.Input.Add(ReadItem()); } ReadInt(); // 1? recipe.Result = ReadItem(); recipe.Id = ReadUUID(); // Id recipes.Add(recipe); } else if (recipeType == 1) { //const ENTRY_SHAPED = 1; int width = ReadInt(); // Width int height = ReadInt(); // Height ShapedRecipe recipe = new ShapedRecipe(width, height); if (width > 3 || height > 3) { throw new Exception("Wrong number of ingredience. Width=" + width + ", height=" + height); } for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { recipe.Input[(h * width) + w] = ReadItem(); } } int resultCount = ReadInt(); // 1? for (int j = 0; j < resultCount; j++) { recipe.Result = ReadItem(); } recipe.Id = ReadUUID(); // Id recipes.Add(recipe); } else if (recipeType == 2) { //const ENTRY_FURNACE = 2; SmeltingRecipe recipe = new SmeltingRecipe(); short meta = ReadShort(); // input (with metadata) short id = ReadShort(); // input (with metadata) Item result = ReadItem(); // Result recipe.Input = ItemFactory.GetItem(id, meta); recipe.Result = result; recipes.Add(recipe); } else if (recipeType == 3) { //const ENTRY_FURNACE_DATA = 3; SmeltingRecipe recipe = new SmeltingRecipe(); short id = ReadShort(); // input (with metadata) short meta = ReadShort(); // input (with metadata) Item result = ReadItem(); // Result recipe.Input = ItemFactory.GetItem(id, meta); recipe.Result = result; recipes.Add(recipe); } else if (recipeType == 4) { Log.Error("Reading ENCHANT_LIST"); //const ENTRY_ENCHANT_LIST = 4; int enchantmentListCount = ReadByte(); // count for (int j = 0; j < enchantmentListCount; j++) { ReadInt(); // Cost byte enchantmentCount = ReadByte(); // EnchantCount for (int k = 0; k < enchantmentCount; k++) { ReadInt(); // Id ReadInt(); // Level(strenght) } string name = ReadString(); // Name Log.Error("Enchant: " + name); } } else { Log.Error($"Read unknown recipe type: {recipeType}, lenght: {len}"); ReadBytes(len); } } ReadByte(); // Clean (1) or update (0) return(recipes); }
public Recipes ReadRecipes() { Recipes recipes = new Recipes(); int count = ReadInt(); Log.InfoFormat("Recipes Count: {0}", count); for (int i = 0; i < count; i++) { int recipeType = ReadInt(); int len = ReadInt(); if (recipeType < 0 || len == 0) { Log.Warn("Read void recipe"); return(recipes); } if (recipeType == 0) { //const ENTRY_SHAPELESS = 0; ShapelessRecipe recipe = new ShapelessRecipe(); int ingrediensCount = ReadInt(); // for (int j = 0; j < ingrediensCount; j++) { recipe.Input.Add(ReadMetadataSlot().Value); } ReadInt(); // 1? recipe.Result = ReadMetadataSlot().Value; recipe.Id = ReadUUID(); // Id recipes.Add(recipe); } else if (recipeType == 1) { //const ENTRY_SHAPED = 1; int width = ReadInt(); // Width int height = ReadInt(); // Height ShapedRecipe recipe = new ShapedRecipe(width, height); //if(width > 3 || height > 3) throw new Exception("Wrong number of ingredience. Width=" + width + ", height=" + height); for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { recipe.Input[(h * width) + w] = ReadMetadataSlot().Value.Item; } } ReadInt(); // 1? recipe.Result = ReadMetadataSlot().Value; recipe.Id = ReadUUID(); // Id recipes.Add(recipe); } else if (recipeType == 2) { //const ENTRY_FURNACE = 2; ReadInt(); // type ReadInt(); // input (with metadata) ReadMetadataSlot(); // Result } else if (recipeType == 3) { //const ENTRY_FURNACE_DATA = 3; ReadInt(); // input ReadMetadataSlot(); // Result } else if (recipeType == 4) { //const ENTRY_ENCHANT_LIST = 4; int enchantmentListCount = ReadByte(); // count for (int j = 0; j < enchantmentListCount; j++) { ReadMetadataSlot(); ReadInt(); // Cost byte enchantmentCount = ReadByte(); // EnchantCount for (int k = 0; k < enchantmentCount; k++) { ReadInt(); // Id ReadInt(); // Level(strenght) } ReadString(); // Name } } } return(recipes); }
private static void OnMcpeCraftingData(Package message) { string fileName = Path.GetTempPath() + "Recipes_" + Guid.NewGuid() + ".txt"; Log.Info("Writing recipes to filename: " + fileName); FileStream file = File.OpenWrite(fileName); McpeCraftingData msg = (McpeCraftingData)message; IndentedTextWriter writer = new IndentedTextWriter(new StreamWriter(file)); writer.WriteLine("static RecipeManager()"); writer.WriteLine("{"); writer.Indent++; writer.WriteLine("Recipes = new Recipes"); writer.WriteLine("{"); writer.Indent++; foreach (Recipe recipe in msg.recipes) { ShapelessRecipe shapelessRecipe = recipe as ShapelessRecipe; if (shapelessRecipe != null) { writer.WriteLine(string.Format("new ShapelessRecipe(new ItemStack(ItemFactory.GetItem({0}, {1}), {2}),", shapelessRecipe.Result.Id, shapelessRecipe.Result.Metadata, shapelessRecipe.Result.Count)); writer.Indent++; writer.WriteLine("new List<ItemStack>"); writer.WriteLine("{"); writer.Indent++; foreach (var itemStack in shapelessRecipe.Input) { writer.WriteLine(string.Format("new ItemStack(ItemFactory.GetItem({0}, {1}), {2}),", itemStack.Id, itemStack.Metadata, itemStack.Count)); } writer.Indent--; writer.WriteLine("}),"); writer.Indent--; continue; } ShapedRecipe shapedRecipe = recipe as ShapedRecipe; if (shapedRecipe != null) { writer.WriteLine(string.Format("new ShapedRecipe({0}, {1}, new ItemStack(ItemFactory.GetItem({2}, {3}), {4}),", shapedRecipe.Width, shapedRecipe.Height, shapedRecipe.Result.Id, shapedRecipe.Result.Metadata, shapedRecipe.Result.Count)); writer.Indent++; writer.WriteLine("new Item[]"); writer.WriteLine("{"); writer.Indent++; foreach (Item item in shapedRecipe.Input) { writer.WriteLine(string.Format("ItemFactory.GetItem({0}, {1}),", item.Id, item.Metadata)); } writer.Indent--; writer.WriteLine("}),"); writer.Indent--; continue; } } writer.WriteLine("};"); writer.Indent--; writer.WriteLine("}"); writer.Indent--; writer.Flush(); file.Close(); //Environment.Exit(0); }
/// <summary> /// Handles the specified package. /// </summary> /// <param name="message">The package.</param> /// <param name="senderEndpoint">The sender's endpoint.</param> private void HandlePackage(Package message, IPEndPoint senderEndpoint) { if (typeof(McpeBatch) == message.GetType()) { Log.Debug("Processing Batch package"); McpeBatch batch = (McpeBatch)message; var messages = new List <Package>(); // Get bytes byte[] payload = batch.payload; // Decompress bytes MemoryStream stream = new MemoryStream(payload); if (stream.ReadByte() != 0x78) { throw new InvalidDataException("Incorrect ZLib header. Expected 0x78"); } stream.ReadByte(); using (var defStream2 = new DeflateStream(stream, CompressionMode.Decompress, false)) { // Get actual package out of bytes MemoryStream destination = new MemoryStream(); defStream2.CopyTo(destination); destination.Position = 0; NbtBinaryReader reader = new NbtBinaryReader(destination, true); do { int len = reader.ReadInt32(); byte[] internalBuffer = reader.ReadBytes(len); messages.Add(PackageFactory.CreatePackage(internalBuffer[0], internalBuffer) ?? new UnknownPackage(internalBuffer[0], internalBuffer)); } while (destination.Position < destination.Length); // throw new Exception("Have more data"); } foreach (var msg in messages) { HandlePackage(msg, senderEndpoint); msg.PutPool(); } return; } TraceReceive(message); if (typeof(UnknownPackage) == message.GetType()) { return; } if (typeof(McpeDisconnect) == message.GetType()) { McpeDisconnect msg = (McpeDisconnect)message; Log.InfoFormat("Disconnect {1}: {0}", msg.message, Username); SendDisconnectionNotification(); StopClient(); return; } if (typeof(ConnectedPing) == message.GetType()) { ConnectedPing msg = (ConnectedPing)message; SendConnectedPong(msg.sendpingtime); return; } if (typeof(McpeFullChunkData) == message.GetType()) { //McpeFullChunkData msg = (McpeFullChunkData) message; //ChunkColumn chunk = ClientUtils.DecocedChunkColumn(msg.chunkData); //if (chunk != null) //{ // Log.DebugFormat("Chunk X={0}", chunk.x); // Log.DebugFormat("Chunk Z={0}", chunk.z); // //ClientUtils.SaveChunkToAnvil(chunk); //} return; } if (typeof(ConnectionRequestAccepted) == message.GetType()) { Thread.Sleep(50); SendNewIncomingConnection(); //_connectedPingTimer = new Timer(state => SendConnectedPing(), null, 1000, 1000); Thread.Sleep(50); SendLogin(Username); return; } if (typeof(McpeSetSpawnPosition) == message.GetType()) { McpeSetSpawnPosition msg = (McpeSetSpawnPosition)message; _spawn = new Vector3(msg.x, msg.y, msg.z); _level.SpawnX = (int)_spawn.X; _level.SpawnY = (int)_spawn.Y; _level.SpawnZ = (int)_spawn.Z; return; } if (typeof(McpeStartGame) == message.GetType()) { McpeStartGame msg = (McpeStartGame)message; _entityId = msg.entityId; _spawn = new Vector3(msg.x, msg.y, msg.z); _level.LevelName = "Default"; _level.Version = 19133; _level.GameType = msg.gamemode; //ClientUtils.SaveLevel(_level); return; } if (typeof(McpeTileEvent) == message.GetType()) { McpeTileEvent msg = (McpeTileEvent)message; Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Case 1: {0}", msg.case1); Log.DebugFormat("Case 2: {0}", msg.case2); return; } if (typeof(McpeAddEntity) == message.GetType()) { McpeAddEntity msg = (McpeAddEntity)message; Log.DebugFormat("Entity ID: {0}", msg.entityId); Log.DebugFormat("Entity Type: {0}", msg.entityType); Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Yaw: {0}", msg.yaw); Log.DebugFormat("Pitch: {0}", msg.pitch); Log.DebugFormat("Velocity X: {0}", msg.speedX); Log.DebugFormat("Velocity Y: {0}", msg.speedY); Log.DebugFormat("Velocity Z: {0}", msg.speedZ); //Log.DebugFormat("Metadata: {0}", msg.metadata.ToString()); //Log.DebugFormat("Links count: {0}", msg.links); return; } if (typeof(McpeSetEntityData) == message.GetType()) { McpeSetEntityData msg = (McpeSetEntityData)message; Log.DebugFormat("Entity ID: {0}", msg.entityId); MetadataDictionary metadata = msg.metadata; Log.DebugFormat("Metadata: {0}", metadata.ToString()); return; } if (typeof(McpeMovePlayer) == message.GetType()) { //McpeMovePlayer msg = (McpeMovePlayer) message; //Log.DebugFormat("Entity ID: {0}", msg.entityId); //CurrentLocation = new PlayerLocation(msg.x, msg.y + 10, msg.z); //SendMcpeMovePlayer(); return; } if (typeof(McpeUpdateBlock) == message.GetType()) { McpeUpdateBlock msg = (McpeUpdateBlock)message; Log.DebugFormat("No of Blocks: {0}", msg.blocks.Count); return; } if (typeof(McpeLevelEvent) == message.GetType()) { McpeLevelEvent msg = (McpeLevelEvent)message; Log.DebugFormat("Event ID: {0}", msg.eventId); Log.DebugFormat("X: {0}", msg.x); Log.DebugFormat("Y: {0}", msg.y); Log.DebugFormat("Z: {0}", msg.z); Log.DebugFormat("Data: {0}", msg.data); return; } if (typeof(McpeContainerSetContent) == message.GetType()) { McpeContainerSetContent msg = (McpeContainerSetContent)message; Log.DebugFormat("Window ID: 0x{0:x2}, Count: {1}", msg.windowId, msg.slotData.Count); var slots = msg.slotData.GetValues(); foreach (var entry in slots) { MetadataSlot slot = (MetadataSlot)entry; //Log.DebugFormat(" - Id: {0}, Metadata: {1}, Count: {2}", slot.Value.Item.Id, slot.Value.Item.Metadata, slot.Value.Count); } return; } if (typeof(McpeCraftingData) == message.GetType()) { string fileName = Path.GetTempPath() + "Recipes_" + Guid.NewGuid() + ".txt"; Log.Info("Writing recipes to filename: " + fileName); FileStream file = File.OpenWrite(fileName); McpeCraftingData msg = (McpeCraftingData)message; IndentedTextWriter writer = new IndentedTextWriter(new StreamWriter(file)); writer.WriteLine("static RecipeManager()"); writer.WriteLine("{"); writer.Indent++; writer.WriteLine("Recipes = new Recipes"); writer.WriteLine("{"); writer.Indent++; foreach (Recipe recipe in msg.recipes) { ShapelessRecipe shapelessRecipe = recipe as ShapelessRecipe; if (shapelessRecipe != null) { writer.WriteLine(string.Format("new ShapelessRecipe(new ItemStack(ItemFactory.GetItem({0}, {1}), {2}),", shapelessRecipe.Result.Id, shapelessRecipe.Result.Metadata, shapelessRecipe.Result.Count)); writer.Indent++; writer.WriteLine("new List<ItemStack>"); writer.WriteLine("{"); writer.Indent++; foreach (var itemStack in shapelessRecipe.Input) { writer.WriteLine(string.Format("new ItemStack(ItemFactory.GetItem({0}, {1}), {2}),", itemStack.Id, itemStack.Metadata, itemStack.Count)); } writer.Indent--; writer.WriteLine("}),"); writer.Indent--; continue; } ShapedRecipe shapedRecipe = recipe as ShapedRecipe; if (shapedRecipe != null) { writer.WriteLine(string.Format("new ShapedRecipe({0}, {1}, new ItemStack(ItemFactory.GetItem({2}, {3}), {4}),", shapedRecipe.Width, shapedRecipe.Height, shapedRecipe.Result.Id, shapedRecipe.Result.Metadata, shapedRecipe.Result.Count)); writer.Indent++; writer.WriteLine("new Item[]"); writer.WriteLine("{"); writer.Indent++; foreach (Item item in shapedRecipe.Input) { writer.WriteLine(string.Format("ItemFactory.GetItem({0}, {1}),", item.Id, item.Metadata)); } writer.Indent--; writer.WriteLine("}),"); writer.Indent--; continue; } } writer.WriteLine("};"); writer.Indent--; writer.WriteLine("}"); writer.Indent--; writer.Flush(); file.Close(); Environment.Exit(0); return; } }
public override void HandleMcpeCraftingData(McpeCraftingData message) { if (Client.IsEmulator) { return; } string fileName = Path.GetTempPath() + "Recipes_" + Guid.NewGuid() + ".txt"; Log.Info("Writing recipes to filename: " + fileName); FileStream file = File.OpenWrite(fileName); IndentedTextWriter writer = new IndentedTextWriter(new StreamWriter(file)); writer.WriteLine("static RecipeManager()"); writer.WriteLine("{"); writer.Indent++; writer.WriteLine("Recipes = new Recipes"); writer.WriteLine("{"); writer.Indent++; foreach (Recipe recipe in message.recipes) { ShapelessRecipe shapelessRecipe = recipe as ShapelessRecipe; if (shapelessRecipe != null) { writer.WriteLine($"new ShapelessRecipe(new Item({shapelessRecipe.Result.Id}, {shapelessRecipe.Result.Metadata}, {shapelessRecipe.Result.Count}),"); writer.Indent++; writer.WriteLine("new List<Item>"); writer.WriteLine("{"); writer.Indent++; foreach (var itemStack in shapelessRecipe.Input) { writer.WriteLine($"new Item({itemStack.Id}, {itemStack.Metadata}, {itemStack.Count}),"); } writer.Indent--; writer.WriteLine("}),"); writer.Indent--; continue; } ShapedRecipe shapedRecipe = recipe as ShapedRecipe; if (shapedRecipe != null && Client._recipeToSend == null) { if (shapedRecipe.Result.Id == 5 && shapedRecipe.Result.Count == 4 && shapedRecipe.Result.Metadata == 0) { Log.Error("Setting recipe! " + shapedRecipe.Id); Client._recipeToSend = shapedRecipe; } } if (shapedRecipe != null) { writer.WriteLine($"new ShapedRecipe({shapedRecipe.Width}, {shapedRecipe.Height}, new Item({shapedRecipe.Result.Id}, {shapedRecipe.Result.Metadata}, {shapedRecipe.Result.Count}),"); writer.Indent++; writer.WriteLine("new Item[]"); writer.WriteLine("{"); writer.Indent++; foreach (Item item in shapedRecipe.Input) { writer.WriteLine($"new Item({item.Id}, {item.Metadata}),"); } writer.Indent--; writer.WriteLine("}),"); writer.Indent--; continue; } SmeltingRecipe smeltingRecipe = recipe as SmeltingRecipe; if (smeltingRecipe != null) { writer.WriteLine($"new SmeltingRecipe(new Item({smeltingRecipe.Result.Id}, {smeltingRecipe.Result.Metadata}, {smeltingRecipe.Result.Count}), new Item({smeltingRecipe.Input.Id}, {smeltingRecipe.Input.Metadata})),"); continue; } MultiRecipe multiRecipe = recipe as MultiRecipe; if (multiRecipe != null) { writer.WriteLine($"new MultiRecipe() {{ Id = new UUID(\"{recipe.Id}\") }}, // {recipe.Id}"); continue; } } writer.WriteLine("};"); writer.Indent--; writer.WriteLine("}"); writer.Indent--; writer.Flush(); file.Close(); //Environment.Exit(0); }
private void ReadNewRecipes() { recipes = new Recipes(); var count = ReadUnsignedVarInt(); Log.Trace($"Reading {count} recipes"); for (int i = 0; i < count; i++) { int recipeType = ReadSignedVarInt(); Log.Trace($"Read recipe no={i} type={recipeType}"); if (recipeType < 0 /*|| len == 0*/) { Log.Error("Read void recipe"); break; } switch (recipeType) { case ShapelessChemistry: case Shapeless: case ShulkerBox: { var recipe = new ShapelessRecipe(); ReadString(); // some unique id int ingrediensCount = ReadVarInt(); // for (int j = 0; j < ingrediensCount; j++) { recipe.Input.Add(ReadRecipeIngredient()); } int resultCount = ReadVarInt(); // 1? for (int j = 0; j < resultCount; j++) { recipe.Result.Add(this.ReadItem2(false)); } recipe.Id = ReadUUID(); // Id recipe.Block = ReadString(); // block? ReadSignedVarInt(); // priority recipe.UniqueId = ReadVarInt(); // unique id recipes.Add(recipe); //Log.Error("Read shapeless recipe"); break; } case ShapedChemistry: case Shaped: { ReadString(); // some unique id int width = ReadVarInt(); // Width int height = ReadVarInt(); // Height var recipe = new ShapedRecipe(width, height); if (width > 3 || height > 3) { // throw new Exception("Wrong number of ingredience. Width=" + width + ", height=" + height); } for (int w = 0; w < width; w++) { for (int h = 0; h < height; h++) { recipe.Input[(h * width) + w] = ReadRecipeIngredient(); } } int resultCount = ReadVarInt(); // 1? for (int j = 0; j < resultCount; j++) { recipe.Result.Add(this.ReadItem2(false)); } recipe.Id = ReadUUID(); // Id recipe.Block = ReadString(); // block? ReadSignedVarInt(); // priority recipe.UniqueId = ReadVarInt(); // unique id recipes.Add(recipe); //Log.Error("Read shaped recipe"); break; } case FurnaceData: case Furnace: { var recipe = new SmeltingRecipe(); short id = (short)ReadSignedVarInt(); // input (with metadata) Item result = this.ReadItem2(false); // Result recipe.Block = ReadString(); // block? recipe.Input = ItemFactory.GetItem(id, 0); recipe.Result = result; recipes.Add(recipe); //Log.Error("Read furnace recipe"); //Log.Error($"Input={id}, meta={""} Item={result.Id}, Meta={result.Metadata}"); break; } /*case FurnaceData: * { * //const ENTRY_FURNACE_DATA = 3; * var recipe = new SmeltingRecipe(); * short id = (short) ReadSignedVarInt(); // input (with metadata) * short meta = (short) ReadSignedVarInt(); // input (with metadata) * Item result = ReadItem(); // Result * recipe.Block = ReadString(); // block? * recipe.Input = ItemFactory.GetItem(id, meta); * recipe.Result = result; * recipes.Add(recipe); * //Log.Error("Read smelting recipe"); * //Log.Error($"Input={id}, meta={meta} Item={result.Id}, Meta={result.Metadata}"); * break; * }*/ case Multi: { //Log.Error("Reading MULTI"); var recipe = new MultiRecipe(); recipe.Id = ReadUUID(); recipe.UniqueId = ReadVarInt(); // unique id recipes.Add(recipe); break; } /*case ShapelessChemistry: * { * var recipe = new ShapelessRecipe(); * ReadString(); // some unique id * int ingrediensCount = ReadVarInt(); // * for (int j = 0; j < ingrediensCount; j++) * { * recipe.Input.Add(ReadRecipeIngredient()); * } * int resultCount = ReadVarInt(); // 1? * for (int j = 0; j < resultCount; j++) * { * recipe.Result.Add(ReadItem()); * } * recipe.Id = ReadUUID(); // Id * recipe.Block = ReadString(); // block? * ReadSignedVarInt(); // priority * recipe.UniqueId = ReadVarInt(); // unique id * //recipes.Add(recipe); * //Log.Error("Read shapeless recipe"); * break; * }*/ /*case ShapedChemistry: * { * ReadString(); // some unique id * int width = ReadSignedVarInt(); // Width * int height = ReadSignedVarInt(); // Height * var recipe = new ShapedRecipe(width, height); * if (width > 3 || height > 3) throw new Exception("Wrong number of ingredience. Width=" + width + ", height=" + height); * for (int w = 0; w < width; w++) * { * for (int h = 0; h < height; h++) * { * recipe.Input[(h * width) + w] = ReadRecipeIngredient(); * } * } * * int resultCount = ReadVarInt(); // 1? * for (int j = 0; j < resultCount; j++) * { * recipe.Result.Add(ReadItem()); * } * recipe.Id = ReadUUID(); // Id * recipe.Block = ReadString(); // block? * ReadSignedVarInt(); // priority * recipe.UniqueId = ReadVarInt(); // unique id * //recipes.Add(recipe); * //Log.Error("Read shaped recipe"); * break; * } */ default: Log.Error($"Read unknown recipe type: {recipeType}"); //ReadBytes(len); break; } } Log.Trace($"Done reading {count} recipes"); }