From 330d7e6c3ee4b2188d445df74197228c6d9516ea Mon Sep 17 00:00:00 2001 From: Tnze Date: Sun, 28 Jul 2024 16:21:24 +0800 Subject: [PATCH] fix item displays name on example/daze --- bot/client.go | 2 +- data/registryid/bootstrap/builtinregistries.go | 17 +++++++++++++++++ examples/daze/daze.go | 13 ++++++++----- registry/codec.go | 8 ++++---- server/configuration.go | 2 +- 5 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 data/registryid/bootstrap/builtinregistries.go diff --git a/bot/client.go b/bot/client.go index ae29081..607b583 100644 --- a/bot/client.go +++ b/bot/client.go @@ -21,7 +21,7 @@ type Client struct { // These are filled when login process Name string UUID uuid.UUID - Registries registry.NetworkCodec + Registries registry.Registries Cookies map[string][]byte // Ingame packet handlers diff --git a/data/registryid/bootstrap/builtinregistries.go b/data/registryid/bootstrap/builtinregistries.go new file mode 100644 index 0000000..30000c9 --- /dev/null +++ b/data/registryid/bootstrap/builtinregistries.go @@ -0,0 +1,17 @@ +package bootstrap + +import ( + "github.com/Tnze/go-mc/data/registryid" + "github.com/Tnze/go-mc/level/block" + "github.com/Tnze/go-mc/registry" +) + +func RegisterBlocks(reg *registry.Registry[block.Block]) { + reg.Clear() + for i, key := range registryid.Block { + id, val := reg.Put(key, block.FromID[key]) + if int32(i) != id || val == nil || *val == nil { + panic("register blocks failed") + } + } +} diff --git a/examples/daze/daze.go b/examples/daze/daze.go index f33b330..c7c67c1 100644 --- a/examples/daze/daze.go +++ b/examples/daze/daze.go @@ -23,8 +23,8 @@ import ( "github.com/Tnze/go-mc/bot/screen" "github.com/Tnze/go-mc/bot/world" "github.com/Tnze/go-mc/chat" - "github.com/Tnze/go-mc/data/item" _ "github.com/Tnze/go-mc/data/lang/zh-cn" + "github.com/Tnze/go-mc/data/registryid" "github.com/Tnze/go-mc/level" ) @@ -161,11 +161,14 @@ func onScreenSlotChange(id, index int) error { container, ok := screenManager.Screens[id] if ok { // Currently, only inventory container is supported - switch container.(type) { + switch container := container.(type) { case *screen.Inventory: - slot := container.(*screen.Inventory).Slots[index] - itemInfo := item.ByID[item.ID(slot.ID)] - log.Printf("Slot: Screen[%d].Slot[%d]: [%v] * %d | NBT: %v", id, index, itemInfo, slot.Count, slot.NBT) + slot := container.Slots[index] + itemName := "nil" + if slot.ID >= 0 && int(slot.ID) < len(registryid.Item) { + itemName = registryid.Item[slot.ID] + } + log.Printf("Slot: Screen[%d].Slot[%d]: [%v] * %d | NBT: %v", id, index, itemName, slot.Count, slot.NBT) } } } diff --git a/registry/codec.go b/registry/codec.go index 6aefbb8..8233a9b 100644 --- a/registry/codec.go +++ b/registry/codec.go @@ -9,7 +9,7 @@ import ( pk "github.com/Tnze/go-mc/net/packet" ) -type NetworkCodec struct { +type Registries struct { ChatType Registry[ChatType] `registry:"minecraft:chat_type"` DamageType Registry[DamageType] `registry:"minecraft:damage_type"` DimensionType Registry[Dimension] `registry:"minecraft:dimension_type"` @@ -23,8 +23,8 @@ type NetworkCodec struct { JukeboxSong Registry[nbt.RawMessage] `registry:"minecraft:jukebox_song"` } -func NewNetworkCodec() NetworkCodec { - return NetworkCodec{ +func NewNetworkCodec() Registries { + return Registries{ ChatType: NewRegistry[ChatType](), DamageType: NewRegistry[DamageType](), DimensionType: NewRegistry[Dimension](), @@ -79,7 +79,7 @@ type RegistryCodec interface { ReadTagsFrom(r io.Reader) (int64, error) } -func (c *NetworkCodec) Registry(id string) RegistryCodec { +func (c *Registries) Registry(id string) RegistryCodec { codecVal := reflect.ValueOf(c).Elem() codecTyp := codecVal.Type() numField := codecVal.NumField() diff --git a/server/configuration.go b/server/configuration.go index 8aa7f30..ae01b41 100644 --- a/server/configuration.go +++ b/server/configuration.go @@ -13,7 +13,7 @@ type ConfigHandler interface { } type Configurations struct { - Registries registry.NetworkCodec + Registries registry.Registries } func (c *Configurations) AcceptConfig(conn *net.Conn) error {