Skip to content

Commit

Permalink
fix item displays name on example/daze
Browse files Browse the repository at this point in the history
  • Loading branch information
Tnze committed Jul 28, 2024
1 parent cbd9bb9 commit 330d7e6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bot/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions data/registryid/bootstrap/builtinregistries.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}
13 changes: 8 additions & 5 deletions examples/daze/daze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions registry/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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](),
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ConfigHandler interface {
}

type Configurations struct {
Registries registry.NetworkCodec
Registries registry.Registries
}

func (c *Configurations) AcceptConfig(conn *net.Conn) error {
Expand Down

0 comments on commit 330d7e6

Please sign in to comment.