Repository navigation
Expand file tree
/
Copy pathplayer_transfer.go
More file actions
198 lines (184 loc) · 5.79 KB
/
Copy pathplayer_transfer.go
File metadata and controls
198 lines (184 loc) · 5.79 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package framework
import (
"context"
"errors"
"sync"
"github.com/bedrock-gophers/plugins/internal/native"
"github.com/df-mc/dragonfly/server/player"
"github.com/df-mc/dragonfly/server/world"
"github.com/go-gl/mathgl/mgl64"
)
// worldTransferLeases keeps both managed worlds open until a player handle is
// attached to one of them. release is safe from all completion paths.
type worldTransferLeases struct {
once sync.Once
source *managedWorld
destination *managedWorld
}
func (l *worldTransferLeases) release() {
l.once.Do(func() {
if l.destination != l.source {
l.destination.lifecycle.RUnlock()
}
l.source.lifecycle.RUnlock()
})
}
// TransferPlayer moves a registered player to a managed world. Cross-world
// movement is deferred until the current plugin callback has finished so all
// later player actions in that callback retain a valid source transaction.
func (m *WorldManager) TransferPlayer(invocation native.InvocationID, id native.PlayerID, destinationID native.WorldID, position native.Vec3) bool {
if m.players == nil || !finiteVec3(position) {
return false
}
handle, ok := m.players.Handle(id)
if !ok {
return false
}
if invocation == 0 {
if _, ok := m.entryByHandle(destinationID); !ok {
return false
}
accepted := false
task := handle.Do(func(tx *world.Tx, entity world.Entity) {
connected, playerOK := entity.(*player.Player)
if playerOK {
accepted = m.transferPlayerFromTransaction(tx, connected, handle, destinationID, position)
}
})
return task.Wait(context.Background()) == nil && accepted
}
tx, ok := m.players.InvocationTx(invocation)
if !ok {
return false
}
connected, ok := transferPlayerInTransaction(handle, tx)
if !ok {
return false
}
return m.transferPlayerFromTransaction(tx, connected, handle, destinationID, position)
}
func (m *WorldManager) transferPlayerFromTransaction(tx *world.Tx, connected *player.Player, handle *world.EntityHandle, destinationID native.WorldID, position native.Vec3) bool {
sourceWorld, ok := transactionWorld(tx)
if !ok {
return false
}
source, destination, leases, ok := m.acquireTransferLeases(sourceWorld, destinationID)
if !ok {
return false
}
target := mgl64.Vec3{position.X, position.Y, position.Z}
if source == destination {
defer leases.release()
connected.Teleport(target)
return true
}
tx.Defer(func(sourceTx *world.Tx) {
m.runDeferredPlayerTransfer(sourceTx, handle, source, destination, target, leases)
})
return true
}
func (m *WorldManager) acquireTransferLeases(sourceWorld *world.World, destinationID native.WorldID) (*managedWorld, *managedWorld, *worldTransferLeases, bool) {
m.mu.RLock()
defer m.mu.RUnlock()
source := m.byWorld[sourceWorld]
destination := m.handles[destinationID]
if source == nil || destination == nil || source.unloading || destination.unloading {
return nil, nil, nil, false
}
if !source.lifecycle.TryRLock() {
return nil, nil, nil, false
}
if destination != source && !destination.lifecycle.TryRLock() {
source.lifecycle.RUnlock()
return nil, nil, nil, false
}
if source.closed || destination.closed {
if destination != source {
destination.lifecycle.RUnlock()
}
source.lifecycle.RUnlock()
return nil, nil, nil, false
}
leases := &worldTransferLeases{source: source, destination: destination}
return source, destination, leases, true
}
func (m *WorldManager) runDeferredPlayerTransfer(sourceTx *world.Tx, handle *world.EntityHandle, source, destination *managedWorld, position mgl64.Vec3, leases *worldTransferLeases) {
deferredActive := true
defer func() {
if recover() != nil || deferredActive {
leases.release()
}
}()
connected, ok := transferPlayerInTransaction(handle, sourceTx)
if !ok || sourceTx.World() != source.world || handle.Closed() {
return
}
if removed := sourceTx.RemoveEntity(connected); removed != handle {
return
}
task := destination.world.Do(func(destinationTx *world.Tx) {
if !handle.Closed() {
destinationTx.AddEntityAt(handle, position)
}
})
deferredActive = false
select {
case <-task.Done():
m.finishPlayerTransfer(task.Err(), sourceTx, handle, source, leases)
default:
task.OnDone(func(err error) {
m.finishPlayerTransfer(err, nil, handle, source, leases)
})
}
}
func (m *WorldManager) finishPlayerTransfer(err error, sourceTx *world.Tx, handle *world.EntityHandle, source *managedWorld, leases *worldTransferLeases) {
if !errors.Is(err, world.ErrWorldClosed) {
leases.release()
return
}
if sourceTx != nil {
m.restoreTransferredPlayer(sourceTx, handle)
leases.release()
return
}
task := source.world.Do(func(tx *world.Tx) {
m.restoreTransferredPlayer(tx, handle)
})
select {
case <-task.Done():
m.finishPlayerRestore(task.Err(), handle, source, leases)
default:
task.OnDone(func(err error) { m.finishPlayerRestore(err, handle, source, leases) })
}
}
func (m *WorldManager) finishPlayerRestore(err error, handle *world.EntityHandle, source *managedWorld, leases *worldTransferLeases) {
if errors.Is(err, world.ErrWorldClosed) || errors.Is(err, world.ErrTaskCancelled) || errors.Is(err, world.ErrEntityClosed) {
m.players.UnregisterHandle(handle)
_ = handle.Close()
m.log.Error("player transfer lost both worlds; evicted player handle", "source", source.name, "error", err)
}
leases.release()
}
func (m *WorldManager) restoreTransferredPlayer(tx *world.Tx, handle *world.EntityHandle) {
if handle.Closed() {
return
}
tx.AddEntity(handle)
m.players.ForgetWorldDeparture(handle)
}
func transferPlayerInTransaction(handle *world.EntityHandle, tx *world.Tx) (connected *player.Player, ok bool) {
if handle == nil || tx == nil {
return nil, false
}
defer func() {
if recover() != nil {
connected, ok = nil, false
}
}()
entity, ok := handle.Entity(tx)
if !ok {
return nil, false
}
connected, ok = entity.(*player.Player)
return connected, ok
}