Translate camera instruction set

This commit is contained in:
AkmalFairuz
2025-06-20 02:11:25 +07:00
parent 10608d39cf
commit fa70365ddf
3 changed files with 100 additions and 28 deletions

View File

@@ -9,7 +9,7 @@ import (
// CameraInstruction gives a custom camera specific instructions to operate. // CameraInstruction gives a custom camera specific instructions to operate.
type CameraInstruction struct { type CameraInstruction struct {
// Set is a camera instruction that sets the camera to a specified preset. // Set is a camera instruction that sets the camera to a specified preset.
Set protocol.Optional[protocol.CameraInstructionSet] Set protocol.Optional[proto.CameraInstructionSet]
// Clear can be set to true to clear all the current camera instructions. // Clear can be set to true to clear all the current camera instructions.
Clear protocol.Optional[bool] Clear protocol.Optional[bool]
// Fade is a camera instruction that fades the screen to a specified colour. // Fade is a camera instruction that fades the screen to a specified colour.

View File

@@ -88,7 +88,6 @@ func (x *CameraPreset) FromLatest(cp protocol.CameraPreset) CameraPreset {
x.Radius = cp.Radius x.Radius = cp.Radius
x.AudioListener = cp.AudioListener x.AudioListener = cp.AudioListener
x.PlayerEffects = cp.PlayerEffects x.PlayerEffects = cp.PlayerEffects
x.AlignTargetAndCameraForward = cp.AlignTargetAndCameraForward
x.AimAssist = cp.AimAssist x.AimAssist = cp.AimAssist
x.ControlScheme = cp.ControlScheme x.ControlScheme = cp.ControlScheme
return *x return *x
@@ -96,29 +95,28 @@ func (x *CameraPreset) FromLatest(cp protocol.CameraPreset) CameraPreset {
func (x *CameraPreset) ToLatest() protocol.CameraPreset { func (x *CameraPreset) ToLatest() protocol.CameraPreset {
return protocol.CameraPreset{ return protocol.CameraPreset{
Name: x.Name, Name: x.Name,
Parent: x.Parent, Parent: x.Parent,
PosX: x.PosX, PosX: x.PosX,
PosY: x.PosY, PosY: x.PosY,
PosZ: x.PosZ, PosZ: x.PosZ,
RotX: x.RotX, RotX: x.RotX,
RotY: x.RotY, RotY: x.RotY,
RotationSpeed: x.RotationSpeed, RotationSpeed: x.RotationSpeed,
SnapToTarget: x.SnapToTarget, SnapToTarget: x.SnapToTarget,
HorizontalRotationLimit: x.HorizontalRotationLimit, HorizontalRotationLimit: x.HorizontalRotationLimit,
VerticalRotationLimit: x.VerticalRotationLimit, VerticalRotationLimit: x.VerticalRotationLimit,
ContinueTargeting: x.ContinueTargeting, ContinueTargeting: x.ContinueTargeting,
TrackingRadius: x.TrackingRadius, TrackingRadius: x.TrackingRadius,
ViewOffset: x.ViewOffset, ViewOffset: x.ViewOffset,
EntityOffset: x.EntityOffset, EntityOffset: x.EntityOffset,
Radius: x.Radius, Radius: x.Radius,
MinYawLimit: x.MinYawLimit, MinYawLimit: x.MinYawLimit,
MaxYawLimit: x.MaxYawLimit, MaxYawLimit: x.MaxYawLimit,
AudioListener: x.AudioListener, AudioListener: x.AudioListener,
PlayerEffects: x.PlayerEffects, PlayerEffects: x.PlayerEffects,
AlignTargetAndCameraForward: x.AlignTargetAndCameraForward, AimAssist: x.AimAssist,
AimAssist: x.AimAssist, ControlScheme: x.ControlScheme,
ControlScheme: x.ControlScheme,
} }
} }
@@ -154,7 +152,7 @@ func (x *CameraPreset) Marshal(r protocol.IO) {
protocol.OptionalFunc(r, &x.Radius, r.Float32) protocol.OptionalFunc(r, &x.Radius, r.Float32)
protocol.OptionalFunc(r, &x.AudioListener, r.Uint8) protocol.OptionalFunc(r, &x.AudioListener, r.Uint8)
protocol.OptionalFunc(r, &x.PlayerEffects, r.Bool) protocol.OptionalFunc(r, &x.PlayerEffects, r.Bool)
if IsProtoGTE(r, ID748) { if IsProtoGTE(r, ID748) && IsProtoLT(r, ID818) {
protocol.OptionalFunc(r, &x.AlignTargetAndCameraForward, r.Bool) protocol.OptionalFunc(r, &x.AlignTargetAndCameraForward, r.Bool)
} }
if IsProtoGTE(r, ID766) { if IsProtoGTE(r, ID766) {
@@ -164,3 +162,69 @@ func (x *CameraPreset) Marshal(r protocol.IO) {
protocol.OptionalFunc(r, &x.ControlScheme, r.Uint8) protocol.OptionalFunc(r, &x.ControlScheme, r.Uint8)
} }
} }
// CameraInstructionSet represents a camera instruction that sets the camera to a specified preset and can be extended
// with easing functions and translations to the camera's position and rotation.
type CameraInstructionSet struct {
// Preset is the index of the preset in the CameraPresets packet sent to the player.
Preset uint32
// Ease represents the easing function that is used by the instruction.
Ease protocol.Optional[protocol.CameraEase]
// Position represents the position of the camera.
Position protocol.Optional[mgl32.Vec3]
// Rotation represents the rotation of the camera.
Rotation protocol.Optional[mgl32.Vec2]
// Facing is a vector that the camera will always face towards during the duration of the instruction.
Facing protocol.Optional[mgl32.Vec3]
// ViewOffset is an offset based on a pivot point to the player, causing the camera to be shifted in a
// certain direction.
ViewOffset protocol.Optional[mgl32.Vec2]
// EntityOffset is an offset from the entity that the camera should be rendered at.
EntityOffset protocol.Optional[mgl32.Vec3]
// Default determines whether the camera is a default camera or not.
Default protocol.Optional[bool]
// IgnoreStartingValuesComponent behavior is currently unknown.
IgnoreStartingValuesComponent bool
}
func (x *CameraInstructionSet) FromLatest(cis protocol.CameraInstructionSet) CameraInstructionSet {
x.Preset = cis.Preset
x.Ease = cis.Ease
x.Position = cis.Position
x.Rotation = cis.Rotation
x.Facing = cis.Facing
x.ViewOffset = cis.ViewOffset
x.EntityOffset = cis.EntityOffset
x.Default = cis.Default
x.IgnoreStartingValuesComponent = cis.IgnoreStartingValuesComponent
return *x
}
func (x *CameraInstructionSet) ToLatest() protocol.CameraInstructionSet {
return protocol.CameraInstructionSet{
Preset: x.Preset,
Ease: x.Ease,
Position: x.Position,
Rotation: x.Rotation,
Facing: x.Facing,
ViewOffset: x.ViewOffset,
EntityOffset: x.EntityOffset,
Default: x.Default,
IgnoreStartingValuesComponent: x.IgnoreStartingValuesComponent,
}
}
// Marshal encodes/decodes a CameraInstructionSet.
func (x *CameraInstructionSet) Marshal(r protocol.IO) {
r.Uint32(&x.Preset)
protocol.OptionalMarshaler(r, &x.Ease)
protocol.OptionalFunc(r, &x.Position, r.Vec3)
protocol.OptionalFunc(r, &x.Rotation, r.Vec2)
protocol.OptionalFunc(r, &x.Facing, r.Vec3)
protocol.OptionalFunc(r, &x.ViewOffset, r.Vec2)
protocol.OptionalFunc(r, &x.EntityOffset, r.Vec3)
protocol.OptionalFunc(r, &x.Default, r.Bool)
if IsProtoGTE(r, ID818) {
r.Bool(&x.IgnoreStartingValuesComponent)
}
}

View File

@@ -382,8 +382,12 @@ func (p *Protocol) downgradePackets(pks []packet.Packet, conn *minecraft.Conn) [
EntityLink: (&proto.EntityLink{}).FromLatest(pk.EntityLink), EntityLink: (&proto.EntityLink{}).FromLatest(pk.EntityLink),
} }
case *packet.CameraInstruction: case *packet.CameraInstruction:
var iSet protocol.Optional[proto.CameraInstructionSet]
if v, ok := pk.Set.Value(); ok {
iSet = protocol.Option((&proto.CameraInstructionSet{}).FromLatest(v))
}
pks[pkIndex] = &legacypacket.CameraInstruction{ pks[pkIndex] = &legacypacket.CameraInstruction{
Set: pk.Set, Set: iSet,
Clear: pk.Clear, Clear: pk.Clear,
Fade: pk.Fade, Fade: pk.Fade,
Target: pk.Target, Target: pk.Target,
@@ -952,8 +956,12 @@ func (p *Protocol) upgradePackets(pks []packet.Packet, conn *minecraft.Conn) []p
EntityLink: pk.EntityLink.ToLatest(), EntityLink: pk.EntityLink.ToLatest(),
} }
case *legacypacket.CameraInstruction: case *legacypacket.CameraInstruction:
var iSet protocol.Optional[protocol.CameraInstructionSet]
if v, ok := pk.Set.Value(); ok {
iSet = protocol.Option(v.ToLatest())
}
pks[pkIndex] = &packet.CameraInstruction{ pks[pkIndex] = &packet.CameraInstruction{
Set: pk.Set, Set: iSet,
Clear: pk.Clear, Clear: pk.Clear,
Fade: pk.Fade, Fade: pk.Fade,
Target: pk.Target, Target: pk.Target,