Refactoring package go-mc/bot

This commit is contained in:
Tnze
2021-02-27 01:06:07 +08:00
parent e864580903
commit 3da9321f59
44 changed files with 564 additions and 3715 deletions

View File

@ -6,13 +6,13 @@ import (
)
type Ary struct {
Len Field // One of VarInt, VarLong, Int or Long
Ary interface{} // FieldEncoder, FieldDecoder or both (Field)
Len Field // Pointer of VarInt, VarLong, Int or Long
Ary interface{} // Slice of FieldEncoder, FieldDecoder or both (Field)
}
func (a Ary) WriteTo(r io.Writer) (n int64, err error) {
length := int(reflect.ValueOf(a.Len).Int())
array := reflect.ValueOf(a.Ary).Elem()
array := reflect.ValueOf(a.Ary)
for i := 0; i < length; i++ {
elem := array.Index(i)
nn, err := elem.Interface().(FieldEncoder).WriteTo(r)
@ -25,11 +25,15 @@ func (a Ary) WriteTo(r io.Writer) (n int64, err error) {
}
func (a Ary) ReadFrom(r io.Reader) (n int64, err error) {
length := int(reflect.ValueOf(a.Len).Int())
length := int(reflect.ValueOf(a.Len).Elem().Int())
array := reflect.ValueOf(a.Ary).Elem()
if array.Cap() < length {
array = reflect.MakeSlice(array.Type(), length, length)
a.Ary = array.Interface()
}
for i := 0; i < length; i++ {
elem := array.Index(i)
nn, err := elem.Interface().(FieldDecoder).ReadFrom(r)
nn, err := elem.Addr().Interface().(FieldDecoder).ReadFrom(r)
n += nn
if err != nil {
return n, err