From 6ee550883f90437ecd399453543904acd8effc6d Mon Sep 17 00:00:00 2001 From: T14Raptor Date: Mon, 3 Jun 2024 02:07:15 -0400 Subject: [PATCH] server/world: Split world.NBTer up into world.ItemNBTer and world.BlockNBTer. --- server/block/banner.go | 4 ++-- server/block/banner_pattern_layer.go | 2 +- server/block/barrel.go | 2 +- server/block/beacon.go | 2 +- server/block/blast_furnace.go | 2 +- server/block/chest.go | 2 +- server/block/decorated_pot.go | 2 +- server/block/enchanting_table.go | 4 ++-- server/block/ender_chest.go | 2 +- server/block/furnace.go | 2 +- server/block/item_frame.go | 2 +- server/block/jukebox.go | 2 +- server/block/lectern.go | 2 +- server/block/note.go | 2 +- server/block/sign.go | 2 +- server/block/skull.go | 2 +- server/block/smoker.go | 2 +- server/internal/nbtconv/read.go | 4 ++-- server/internal/nbtconv/write.go | 2 +- server/item/book_and_quill.go | 7 +++++-- server/item/boots.go | 2 +- server/item/chestplate.go | 2 +- server/item/creative/creative.go | 4 ++-- server/item/firework.go | 4 ++-- server/item/firework_explosion.go | 2 +- server/item/firework_star.go | 6 ++++-- server/item/helmet.go | 2 +- server/item/leggings.go | 2 +- server/item/recipe/item.go | 4 ++-- server/item/stack.go | 4 ++-- server/item/written_book.go | 4 +++- server/session/chunk.go | 6 +++--- server/session/player.go | 4 ++-- server/session/world.go | 2 +- server/world/block.go | 19 +++++++++---------- server/world/block_state.go | 4 ++-- server/world/item.go | 10 ++++++++++ server/world/mcdb/db.go | 6 +++--- server/world/world.go | 2 +- 39 files changed, 78 insertions(+), 62 deletions(-) diff --git a/server/block/banner.go b/server/block/banner.go index d9cebdf88..a031e83f0 100644 --- a/server/block/banner.go +++ b/server/block/banner.go @@ -102,13 +102,13 @@ func (b Banner) EncodeNBT() map[string]any { } // DecodeNBT ... -func (b Banner) DecodeNBT(m map[string]any) any { +func (b Banner) DecodeNBT(m map[string]any) world.Block { b.Colour = invertColourID(int16(nbtconv.Int32(m, "Base"))) b.Illager = nbtconv.Int32(m, "Type") == 1 if patterns := nbtconv.Slice[any](m, "Patterns"); patterns != nil { b.Patterns = make([]BannerPatternLayer, len(patterns)) for i, p := range b.Patterns { - b.Patterns[i] = p.DecodeNBT(patterns[i].(map[string]any)).(BannerPatternLayer) + b.Patterns[i] = p.DecodeNBT(patterns[i].(map[string]any)) } } return b diff --git a/server/block/banner_pattern_layer.go b/server/block/banner_pattern_layer.go index 3fef11f13..7e2456555 100644 --- a/server/block/banner_pattern_layer.go +++ b/server/block/banner_pattern_layer.go @@ -22,7 +22,7 @@ func (b BannerPatternLayer) EncodeNBT() map[string]any { } // DecodeNBT decodes the given NBT map into a BannerPatternLayer and returns it. -func (b BannerPatternLayer) DecodeNBT(data map[string]any) any { +func (b BannerPatternLayer) DecodeNBT(data map[string]any) BannerPatternLayer { b.Type = BannerPatternByID(nbtconv.String(data, "Pattern")) b.Colour = invertColourID(int16(nbtconv.Int32(data, "Color"))) return b diff --git a/server/block/barrel.go b/server/block/barrel.go index 3558ec1ff..0e7620771 100644 --- a/server/block/barrel.go +++ b/server/block/barrel.go @@ -138,7 +138,7 @@ func (Barrel) FuelInfo() item.FuelInfo { } // DecodeNBT ... -func (b Barrel) DecodeNBT(data map[string]any) any { +func (b Barrel) DecodeNBT(data map[string]any) world.Block { facing := b.Facing //noinspection GoAssignmentToReceiver b = NewBarrel() diff --git a/server/block/beacon.go b/server/block/beacon.go index e391d5e53..fc1f9e198 100644 --- a/server/block/beacon.go +++ b/server/block/beacon.go @@ -48,7 +48,7 @@ func (b Beacon) Activate(pos cube.Pos, _ cube.Face, _ *world.World, u item.User, } // DecodeNBT ... -func (b Beacon) DecodeNBT(data map[string]any) any { +func (b Beacon) DecodeNBT(data map[string]any) world.Block { b.level = int(nbtconv.Int32(data, "Levels")) if primary, ok := effect.ByID(int(nbtconv.Int32(data, "Primary"))); ok { b.Primary = primary.(effect.LastingType) diff --git a/server/block/blast_furnace.go b/server/block/blast_furnace.go index e194f74f2..43a79e2d2 100644 --- a/server/block/blast_furnace.go +++ b/server/block/blast_furnace.go @@ -103,7 +103,7 @@ func (b BlastFurnace) EncodeNBT() map[string]interface{} { } // DecodeNBT ... -func (b BlastFurnace) DecodeNBT(data map[string]interface{}) interface{} { +func (b BlastFurnace) DecodeNBT(data map[string]any) world.Block { remaining := nbtconv.TickDuration[int16](data, "BurnTime") maximum := nbtconv.TickDuration[int16](data, "BurnDuration") cook := nbtconv.TickDuration[int16](data, "CookTime") diff --git a/server/block/chest.go b/server/block/chest.go index db723f75b..a793e43ff 100644 --- a/server/block/chest.go +++ b/server/block/chest.go @@ -149,7 +149,7 @@ func (c Chest) FlammabilityInfo() FlammabilityInfo { } // DecodeNBT ... -func (c Chest) DecodeNBT(data map[string]any) any { +func (c Chest) DecodeNBT(data map[string]any) world.Block { facing := c.Facing //noinspection GoAssignmentToReceiver c = NewChest() diff --git a/server/block/decorated_pot.go b/server/block/decorated_pot.go index ef126f3f3..fe4ce00f3 100644 --- a/server/block/decorated_pot.go +++ b/server/block/decorated_pot.go @@ -81,7 +81,7 @@ func (p DecoratedPot) EncodeNBT() map[string]any { } // DecodeNBT ... -func (p DecoratedPot) DecodeNBT(data map[string]any) any { +func (p DecoratedPot) DecodeNBT(data map[string]any) world.Block { p.Decorations = [4]PotDecoration{} if sherds := nbtconv.Slice[string](data, "sherds"); sherds != nil { for i, name := range sherds { diff --git a/server/block/enchanting_table.go b/server/block/enchanting_table.go index 86c7f01a9..270fa3c37 100644 --- a/server/block/enchanting_table.go +++ b/server/block/enchanting_table.go @@ -60,7 +60,7 @@ func (e EnchantingTable) EncodeNBT() map[string]any { return map[string]any{"id": "EnchantTable"} } -// DecodeNBT is used to implement world.NBTer. -func (e EnchantingTable) DecodeNBT(map[string]any) any { +// DecodeNBT is used to implement world.BlockNBTer. +func (e EnchantingTable) DecodeNBT(map[string]any) world.Block { return e } diff --git a/server/block/ender_chest.go b/server/block/ender_chest.go index e97f6dbc5..3d8f6af93 100644 --- a/server/block/ender_chest.go +++ b/server/block/ender_chest.go @@ -112,7 +112,7 @@ func (c EnderChest) EncodeNBT() map[string]interface{} { } // DecodeNBT ... -func (c EnderChest) DecodeNBT(map[string]interface{}) interface{} { +func (c EnderChest) DecodeNBT(map[string]any) world.Block { ec := NewEnderChest() ec.Facing = c.Facing return ec diff --git a/server/block/furnace.go b/server/block/furnace.go index 4c758bc64..f1570c8a9 100644 --- a/server/block/furnace.go +++ b/server/block/furnace.go @@ -102,7 +102,7 @@ func (f Furnace) EncodeNBT() map[string]interface{} { } // DecodeNBT ... -func (f Furnace) DecodeNBT(data map[string]interface{}) interface{} { +func (f Furnace) DecodeNBT(data map[string]any) world.Block { remaining := nbtconv.TickDuration[int16](data, "BurnTime") maximum := nbtconv.TickDuration[int16](data, "BurnDuration") cook := nbtconv.TickDuration[int16](data, "CookTime") diff --git a/server/block/item_frame.go b/server/block/item_frame.go index 4cbf49b35..d03415ba1 100644 --- a/server/block/item_frame.go +++ b/server/block/item_frame.go @@ -112,7 +112,7 @@ func (i ItemFrame) EncodeBlock() (name string, properties map[string]any) { } // DecodeNBT ... -func (i ItemFrame) DecodeNBT(data map[string]any) any { +func (i ItemFrame) DecodeNBT(data map[string]any) world.Block { i.DropChance = float64(nbtconv.Float32(data, "ItemDropChance")) i.Rotations = int(nbtconv.Uint8(data, "ItemRotation")) i.Item = nbtconv.MapItem(data, "Item") diff --git a/server/block/jukebox.go b/server/block/jukebox.go index 2cdd43b8c..ceaf0fed5 100644 --- a/server/block/jukebox.go +++ b/server/block/jukebox.go @@ -89,7 +89,7 @@ func (j Jukebox) EncodeNBT() map[string]any { } // DecodeNBT ... -func (j Jukebox) DecodeNBT(data map[string]any) any { +func (j Jukebox) DecodeNBT(data map[string]any) world.Block { s := nbtconv.MapItem(data, "RecordItem") if _, ok := s.Item().(item.MusicDisc); ok { j.Item = s diff --git a/server/block/lectern.go b/server/block/lectern.go index 9ea0fc449..d88559e70 100644 --- a/server/block/lectern.go +++ b/server/block/lectern.go @@ -138,7 +138,7 @@ func (l Lectern) EncodeNBT() map[string]any { } // DecodeNBT ... -func (l Lectern) DecodeNBT(m map[string]any) any { +func (l Lectern) DecodeNBT(m map[string]any) world.Block { l.Page = int(nbtconv.Int32(m, "page")) l.Book = nbtconv.MapItem(m, "book") return l diff --git a/server/block/note.go b/server/block/note.go index 11d4a0ae5..171f78a23 100644 --- a/server/block/note.go +++ b/server/block/note.go @@ -36,7 +36,7 @@ func (n Note) instrument(pos cube.Pos, w *world.World) sound.Instrument { } // DecodeNBT ... -func (n Note) DecodeNBT(data map[string]any) any { +func (n Note) DecodeNBT(data map[string]any) world.Block { n.Pitch = int(nbtconv.Uint8(data, "note")) return n } diff --git a/server/block/sign.go b/server/block/sign.go index fe65be77e..22b03e49f 100644 --- a/server/block/sign.go +++ b/server/block/sign.go @@ -189,7 +189,7 @@ func (s Sign) EncodeBlock() (name string, properties map[string]any) { } // DecodeNBT ... -func (s Sign) DecodeNBT(data map[string]any) any { +func (s Sign) DecodeNBT(data map[string]any) world.Block { if nbtconv.String(data, "Text") != "" { // The NBT format changed in 1.19.80 to have separate data for each side of the sign. The old format must still // be supported for backwards compatibility. diff --git a/server/block/skull.go b/server/block/skull.go index 3a9ccffd5..805f5384d 100644 --- a/server/block/skull.go +++ b/server/block/skull.go @@ -87,7 +87,7 @@ func (s Skull) EncodeItem() (name string, meta int16) { } // DecodeNBT ... -func (s Skull) DecodeNBT(data map[string]interface{}) interface{} { +func (s Skull) DecodeNBT(data map[string]any) world.Block { s.Type = SkullType{skull(nbtconv.Uint8(data, "SkullType"))} s.Attach.o = cube.Orientation(nbtconv.Uint8(data, "Rot")) if s.Attach.facing >= 0 { diff --git a/server/block/smoker.go b/server/block/smoker.go index da94ea200..bec2cc12a 100644 --- a/server/block/smoker.go +++ b/server/block/smoker.go @@ -103,7 +103,7 @@ func (s Smoker) EncodeNBT() map[string]interface{} { } // DecodeNBT ... -func (s Smoker) DecodeNBT(data map[string]interface{}) interface{} { +func (s Smoker) DecodeNBT(data map[string]any) world.Block { remaining := nbtconv.TickDuration[int16](data, "BurnTime") maximum := nbtconv.TickDuration[int16](data, "BurnDuration") cook := nbtconv.TickDuration[int16](data, "CookTime") diff --git a/server/internal/nbtconv/read.go b/server/internal/nbtconv/read.go index 3263dda0d..6d936c1e5 100644 --- a/server/internal/nbtconv/read.go +++ b/server/internal/nbtconv/read.go @@ -204,8 +204,8 @@ func readItemStack(m, t map[string]any) item.Stack { if it == nil { return item.Stack{} } - if n, ok := it.(world.NBTer); ok { - it = n.DecodeNBT(t).(world.Item) + if n, ok := it.(world.ItemNBTer); ok { + it = n.DecodeNBT(t) } return item.NewStack(it, int(Uint8(m, "Count"))) } diff --git a/server/internal/nbtconv/write.go b/server/internal/nbtconv/write.go index 3c6ee5b65..07cccb3af 100644 --- a/server/internal/nbtconv/write.go +++ b/server/internal/nbtconv/write.go @@ -12,7 +12,7 @@ import ( // WriteItem encodes an item stack into a map that can be encoded using NBT. func WriteItem(s item.Stack, disk bool) map[string]any { tag := make(map[string]any) - if nbt, ok := s.Item().(world.NBTer); ok { + if nbt, ok := s.Item().(world.ItemNBTer); ok { for k, v := range nbt.EncodeNBT() { tag[k] = v } diff --git a/server/item/book_and_quill.go b/server/item/book_and_quill.go index 76ef0d313..82029812f 100644 --- a/server/item/book_and_quill.go +++ b/server/item/book_and_quill.go @@ -1,6 +1,9 @@ package item -import "slices" +import ( + "github.com/df-mc/dragonfly/server/world" + "slices" +) // BookAndQuill is an item used to write WrittenBook(s). type BookAndQuill struct { @@ -88,7 +91,7 @@ func (b BookAndQuill) SwapPages(pageOne, pageTwo int) BookAndQuill { } // DecodeNBT ... -func (b BookAndQuill) DecodeNBT(data map[string]any) any { +func (b BookAndQuill) DecodeNBT(data map[string]any) world.Item { pages, _ := data["pages"].([]any) for _, page := range pages { if pageData, ok := page.(map[string]any); ok { diff --git a/server/item/boots.go b/server/item/boots.go index 938980246..720d13241 100644 --- a/server/item/boots.go +++ b/server/item/boots.go @@ -86,7 +86,7 @@ func (b Boots) EncodeItem() (name string, meta int16) { } // DecodeNBT ... -func (b Boots) DecodeNBT(data map[string]any) any { +func (b Boots) DecodeNBT(data map[string]any) world.Item { if t, ok := b.Tier.(ArmourTierLeather); ok { if v, ok := data["customColor"].(int32); ok { t.Colour = rgbaFromInt32(v) diff --git a/server/item/chestplate.go b/server/item/chestplate.go index 43cf8295e..ec51cbacb 100644 --- a/server/item/chestplate.go +++ b/server/item/chestplate.go @@ -88,7 +88,7 @@ func (c Chestplate) EncodeItem() (name string, meta int16) { } // DecodeNBT ... -func (c Chestplate) DecodeNBT(data map[string]any) any { +func (c Chestplate) DecodeNBT(data map[string]any) world.Item { if t, ok := c.Tier.(ArmourTierLeather); ok { if v, ok := data["customColor"].(int32); ok { t.Colour = rgbaFromInt32(v) diff --git a/server/item/creative/creative.go b/server/item/creative/creative.go index 9d3507cab..6381d29a7 100644 --- a/server/item/creative/creative.go +++ b/server/item/creative/creative.go @@ -71,9 +71,9 @@ func init() { } } - if n, ok := it.(world.NBTer); ok { + if n, ok := it.(world.ItemNBTer); ok { if len(data.NBT) > 0 { - it = n.DecodeNBT(data.NBT).(world.Item) + it = n.DecodeNBT(data.NBT) } } diff --git a/server/item/firework.go b/server/item/firework.go index 539d02fa1..d006be3e7 100644 --- a/server/item/firework.go +++ b/server/item/firework.go @@ -60,12 +60,12 @@ func (f Firework) EncodeNBT() map[string]any { } // DecodeNBT ... -func (f Firework) DecodeNBT(data map[string]any) any { +func (f Firework) DecodeNBT(data map[string]any) world.Item { if fireworks, ok := data["Fireworks"].(map[string]any); ok { if explosions, ok := fireworks["Explosions"].([]any); ok { f.Explosions = make([]FireworkExplosion, len(explosions)) for i, explosion := range f.Explosions { - f.Explosions[i] = explosion.DecodeNBT(explosions[i].(map[string]any)).(FireworkExplosion) + f.Explosions[i] = explosion.DecodeNBT(explosions[i].(map[string]any)) } } if durationTicks, ok := fireworks["Flight"].(uint8); ok { diff --git a/server/item/firework_explosion.go b/server/item/firework_explosion.go index d52083d39..1d93a64f7 100644 --- a/server/item/firework_explosion.go +++ b/server/item/firework_explosion.go @@ -32,7 +32,7 @@ func (f FireworkExplosion) EncodeNBT() map[string]any { } // DecodeNBT ... -func (f FireworkExplosion) DecodeNBT(data map[string]any) any { +func (f FireworkExplosion) DecodeNBT(data map[string]any) FireworkExplosion { f.Shape = FireworkShapes()[data["FireworkType"].(uint8)] f.Twinkle = data["FireworkFlicker"].(uint8) == 1 f.Trail = data["FireworkTrail"].(uint8) == 1 diff --git a/server/item/firework_star.go b/server/item/firework_star.go index 023497133..8b76336b6 100644 --- a/server/item/firework_star.go +++ b/server/item/firework_star.go @@ -1,5 +1,7 @@ package item +import "github.com/df-mc/dragonfly/server/world" + // FireworkStar is an item used to determine the color, effect, and shape of firework rockets. type FireworkStar struct { FireworkExplosion @@ -14,9 +16,9 @@ func (f FireworkStar) EncodeNBT() map[string]any { } // DecodeNBT ... -func (f FireworkStar) DecodeNBT(data map[string]any) any { +func (f FireworkStar) DecodeNBT(data map[string]any) world.Item { if i, ok := data["FireworksItem"].(map[string]any); ok { - f.FireworkExplosion = f.FireworkExplosion.DecodeNBT(i).(FireworkExplosion) + f.FireworkExplosion = f.FireworkExplosion.DecodeNBT(i) } return f } diff --git a/server/item/helmet.go b/server/item/helmet.go index c1968961e..ff2507e09 100644 --- a/server/item/helmet.go +++ b/server/item/helmet.go @@ -86,7 +86,7 @@ func (h Helmet) EncodeItem() (name string, meta int16) { } // DecodeNBT ... -func (h Helmet) DecodeNBT(data map[string]any) any { +func (h Helmet) DecodeNBT(data map[string]any) world.Item { if t, ok := h.Tier.(ArmourTierLeather); ok { if v, ok := data["customColor"].(int32); ok { t.Colour = rgbaFromInt32(v) diff --git a/server/item/leggings.go b/server/item/leggings.go index 3e7b10123..2654ab7d5 100644 --- a/server/item/leggings.go +++ b/server/item/leggings.go @@ -90,7 +90,7 @@ func (l Leggings) EncodeItem() (name string, meta int16) { } // DecodeNBT ... -func (l Leggings) DecodeNBT(data map[string]any) any { +func (l Leggings) DecodeNBT(data map[string]any) world.Item { if t, ok := l.Tier.(ArmourTierLeather); ok { if v, ok := data["customColor"].(int32); ok { t.Colour = rgbaFromInt32(v) diff --git a/server/item/recipe/item.go b/server/item/recipe/item.go index 623d76dd0..c72718377 100644 --- a/server/item/recipe/item.go +++ b/server/item/recipe/item.go @@ -91,8 +91,8 @@ func (d outputItems) Stacks() ([]item.Stack, bool) { return nil, false } } - if n, ok := it.(world.NBTer); ok { - it = n.DecodeNBT(o.NBTData).(world.Item) + if n, ok := it.(world.ItemNBTer); ok { + it = n.DecodeNBT(o.NBTData) } s = append(s, item.NewStack(it, int(o.Count))) } diff --git a/server/item/stack.go b/server/item/stack.go index 1323e401f..99e7ed182 100644 --- a/server/item/stack.go +++ b/server/item/stack.go @@ -373,8 +373,8 @@ func (s Stack) Comparable(s2 Stack) bool { if !reflect.DeepEqual(s.data, s2.data) { return false } - if nbt, ok := s.Item().(world.NBTer); ok { - nbt2, ok := s2.Item().(world.NBTer) + if nbt, ok := s.Item().(world.ItemNBTer); ok { + nbt2, ok := s2.Item().(world.ItemNBTer) return ok && reflect.DeepEqual(nbt.EncodeNBT(), nbt2.EncodeNBT()) } return true diff --git a/server/item/written_book.go b/server/item/written_book.go index 762e7990b..e6989f916 100644 --- a/server/item/written_book.go +++ b/server/item/written_book.go @@ -1,5 +1,7 @@ package item +import "github.com/df-mc/dragonfly/server/world" + // WrittenBook is the item created after a book and quill is signed. It appears the same as a regular book, but // without the quill, and has an enchanted-looking glint. type WrittenBook struct { @@ -34,7 +36,7 @@ func (w WrittenBook) Page(page int) (string, bool) { } // DecodeNBT ... -func (w WrittenBook) DecodeNBT(data map[string]any) any { +func (w WrittenBook) DecodeNBT(data map[string]any) world.Item { if pages, ok := data["pages"].([]any); ok { w.Pages = make([]string, len(pages)) for i, page := range pages { diff --git a/server/session/chunk.go b/server/session/chunk.go index 93880512b..13d0e1f76 100644 --- a/server/session/chunk.go +++ b/server/session/chunk.go @@ -101,7 +101,7 @@ func (s *Session) subChunkEntry(offset protocol.SubChunkOffset, ind int16, col * blockEntityBuf := bytes.NewBuffer(nil) enc := nbt.NewEncoderWithEncoding(blockEntityBuf, nbt.NetworkLittleEndian) for pos, b := range col.BlockEntities { - if n, ok := b.(world.NBTer); ok && col.Chunk.SubIndex(int16(pos.Y())) == ind { + if n, ok := b.(world.BlockNBTer); ok && col.Chunk.SubIndex(int16(pos.Y())) == ind { d := n.EncodeNBT() d["x"], d["y"], d["z"] = int32(pos[0]), int32(pos[1]), int32(pos[2]) _ = enc.Encode(d) @@ -180,7 +180,7 @@ func (s *Session) sendBlobHashes(pos world.ChunkPos, c *chunk.Chunk, blockEntiti raw := bytes.NewBuffer(make([]byte, 1, 32)) enc := nbt.NewEncoderWithEncoding(raw, nbt.NetworkLittleEndian) for bp, b := range blockEntities { - if n, ok := b.(world.NBTer); ok { + if n, ok := b.(world.BlockNBTer); ok { d := n.EncodeNBT() d["x"], d["y"], d["z"] = int32(bp[0]), int32(bp[1]), int32(bp[2]) _ = enc.Encode(d) @@ -222,7 +222,7 @@ func (s *Session) sendNetworkChunk(pos world.ChunkPos, c *chunk.Chunk, blockEnti enc := nbt.NewEncoderWithEncoding(chunkBuf, nbt.NetworkLittleEndian) for bp, b := range blockEntities { - if n, ok := b.(world.NBTer); ok { + if n, ok := b.(world.BlockNBTer); ok { d := n.EncodeNBT() d["x"], d["y"], d["z"] = int32(bp[0]), int32(bp[1]), int32(bp[2]) _ = enc.Encode(d) diff --git a/server/session/player.go b/server/session/player.go index 0552af589..8ecd98017 100644 --- a/server/session/player.go +++ b/server/session/player.go @@ -781,8 +781,8 @@ func stackToItem(it protocol.ItemStack) item.Stack { } } //noinspection SpellCheckingInspection - if nbter, ok := t.(world.NBTer); ok && len(it.NBTData) != 0 { - t = nbter.DecodeNBT(it.NBTData).(world.Item) + if nbter, ok := t.(world.ItemNBTer); ok && len(it.NBTData) != 0 { + t = nbter.DecodeNBT(it.NBTData) } s := item.NewStack(t, int(it.Count)) return nbtconv.Item(it.NBTData, &s) diff --git a/server/session/world.go b/server/session/world.go index 18d9deab5..e68afe4b9 100644 --- a/server/session/world.go +++ b/server/session/world.go @@ -839,7 +839,7 @@ func (s *Session) ViewBlockUpdate(pos cube.Pos, b world.Block, layer int) { Flags: packet.BlockUpdateNetwork, Layer: uint32(layer), }) - if v, ok := b.(world.NBTer); ok { + if v, ok := b.(world.BlockNBTer); ok { NBTData := v.EncodeNBT() NBTData["x"], NBTData["y"], NBTData["z"] = int32(pos.X()), int32(pos.Y()), int32(pos.Z()) s.writePacket(&packet.BlockActorData{ diff --git a/server/world/block.go b/server/world/block.go index eba68c54e..d5e8b6395 100644 --- a/server/world/block.go +++ b/server/world/block.go @@ -104,7 +104,7 @@ func RegisterBlock(b Block) { if emitter, ok := b.(lightEmitter); ok { chunk.LightBlocks[rid] = emitter.LightEmissionLevel() } - if _, ok := b.(NBTer); ok { + if _, ok := b.(BlockNBTer); ok { nbtBlocks[rid] = true } if _, ok := b.(RandomTicker); ok { @@ -197,10 +197,10 @@ type ScheduledTicker interface { ScheduledTick(pos cube.Pos, w *World, r *rand.Rand) } -// TickerBlock is an implementation of NBTer with an additional Tick method that is called on every world +// TickerBlock is an implementation of BlockNBTer with an additional Tick method that is called on every world // tick for loaded blocks that implement this interface. type TickerBlock interface { - NBTer + BlockNBTer Tick(currentTick int64, pos cube.Pos, w *World) } @@ -212,13 +212,12 @@ type NeighbourUpdateTicker interface { NeighbourUpdateTick(pos, changedNeighbour cube.Pos, w *World) } -// NBTer represents either an item or a block which may decode NBT data and encode to NBT data. Typically, -// this is done to store additional data. -type NBTer interface { - // DecodeNBT returns the (new) item, block or entity, depending on which of those the NBTer was, with the NBT data - // decoded into it. - DecodeNBT(data map[string]any) any - // EncodeNBT encodes the entity into a map which can then be encoded as NBT to be written. +// BlockNBTer represents a block which may decode NBT data and encode to NBT data. Typically, this is done +// to store additional data. +type BlockNBTer interface { + // DecodeNBT returns the (new) block with the NBT data decoded into it. + DecodeNBT(data map[string]any) Block + // EncodeNBT encodes the block into a map which can then be encoded as NBT to be written. EncodeNBT() map[string]any } diff --git a/server/world/block_state.go b/server/world/block_state.go index 4cb0a23d6..dae40a618 100644 --- a/server/world/block_state.go +++ b/server/world/block_state.go @@ -26,8 +26,8 @@ var ( customBlocks = map[string]CustomBlock{} // stateRuntimeIDs holds a map for looking up the runtime ID of a block by the stateHash it produces. stateRuntimeIDs = map[stateHash]uint32{} - // nbtBlocks holds a list of NBTer implementations for blocks registered that implement the NBTer interface. - // These are indexed by their runtime IDs. Blocks that do not implement NBTer have a false value in this slice. + // nbtBlocks holds a list of BlockNBTer implementations for blocks registered that implement the BlockNBTer interface. + // These are indexed by their runtime IDs. Blocks that do not implement BlockNBTer have a false value in this slice. nbtBlocks []bool // randomTickBlocks holds a list of RandomTicker implementations for blocks registered that implement the RandomTicker interface. // These are indexed by their runtime IDs. Blocks that do not implement RandomTicker have a false value in this slice. diff --git a/server/world/item.go b/server/world/item.go index d7e713c71..d533a452f 100644 --- a/server/world/item.go +++ b/server/world/item.go @@ -16,6 +16,16 @@ type Item interface { EncodeItem() (name string, meta int16) } +// ItemNBTer represents an item which may decode NBT data and encode to NBT data. Typically, this is done +// to store additional data. +type ItemNBTer interface { + Item + // DecodeNBT returns the (new) item with the NBT data decoded into it. + DecodeNBT(data map[string]any) Item + // EncodeNBT encodes the item into a map which can then be encoded as NBT to be written. + EncodeNBT() map[string]any +} + // CustomItem represents an item that is non-vanilla and requires a resource pack and extra steps to show it // to the client. type CustomItem interface { diff --git a/server/world/mcdb/db.go b/server/world/mcdb/db.go index 035a73eb1..9e6191459 100644 --- a/server/world/mcdb/db.go +++ b/server/world/mcdb/db.go @@ -295,12 +295,12 @@ func (db *DB) blockEntities(k dbKey, c *chunk.Chunk) (map[cube.Pos]world.Block, db.conf.Log.Errorf("no block registered with runtime id %v", id) continue } - nbter, ok := b.(world.NBTer) + nbter, ok := b.(world.BlockNBTer) if !ok { db.conf.Log.Errorf("block %#v has nbt but does not implement world.nbter", b) continue } - blockEntities[pos] = nbter.DecodeNBT(m).(world.Block) + blockEntities[pos] = nbter.DecodeNBT(m) } return blockEntities, nil } @@ -383,7 +383,7 @@ func (db *DB) storeBlockEntities(batch *leveldb.Batch, k dbKey, blockEntities ma buf := bytes.NewBuffer(nil) enc := nbt.NewEncoderWithEncoding(buf, nbt.LittleEndian) for pos, b := range blockEntities { - n, ok := b.(world.NBTer) + n, ok := b.(world.BlockNBTer) if !ok { continue } diff --git a/server/world/world.go b/server/world/world.go index fed71a25d..3df7b5eb8 100644 --- a/server/world/world.go +++ b/server/world/world.go @@ -125,7 +125,7 @@ func (w *World) Block(pos cube.Pos) Block { return nbtB } b, _ := BlockByRuntimeID(rid) - nbtB := b.(NBTer).DecodeNBT(map[string]any{}).(Block) + nbtB := b.(BlockNBTer).DecodeNBT(map[string]any{}) c.BlockEntities[pos] = nbtB viewers := slices.Clone(c.viewers) c.Unlock()