Beispiel #1
0
 /// <summary>
 /// Creates a new <see cref="HandshakePacket"/> instance.
 /// </summary>
 /// <param name="packet">Incoming packet from client.</param>
 public HandshakePacket(IMinecraftPacket packet)
 {
     ProtocolVersion = packet.ReadVarInt32();
     ServerAddress   = packet.ReadString();
     ServerPort      = packet.ReadUInt16();
     NextState       = (MinecraftUserStatus)packet.ReadVarInt32();
 }
Beispiel #2
0
    public void OnPlayerDigging(IMinecraftUser user, IMinecraftPacket packet)
    {
        var status   = (DiggingType)packet.ReadVarInt32();
        var position = packet.ReadPosition();
        var face     = (BlockFaceType)packet.ReadByte();

        _logger.LogTrace($"Status={status};Position={position};Face={face}");

        if (user.Player.GameMode is ServerGameModeType.Creative)
        {
            IBlock previousBlock = user.Player.Map.GetBlock(position);

            if (previousBlock.IsAir)
            {
                throw new InvalidOperationException($"Cannot dig air blocks ({position})");
            }

            using var playerDiggingAck = new AcknowledgePlayerDiggingPacket(position, previousBlock, DiggingType.Started);
            user.Player.SendPacketToVisibleEntities(playerDiggingAck);

            using var blockChange = new BlockChangePacket(BlockType.Air, position);
            user.Player.SendPacketToVisibleEntities(blockChange);

            IBlock block = user.Player.Map.SetBlock(BlockType.Air, position);

            using var chunkPacket = new ChunkDataPacket(block.Chunk);
            user.Player.SendPacketToVisibleEntities(chunkPacket, includeEntity: true);
        }
        else
        {
            // TODO: other modes
        }
    }
    public void OnPlayerBlockPlacement(IMinecraftUser user, IMinecraftPacket packet)
    {
        var      handType      = (HandType)packet.ReadVarInt32();
        Position blockPosition = packet.ReadPosition();
        var      blockFace     = (BlockFaceType)packet.ReadVarInt32();
        float    cursorX       = packet.ReadSingle();
        float    cursorY       = packet.ReadSingle();
        float    cursorZ       = packet.ReadSingle();
        bool     isInsideBlock = packet.ReadBoolean();

        // TODO: check if the current player is interacting with an interactable object.
        // Like: Chest, anvil, crafting table.

        IItemSlot currentHeldItem = user.Player.HotBar.SelectedSlot;

        if (currentHeldItem.HasItem)
        {
            BlockData block        = _registry.Blocks.FirstOrDefault(x => x.ItemId == currentHeldItem.ItemId);
            BlockType blockToPlace = block.Type;

            if (blockToPlace is not BlockType.Air)
            {
                Position realBlockPosition = blockFace switch
                {
                    BlockFaceType.Bottom => new Position(blockPosition.X, blockPosition.Y - 1, blockPosition.Z),
                    BlockFaceType.Top => new Position(blockPosition.X, blockPosition.Y + 1, blockPosition.Z),
                    BlockFaceType.North => new Position(blockPosition.X, blockPosition.Y, blockPosition.Z - 1),
                    BlockFaceType.South => new Position(blockPosition.X, blockPosition.Y, blockPosition.Z + 1),
                    BlockFaceType.West => new Position(blockPosition.X - 1, blockPosition.Y, blockPosition.Z),
                    BlockFaceType.East => new Position(blockPosition.X + 1, blockPosition.Y, blockPosition.Z),
                    _ => throw new InvalidOperationException("Invalid block face type.")
                };

                _logger.LogDebug($"Placing block '{blockToPlace}' at position {realBlockPosition}");

                user.Player.Map.SetBlock(blockToPlace, (int)realBlockPosition.X, (int)realBlockPosition.Y, (int)realBlockPosition.Z);

                using var blockChangePacket = new BlockChangePacket(blockToPlace, blockPosition);
                user.Player.SendPacketToVisibleEntities(blockChangePacket);

                using var chunkDataPacket = new ChunkDataPacket(user.Player.Chunk);
                user.Player.SendPacketToVisibleEntities(chunkDataPacket);
            }
        }
    }
}
Beispiel #4
0
    public void OnCreativeInventoryAction(IMinecraftUser user, IMinecraftPacket packet)
    {
        short slot = (short)(packet.ReadInt16() - RedstoneContants.PlayerInventoryHotbarOffset);
        // item slot structure
        bool present = packet.ReadBoolean();

        if (present)
        {
            int         itemId     = packet.ReadVarInt32();
            byte        itemCount  = packet.ReadByte();
            NbtCompound itemExtras = packet.ReadNbtCompound();

            _logger.LogInformation($"Item with id: {itemId} (x{itemCount}) set at slot {slot}");
            user.Player.HotBar.SetItem(slot, itemId, itemCount);
        }
        else
        {
            _logger.LogInformation($"Clearing item slot '{slot}'");
            user.Player.HotBar.ClearItem(slot);
        }
    }
Beispiel #5
0
    public void OnAnimation(IMinecraftUser user, IMinecraftPacket packet)
    {
        var hand = (HandType)packet.ReadVarInt32();

        user.Player.SwingHand(hand);
    }
    public void OnTeleportConfirm(MinecraftUser user, IMinecraftPacket packet)
    {
        int teleportId = packet.ReadVarInt32();

        _logger.LogInformation($"Teleport ID: {teleportId}");
    }