Skip to content

Commit 330d7e6

Browse files
committed
fix item displays name on example/daze
1 parent cbd9bb9 commit 330d7e6

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

bot/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Client struct {
2121
// These are filled when login process
2222
Name string
2323
UUID uuid.UUID
24-
Registries registry.NetworkCodec
24+
Registries registry.Registries
2525
Cookies map[string][]byte
2626

2727
// Ingame packet handlers
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package bootstrap
2+
3+
import (
4+
"github.com/Tnze/go-mc/data/registryid"
5+
"github.com/Tnze/go-mc/level/block"
6+
"github.com/Tnze/go-mc/registry"
7+
)
8+
9+
func RegisterBlocks(reg *registry.Registry[block.Block]) {
10+
reg.Clear()
11+
for i, key := range registryid.Block {
12+
id, val := reg.Put(key, block.FromID[key])
13+
if int32(i) != id || val == nil || *val == nil {
14+
panic("register blocks failed")
15+
}
16+
}
17+
}

examples/daze/daze.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"github.com/Tnze/go-mc/bot/screen"
2424
"github.com/Tnze/go-mc/bot/world"
2525
"github.com/Tnze/go-mc/chat"
26-
"github.com/Tnze/go-mc/data/item"
2726
_ "github.com/Tnze/go-mc/data/lang/zh-cn"
27+
"github.com/Tnze/go-mc/data/registryid"
2828
"github.com/Tnze/go-mc/level"
2929
)
3030

@@ -161,11 +161,14 @@ func onScreenSlotChange(id, index int) error {
161161
container, ok := screenManager.Screens[id]
162162
if ok {
163163
// Currently, only inventory container is supported
164-
switch container.(type) {
164+
switch container := container.(type) {
165165
case *screen.Inventory:
166-
slot := container.(*screen.Inventory).Slots[index]
167-
itemInfo := item.ByID[item.ID(slot.ID)]
168-
log.Printf("Slot: Screen[%d].Slot[%d]: [%v] * %d | NBT: %v", id, index, itemInfo, slot.Count, slot.NBT)
166+
slot := container.Slots[index]
167+
itemName := "nil"
168+
if slot.ID >= 0 && int(slot.ID) < len(registryid.Item) {
169+
itemName = registryid.Item[slot.ID]
170+
}
171+
log.Printf("Slot: Screen[%d].Slot[%d]: [%v] * %d | NBT: %v", id, index, itemName, slot.Count, slot.NBT)
169172
}
170173
}
171174
}

registry/codec.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
pk "github.com/Tnze/go-mc/net/packet"
1010
)
1111

12-
type NetworkCodec struct {
12+
type Registries struct {
1313
ChatType Registry[ChatType] `registry:"minecraft:chat_type"`
1414
DamageType Registry[DamageType] `registry:"minecraft:damage_type"`
1515
DimensionType Registry[Dimension] `registry:"minecraft:dimension_type"`
@@ -23,8 +23,8 @@ type NetworkCodec struct {
2323
JukeboxSong Registry[nbt.RawMessage] `registry:"minecraft:jukebox_song"`
2424
}
2525

26-
func NewNetworkCodec() NetworkCodec {
27-
return NetworkCodec{
26+
func NewNetworkCodec() Registries {
27+
return Registries{
2828
ChatType: NewRegistry[ChatType](),
2929
DamageType: NewRegistry[DamageType](),
3030
DimensionType: NewRegistry[Dimension](),
@@ -79,7 +79,7 @@ type RegistryCodec interface {
7979
ReadTagsFrom(r io.Reader) (int64, error)
8080
}
8181

82-
func (c *NetworkCodec) Registry(id string) RegistryCodec {
82+
func (c *Registries) Registry(id string) RegistryCodec {
8383
codecVal := reflect.ValueOf(c).Elem()
8484
codecTyp := codecVal.Type()
8585
numField := codecVal.NumField()

server/configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type ConfigHandler interface {
1313
}
1414

1515
type Configurations struct {
16-
Registries registry.NetworkCodec
16+
Registries registry.Registries
1717
}
1818

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

0 commit comments

Comments
 (0)