optimize registry insertion
This commit is contained in:
@ -2,6 +2,7 @@ package bot
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/Tnze/go-mc/chat"
|
"github.com/Tnze/go-mc/chat"
|
||||||
@ -9,7 +10,6 @@ import (
|
|||||||
"github.com/Tnze/go-mc/nbt"
|
"github.com/Tnze/go-mc/nbt"
|
||||||
"github.com/Tnze/go-mc/net"
|
"github.com/Tnze/go-mc/net"
|
||||||
pk "github.com/Tnze/go-mc/net/packet"
|
pk "github.com/Tnze/go-mc/net/packet"
|
||||||
"github.com/Tnze/go-mc/registry"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfigHandler interface {
|
type ConfigHandler interface {
|
||||||
@ -159,6 +159,11 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
|
|||||||
return ConfigErr{ErrStage, err}
|
return ConfigErr{ErrStage, err}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
registry := c.Registries.Registry(string(registryID))
|
||||||
|
if registry == nil {
|
||||||
|
return ConfigErr{ErrStage, errors.New("unknown registry: " + string(registryID))}
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; i < int(length); i++ {
|
for i := 0; i < int(length); i++ {
|
||||||
var entryId pk.Identifier
|
var entryId pk.Identifier
|
||||||
var hasData pk.Boolean
|
var hasData pk.Boolean
|
||||||
@ -178,7 +183,7 @@ func (c *Client) joinConfiguration(conn *net.Conn) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigErr{ErrStage, err}
|
return ConfigErr{ErrStage, err}
|
||||||
}
|
}
|
||||||
err = registry.InsertNBTDataIntoRegistry(&c.Registries, string(registryID), string(entryId), data)
|
err = registry.InsertWithNBT(string(entryId), data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigErr{ErrStage, err}
|
return ConfigErr{ErrStage, err}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package registry
|
package registry
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/Tnze/go-mc/chat"
|
"github.com/Tnze/go-mc/chat"
|
||||||
@ -58,41 +56,22 @@ type Dimension struct {
|
|||||||
MonsterSpawnBlockLightLimit int32 `nbt:"monster_spawn_block_light_limit"`
|
MonsterSpawnBlockLightLimit int32 `nbt:"monster_spawn_block_light_limit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// InsertNBTDataIntoRegistry insert data (entry, data) into the registry.
|
func (c *NetworkCodec) Registry(id string) RegistryHandler {
|
||||||
// The codec should be a pointer of a struct. And the registry should be a field of the codec struct.
|
codecVal := reflect.ValueOf(c).Elem()
|
||||||
//
|
|
||||||
// This function is a temporary solution while the registry system isn't implemented well.
|
|
||||||
func InsertNBTDataIntoRegistry(codec any, registry, entry string, data nbt.RawMessage) error {
|
|
||||||
codecVal := reflect.ValueOf(codec)
|
|
||||||
if codecVal.Kind() != reflect.Pointer {
|
|
||||||
return errors.New("codec is not a pointer")
|
|
||||||
}
|
|
||||||
|
|
||||||
codecVal = codecVal.Elem()
|
|
||||||
if codecVal.Kind() != reflect.Struct {
|
|
||||||
return errors.New("codec is not a pointer of struct")
|
|
||||||
}
|
|
||||||
|
|
||||||
codecTyp := codecVal.Type()
|
codecTyp := codecVal.Type()
|
||||||
|
|
||||||
numField := codecVal.NumField()
|
numField := codecVal.NumField()
|
||||||
for i := 0; i < numField; i++ {
|
for i := 0; i < numField; i++ {
|
||||||
registryID, ok := codecTyp.Field(i).Tag.Lookup("registry")
|
registryID, ok := codecTyp.Field(i).Tag.Lookup("registry")
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if registryID == registry {
|
if registryID == id {
|
||||||
fieldVal := codecVal.Field(i).Addr()
|
return codecVal.Field(i).Addr().Interface().(RegistryHandler)
|
||||||
args := []reflect.Value{reflect.ValueOf(entry), reflect.ValueOf(data)}
|
|
||||||
err := fieldVal.MethodByName("InsertNBT").Call(args)[0]
|
|
||||||
if !err.IsNil() {
|
|
||||||
return err.Interface().(error)
|
|
||||||
}
|
}
|
||||||
if registry == "minecraft:chat_type" {
|
|
||||||
fmt.Println(fieldVal.Interface())
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return errors.New("registry " + registry + " not found in the codec")
|
type RegistryHandler interface {
|
||||||
|
InsertWithNBT(name string, data nbt.RawMessage) error
|
||||||
}
|
}
|
||||||
|
@ -35,16 +35,21 @@ func (r *Registry[E]) FindByID(id int32) *E {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry[E]) Insert(name string, data E) {
|
func (r *Registry[E]) Insert(name string, data E) {
|
||||||
r.Value = append(r.Value, Entry[E]{Name: name, Element: data})
|
entry := Entry[E]{
|
||||||
|
Name: name,
|
||||||
|
ID: int32(len(r.Value)),
|
||||||
|
Element: data,
|
||||||
|
}
|
||||||
|
r.Value = append(r.Value, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry[E]) InsertNBT(name string, data nbt.RawMessage) error {
|
func (r *Registry[E]) InsertWithNBT(name string, data nbt.RawMessage) error {
|
||||||
entry := Entry[E]{Name: name, ID: int32(len(r.Value))}
|
var elem E
|
||||||
if data.Type != 0 {
|
if data.Type != 0 {
|
||||||
if err := data.UnmarshalDisallowUnknownField(&entry.Element); err != nil {
|
if err := data.UnmarshalDisallowUnknownField(&elem); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r.Value = append(r.Value, entry)
|
r.Insert(name, elem)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user