-
Notifications
You must be signed in to change notification settings - Fork 141
/
handler_block_actor_data.go
111 lines (102 loc) · 3.47 KB
/
handler_block_actor_data.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package session
import (
"fmt"
"github.com/df-mc/dragonfly/server/block"
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/entity"
"github.com/go-gl/mathgl/mgl64"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"strings"
"unicode/utf8"
)
// BlockActorDataHandler handles an incoming BlockActorData packet from the client, sent for some block entities like
// signs when they are edited.
type BlockActorDataHandler struct{}
// Handle ...
func (b BlockActorDataHandler) Handle(p packet.Packet, s *Session) error {
pk := p.(*packet.BlockActorData)
if id, ok := pk.NBTData["id"]; ok {
pos := blockPosFromProtocol(pk.Position)
if !canReach(s.c, pos.Vec3Middle()) {
return fmt.Errorf("block at %v is not within reach", pos)
}
switch id {
case "Sign":
return b.handleSign(pk, pos, s)
}
return fmt.Errorf("unhandled block actor data ID %v", id)
}
return fmt.Errorf("block actor data without 'id' tag: %v", pk.NBTData)
}
// handleSign handles the BlockActorData packet sent when editing a sign.
func (b BlockActorDataHandler) handleSign(pk *packet.BlockActorData, pos cube.Pos, s *Session) error {
if _, ok := s.c.World().Block(pos).(block.Sign); !ok {
s.log.Debugf("sign block actor data for position without sign %v", pos)
return nil
}
frontText, err := b.textFromNBTData(pk.NBTData, true)
if err != nil {
return err
}
backText, err := b.textFromNBTData(pk.NBTData, false)
if err != nil {
return err
}
if err := s.c.EditSign(pos, frontText, backText); err != nil {
return err
}
return nil
}
// textFromNBTData attempts to retrieve the text from the NBT data of specific sign from the BlockActorData packet.
func (b BlockActorDataHandler) textFromNBTData(data map[string]any, frontSide bool) (string, error) {
var sideData map[string]any
var side string
if frontSide {
frontSide, ok := data["FrontText"].(map[string]any)
if !ok {
return "", fmt.Errorf("sign block actor data 'FrontText' tag was not found or was not a map: %#v", data["FrontText"])
}
sideData = frontSide
side = "front"
} else {
backSide, ok := data["BackText"].(map[string]any)
if !ok {
return "", fmt.Errorf("sign block actor data 'BackText' tag was not found or was not a map: %#v", data["BackText"])
}
sideData = backSide
side = "back"
}
var text string
pkText, ok := sideData["Text"]
if !ok {
return "", fmt.Errorf("sign block actor data had no 'Text' tag for side %s", side)
}
if text, ok = pkText.(string); !ok {
return "", fmt.Errorf("sign block actor data 'Text' tag was not a string for side %s: %#v", side, pkText)
}
// Verify that the text was valid. It must be valid UTF8 and not more than 100 characters long.
text = strings.TrimRight(text, "\n")
if len(text) > 256 {
return "", fmt.Errorf("sign block actor data text was longer than 256 characters for side %s", side)
}
if !utf8.ValidString(text) {
return "", fmt.Errorf("sign block actor data text was not valid UTF8 for side %s", side)
}
return text, nil
}
// canReach checks if a player can reach a position with its current range. The range depends on if the player
// is either survival or creative mode.
func canReach(c Controllable, pos mgl64.Vec3) bool {
const (
creativeRange = 14.0
survivalRange = 8.0
)
if !c.GameMode().AllowsInteraction() {
return false
}
eyes := entity.EyePosition(c)
if c.GameMode().CreativeInventory() {
return eyes.Sub(pos).Len() <= creativeRange && !c.Dead()
}
return eyes.Sub(pos).Len() <= survivalRange && !c.Dead()
}