예제 #1
0
파일: Package.cs 프로젝트: qtx0213/MiNET
        public void Write(ItemStacks metadata)
        {
            McpeContainerSetContent msg = this as McpeContainerSetContent;
            bool signItems = msg == null || msg.windowId != 0x79;

            if (metadata == null)
            {
                if (this is McpeCraftingEvent)
                {
                    Write((int)0);
                }
                else
                {
                    Write((short)0);
                }
                return;
            }

            if (this is McpeCraftingEvent)
            {
                Write((int)metadata.Count);
            }
            else
            {
                Write((short)metadata.Count);
            }


            for (int i = 0; i < metadata.Count; i++)
            {
                Write(metadata[i], signItems);
            }
        }
예제 #2
0
        public void GameMode(Player player, int gameMode)
        {
            if (gameMode == 1)
            {
                player.Inventory.Slots.Clear();

                player.Inventory.Slots.AddRange(InventoryUtils.CreativeInventoryItems);
            }

            //player.GameMode = (GameMode) gameMode;

            player.SendPackage(new McpeStartGame
            {
                seed      = -1,
                generator = 1,
                gamemode  = gameMode,
                entityId  = player.EntityId,
                spawnX    = (int)player.Level.SpawnPoint.X,
                spawnY    = (int)player.Level.SpawnPoint.Y,
                spawnZ    = (int)player.Level.SpawnPoint.Z,
                x         = player.KnownPosition.X,
                y         = player.KnownPosition.Y,
                z         = player.KnownPosition.Z
            });

            {
                McpeContainerSetContent creativeContent = McpeContainerSetContent.CreateObject();
                creativeContent.windowId   = (byte)0x79;
                creativeContent.slotData   = player.Inventory.GetSlots();
                creativeContent.hotbarData = player.Inventory.GetHotbar();
                player.SendPackage(creativeContent);
            }

            player.Level.BroadcastMessage(string.Format("{0} changed to game mode {1}.", player.Username, gameMode), type: MessageType.Raw);
        }
예제 #3
0
파일: Package.cs 프로젝트: Eros/MiNET
        public void Write(MetadataSlots metadata)
        {
            McpeContainerSetContent msg = this as McpeContainerSetContent;
            bool signItems = msg == null || msg.windowId != 0x79;

            if (metadata == null)
            {
                if (this is McpeCraftingEvent)
                {
                    Write((int)0);
                }
                else
                {
                    Write((short)0);
                }
                return;
            }

            if (this is McpeCraftingEvent)
            {
                Write((int)metadata.Count);
            }
            else
            {
                Write((short)metadata.Count);
            }


            for (int i = 0; i < metadata.Count; i++)
            {
                MetadataSlot slot = metadata[i] as MetadataSlot;
                if (slot != null)
                {
                    if (slot.Value.Id == 0)
                    {
                        Write((short)0);
                        continue;
                    }

                    Write(slot.Value.Id);
                    Write(slot.Value.Count);
                    Write(slot.Value.Metadata);
                    var extraData = slot.Value.ExtraData;
                    if (signItems)
                    {
                        extraData = ItemSigner.DefualtItemSigner?.SignNbt(extraData);
                    }
                    if (extraData != null)
                    {
                        var bytes = GetNbtData(extraData);
                        Write((short)bytes.Length);
                        Write(bytes);
                    }
                    else
                    {
                        Write((short)0);
                    }
                }
            }
        }
예제 #4
0
        private static void OnMcpeContainerSetContent(Package message)
        {
            McpeContainerSetContent msg = (McpeContainerSetContent)message;

            Log.Error($"Set container content on Window ID: 0x{msg.windowId:x2}, Count: {msg.slotData.Count}");
            var slots = msg.slotData.GetValues();

            if (msg.windowId == 0x79)
            {
                string fileName = Path.GetTempPath() + "Inventory_0x79_" + Guid.NewGuid() + ".txt";
                WriteInventoryToFile(fileName, slots);
            }
            else if (msg.windowId == 0x00)
            {
                string fileName = Path.GetTempPath() + "Inventory_0x00_" + Guid.NewGuid() + ".txt";
                WriteInventoryToFile(fileName, slots);
                var hotbar = msg.hotbarData.GetValues();
                int i      = 0;
                foreach (MetadataEntry entry in hotbar)
                {
                    MetadataInt val = (MetadataInt)entry;
                    Log.Error($"Hotbar slot: {i} val: {val.Value}");
                    i++;
                }
            }
        }
예제 #5
0
        public void SetInventorySlot(int slot, short itemId, byte amount = 1, short metadata = 0)
        {
            Slots[slot] = new ItemStack(itemId, amount, metadata);

            var containerSetContent = McpeContainerSetContent.CreateObject();

            containerSetContent.windowId   = 0;
            containerSetContent.slotData   = GetSlots();
            containerSetContent.hotbarData = GetHotbar();
            Player.SendPackage(containerSetContent);
        }
예제 #6
0
        public void Write(ItemStacks metadata)
        {
            McpeContainerSetContent msg = this as McpeContainerSetContent;
            bool signItems = msg == null || msg.windowId != 0x79;

            if (metadata == null)
            {
                WriteVarInt(0);
                return;
            }

            WriteVarInt(metadata.Count);

            for (int i = 0; i < metadata.Count; i++)
            {
                Write(metadata[i], signItems);
            }
        }
예제 #7
0
        private static void OnMcpeContainerSetContent(Package message)
        {
            McpeContainerSetContent msg = (McpeContainerSetContent)message;

            Log.DebugFormat("Window ID: 0x{0:x2}, Count: {1}", msg.windowId, msg.slotData.Count);
            var slots = msg.slotData.GetValues();

            if (msg.windowId == 0x79)
            {
                string fileName = Path.GetTempPath() + "Inventory_0x79_" + Guid.NewGuid() + ".txt";
                WriteInventoryToFile(fileName, slots);
            }
            else if (msg.windowId == 0x00)
            {
                string fileName = Path.GetTempPath() + "Inventory_0x00_" + Guid.NewGuid() + ".txt";
                WriteInventoryToFile(fileName, slots);
            }
        }
예제 #8
0
파일: xPlayer.cs 프로젝트: rdcpe/xCoreSplit
        public override void HandleMcpeContainerSetSlot(McpeContainerSetSlot message)
        {
            if (message.item.Id == Inventory.Slots[message.slot].Id)
            {
                if (message.item.Count == Inventory.Slots[message.slot].Count)
                {
                    return;
                }
            }
            if (Level is xCoreLevel)
            {
                if (!(((xCoreLevel)Level).Status == Status.Start))
                {
                    base.HandleMcpeContainerSetSlot(message);
                }
                else
                {
                    if (message.hotbarslot == 0)
                    {
                        McpeContainerSetContent strangeContent = McpeContainerSetContent.CreateObject();
                        strangeContent.windowId     = (byte)0x7b;
                        strangeContent.entityIdSelf = EntityId;
                        strangeContent.slotData     = new ItemStacks();
                        strangeContent.hotbarData   = new MetadataInts();
                        SendPackage(strangeContent);

                        McpeContainerSetContent inventoryContent = McpeContainerSetContent.CreateObject();
                        inventoryContent.windowId     = (byte)0x00;
                        inventoryContent.entityIdSelf = EntityId;
                        inventoryContent.slotData     = Inventory.GetSlots();
                        inventoryContent.hotbarData   = Inventory.GetHotbar();
                        SendPackage(inventoryContent);

                        McpeMobEquipment order = McpeMobEquipment.CreateObject();
                        order.runtimeEntityId = EntityManager.EntityIdSelf;
                        order.item            = message.item;
                        order.selectedSlot    = (byte)message.selectedSlot;                      // Selected hotbar slot
                        order.slot            = (byte)message.slot;
                        SendPackage(order);
                        //SendPlayerInventory();
                    }
                    return;
                }
            }
            else
            {
                if (message.hotbarslot == 0)
                {
                    McpeContainerSetContent strangeContent = McpeContainerSetContent.CreateObject();
                    strangeContent.windowId     = (byte)0x7b;
                    strangeContent.entityIdSelf = EntityId;
                    strangeContent.slotData     = new ItemStacks();
                    strangeContent.hotbarData   = new MetadataInts();
                    SendPackage(strangeContent);

                    McpeContainerSetContent inventoryContent = McpeContainerSetContent.CreateObject();
                    inventoryContent.windowId     = (byte)0x00;
                    inventoryContent.entityIdSelf = EntityId;
                    inventoryContent.slotData     = Inventory.GetSlots();
                    inventoryContent.hotbarData   = Inventory.GetHotbar();
                    SendPackage(inventoryContent);
                }
                return;
            }
            base.HandleMcpeContainerSetSlot(message);
        }
예제 #9
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;
            }
        }