Add registry receiving function
This commit is contained in:
@ -1,17 +1,26 @@
|
||||
package registry
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"github.com/Tnze/go-mc/chat"
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
)
|
||||
|
||||
type NetworkCodec struct {
|
||||
ChatType Registry[ChatType] `nbt:"minecraft:chat_type"`
|
||||
DamageType Registry[DamageType] `nbt:"minecraft:damage_type"`
|
||||
DimensionType Registry[Dimension] `nbt:"minecraft:dimension_type"`
|
||||
TrimMaterial Registry[nbt.RawMessage] `nbt:"minecraft:trim_material"`
|
||||
TrimPattern Registry[nbt.RawMessage] `nbt:"minecraft:trim_pattern"`
|
||||
WorldGenBiome Registry[nbt.RawMessage] `nbt:"minecraft:worldgen/biome"`
|
||||
ChatType Registry[ChatType] `registry:"minecraft:chat_type"`
|
||||
DamageType Registry[DamageType] `registry:"minecraft:damage_type"`
|
||||
DimensionType Registry[Dimension] `registry:"minecraft:dimension_type"`
|
||||
TrimMaterial Registry[nbt.RawMessage] `registry:"minecraft:trim_material"`
|
||||
TrimPattern Registry[nbt.RawMessage] `registry:"minecraft:trim_pattern"`
|
||||
WorldGenBiome Registry[nbt.RawMessage] `registry:"minecraft:worldgen/biome"`
|
||||
Wolfvariant Registry[nbt.RawMessage] `registry:"minecraft:wolf_variant"`
|
||||
PaintingVariant Registry[nbt.RawMessage] `registry:"minecraft:painting_variant"`
|
||||
BannerPattern Registry[nbt.RawMessage] `registry:"minecraft:banner_pattern"`
|
||||
Enchantment Registry[nbt.RawMessage] `registry:"minecraft:enchantment"`
|
||||
JukeboxSong Registry[nbt.RawMessage] `registry:"minecraft:jukebox_song"`
|
||||
}
|
||||
|
||||
type ChatType struct {
|
||||
@ -48,3 +57,42 @@ type Dimension struct {
|
||||
MonsterSpawnLightLevel nbt.RawMessage `nbt:"monster_spawn_light_level"` // Tag_Int or {type:"minecraft:uniform", value:{min_inclusive: Tag_Int, max_inclusive: Tag_Int}}
|
||||
MonsterSpawnBlockLightLimit int32 `nbt:"monster_spawn_block_light_limit"`
|
||||
}
|
||||
|
||||
// InsertNBTDataIntoRegistry insert data (entry, data) into the registry.
|
||||
// The codec should be a pointer of a struct. And the registry should be a field of the codec struct.
|
||||
//
|
||||
// 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()
|
||||
|
||||
numField := codecVal.NumField()
|
||||
for i := 0; i < numField; i++ {
|
||||
registryID, ok := codecTyp.Field(i).Tag.Lookup("registry")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if registryID == registry {
|
||||
fieldVal := codecVal.Field(i).Addr()
|
||||
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 errors.New("registry " + registry + " not found in the codec")
|
||||
}
|
||||
|
@ -1,12 +1,16 @@
|
||||
package registry
|
||||
|
||||
import "github.com/Tnze/go-mc/nbt"
|
||||
|
||||
type Registry[E any] struct {
|
||||
Type string `nbt:"type"`
|
||||
Value []struct {
|
||||
Name string `nbt:"name"`
|
||||
ID int32 `nbt:"id"`
|
||||
Element E `nbt:"element"`
|
||||
} `nbt:"value"`
|
||||
Type string `nbt:"type"`
|
||||
Value []Entry[E] `nbt:"value"`
|
||||
}
|
||||
|
||||
type Entry[E any] struct {
|
||||
Name string `nbt:"name"`
|
||||
ID int32 `nbt:"id"`
|
||||
Element E `nbt:"element"`
|
||||
}
|
||||
|
||||
func (r *Registry[E]) Find(name string) (int32, *E) {
|
||||
@ -29,3 +33,18 @@ func (r *Registry[E]) FindByID(id int32) *E {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Registry[E]) Insert(name string, data E) {
|
||||
r.Value = append(r.Value, Entry[E]{Name: name, Element: data})
|
||||
}
|
||||
|
||||
func (r *Registry[E]) InsertNBT(name string, data nbt.RawMessage) error {
|
||||
entry := Entry[E]{Name: name, ID: int32(len(r.Value))}
|
||||
if data.Type != 0 {
|
||||
if err := data.UnmarshalDisallowUnknownField(&entry.Element); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
r.Value = append(r.Value, entry)
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user