Add registry receiving function

This commit is contained in:
Tnze
2024-06-16 02:12:56 +08:00
parent 40b32dfdd7
commit 19945a6d0b
7 changed files with 158 additions and 27 deletions

View File

@ -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
}