Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split world.NBTer up into world.ItemNBTer and world.BlockNBTer #889

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/block/banner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/block/banner_pattern_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/block/barrel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion server/block/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/block/blast_furnace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion server/block/chest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion server/block/decorated_pot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions server/block/enchanting_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion server/block/ender_chest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/block/furnace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion server/block/item_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion server/block/jukebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/block/lectern.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion server/block/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion server/block/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion server/block/skull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/block/smoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions server/internal/nbtconv/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
Expand Down
2 changes: 1 addition & 1 deletion server/internal/nbtconv/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions server/item/book_and_quill.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/item/boots.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/item/chestplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions server/item/creative/creative.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
4 changes: 2 additions & 2 deletions server/item/firework.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/item/firework_explosion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions server/item/firework_star.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion server/item/helmet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/item/leggings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions server/item/recipe/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}
Expand Down
4 changes: 2 additions & 2 deletions server/item/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion server/item/written_book.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions server/session/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions server/session/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/session/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading
Loading