From 46978abfdf63ade73b61c4af33e96345425fa521 Mon Sep 17 00:00:00 2001 From: Kenny van Vulpen Date: Thu, 30 Dec 2021 19:06:18 +0100 Subject: [PATCH] First version of Server Authorized movement logic for bedrock --- src/Alex/Entities/PlayerController.cs | 48 ++++++++++++++----- src/Alex/Net/Bedrock/BedrockClient.cs | 1 + .../Net/Bedrock/BedrockClientPacketHandler.cs | 3 +- .../Multiplayer/BedrockWorldProvider.cs | 34 +++++++++++-- submodules/MiNET | 2 +- 5 files changed, 70 insertions(+), 18 deletions(-) diff --git a/src/Alex/Entities/PlayerController.cs b/src/Alex/Entities/PlayerController.cs index 3eaa69198..e43c70ac6 100644 --- a/src/Alex/Entities/PlayerController.cs +++ b/src/Alex/Entities/PlayerController.cs @@ -20,6 +20,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using MiNET.Net; using MiNET.Utils; using MiNET.Utils.Vectors; using NLog; @@ -462,19 +463,7 @@ private void UpdateMovementInput(GameTime gt) var moveVector = Vector3.Zero; var now = DateTime.UtcNow; - /*if (InputManager.IsPressed(AlexInputCommand.Jump) || InputManager.IsPressed(InputCommand.MoveUp)) - { - if (now.Subtract(_lastUp).TotalMilliseconds <= 250) - { - Player.SetFlying(!Player.IsFlying); - } - - _lastUp = now; - }*/ - - //float speedFactor = (float)Player.CalculateMovementSpeed(); - - bool holdingDownSprint = InputManager.IsDown(AlexInputCommand.Sprint); + bool holdingDownSprint = InputManager.IsDown(AlexInputCommand.Sprint); bool canSwim = Player.CanSwim && Player.FeetInWater && Player.HeadInWater; @@ -639,6 +628,39 @@ private void UpdateMovementInput(GameTime gt) LastVelocity = Player.Velocity; } + public AuthInputFlags GetInputFlags() + { + AuthInputFlags inputFlags = 0; + + // bool holdingDownSprint = InputManager.IsDown(AlexInputCommand.Sprint); + + if (InputManager.IsDown(AlexInputCommand.MoveForwards)) + inputFlags |= AuthInputFlags.Up; + + if (InputManager.IsDown(AlexInputCommand.MoveBackwards)) + inputFlags |= AuthInputFlags.Down; + + if (InputManager.IsDown(AlexInputCommand.MoveLeft)) + inputFlags |= AuthInputFlags.Left; + + if (InputManager.IsDown(AlexInputCommand.MoveRight)) + inputFlags |= AuthInputFlags.Right; + + if (InputManager.IsDown(AlexInputCommand.Sneak)) + inputFlags |= AuthInputFlags.SneakDown; + + if (InputManager.IsDown(AlexInputCommand.Sprint)) + inputFlags |= AuthInputFlags.SprintDown; + + if (InputManager.IsDown(AlexInputCommand.Jump)) + inputFlags |= AuthInputFlags.Jumping; + + //if (InputManager.IsDown(AlexInputCommand.)) + // inputFlags |= AuthInputFlags.WantUp; + + return inputFlags; + } + public bool Disposed { get; private set; } = false; /// public void Dispose() diff --git a/src/Alex/Net/Bedrock/BedrockClient.cs b/src/Alex/Net/Bedrock/BedrockClient.cs index 269412292..1caa992fd 100755 --- a/src/Alex/Net/Bedrock/BedrockClient.cs +++ b/src/Alex/Net/Bedrock/BedrockClient.cs @@ -100,6 +100,7 @@ public class BedrockClient : NetworkProvider, IDisposable private readonly List _disposables = new List(); public BedrockTransactionTracker TransactionTracker { get; } public bool ServerAuthoritiveInventory { get; set; } = false; + public bool ServerAuthoritiveMovement { get; set; } = false; public BlockCoordinates ChunkPublisherPosition { get; set; } = BlockCoordinates.Zero; public uint ChunkPublisherRadius { get; set; } = 0; diff --git a/src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs b/src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs index 47c617dd5..bde252197 100755 --- a/src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs +++ b/src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs @@ -225,7 +225,8 @@ public void HandleMcpeStartGame(McpeStartGame message) { if (message.movementType > 0) { - Log.Warn($"!!! Server uses server-authoritive movement, only client-auth is currently supported."); + Client.ServerAuthoritiveMovement = true; + Log.Warn($"!!! Server uses server-authoritive movement, this is not yet FULLY supported!"); } try diff --git a/src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs b/src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs index fbc98b6c1..71d11b6f4 100644 --- a/src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs +++ b/src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs @@ -109,12 +109,40 @@ public override void OnTick() } } + private PlayerLocation _previousPlayerLocation = new PlayerLocation(); private void SendLocation(PlayerLocation location) { - Client.SendMcpeMovePlayer(new PlayerLocation(location.X, location.Y + Player.EyeLevel, location.Z, -location.HeadYaw,-location.Yaw, location.Pitch) + if (Client.ServerAuthoritiveMovement) { - OnGround = location.OnGround - }, 0, World.Time); + var player = World?.Player; + + if (player == null) + return; + + var delta = location.ToVector3() - _previousPlayerLocation.ToVector3(); + + var heading = player.Movement.Heading; + McpePlayerAuthInput input = McpePlayerAuthInput.CreateObject(); + input.InputFlags = player.Controller.GetInputFlags(); + input.Position = new System.Numerics.Vector3(location.X, location.Y, location.Z); + input.Yaw = location.Yaw; + input.HeadYaw = location.HeadYaw; + input.Pitch = location.Pitch; + input.MoveVecX = heading.X; + input.MoveVecZ = heading.Z; + input.Delta = new System.Numerics.Vector3(delta.X, delta.Y, delta.Z); + + Client.SendPacket(input); + + _previousPlayerLocation = location; + } + else + { + Client.SendMcpeMovePlayer( + new PlayerLocation( + location.X, location.Y + Player.EyeLevel, location.Z, -location.HeadYaw, -location.Yaw, + location.Pitch) { OnGround = location.OnGround }, 0, World.Time); + } } private void UnloadChunks(ChunkCoordinates center, double maxViewDistance) diff --git a/submodules/MiNET b/submodules/MiNET index d9597bc06..fc80766d6 160000 --- a/submodules/MiNET +++ b/submodules/MiNET @@ -1 +1 @@ -Subproject commit d9597bc0690d3c135ebf30ceff2adba2780ea7a1 +Subproject commit fc80766d69d27e02b04b57276e5744a5f17e3b28