From 90505dbc4c9375dbbefb5992112c74ed6e2122e1 Mon Sep 17 00:00:00 2001
From: ReinforceZwei <39955851+ReinforceZwei@users.noreply.github.com>
Date: Sat, 13 Mar 2021 22:23:58 +0800
Subject: [PATCH] New method for getting looking location (#1503)
* New method for getting looking location
* improve
---
MinecraftClient/ChatBot.cs | 20 +++++++++++-
MinecraftClient/Mapping/Entity.cs | 31 +++++++++++++++++++
MinecraftClient/Mapping/World.cs | 31 +++++++++++++++++++
MinecraftClient/McClient.cs | 24 ++++++++------
.../Protocol/Handlers/DataTypes.cs | 4 +--
5 files changed, 98 insertions(+), 12 deletions(-)
diff --git a/MinecraftClient/ChatBot.cs b/MinecraftClient/ChatBot.cs
index 5afa1f99..f018d093 100644
--- a/MinecraftClient/ChatBot.cs
+++ b/MinecraftClient/ChatBot.cs
@@ -1016,7 +1016,25 @@ namespace MinecraftClient
{
return Handler.GetGamemode();
}
-
+
+ ///
+ /// Return the head yaw of the client player
+ ///
+ /// Yaw of the client player
+ protected float GetYaw()
+ {
+ return Handler.GetYaw();
+ }
+
+ ///
+ /// Return the head pitch of the client player
+ ///
+ /// Pitch of the client player
+ protected float GetPitch()
+ {
+ return Handler.GetPitch();
+ }
+
///
/// Return the UserUUID of the current account
///
diff --git a/MinecraftClient/Mapping/Entity.cs b/MinecraftClient/Mapping/Entity.cs
index 740100da..e9c153fe 100644
--- a/MinecraftClient/Mapping/Entity.cs
+++ b/MinecraftClient/Mapping/Entity.cs
@@ -54,6 +54,18 @@ namespace MinecraftClient.Mapping
///
public Location Location;
+ ///
+ /// Entity head yaw
+ ///
+ /// Untested
+ public float Yaw = 0;
+
+ ///
+ /// Entity head pitch
+ ///
+ /// Untested
+ public float Pitch = 0;
+
///
/// Health of the entity
///
@@ -94,6 +106,25 @@ namespace MinecraftClient.Mapping
this.Equipment = new Dictionary();
this.Item = new Item(ItemType.Air, 0, null);
}
+
+ ///
+ /// Create a new entity based on Entity ID, Entity Type and location
+ ///
+ /// Entity ID
+ /// Entity Type Enum
+ /// Entity location
+ public Entity(int ID, EntityType type, Location location, byte yaw, byte pitch)
+ {
+ this.ID = ID;
+ this.Type = type;
+ this.Location = location;
+ this.Health = 1.0f;
+ this.Equipment = new Dictionary();
+ this.Item = new Item(ItemType.Air, 0, null);
+ this.Yaw = yaw * (1 / 256) * 360; // to angle in 360 degree
+ this.Pitch = pitch * (1 / 256) * 360;
+ }
+
///
/// Create a new entity based on Entity ID, Entity Type, location, name and UUID
///
diff --git a/MinecraftClient/Mapping/World.cs b/MinecraftClient/Mapping/World.cs
index c6eab89a..55ceaa75 100644
--- a/MinecraftClient/Mapping/World.cs
+++ b/MinecraftClient/Mapping/World.cs
@@ -152,5 +152,36 @@ namespace MinecraftClient.Mapping
{
chunks = new Dictionary>();
}
+
+ ///
+ /// Get the location of block of the entity is looking
+ ///
+ /// Location of the entity
+ /// Yaw of the entity
+ /// Pitch of the entity
+ /// Location of the block or empty Location if no block was found
+ public Location GetLookingBlockLocation(Location location, double yaw, double pitch)
+ {
+ double rotX = (Math.PI / 180) * yaw;
+ double rotY = (Math.PI / 180) * pitch;
+ double x = -Math.Cos(rotY) * Math.Sin(rotX);
+ double y = -Math.Sin(rotY);
+ double z = Math.Cos(rotY) * Math.Cos(rotX);
+ Location vector = new Location(x, y, z);
+ for (int i = 0; i < 5; i++)
+ {
+ Location newVector = vector * i;
+ Location blockLocation = location.EyesLocation() + new Location(newVector.X, newVector.Y, newVector.Z);
+ blockLocation.X = Math.Floor(blockLocation.X);
+ blockLocation.Y = Math.Floor(blockLocation.Y);
+ blockLocation.Z = Math.Floor(blockLocation.Z);
+ Block b = GetBlock(blockLocation);
+ if (b.Type != Material.Air)
+ {
+ return blockLocation;
+ }
+ }
+ return new Location();
+ }
}
}
diff --git a/MinecraftClient/McClient.cs b/MinecraftClient/McClient.cs
index 1c07258f..ad44795d 100644
--- a/MinecraftClient/McClient.cs
+++ b/MinecraftClient/McClient.cs
@@ -50,8 +50,10 @@ namespace MinecraftClient
private Queue steps;
private Queue path;
private Location location;
- private float? yaw;
- private float? pitch;
+ private float? _yaw; // Used for calculation ONLY!!! Doesn't reflect the client yaw
+ private float? _pitch; // Used for calculation ONLY!!! Doesn't reflect the client pitch
+ private float playerYaw;
+ private float playerPitch;
private double motionY;
private string host;
@@ -98,6 +100,8 @@ namespace MinecraftClient
public string GetUserUUID() { return uuid; }
public string GetSessionID() { return sessionid; }
public Location GetCurrentLocation() { return location; }
+ public float GetYaw() { return playerYaw; }
+ public float GetPitch() { return playerPitch; }
public World GetWorld() { return world; }
public Double GetServerTPS() { return averageTPS; }
public float GetHealth() { return playerHealth; }
@@ -576,7 +580,7 @@ namespace MinecraftClient
{
for (int i = 0; i < 2; i++) //Needs to run at 20 tps; MCC runs at 10 tps
{
- if (yaw == null || pitch == null)
+ if (_yaw == null || _pitch == null)
{
if (steps != null && steps.Count > 0)
{
@@ -593,12 +597,14 @@ namespace MinecraftClient
location = Movement.HandleGravity(world, location, ref motionY);
}
}
- handler.SendLocationUpdate(location, Movement.IsOnGround(world, location), yaw, pitch);
+ playerYaw = _yaw == null ? playerYaw : _yaw.Value;
+ playerPitch = _pitch == null ? playerPitch : _pitch.Value;
+ handler.SendLocationUpdate(location, Movement.IsOnGround(world, location), _yaw, _pitch);
}
// First 2 updates must be player position AND look, and player must not move (to conform with vanilla)
// Once yaw and pitch have been sent, switch back to location-only updates (without yaw and pitch)
- yaw = null;
- pitch = null;
+ _yaw = null;
+ _pitch = null;
}
}
@@ -916,7 +922,7 @@ namespace MinecraftClient
{
// 1-step path to the desired location without checking anything
UpdateLocation(location, location); // Update yaw and pitch to look at next step
- handler.SendLocationUpdate(location, Movement.IsOnGround(world, location), yaw, pitch);
+ handler.SendLocationUpdate(location, Movement.IsOnGround(world, location), _yaw, _pitch);
return true;
}
else
@@ -1651,8 +1657,8 @@ namespace MinecraftClient
/// Pitch to look at
public void UpdateLocation(Location location, float yaw, float pitch)
{
- this.yaw = yaw;
- this.pitch = pitch;
+ this._yaw = yaw;
+ this._pitch = pitch;
UpdateLocation(location, false);
}
diff --git a/MinecraftClient/Protocol/Handlers/DataTypes.cs b/MinecraftClient/Protocol/Handlers/DataTypes.cs
index 8aabe11b..8cbea23b 100644
--- a/MinecraftClient/Protocol/Handlers/DataTypes.cs
+++ b/MinecraftClient/Protocol/Handlers/DataTypes.cs
@@ -410,7 +410,7 @@ namespace MinecraftClient.Protocol.Handlers
if (living)
{
- byte entityHeadPitch = ReadNextByte(cache);
+ entityPitch = ReadNextByte(cache);
}
else
{
@@ -421,7 +421,7 @@ namespace MinecraftClient.Protocol.Handlers
short velocityY = ReadNextShort(cache);
short velocityZ = ReadNextShort(cache);
- return new Entity(entityID, entityType, new Location(entityX, entityY, entityZ));
+ return new Entity(entityID, entityType, new Location(entityX, entityY, entityZ), entityYaw, entityPitch);
}
///