feat(autofishing): add velocity and sound bite detection

This commit is contained in:
Anon 2026-03-31 00:18:31 +02:00
parent d5358d3d1a
commit c9b0913c1a
8 changed files with 380 additions and 17 deletions

View file

@ -1054,20 +1054,44 @@ namespace MinecraftClient.Protocol.Handlers
}
}
private static bool HasLpVec3Continuation(int firstByte) => (firstByte & 4) == 4;
private static double UnpackLpVec3(long packedAxis)
{
return Math.Min((double)(packedAxis & 32767L), 32766.0) * 2.0 / 32766.0 - 1.0;
}
/// <summary>
/// Read an LpVec3 (low-precision vec3) from the cache (1.21.9+).
/// Variable-length encoding: first byte 0 = zero vector; otherwise
/// 2 bytes + 4 bytes (6 total), plus an optional VarInt continuation.
/// Read and decode an LpVec3 (low-precision vec3) from the cache (1.21.9+).
/// Returned vector is expressed in blocks per tick.
/// </summary>
public void ReadNextLpVec3(Queue<byte> cache)
public (double X, double Y, double Z) ReadNextLpVec3Values(Queue<byte> cache)
{
int first = ReadNextByte(cache);
if (first == 0)
return;
ReadNextByte(cache); // second byte
ReadData(4, cache); // uint32
if ((first & 4) == 4) // continuation bit set
ReadNextVarInt(cache);
return (0.0, 0.0, 0.0);
int second = ReadNextByte(cache);
uint high = (uint)ReadNextInt(cache);
long packed = ((long)high << 16) | (long)(second << 8) | (uint)first;
long scale = first & 3;
if (HasLpVec3Continuation(first))
scale |= ((long)ReadNextVarInt(cache) & 0xFFFFFFFFL) << 2;
return (
UnpackLpVec3(packed >> 3) * scale,
UnpackLpVec3(packed >> 18) * scale,
UnpackLpVec3(packed >> 33) * scale
);
}
/// <summary>
/// Read an LpVec3 (low-precision vec3) from the cache (1.21.9+) and discard it.
/// </summary>
public void ReadNextLpVec3(Queue<byte> cache)
{
ReadNextLpVec3Values(cache);
}
/// <summary>

View file

@ -2642,6 +2642,27 @@ namespace MinecraftClient.Protocol.Handlers
handler.OnEntityRotation(entityId, yaw, pitch, isOnGround);
}
break;
case PacketTypesIn.EntityVelocity:
if (handler.GetEntityHandlingEnabled())
{
var entityId = dataTypes.ReadNextVarInt(packetData);
double velocityX, velocityY, velocityZ;
if (protocolVersion >= MC_1_21_9_Version)
{
(velocityX, velocityY, velocityZ) = dataTypes.ReadNextLpVec3Values(packetData);
}
else
{
velocityX = dataTypes.ReadNextShort(packetData) / 8000.0D;
velocityY = dataTypes.ReadNextShort(packetData) / 8000.0D;
velocityZ = dataTypes.ReadNextShort(packetData) / 8000.0D;
}
handler.OnEntityVelocity(entityId, velocityX, velocityY, velocityZ);
}
break;
case PacketTypesIn.EntityProperties:
if (handler.GetEntityHandlingEnabled())
@ -2892,6 +2913,65 @@ namespace MinecraftClient.Protocol.Handlers
handler.OnExplosion(explosionLocation, explosionStrength, explosionBlockCount);
break;
case PacketTypesIn.NamedSoundEffect:
{
string? soundName = dataTypes.ReadNextString(packetData);
int category = dataTypes.ReadNextVarInt(packetData);
double x = dataTypes.ReadNextInt(packetData) / 8.0D;
double y = dataTypes.ReadNextInt(packetData) / 8.0D;
double z = dataTypes.ReadNextInt(packetData) / 8.0D;
float volume = dataTypes.ReadNextFloat(packetData);
float pitch = dataTypes.ReadNextFloat(packetData);
handler.OnSoundEffect(soundName, new Location(x, y, z), category, volume, pitch, null);
break;
}
case PacketTypesIn.SoundEffect:
{
string? soundName;
if (protocolVersion >= MC_1_19_Version)
soundName = ReadSoundEventHolderName(packetData);
else
{
dataTypes.ReadNextVarInt(packetData); // Sound id
soundName = null;
}
int category = dataTypes.ReadNextVarInt(packetData);
double x = dataTypes.ReadNextInt(packetData) / 8.0D;
double y = dataTypes.ReadNextInt(packetData) / 8.0D;
double z = dataTypes.ReadNextInt(packetData) / 8.0D;
float volume = dataTypes.ReadNextFloat(packetData);
float pitch = dataTypes.ReadNextFloat(packetData);
if (protocolVersion >= MC_1_19_Version)
dataTypes.ReadNextLong(packetData); // Seed
handler.OnSoundEffect(soundName, new Location(x, y, z), category, volume, pitch, null);
break;
}
case PacketTypesIn.EntitySoundEffect:
{
string? soundName;
if (protocolVersion >= MC_1_19_Version)
soundName = ReadSoundEventHolderName(packetData);
else
{
dataTypes.ReadNextVarInt(packetData); // Sound id
soundName = null;
}
int category = dataTypes.ReadNextVarInt(packetData);
int entityId = dataTypes.ReadNextVarInt(packetData);
float volume = dataTypes.ReadNextFloat(packetData);
float pitch = dataTypes.ReadNextFloat(packetData);
if (protocolVersion >= MC_1_19_Version)
dataTypes.ReadNextLong(packetData); // Seed
handler.OnSoundEffect(soundName, null, category, volume, pitch, entityId);
break;
}
case PacketTypesIn.HeldItemChange:
case PacketTypesIn.SetHeldSlot:
handler.OnHeldItemChange(dataTypes.ReadNextByte(packetData)); // Slot
@ -3154,6 +3234,23 @@ namespace MinecraftClient.Protocol.Handlers
return true; //Packet processed
}
/// <summary>
/// Read a Holder&lt;SoundEvent&gt; from packet data and return its key when inline.
/// Returns null when the holder is a registry reference.
/// </summary>
private string? ReadSoundEventHolderName(Queue<byte> packetData)
{
int soundHolderId = dataTypes.ReadNextVarInt(packetData);
if (soundHolderId != 0)
return null;
string soundName = dataTypes.ReadNextString(packetData);
bool hasFixedRange = dataTypes.ReadNextBool(packetData);
if (hasFixedRange)
dataTypes.ReadNextFloat(packetData);
return soundName;
}
/// <summary>
/// Handle the Statistics packet for pre-1.12 legacy achievements.
/// </summary>

View file

@ -295,6 +295,16 @@ namespace MinecraftClient.Protocol
/// <param name="onGround">TRUE if on ground</param>
void OnEntityTeleport(int entityID, Double x, Double y, Double z, bool onGround);
/// <summary>
/// Called when an entity velocity update packet is received.
/// Velocity values are in blocks per tick.
/// </summary>
/// <param name="entityID">Entity ID</param>
/// <param name="velocityX">Velocity X</param>
/// <param name="velocityY">Velocity Y</param>
/// <param name="velocityZ">Velocity Z</param>
void OnEntityVelocity(int entityID, double velocityX, double velocityY, double velocityZ);
/// <summary>
/// Called when additional properties have been received for an entity
/// </summary>
@ -371,6 +381,17 @@ namespace MinecraftClient.Protocol
/// <param name="affectedBlocks">Amount of affected blocks</param>
void OnExplosion(Location location, float strength, int affectedBlocks);
/// <summary>
/// Called when a sound packet is received.
/// </summary>
/// <param name="soundName">Sound key if available, otherwise null</param>
/// <param name="location">Sound location for world sounds, or null if unavailable</param>
/// <param name="category">Sound category id</param>
/// <param name="volume">Sound volume</param>
/// <param name="pitch">Sound pitch</param>
/// <param name="entityID">Source entity id for entity-sound packets, if any</param>
void OnSoundEffect(string? soundName, Location? location, int category, float volume, float pitch, int? entityID);
/// <summary>
/// Called when a player's game mode has changed
/// </summary>