From 1aea8d3a4e028a94d97d22202580c757b1e302b6 Mon Sep 17 00:00:00 2001
From: Domracz <112401337+Domracz@users.noreply.github.com>
Date: Wed, 11 Oct 2023 02:44:46 -0400
Subject: [PATCH] Fixed entity rotations (#2596)
* Fixed entity rotations
Fixed entity yaw and pitch not changing when entity moves head.
* Update ChatBot.cs
* Update McClient.cs
* Update Protocol18.cs
* Update McClient.cs
* Finalize code style
* Fix incorrect variable type
---------
Co-authored-by: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com>
---
MinecraftClient/McClient.cs | 41 +++++++++++++++++++
.../Protocol/Handlers/Protocol18.cs | 20 +++++++--
.../Protocol/IMinecraftComHandler.cs | 21 ++++++++++
MinecraftClient/Scripting/ChatBot.cs | 6 +++
4 files changed, 85 insertions(+), 3 deletions(-)
diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs
index 48856f18..f9bd5546 100644
--- a/MinecraftClient/McClient.cs
+++ b/MinecraftClient/McClient.cs
@@ -3146,6 +3146,31 @@ namespace MinecraftClient
}
}
+ ///
+ /// Called when an entity's position changed within 8 block of its previous position with rotation.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public void OnEntityPosition(int EntityID, Double Dx, Double Dy, Double Dz, float yaw, float pitch, bool onGround)
+ {
+ if (entities.ContainsKey(EntityID))
+ {
+ Location L = entities[EntityID].Location;
+ L.X += Dx;
+ L.Y += Dy;
+ L.Z += Dz;
+ entities[EntityID].Location = L;
+ entities[EntityID].Yaw = yaw;
+ entities[EntityID].Pitch = pitch;
+ DispatchBotEvent(bot => bot.OnEntityMove(entities[EntityID]));
+ }
+ }
+
///
/// Called when an entity's position changed within 8 block of its previous position.
///
@@ -3165,7 +3190,23 @@ namespace MinecraftClient
entities[EntityID].Location = L;
DispatchBotEvent(bot => bot.OnEntityMove(entities[EntityID]));
}
+ }
+ ///
+ /// Called when an entity's rotation changed.
+ ///
+ ///
+ ///
+ ///
+ ///
+ public void OnEntityRotation(int EntityID, float yaw, float pitch, bool onGround)
+ {
+ if (entities.ContainsKey(EntityID))
+ {
+ entities[EntityID].Yaw = yaw;
+ entities[EntityID].Pitch = pitch;
+ DispatchBotEvent(bot => bot.OnEntityRotate(entities[EntityID]));
+ }
}
///
diff --git a/MinecraftClient/Protocol/Handlers/Protocol18.cs b/MinecraftClient/Protocol/Handlers/Protocol18.cs
index 677b6848..f96affae 100644
--- a/MinecraftClient/Protocol/Handlers/Protocol18.cs
+++ b/MinecraftClient/Protocol/Handlers/Protocol18.cs
@@ -2117,14 +2117,28 @@ namespace MinecraftClient.Protocol.Handlers
}
- byte _yaw = dataTypes.ReadNextByte(packetData);
- byte _pitch = dataTypes.ReadNextByte(packetData);
+ float _yaw = dataTypes.ReadNextByte(packetData) * (1F / 256) * 360;
+ float _pitch = dataTypes.ReadNextByte(packetData) * (1F / 256) * 360;
bool OnGround = dataTypes.ReadNextBool(packetData);
DeltaX = DeltaX / (128 * 32);
DeltaY = DeltaY / (128 * 32);
DeltaZ = DeltaZ / (128 * 32);
- handler.OnEntityPosition(EntityID, DeltaX, DeltaY, DeltaZ, OnGround);
+ handler.OnEntityPosition(EntityID, DeltaX, DeltaY, DeltaZ, _yaw, _pitch, OnGround);
+ }
+
+ break;
+ case PacketTypesIn.EntityRotation:
+ if (handler.GetEntityHandlingEnabled())
+ {
+ int EntityID = dataTypes.ReadNextVarInt(packetData);
+
+
+ float _yaw = dataTypes.ReadNextByte(packetData) * (1F / 256) * 360;
+ float _pitch = dataTypes.ReadNextByte(packetData)* (1F / 256) * 360;
+ bool OnGround = dataTypes.ReadNextBool(packetData);
+
+ handler.OnEntityRotation(EntityID, _yaw, _pitch, OnGround);
}
break;
diff --git a/MinecraftClient/Protocol/IMinecraftComHandler.cs b/MinecraftClient/Protocol/IMinecraftComHandler.cs
index 99d098f6..e2a5be5c 100644
--- a/MinecraftClient/Protocol/IMinecraftComHandler.cs
+++ b/MinecraftClient/Protocol/IMinecraftComHandler.cs
@@ -259,6 +259,27 @@ namespace MinecraftClient.Protocol
/// TRUE if on ground
void OnEntityPosition(int entityID, Double dx, Double dy, Double dz, bool onGround);
+ ///
+ /// Called when an entity moved and rotated
+ ///
+ /// Entity ID
+ /// X offset
+ /// Y offset
+ /// Z offset
+ /// Yaw
+ /// Pitch
+ /// TRUE if on ground
+ void OnEntityPosition(int entityID, Double dx, Double dy, Double dz, float yaw, float pitch, bool onGround);
+
+ ///
+ /// Called when an entity rotated
+ ///
+ /// Entity ID
+ /// Yaw
+ /// Pitch
+ /// TRUE if on ground
+ void OnEntityRotation(int entityID, float yaw, float pitch, bool onGround);
+
///
/// Called when an entity moved to fixed coordinates
///
diff --git a/MinecraftClient/Scripting/ChatBot.cs b/MinecraftClient/Scripting/ChatBot.cs
index 618169d1..af5b5a43 100644
--- a/MinecraftClient/Scripting/ChatBot.cs
+++ b/MinecraftClient/Scripting/ChatBot.cs
@@ -195,6 +195,12 @@ namespace MinecraftClient.Scripting
/// Entity with updated location
public virtual void OnEntityMove(Entity entity) { }
+ ///
+ /// Called when an entity rotates
+ ///
+ /// Entity with updated rotation
+ public virtual void OnEntityRotate(Entity entity) { }
+
///
/// Called after an internal MCC command has been performed
///