private void Start(object o) { Stopwatch watch = new Stopwatch(); watch.Restart(); var serverInfo = Server.ServerInfo; try { Session = Server.SessionManager.CreateSession(this); if (Server.IsSecurityEnabled) { User = Server.UserManager.FindByName(Username); } lock (_disconnectSync) { if (!IsConnected) return; Level = Server.LevelManager.GetLevel(this, "Default"); } if (Level == null) { Disconnect("No level assigned."); return; } SpawnPosition = SpawnPosition ?? Level.SpawnPoint; KnownPosition = SpawnPosition; // Check if the user already exist, that case bumpt the old one Level.RemoveDuplicatePlayers(Username, ClientId); Level.EntityManager.AddEntity(null, this); GameMode = Level.GameMode; // Start game SendStartGame(); SendSetSpawnPosition(); SetPosition(SpawnPosition); SendSetTime(); SendSetDificulty(); SendAdventureSettings(); SendSetHealth(); SendSetEntityData(); Level.AddPlayer(this, false); { var craftingData = new McpeCraftingData(); craftingData.recipes = RecipeManager.Recipes; SendPackage(craftingData); } } finally { Interlocked.Decrement(ref serverInfo.ConnectionsInConnectPhase); } LastUpdatedTime = DateTime.UtcNow; SendCreativeInventory(); SendPlayerInventory(); ThreadPool.QueueUserWorkItem(delegate(object state) { SendChunksForKnownPosition(); }); LastUpdatedTime = DateTime.UtcNow; Log.InfoFormat("Login complete by: {0} from {2} in {1}ms", Username, watch.ElapsedMilliseconds, EndPoint); }
public virtual void HandleMcpeCraftingData(McpeCraftingData message) { }
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 container set slot. /// </summary> /// <param name="message">The message.</param> protected virtual void HandleContainerSetSlot(McpeContainerSetSlot message) { lock (Inventory) { if (HealthManager.IsDead) return; ItemStack itemStack = message.item.Value; if (GameMode != GameMode.Creative) { if (!VerifyItemStack(itemStack)) { Log.Error($"Kicked {Username} for inventory hacking."); Disconnect("Error #324. Please report this error."); return; } } Log.Info($"Player {Username} set inventory item on window 0x{message.windowId:X2} with slot: {message.slot} HOTBAR: {message.unknown} Item ID: {itemStack.Id} Item Count: {itemStack.Count} Meta: {itemStack.Metadata}: DatagramSequenceNumber: {message.DatagramSequenceNumber}, ReliableMessageNumber: {message.ReliableMessageNumber}, OrderingIndex: {message.OrderingIndex}"); // on all set container content, check if we have active inventory // and update that inventory. // Inventory manager makes sure other players with the same inventory open will // also get the update. if (_openInventory != null) { if (_openInventory.WindowsId == message.windowId) { if (_openInventory.Type == 4) { Recipes recipes = new Recipes(); recipes.Add(new EnchantingRecipe()); McpeCraftingData crafting = new McpeCraftingData {recipes = recipes}; SendPackage(crafting); } // block inventories of various kinds (chests, furnace, etc) _openInventory.SetSlot(this, (byte) message.slot, itemStack); return; } } switch (message.windowId) { case 0: Inventory.Slots[message.slot] = itemStack; break; case 0x79: Inventory.Slots[message.slot] = itemStack; break; case 0x78: var armorItem = itemStack.Item; switch (message.slot) { case 0: Inventory.Helmet = armorItem; break; case 1: Inventory.Chest = armorItem; break; case 2: Inventory.Leggings = armorItem; break; case 3: Inventory.Boots = armorItem; break; } McpePlayerArmorEquipment armorEquipment = McpePlayerArmorEquipment.CreateObject(); armorEquipment.entityId = EntityId; armorEquipment.helmet = new MetadataSlot(new ItemStack(Inventory.Helmet, 1)); armorEquipment.chestplate = new MetadataSlot(new ItemStack(Inventory.Chest, 1)); armorEquipment.leggings = new MetadataSlot(new ItemStack(Inventory.Leggings, 1)); armorEquipment.boots = new MetadataSlot(new ItemStack(Inventory.Boots, 1)); Level.RelayBroadcast(this, armorEquipment); break; case 0x7A: //Inventory.ItemHotbar[message.unknown] = message.slot/* + PlayerInventory.HotbarSize*/; break; } } }
public abstract void HandleMcpeCraftingData(McpeCraftingData message);
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), "\t"); writer.WriteLine(); writer.Indent++; writer.Indent++; writer.WriteLine("static RecipeManager()"); writer.WriteLine("{"); writer.Indent++; writer.WriteLine("Recipes = new Recipes"); writer.WriteLine("{"); writer.Indent++; foreach (Recipe recipe in message.recipes) { var shapelessRecipe = recipe as ShapelessRecipe; if (shapelessRecipe != null) { writer.WriteLine($"new ShapelessRecipe("); writer.Indent++; writer.WriteLine("new List<Item>"); writer.WriteLine("{"); writer.Indent++; foreach (var itemStack in shapelessRecipe.Result) { writer.WriteLine($"new Item({itemStack.Id}, {itemStack.Metadata}, {itemStack.Count}),"); } writer.Indent--; writer.WriteLine($"}},"); 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($"}}, \"{shapelessRecipe.Block}\"),"); writer.Indent--; continue; } var 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},"); writer.Indent++; writer.WriteLine("new List<Item>"); writer.WriteLine("{"); writer.Indent++; foreach (Item item in shapedRecipe.Result) { writer.WriteLine($"new Item({item.Id}, {item.Metadata}),"); } writer.Indent--; writer.WriteLine($"}},"); 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($"}}, \"{shapedRecipe.Block}\"),"); writer.Indent--; continue; } var 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}), \"{smeltingRecipe.Block}\"),"); continue; } var multiRecipe = recipe as MultiRecipe; if (multiRecipe != null) { writer.WriteLine($"new MultiRecipe() {{ Id = new UUID(\"{recipe.Id}\") }}, // {recipe.Id}"); continue; } } writer.Indent--; writer.WriteLine("};"); writer.Indent--; writer.WriteLine("}"); 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) { UnhandledPackage(message); }
public override void HandleMcpeCraftingData(McpeCraftingData message) { }