-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathhandler_player_action.go
77 lines (69 loc) · 2.76 KB
/
handler_player_action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package session
import (
"fmt"
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/world"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)
// PlayerActionHandler handles the PlayerAction packet.
type PlayerActionHandler struct{}
// Handle ...
func (*PlayerActionHandler) Handle(p packet.Packet, s *Session, _ *world.Tx, c Controllable) error {
pk := p.(*packet.PlayerAction)
return handlePlayerAction(pk.ActionType, pk.BlockFace, pk.BlockPosition, pk.EntityRuntimeID, s, c)
}
// handlePlayerAction handles an action performed by a player, found in packet.PlayerAction and packet.PlayerAuthInput.
func handlePlayerAction(action int32, face int32, pos protocol.BlockPos, entityRuntimeID uint64, s *Session, c Controllable) error {
if entityRuntimeID != selfEntityRuntimeID {
return errSelfRuntimeID
}
switch action {
case protocol.PlayerActionRespawn, protocol.PlayerActionDimensionChangeDone:
// Don't do anything for these actions.
case protocol.PlayerActionStopSleeping:
if mode := c.GameMode(); !mode.Visible() && !mode.HasCollision() {
// As of v1.19.50, the client sends this packet when switching to spectator mode... even if it wasn't
// sleeping in the first place. This accounts for that.
return nil
}
case protocol.PlayerActionStartBreak, protocol.PlayerActionContinueDestroyBlock:
s.swingingArm.Store(true)
defer s.swingingArm.Store(false)
s.breakingPos = cube.Pos{int(pos[0]), int(pos[1]), int(pos[2])}
c.StartBreaking(s.breakingPos, cube.Face(face))
case protocol.PlayerActionAbortBreak:
c.AbortBreaking()
case protocol.PlayerActionPredictDestroyBlock, protocol.PlayerActionStopBreak:
s.swingingArm.Store(true)
defer s.swingingArm.Store(false)
c.FinishBreaking()
case protocol.PlayerActionCrackBreak:
s.swingingArm.Store(true)
defer s.swingingArm.Store(false)
newPos := cube.Pos{int(pos[0]), int(pos[1]), int(pos[2])}
// Sometimes no new position will be sent using a StartBreak action, so we need to detect a change in the
// block to be broken by comparing positions.
if newPos != s.breakingPos {
s.breakingPos = newPos
c.StartBreaking(newPos, cube.Face(face))
return nil
}
c.ContinueBreaking(cube.Face(face))
case protocol.PlayerActionStartItemUseOn:
// TODO: Properly utilize these actions.
case protocol.PlayerActionStopItemUseOn:
c.ReleaseItem()
case protocol.PlayerActionStartBuildingBlock:
// Don't do anything for this action.
case protocol.PlayerActionCreativePlayerDestroyBlock:
// Don't do anything for this action.
case protocol.PlayerActionMissedSwing:
s.swingingArm.Store(true)
defer s.swingingArm.Store(false)
c.PunchAir()
default:
return fmt.Errorf("unhandled ActionType %v", action)
}
return nil
}