From 2761cf094c7f084bfbba42f1450d005033ab5078 Mon Sep 17 00:00:00 2001 From: olebeck <31539311+olebeck@users.noreply.github.com> Date: Sun, 30 Apr 2023 19:35:18 +0200 Subject: [PATCH] fix block entities --- go.mod | 4 ++-- go.sum | 8 ++----- handlers/worlds/chunk.go | 20 +++++++++------- handlers/worlds/items.go | 4 +++- handlers/worlds/world.go | 51 ++++++++++++++++++++++++++++++++++------ 5 files changed, 62 insertions(+), 25 deletions(-) diff --git a/go.mod b/go.mod index e95870b..4eeeda3 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ go 1.20 replace github.com/sandertv/gophertunnel => github.com/olebeck/gophertunnel v1.29.0-1 //replace github.com/df-mc/dragonfly => ./dragonfly -replace github.com/df-mc/dragonfly => github.com/olebeck/dragonfly v0.9.4-12 +replace github.com/df-mc/dragonfly => github.com/olebeck/dragonfly v0.9.4-13 //replace gioui.org => ./gio replace gioui.org => github.com/olebeck/gio v0.0.0-20230427194143-c9c9d8bc704d @@ -28,7 +28,7 @@ require ( github.com/repeale/fp-go v0.11.1 github.com/sanbornm/go-selfupdate v0.0.0-20210106163404-c9b625feac49 github.com/sandertv/go-raknet v1.12.0 - github.com/sandertv/gophertunnel v1.28.2 + github.com/sandertv/gophertunnel v1.29.0 github.com/shirou/gopsutil/v3 v3.23.3 github.com/sirupsen/logrus v1.9.0 golang.design/x/lockfree v0.0.1 diff --git a/go.sum b/go.sum index 43ef327..78dcb41 100644 --- a/go.sum +++ b/go.sum @@ -85,14 +85,10 @@ github.com/muhammadmuzzammil1998/jsonc v1.0.0 h1:8o5gBQn4ZA3NBA9DlTujCj2a4w0tqWr github.com/muhammadmuzzammil1998/jsonc v1.0.0/go.mod h1:saF2fIVw4banK0H4+/EuqfFLpRnoy5S+ECwTOCcRcSU= github.com/nicksnyder/go-i18n/v2 v2.2.1 h1:aOzRCdwsJuoExfZhoiXHy4bjruwCMdt5otbYojM/PaA= github.com/nicksnyder/go-i18n/v2 v2.2.1/go.mod h1:fF2++lPHlo+/kPaj3nB0uxtPwzlPm+BlgwGX7MkeGj0= -github.com/olebeck/dragonfly v0.9.4-11 h1:FkDKWUT1Kz6WRRxCLmCcFsZiocu9/htkyyEfTgKaVio= -github.com/olebeck/dragonfly v0.9.4-11/go.mod h1:1XIP+EcgRnxAEkFEbqZE3T4vXyMufzdpnigwczMBbyo= -github.com/olebeck/dragonfly v0.9.4-12 h1:jiD3Nr5SA/Asj6bdePAx25uMBkyUr9jxAwbjODHYgKM= -github.com/olebeck/dragonfly v0.9.4-12/go.mod h1:1XIP+EcgRnxAEkFEbqZE3T4vXyMufzdpnigwczMBbyo= +github.com/olebeck/dragonfly v0.9.4-13 h1:JF72hfG3/BBCXU1GSBaEKKXQy/gt+0mEOua3RdKXdJ8= +github.com/olebeck/dragonfly v0.9.4-13/go.mod h1:ZNcbAATEeTNyN3Cumtwzox7STtFve469HHzL5c1K3nY= github.com/olebeck/gio v0.0.0-20230427194143-c9c9d8bc704d h1:D+Ryca52xv37/p0FsEWfGwAGUZ1vPWpvimA2eMfBijc= github.com/olebeck/gio v0.0.0-20230427194143-c9c9d8bc704d/go.mod h1:8CFQM/4LurRd9G3NUYdacFb9j2pK0LrAyVO2mAZo4mw= -github.com/olebeck/gophertunnel v1.28.2-1 h1:+3LaX37suEjMUnUTUKJMetoBqSg5PUkUBJc5dW02WI4= -github.com/olebeck/gophertunnel v1.28.2-1/go.mod h1:HxQfl/8mZzvjzhekEH8RO6xLAgan9i/wIyrQzw0tIPY= github.com/olebeck/gophertunnel v1.29.0-1 h1:3x2cZoe8O54xVFgEZqTBJpFEXlzbjlLFoo/d9cWGv+g= github.com/olebeck/gophertunnel v1.29.0-1/go.mod h1:HxQfl/8mZzvjzhekEH8RO6xLAgan9i/wIyrQzw0tIPY= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/handlers/worlds/chunk.go b/handlers/worlds/chunk.go index a93523f..69d2046 100644 --- a/handlers/worlds/chunk.go +++ b/handlers/worlds/chunk.go @@ -2,6 +2,7 @@ package worlds import ( "github.com/bedrock-tool/bedrocktool/locale" + "github.com/df-mc/dragonfly/server/block/cube" "github.com/df-mc/dragonfly/server/world" "github.com/df-mc/dragonfly/server/world/chunk" "github.com/repeale/fp-go" @@ -42,10 +43,10 @@ func (w *worldsHandler) processLevelChunk(pk *packet.LevelChunk) { return } for _, blockNBT := range blockNBTs { - x := blockNBT["x"].(int32) - y := blockNBT["y"].(int32) - z := blockNBT["z"].(int32) - w.worldState.blockNBTs[protocol.BlockPos{x, y, z}] = blockNBT + x := int(blockNBT["x"].(int32)) + y := int(blockNBT["y"].(int32)) + z := int(blockNBT["z"].(int32)) + w.worldState.blockNBTs[cube.Pos{x, y, z}] = blockNBT } w.worldState.chunks[(world.ChunkPos)(pk.Position)] = ch @@ -101,10 +102,10 @@ func (w *worldsHandler) processSubChunk(pk *packet.SubChunk) { logrus.Error(err) } for _, blockNBT := range blockNBTs { - x := blockNBT["x"].(int32) - y := blockNBT["y"].(int32) - z := blockNBT["z"].(int32) - w.worldState.blockNBTs[protocol.BlockPos{x, y, z}] = blockNBT + x := int(blockNBT["x"].(int32)) + y := int(blockNBT["y"].(int32)) + z := int(blockNBT["z"].(int32)) + w.worldState.blockNBTs[cube.Pos{x, y, z}] = blockNBT } posToRedraw[pos] = true @@ -134,7 +135,8 @@ func (w *worldsHandler) ProcessChunkPackets(pk packet.Packet) packet.Packet { case *packet.SubChunk: w.processSubChunk(pk) case *packet.BlockActorData: - w.worldState.blockNBTs[pk.Position] = pk.NBTData + p := pk.Position + w.worldState.blockNBTs[cube.Pos{int(p.X()), int(p.Y()), int(p.Z())}] = pk.NBTData case *packet.UpdateBlock: if w.settings.BlockUpdates { cp := world.ChunkPos{pk.Position.X() >> 4, pk.Position.Z() >> 4} diff --git a/handlers/worlds/items.go b/handlers/worlds/items.go index 47473fe..879eaf3 100644 --- a/handlers/worlds/items.go +++ b/handlers/worlds/items.go @@ -4,6 +4,7 @@ import ( "github.com/bedrock-tool/bedrocktool/locale" "github.com/bedrock-tool/bedrocktool/utils/nbtconv" "github.com/df-mc/dragonfly/server/block" + "github.com/df-mc/dragonfly/server/block/cube" "github.com/df-mc/dragonfly/server/item" "github.com/df-mc/dragonfly/server/item/inventory" "github.com/df-mc/dragonfly/server/world" @@ -92,7 +93,8 @@ func (w *worldsHandler) processItemPacketsServer(pk packet.Packet) packet.Packet } // put into subchunk - nbt, ok := w.worldState.blockNBTs[existing.OpenPacket.ContainerPosition] + p := existing.OpenPacket.ContainerPosition + nbt, ok := w.worldState.blockNBTs[cube.Pos{int(p.X()), int(p.Y()), int(p.Z())}] if ok { nbt["Items"] = nbtconv.InvToNBT(inv) } diff --git a/handlers/worlds/world.go b/handlers/worlds/world.go index aa97ef5..346df9f 100644 --- a/handlers/worlds/world.go +++ b/handlers/worlds/world.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "image" + "image/color" "image/png" "math/rand" "os" @@ -55,7 +56,7 @@ type WorldSettings struct { type worldState struct { dimension world.Dimension chunks map[world.ChunkPos]*chunk.Chunk - blockNBTs map[protocol.BlockPos]map[string]any + blockNBTs map[cube.Pos]map[string]any entities map[uint64]*entityState openItemContainers map[byte]*itemContainer Name string @@ -219,7 +220,7 @@ func (w *worldsHandler) Reset() { w.worldState = worldState{ dimension: w.worldState.dimension, chunks: make(map[world.ChunkPos]*chunk.Chunk), - blockNBTs: make(map[protocol.BlockPos]map[string]any), + blockNBTs: make(map[cube.Pos]map[string]any), entities: make(map[uint64]*entityState), openItemContainers: make(map[byte]*itemContainer), Name: w.currentName(), @@ -241,6 +242,35 @@ func (w *worldState) cullChunks() { } } +type dummyBlock struct { + id string + nbt map[string]any +} + +func (d *dummyBlock) EncodeBlock() (string, map[string]any) { + return d.id, d.nbt +} + +func (d *dummyBlock) Hash() uint64 { + return 0 +} + +func (d *dummyBlock) Model() world.BlockModel { + return nil +} + +func (d *dummyBlock) Color() color.RGBA { + return color.RGBA{0, 0, 0, 0} +} + +func (d *dummyBlock) DecodeNBT(data map[string]any) any { + return nil +} + +func (d *dummyBlock) EncodeNBT() map[string]any { + return d.nbt +} + func (w *worldState) Save(folder string) (*mcdb.DB, error) { provider, err := mcdb.Config{ Log: logrus.StandardLogger(), @@ -250,10 +280,16 @@ func (w *worldState) Save(folder string) (*mcdb.DB, error) { return nil, err } - chunkBlockNBT := make(map[world.ChunkPos][]map[string]any) + chunkBlockNBT := make(map[world.ChunkPos]map[cube.Pos]world.Block) for bp, blockNBT := range w.blockNBTs { // 3d to 2d - cp := world.ChunkPos{bp.X() >> 4, bp.Z() >> 4} - chunkBlockNBT[cp] = append(chunkBlockNBT[cp], blockNBT) + cp := world.ChunkPos{int32(bp.X()) >> 4, int32(bp.Z()) >> 4} + m, ok := chunkBlockNBT[cp] + if !ok { + m = make(map[cube.Pos]world.Block) + chunkBlockNBT[cp] = m + } + id := blockNBT["id"].(string) + m[bp] = &dummyBlock{id, blockNBT} } chunkEntities := make(map[world.ChunkPos][]world.Entity) @@ -265,8 +301,9 @@ func (w *worldState) Save(folder string) (*mcdb.DB, error) { // save chunk data for cp, c := range w.chunks { column := &world.Column{ - Chunk: c, - Entities: chunkEntities[cp], + Chunk: c, + BlockEntities: chunkBlockNBT[cp], + Entities: chunkEntities[cp], } err = provider.StoreColumn(cp, w.dimension, column) if err != nil {