New method for getting looking location (#1503)

* New method for getting looking location

* improve
This commit is contained in:
ReinforceZwei 2021-03-13 22:23:58 +08:00 committed by GitHub
parent 240468ad22
commit 90505dbc4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 98 additions and 12 deletions

View file

@ -1016,7 +1016,25 @@ namespace MinecraftClient
{ {
return Handler.GetGamemode(); return Handler.GetGamemode();
} }
/// <summary>
/// Return the head yaw of the client player
/// </summary>
/// <returns>Yaw of the client player</returns>
protected float GetYaw()
{
return Handler.GetYaw();
}
/// <summary>
/// Return the head pitch of the client player
/// </summary>
/// <returns>Pitch of the client player</returns>
protected float GetPitch()
{
return Handler.GetPitch();
}
/// <summary> /// <summary>
/// Return the UserUUID of the current account /// Return the UserUUID of the current account
/// </summary> /// </summary>

View file

@ -54,6 +54,18 @@ namespace MinecraftClient.Mapping
/// </summary> /// </summary>
public Location Location; public Location Location;
/// <summary>
/// Entity head yaw
/// </summary>
/// <remarks>Untested</remarks>
public float Yaw = 0;
/// <summary>
/// Entity head pitch
/// </summary>
/// <remarks>Untested</remarks>
public float Pitch = 0;
/// <summary> /// <summary>
/// Health of the entity /// Health of the entity
/// </summary> /// </summary>
@ -94,6 +106,25 @@ namespace MinecraftClient.Mapping
this.Equipment = new Dictionary<int, Item>(); this.Equipment = new Dictionary<int, Item>();
this.Item = new Item(ItemType.Air, 0, null); this.Item = new Item(ItemType.Air, 0, null);
} }
/// <summary>
/// Create a new entity based on Entity ID, Entity Type and location
/// </summary>
/// <param name="ID">Entity ID</param>
/// <param name="type">Entity Type Enum</param>
/// <param name="location">Entity location</param>
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<int, Item>();
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;
}
/// <summary> /// <summary>
/// Create a new entity based on Entity ID, Entity Type, location, name and UUID /// Create a new entity based on Entity ID, Entity Type, location, name and UUID
/// </summary> /// </summary>

View file

@ -152,5 +152,36 @@ namespace MinecraftClient.Mapping
{ {
chunks = new Dictionary<int, Dictionary<int, ChunkColumn>>(); chunks = new Dictionary<int, Dictionary<int, ChunkColumn>>();
} }
/// <summary>
/// Get the location of block of the entity is looking
/// </summary>
/// <param name="location">Location of the entity</param>
/// <param name="yaw">Yaw of the entity</param>
/// <param name="pitch">Pitch of the entity</param>
/// <returns>Location of the block or empty Location if no block was found</returns>
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();
}
} }
} }

View file

@ -50,8 +50,10 @@ namespace MinecraftClient
private Queue<Location> steps; private Queue<Location> steps;
private Queue<Location> path; private Queue<Location> path;
private Location location; private Location location;
private float? yaw; private float? _yaw; // Used for calculation ONLY!!! Doesn't reflect the client yaw
private float? pitch; private float? _pitch; // Used for calculation ONLY!!! Doesn't reflect the client pitch
private float playerYaw;
private float playerPitch;
private double motionY; private double motionY;
private string host; private string host;
@ -98,6 +100,8 @@ namespace MinecraftClient
public string GetUserUUID() { return uuid; } public string GetUserUUID() { return uuid; }
public string GetSessionID() { return sessionid; } public string GetSessionID() { return sessionid; }
public Location GetCurrentLocation() { return location; } public Location GetCurrentLocation() { return location; }
public float GetYaw() { return playerYaw; }
public float GetPitch() { return playerPitch; }
public World GetWorld() { return world; } public World GetWorld() { return world; }
public Double GetServerTPS() { return averageTPS; } public Double GetServerTPS() { return averageTPS; }
public float GetHealth() { return playerHealth; } 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 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) if (steps != null && steps.Count > 0)
{ {
@ -593,12 +597,14 @@ namespace MinecraftClient
location = Movement.HandleGravity(world, location, ref motionY); 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) // 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) // Once yaw and pitch have been sent, switch back to location-only updates (without yaw and pitch)
yaw = null; _yaw = null;
pitch = null; _pitch = null;
} }
} }
@ -916,7 +922,7 @@ namespace MinecraftClient
{ {
// 1-step path to the desired location without checking anything // 1-step path to the desired location without checking anything
UpdateLocation(location, location); // Update yaw and pitch to look at next step 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; return true;
} }
else else
@ -1651,8 +1657,8 @@ namespace MinecraftClient
/// <param name="pitch">Pitch to look at</param> /// <param name="pitch">Pitch to look at</param>
public void UpdateLocation(Location location, float yaw, float pitch) public void UpdateLocation(Location location, float yaw, float pitch)
{ {
this.yaw = yaw; this._yaw = yaw;
this.pitch = pitch; this._pitch = pitch;
UpdateLocation(location, false); UpdateLocation(location, false);
} }

View file

@ -410,7 +410,7 @@ namespace MinecraftClient.Protocol.Handlers
if (living) if (living)
{ {
byte entityHeadPitch = ReadNextByte(cache); entityPitch = ReadNextByte(cache);
} }
else else
{ {
@ -421,7 +421,7 @@ namespace MinecraftClient.Protocol.Handlers
short velocityY = ReadNextShort(cache); short velocityY = ReadNextShort(cache);
short velocityZ = 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);
} }
/// <summary> /// <summary>