center map, request all subchunks

This commit is contained in:
olebeck 2022-08-12 04:53:43 +02:00
parent dea7d36504
commit cca8aa0cf6
8 changed files with 138 additions and 93 deletions

View File

@ -84,7 +84,7 @@ func packets_main(ctx context.Context, args []string) error {
}
var hostname string
hostname, server = server_input(ctx, server)
hostname, server = server_input(server)
_status := minecraft.NewStatusProvider("Server")
listener, err := minecraft.ListenConfig{

View File

@ -1,37 +1,24 @@
package main
import (
"hash/crc32"
"image"
"image/color"
"github.com/df-mc/dragonfly/server/world/chunk"
)
func i32tob(val uint32) []byte {
r := make([]byte, 4)
for i := uint32(0); i < 4; i++ {
r[i] = byte((val >> (8 * i)) & 0xff)
}
return r
}
func calcColor(clr int) color.RGBA {
return color.RGBA{
R: uint8((clr >> 24) & 0xFF),
G: uint8((clr >> 16) & 0xFF),
B: uint8((clr >> 8) & 0xFF),
A: 255,
}
}
func Chunk2Img(c *chunk.Chunk) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, 16, 16))
for x := uint8(0); x < 16; x++ {
for z := uint8(0); z < 16; z++ {
block_rid := c.Block(x, c.HighestBlock(x, z), z, 0)
col := crc32.ChecksumIEEE(i32tob(uint32(block_rid)))
img.SetRGBA(int(x), int(z), calcColor(int(col)))
height := c.HighestBlock(x, z)
block_rid := c.Block(x, height, z, 0)
img.SetRGBA(int(x), int(z), color.RGBA{
R: uint8(height),
G: uint8(block_rid & 0xFF),
B: uint8((block_rid >> 8) & 0xFF),
A: 255,
})
}
}
return img

9
go.mod
View File

@ -5,9 +5,11 @@ go 1.19
require (
github.com/df-mc/dragonfly v0.8.1
github.com/df-mc/goleveldb v1.1.9
github.com/go-gl/mathgl v1.0.0
github.com/google/gopacket v1.1.19
github.com/sandertv/gophertunnel v1.24.0
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539
golang.org/x/oauth2 v0.0.0-20220808172628-8227340efae7
)
@ -16,23 +18,20 @@ require (
replace github.com/sandertv/gophertunnel => github.com/olebeck/gophertunnel v1.24.2
replace github.com/df-mc/dragonfly => github.com/olebeck/dragonfly v0.8.1-1
require (
github.com/brentp/intintmap v0.0.0-20190211203843-30dc0ade9af9 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/df-mc/atomic v1.10.0 // indirect
github.com/go-gl/mathgl v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/muhammadmuzzammil1998/jsonc v1.0.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/rogpeppe/go-internal v1.3.0 // indirect
github.com/sandertv/go-raknet v1.11.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/image v0.0.0-20220722155232-062f8c9fd539 // indirect
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced // indirect
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
golang.org/x/text v0.3.7 // indirect

19
go.sum
View File

@ -1,8 +1,5 @@
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/brentp/intintmap v0.0.0-20190211203843-30dc0ade9af9 h1:/G0ghZwrhou0Wq21qc1vXXMm/t/aKWkALWwITptKbE0=
github.com/brentp/intintmap v0.0.0-20190211203843-30dc0ade9af9/go.mod h1:TOk10ahXejq9wkEaym3KPRNeuR/h5Jx+s8QRWIa2oTM=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -34,11 +31,10 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/muhammadmuzzammil1998/jsonc v1.0.0 h1:8o5gBQn4ZA3NBA9DlTujCj2a4w0tqWrPVjDwhzkgTIs=
github.com/muhammadmuzzammil1998/jsonc v1.0.0/go.mod h1:saF2fIVw4banK0H4+/EuqfFLpRnoy5S+ECwTOCcRcSU=
github.com/olebeck/dragonfly v0.8.1-1 h1:D/JJ4wUMvjw7Ao5jQjBgyvNpfnb0tYytIse3AiH6K8k=
github.com/olebeck/dragonfly v0.8.1-1/go.mod h1:xgpCDhHoP03RygPaTrzzDwsSTcEZhxNPMV3CAxETj+I=
github.com/olebeck/gophertunnel v1.24.2 h1:T22XL6m8tJdVGoy7jMEi1d44+xVp+0x7dXsJr/sCJm8=
github.com/olebeck/gophertunnel v1.24.2/go.mod h1:dMOw79FHxr2azEqiGH20AwdljisAN1kqwu5SjPBnZ5k=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@ -46,17 +42,12 @@ github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.3.0 h1:RR9dF3JtopPvtkroDZuVD7qquD0bnHlKSqaQhgwt8yk=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/sandertv/go-raknet v1.11.1 h1:0auvhHoZNyC/Z1l5xqniE3JE+w3MGO3n3JXEGHzdlRE=
github.com/sandertv/go-raknet v1.11.1/go.mod h1:Gx+WgZBMQ0V2UoouGoJ8Wj6CDrMBQ4SB2F/ggpl5/+Y=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
@ -80,8 +71,6 @@ golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced h1:3dYNDff0VT5xj+mbj2XucFst9WKk6PdGOrb9n+SbIvw=
golang.org/x/net v0.0.0-20220809184613-07c6da5e1ced/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20220811182439-13a9a731de15 h1:cik0bxZUSJVDyaHf1hZPSDsU8SZHGQZQMeueXCE7yBQ=
golang.org/x/net v0.0.0-20220811182439-13a9a731de15/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/oauth2 v0.0.0-20220808172628-8227340efae7 h1:dtndE8FcEta75/4kHF3AbpuWzV6f1LjnLrM4pe2SZrw=
golang.org/x/oauth2 v0.0.0-20220808172628-8227340efae7/go.mod h1:h4gKUeWbJ4rQPri7E0u6Gs4e9Ri2zaLxzw5DI5XGrYg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -92,8 +81,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 h1:v1W7bwXHsnLLloWYTVEdvGvA7BHMeBYsPcF0GLDxIRs=
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
@ -109,8 +96,6 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI=

View File

@ -46,7 +46,7 @@ var muted_packets = []string{
"*packet.MovePlayer",
"*packet.BlockActorData",
"*packet.PlayerAuthInput",
//"*packet.LevelChunk",
"*packet.LevelChunk",
"*packet.LevelSoundEvent",
"*packet.ActorEvent",
"*packet.NetworkChunkPublisherUpdate",

View File

@ -1,13 +1,18 @@
package main
import (
"bytes"
"image"
"image/color"
"image/draw"
"math"
"os"
"sync"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
"golang.org/x/image/bmp"
)
const VIEW_MAP_ID = 0x424242
@ -37,6 +42,7 @@ func (w *WorldState) draw_map() {
// get the chunk coord bounds
min := protocol.ChunkPos{}
max := protocol.ChunkPos{}
middle := protocol.ChunkPos{}
for _ch := range w.chunks {
if _ch.X() < min.X() {
min[0] = _ch.X()
@ -50,38 +56,53 @@ func (w *WorldState) draw_map() {
if _ch.Z() > max.Z() {
max[1] = _ch.Z()
}
if _ch.X() == int32(w.PlayerPos.Position.X()/16) && _ch.Z() == int32(w.PlayerPos.Position.Z()/16) {
middle = _ch
}
}
px_per_chunk := 128 / int(max[0]-min[0]+1)
chunks_x := int(max[0] - min[0] + 1) // how many chunk lengths is x
chunks_per_line := math.Min(float64(chunks_x), 64) // at max 64 chunks per line
px_per_chunk := int(128 / chunks_per_line) // how many pixels does every chunk get
for i := 0; i < len(w.img.Pix); i++ { // clear canvas
w.img.Pix[i] = 0
}
for _ch := range w.chunks {
px_pos := image.Point{X: int(_ch.X() - min.X()), Y: int(_ch.Z() - min.Z())}
draw.Draw(
w.img,
image.Rect(
px_pos.X*px_per_chunk,
px_pos.Y*px_per_chunk,
(px_pos.X+1)*px_per_chunk,
(px_pos.Y+1)*px_per_chunk,
),
w.chunks_images[_ch],
image.Point{},
draw.Src,
)
for _ch := range w.chunks_images {
px_pos := image.Point{
X: (int(_ch.X()-middle.X()) * px_per_chunk) + 64,
Y: (int(_ch.Z()-middle.Z()) * px_per_chunk) + 64,
}
if px_pos.In(w.img.Rect) {
draw.Draw(
w.img,
image.Rect(
px_pos.X,
px_pos.Y,
px_pos.X+px_per_chunk,
px_pos.Y+px_per_chunk,
),
w.chunks_images[_ch],
image.Point{},
draw.Src,
)
}
}
{
buf := bytes.NewBuffer(nil)
bmp.Encode(buf, w.img)
os.WriteFile("test.bmp", buf.Bytes(), 0777)
}
}
var _map_send_lock = false
var _map_send_lock = sync.Mutex{}
func (w *WorldState) send_map_update(conn *minecraft.Conn) error {
if _map_send_lock {
if !_map_send_lock.TryLock() {
return nil
}
_map_send_lock = true
if w.needRedraw {
w.needRedraw = false
@ -96,7 +117,7 @@ func (w *WorldState) send_map_update(conn *minecraft.Conn) error {
}
}
_map_send_lock = false
_map_send_lock.Unlock()
return conn.WritePacket(&packet.ClientBoundMapItemData{
MapID: VIEW_MAP_ID,
Width: 128,

View File

@ -49,7 +49,7 @@ func get_token() oauth2.Token {
return token
}
func server_input(ctx context.Context, server string) (string, string) {
func server_input(server string) (string, string) {
if server == "" {
fmt.Printf("Enter Server: ")
reader := bufio.NewReader(os.Stdin)
@ -75,7 +75,7 @@ func connect_server(ctx context.Context, server string) (hostname string, conn *
return "", nil, err
}
} else {
hostname, server = server_input(ctx, server)
hostname, server = server_input(server)
}
var packet_func func(header packet.Header, payload []byte, src, dst net.Addr) = nil

107
world.go
View File

@ -17,18 +17,26 @@ import (
"github.com/df-mc/dragonfly/server/world/chunk"
"github.com/df-mc/dragonfly/server/world/mcdb"
"github.com/df-mc/goleveldb/leveldb/opt"
"github.com/go-gl/mathgl/mgl32"
"github.com/sandertv/gophertunnel/minecraft"
"github.com/sandertv/gophertunnel/minecraft/protocol"
"github.com/sandertv/gophertunnel/minecraft/protocol/packet"
)
type TPlayerPos struct {
Position mgl32.Vec3
Pitch float32
Yaw float32
HeadYaw float32
}
// the state used for drawing and saving
type WorldState struct {
Provider *mcdb.Provider // provider for the current world
chunks map[protocol.ChunkPos]*chunk.Chunk
Dim world.Dimension
WorldName string
PlayerPos packet.MovePlayer
PlayerPos TPlayerPos
// ui
img *image.RGBA
@ -41,7 +49,7 @@ var __world_state *WorldState = &WorldState{
chunks: make(map[protocol.ChunkPos]*chunk.Chunk),
Dim: nil,
WorldName: "world",
PlayerPos: packet.MovePlayer{},
PlayerPos: TPlayerPos{},
img: image.NewRGBA(image.Rect(0, 0, 128, 128)),
chunks_images: make(map[protocol.ChunkPos]*image.RGBA),
needRedraw: true,
@ -61,7 +69,7 @@ func world_main(ctx context.Context, args []string) error {
server = args[0]
args = args[1:]
}
_, server = server_input(ctx, server)
_, server = server_input(server)
flag.CommandLine.Parse(args)
if G_help {
@ -103,34 +111,67 @@ func (w *WorldState) ProcessLevelChunk(pk *packet.LevelChunk, serverConn *minecr
log.Print(err.Error())
return
}
// perhaps just update the current chunk instead of overwrite
w.chunks[pk.Position] = ch
if pk.SubChunkCount == 0 { // no sub chunks = no blocks known
existing := w.chunks[pk.Position]
if existing == nil {
w.chunks[pk.Position] = ch
w.chunks_images[pk.Position] = black_16x16
} else {
}
if pk.SubChunkRequestMode == protocol.SubChunkRequestModeLegacy {
w.chunks_images[pk.Position] = Chunk2Img(ch)
} else {
// request all the subchunks
var Offset_table = [][3]int8{
{0, 0, 0}, {0, 1, 0}, {0, 2, 0}, {0, 3, 0},
{0, 4, 0}, {0, 5, 0}, {0, 6, 0}, {0, 7, 0},
{0, 8, 0}, {0, 9, 0}, {0, 10, 0}, {0, 11, 0},
{0, 12, 0}, {0, 13, 0}, {0, 14, 0}, {0, 15, 0},
{0, 16, 0}, {0, 17, 0}, {0, 18, 0}, {0, 19, 0},
{0, 20, 0}, {0, 21, 0}, {0, 22, 0}, {0, 23, 0},
}
max := len(Offset_table) - 1
if pk.SubChunkRequestMode == protocol.SubChunkRequestModeLimited {
max = int(pk.HighestSubChunk)
}
serverConn.WritePacket(&packet.SubChunkRequest{
Dimension: int32(w.Dim.EncodeDimension()),
Position: protocol.SubChunkPos{
pk.Position.X(), 0, pk.Position.Z(),
},
Offsets: Offset_table[:max],
})
}
w.needRedraw = true
}
func (w *WorldState) ProcessSubChunk(pk *packet.SubChunk) {
pos := protocol.ChunkPos{pk.Position.X(), pk.Position.Z()}
// get or create chunk ptr
ch := w.chunks[pos]
if ch == nil { // create an empty chunk if for some reason the server didnt send the chunk before the subchunk
fmt.Printf("the server didnt send the chunk before the subchunk!\n")
ch = chunk.New(0, w.Dim.Range())
w.chunks[pos] = ch
pos_to_redraw := make(map[protocol.ChunkPos]bool)
for _, sub := range pk.SubChunkEntries {
abs_x := pk.Position[0] + int32(sub.Offset[0])
abs_y := pk.Position[1] + int32(sub.Offset[1])
abs_z := pk.Position[2] + int32(sub.Offset[2])
pos := protocol.ChunkPos{abs_x, abs_z}
ch := w.chunks[pos]
if ch == nil {
fmt.Printf("the server didnt send the chunk before the subchunk!\n")
continue
}
err := ch.ApplySubChunkEntry(int(abs_y), &sub)
if err != nil {
fmt.Print(err)
}
pos_to_redraw[pos] = true
}
// redraw the chunks
for pos := range pos_to_redraw {
w.chunks_images[pos] = Chunk2Img(w.chunks[pos])
w.needRedraw = true
}
// add the new subs
//err := ch.ApplySubChunkEntries(int16(pk.Position.Y()), pk.SubChunkEntries)
//if err != nil {
// fmt.Print(err)
//}
// redraw the chunk
w.chunks_images[pos] = Chunk2Img(ch)
w.needRedraw = true
}
func (w *WorldState) ProcessChangeDimension(pk *packet.ChangeDimension) {
@ -150,8 +191,13 @@ func (w *WorldState) ProcessChangeDimension(pk *packet.ChangeDimension) {
w.needRedraw = true
}
func (w *WorldState) ProcessMove(player *packet.MovePlayer) {
w.PlayerPos = *player
func (w *WorldState) SetPlayerPos(Position mgl32.Vec3, Pitch, Yaw, HeadYaw float32) {
w.PlayerPos = TPlayerPos{
Position: Position,
Pitch: Pitch,
Yaw: Yaw,
HeadYaw: HeadYaw,
}
}
func handleConn(ctx context.Context, conn *minecraft.Conn, listener *minecraft.Listener, target string) {
@ -199,12 +245,18 @@ func handleConn(ctx context.Context, conn *minecraft.Conn, listener *minecraft.L
switch pk := pk.(type) {
case *packet.MovePlayer:
__world_state.ProcessMove(pk)
__world_state.SetPlayerPos(pk.Position, pk.Pitch, pk.Yaw, pk.HeadYaw)
case *packet.PlayerAuthInput:
__world_state.SetPlayerPos(pk.Position, pk.Pitch, pk.Yaw, pk.HeadYaw)
case *packet.MapInfoRequest:
if pk.MapID == VIEW_MAP_ID {
__world_state.send_map_update(conn)
skip = true
}
case *packet.ClientCacheStatus:
pk.Enabled = false
serverConn.WritePacket(pk)
skip = true
}
if !skip {
@ -260,7 +312,8 @@ func handleConn(ctx context.Context, conn *minecraft.Conn, listener *minecraft.L
case *packet.LevelChunk:
__world_state.ProcessLevelChunk(pk, serverConn)
__world_state.send_map_update(conn)
send_popup(conn, fmt.Sprintf("%d chunks loaded\n", len(__world_state.chunks)))
p := __world_state.PlayerPos.Position
send_popup(conn, fmt.Sprintf("%d chunks loaded\npos: %.02f %.02f %.02f\n", len(__world_state.chunks), p.X(), p.Y(), p.Z()))
case *packet.SubChunk:
__world_state.ProcessSubChunk(pk)
}