This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
83 lines (70 loc) · 1.7 KB
/
generator.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
package gen
import (
"github.com/df-mc/dragonfly/dragonfly/world"
"github.com/df-mc/dragonfly/dragonfly/world/chunk"
"github.com/go-gl/mathgl/mgl32"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"time"
)
type Generator struct {
c *Client
}
func NewGenerator(c *Client) Generator {
return Generator{c: c}
}
func (g Generator) GenerateChunk(pos world.ChunkPos, ch *chunk.Chunk) {
c, ok := g.c.Chunk(pos)
if ok {
decodedChunk := decodeNetworkChunk(c)
//noinspection GoVetCopyLock
*ch = *decodedChunk
return
}
teleportPos := mgl32.Vec3{float32(pos.X()<<4) + 10.0, 255.0, float32(pos.Z()<<4) + 10.0}
_ = g.c.conn.WritePacket(&packet.MovePlayer{
EntityRuntimeID: g.c.id,
Position: teleportPos,
})
cha := make(chan ChunkData, 1)
defer close(cha)
g.c.ExpectChunk(pos, cha)
t := time.After(1 * time.Second)
for {
select {
case data := <-cha:
decodedChunk := decodeNetworkChunk(data)
//noinspection GoVetCopyLock
*ch = *decodedChunk
return
case <-t:
g.c.StopExpecting(pos)
g.c.log.Warnln("Failed to get chunk", pos, "from BDS.")
return
default:
_ = g.c.conn.WritePacket(&packet.MovePlayer{
EntityRuntimeID: g.c.id,
Position: teleportPos,
})
}
}
}
func decodeNetworkChunk(data ChunkData) *chunk.Chunk {
c, _ := chunk.NetworkDecode(data.Payload, int(data.SubChunkCount))
for _, s := range c.Sub() {
if s == nil {
continue
}
for _, l := range s.Layers() {
l.Palette().Replace(func(r uint32) uint32 {
block := runtimeIdToState[r]
runtimeId, ok := chunk.StateToRuntimeID(block.Name, block.States)
if ok {
return runtimeId
}
// return old runtime id because fukkit
return r
})
}
}
return c
}