Skip to content

Commit

Permalink
First version of Server Authorized movement logic for bedrock
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyvv committed Dec 30, 2021
1 parent 2aea1e2 commit 46978ab
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 18 deletions.
48 changes: 35 additions & 13 deletions src/Alex/Entities/PlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
/// <inheritdoc />
public void Dispose()
Expand Down
1 change: 1 addition & 0 deletions src/Alex/Net/Bedrock/BedrockClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class BedrockClient : NetworkProvider, IDisposable
private readonly List<IDisposable> _disposables = new List<IDisposable>();
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;
Expand Down
3 changes: 2 additions & 1 deletion src/Alex/Net/Bedrock/BedrockClientPacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 31 additions & 3 deletions src/Alex/Worlds/Multiplayer/BedrockWorldProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 46978ab

Please sign in to comment.