-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
163 lines (128 loc) · 3.45 KB
/
player.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"crypto/rsa"
"github.com/mkorman9/go-minecraft-server/types"
"log"
"time"
)
type Player struct {
Name string
DisplayName *ChatMessage
UUID types.UUID
EntityID int32
IP string
PublicKey *rsa.PublicKey
PublicKeyDER []byte
Signature string
Timestamp int64
ClientSettings *PlayerClientSettings
X float64
Y float64
Z float64
Yaw float32
Pitch float32
OnGround bool
GameMode GameMode
Textures string
TexturesSignature string
Ping int
packetHandler *PlayerPacketHandler
world *World
lastKeepAliveID int64
lastHeartbeat time.Time
lastHeartbeatSent time.Time
}
type PlayerClientSettings struct {
Locale string
ViewDistance byte
ChatFlags int
ChatColors bool
SkinParts byte
MainHand int
EnableTextFiltering bool
EnableServerListing bool
}
func NewPlayer(world *World, ip string) *Player {
return &Player{
Name: "",
DisplayName: NewChatMessage(""),
UUID: types.GetRandomUUID(),
EntityID: -1,
IP: ip,
GameMode: GameModeUnknown,
world: world,
}
}
func (p *Player) Kick(reason *ChatMessage) {
p.packetHandler.Cancel(reason)
}
func (p *Player) AssignPacketHandler(packetHandler *PlayerPacketHandler) {
p.packetHandler = packetHandler
}
func (p *Player) SendSystemChatMessage(message *ChatMessage) {
err := p.packetHandler.SendSystemChatMessage(message)
if err != nil {
log.Printf("Failed to send system chat message: %v\n", err)
}
}
func (p *Player) SetPosition(x, y, z float64) {
p.X = x
p.Y = y
p.Z = z
_ = p.packetHandler.SynchronizePosition(x, y, z)
}
func (p *Player) SendKeepAlive(keepAliveID int64) {
p.lastKeepAliveID = keepAliveID
p.lastHeartbeatSent = time.Now()
_ = p.packetHandler.SendKeepAlive(keepAliveID)
}
func (p *Player) SendAnotherPlayerJoined(player *Player) {
_ = p.packetHandler.sendPlayersAdded([]*Player{player})
}
func (p *Player) SendAnotherPlayerDisconnected(player *Player) {
_ = p.packetHandler.sendPlayersRemoved([]*Player{player})
}
func (p *Player) OnJoin(gameMode GameMode) {
p.world.BroadcastPlayerJoined(p)
p.world.JoinPlayer(p)
p.GameMode = gameMode
p.lastHeartbeat = time.Now()
}
func (p *Player) OnDisconnect() {
p.world.RemovePlayer(p)
p.world.BroadcastPlayerDisconnected(p)
}
func (p *Player) OnClientSettings(clientSettings *PlayerClientSettings) {
p.ClientSettings = clientSettings
}
func (p *Player) OnPositionUpdate(x float64, y float64, z float64) {
p.X = x
p.Y = y
p.Z = z
}
func (p *Player) OnLookUpdate(yaw float32, pitch float32) {
p.Yaw = yaw
p.Pitch = pitch
}
func (p *Player) OnGroundUpdate(onGround bool) {
p.OnGround = onGround
}
func (p *Player) OnPluginChannel(channel string, data []byte) {
}
func (p *Player) OnArmAnimation(hand int) {
}
func (p *Player) OnCloseWindow(windowId byte) {
}
func (p *Player) OnChatCommand(command string, timestamp time.Time) {
}
func (p *Player) OnChatMessage(message string, timestamp time.Time) {
}
func (p *Player) OnKeepAliveResponse(keepAliveID int64) {
if keepAliveID == p.lastKeepAliveID {
p.lastHeartbeat = time.Now()
ping := time.Now().Sub(p.lastHeartbeatSent)
p.Ping = int(ping / time.Millisecond)
}
}
func (p *Player) OnAction(entityID int, actionID EntityAction, jumpBoost int) {
}