diff --git a/chat/message.go b/chat/message.go index aedf2a2..0f0ff7c 100644 --- a/chat/message.go +++ b/chat/message.go @@ -246,7 +246,7 @@ func (m Message) ClearString() string { // handle translate if m.Translate != "" { - args := make([]interface{}, len(m.With)) + args := make([]any, len(m.With)) for i, v := range m.With { args[i] = v.ClearString() } @@ -291,7 +291,7 @@ func (m Message) String() string { // handle translate if m.Translate != "" { - args := make([]interface{}, len(m.With)) + args := make([]any, len(m.With)) for i, v := range m.With { args[i] = v } diff --git a/nbt/decode.go b/nbt/decode.go index b63858e..48f59d1 100644 --- a/nbt/decode.go +++ b/nbt/decode.go @@ -12,7 +12,7 @@ import ( // Unmarshal decode binary NBT data and fill into v // This is a shortcut to `NewDecoder(bytes.NewReader(data)).Decode(v)`. -func Unmarshal(data []byte, v interface{}) error { +func Unmarshal(data []byte, v any) error { _, err := NewDecoder(bytes.NewReader(data)).Decode(v) return err } @@ -28,7 +28,7 @@ func Unmarshal(data []byte, v interface{}) error { // // This method also return tag name of the root tag. // In real world, it is often empty, but the API should allow you to get it when ever you want. -func (d *Decoder) Decode(v interface{}) (string, error) { +func (d *Decoder) Decode(v any) (string, error) { val := reflect.ValueOf(v) if val.Kind() != reflect.Ptr { return "", errors.New("nbt: non-pointer passed to Decode") @@ -329,7 +329,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error { default: return errors.New("cannot parse TagList as " + vk.String()) case reflect.Interface: - buf = reflect.ValueOf(make([]interface{}, listLen)) + buf = reflect.ValueOf(make([]any, listLen)) case reflect.Slice: buf = reflect.MakeSlice(val.Type(), int(listLen), int(listLen)) case reflect.Array: @@ -398,7 +398,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error { val.SetMapIndex(reflect.ValueOf(tn), v.Elem()) } case reflect.Interface: - buf := make(map[string]interface{}) + buf := make(map[string]any) for { tt, tn, err := d.readTag() if err != nil { @@ -407,7 +407,7 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte) error { if tt == TagEnd { break } - var value interface{} + var value any if err = d.unmarshal(reflect.ValueOf(&value).Elem(), tt); err != nil { return fmt.Errorf("fail to decode tag %q: %w", tn, err) } @@ -467,7 +467,7 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnm } // Prevent infinite loop if v is an interface pointing to its own address: - // var v interface{} + // var v any // v = &v if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v { v = v.Elem() diff --git a/nbt/decode_test.go b/nbt/decode_test.go index 29ace2d..3d38de7 100644 --- a/nbt/decode_test.go +++ b/nbt/decode_test.go @@ -27,8 +27,8 @@ func TestUnmarshal_string(t *testing.T) { t.Errorf("Unmarshal NBT fail: get %q, want %q", Name, "Bananrama") } - // Unmarshal to interface{} - var infName interface{} + // Unmarshal to any + var infName any if err := Unmarshal(data, &infName); err != nil { t.Fatal(err) } @@ -210,7 +210,7 @@ func TestDecoder_Decode_bigTest(t *testing.T) { t.Fatal(err) } - var inf interface{} + var inf any r, err = gzip.NewReader(bytes.NewReader(bigTestData[:])) if err != nil { t.Fatal(err) @@ -313,7 +313,7 @@ func TestDecoder_Decode_LongArray(t *testing.T) { } var ( value []int64 - infValue interface{} + infValue any want = []int64{1, 2, 3} ) @@ -342,7 +342,7 @@ func TestDecoder_Decode_ByteArray(t *testing.T) { } var ( value []byte - infValue interface{} + infValue any want = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07} ) @@ -355,7 +355,7 @@ func TestDecoder_Decode_ByteArray(t *testing.T) { } // t.Log(value) - // Unmarshal to interface{} + // Unmarshal to any if err := Unmarshal(data, &infValue); err != nil { t.Fatal(err) } diff --git a/nbt/encode.go b/nbt/encode.go index a27ebfe..e6b02cf 100644 --- a/nbt/encode.go +++ b/nbt/encode.go @@ -16,7 +16,7 @@ import ( // Marshal is the shortcut of NewEncoder().Encode() with empty tag name. // Notices that repeatedly init buffers is low efficiency. // Using Encoder and Reset the buffer in each time is recommended in that cases. -func Marshal(v interface{}) ([]byte, error) { +func Marshal(v any) ([]byte, error) { var buf bytes.Buffer err := NewEncoder(&buf).Encode(v, "") return buf.Bytes(), err @@ -39,7 +39,7 @@ func NewEncoder(w io.Writer) *Encoder { // expect `[]int8`, `[]int32`, `[]int64`, `[]uint8`, `[]uint32` and `[]uint64`, // which TagByteArray, TagIntArray and TagLongArray. // To force encode them as TagList, add a struct field tag. -func (e *Encoder) Encode(v interface{}, tagName string) error { +func (e *Encoder) Encode(v any, tagName string) error { t, val := getTagType(reflect.ValueOf(v)) return e.marshal(val, t, tagName) } @@ -242,7 +242,7 @@ func getTagType(v reflect.Value) (byte, reflect.Value) { } // Prevent infinite loop if v is an interface pointing to its own address: - // var v interface{} + // var v any // v = &v if v.Elem().Kind() == reflect.Interface && v.Elem().Elem() == v { v = v.Elem() diff --git a/nbt/encode_test.go b/nbt/encode_test.go index 3873753..4aaa732 100644 --- a/nbt/encode_test.go +++ b/nbt/encode_test.go @@ -112,12 +112,12 @@ func TestEncoder_Encode_interfaceArray(t *testing.T) { tests := []struct { name string - args []interface{} + args []any want []byte }{ { name: "Two element interface array", - args: []interface{}{Struct1{3}, Struct2{0.3}}, + args: []any{Struct1{3}, Struct2{0.3}}, want: []byte{ TagList, 0x00, 0x00 /*no name*/, TagCompound, 0, 0, 0, 2, // 1st element @@ -136,7 +136,7 @@ func TestEncoder_Encode_interfaceArray(t *testing.T) { if err != nil { t.Error(err) } else if !bytes.Equal(data, tt.want) { - t.Errorf("Marshal([]interface{}) got = % 02x, want % 02x", data, tt.want) + t.Errorf("Marshal([]any) got = % 02x, want % 02x", data, tt.want) return } }) @@ -242,7 +242,7 @@ func TestEncoder_Encode_map(t *testing.T) { } func TestEncoder_Encode_interface(t *testing.T) { - data := map[string]interface{}{ + data := map[string]any{ "Key": int32(12), "Value": "Tnze", } diff --git a/nbt/rawmsg.go b/nbt/rawmsg.go index 6976e71..6c512d5 100644 --- a/nbt/rawmsg.go +++ b/nbt/rawmsg.go @@ -62,7 +62,7 @@ func (m RawMessage) String() string { } // Unmarshal decode the data into v. -func (m RawMessage) Unmarshal(v interface{}) error { +func (m RawMessage) Unmarshal(v any) error { d := NewDecoder(bytes.NewReader(m.Data)) val := reflect.ValueOf(v) if val.Kind() != reflect.Ptr { diff --git a/net/packet/packet.go b/net/packet/packet.go index a0ab2bf..c32ec00 100644 --- a/net/packet/packet.go +++ b/net/packet/packet.go @@ -38,7 +38,7 @@ func (p Packet) Scan(fields ...FieldDecoder) error { } var bufPool = sync.Pool{ - New: func() interface{} { + New: func() any { return new(bytes.Buffer) }, } diff --git a/net/packet/packet_test.go b/net/packet/packet_test.go index 5050785..b8a5082 100644 --- a/net/packet/packet_test.go +++ b/net/packet/packet_test.go @@ -146,10 +146,10 @@ func ExamplePacket_Scan_joinGame() { PreGamemode pk.Byte WorldNames = []pk.Identifier{} // This cannot replace with "var DimensionNames []pk.Identifier" because "nil" has no type information DimensionCodec struct { - DimensionType interface{} `nbt:"minecraft:dimension_type"` - WorldgenBiome interface{} `nbt:"minecraft:worldgen/biome"` + DimensionType any `nbt:"minecraft:dimension_type"` + WorldgenBiome any `nbt:"minecraft:worldgen/biome"` } - Dimension interface{} + Dimension any WorldName pk.Identifier HashedSeed pk.Long MaxPlayers pk.VarInt @@ -181,7 +181,7 @@ func ExampleMarshal_setSlot() { Present bool ItemID int ItemCount byte - NBT interface{} + NBT any }{ {WindowID: 0, Slot: 5, Present: false}, {WindowID: 0, Slot: 5, Present: true, ItemID: 0x01, ItemCount: 1, NBT: pk.Byte(0)}, diff --git a/net/packet/types.go b/net/packet/types.go index 9032254..34b8764 100644 --- a/net/packet/types.go +++ b/net/packet/types.go @@ -467,7 +467,7 @@ func (d *Double) ReadFrom(r io.Reader) (n int64, err error) { } // NBT encode a value as Named Binary Tag -func NBT(v interface{}, optionalTagName ...string) Field { +func NBT(v any, optionalTagName ...string) Field { if len(optionalTagName) > 0 { return nbtField{V: v, FieldName: optionalTagName[0]} } diff --git a/net/packet/util.go b/net/packet/util.go index e6e2bbb..3c7b1aa 100644 --- a/net/packet/util.go +++ b/net/packet/util.go @@ -26,7 +26,7 @@ var ( // // Note that Ary DO read or write the Len. You aren't need to do so by your self. type Ary[LEN VarInt | VarLong | Byte | UnsignedByte | Short | UnsignedShort | Int | Long] struct { - Ary interface{} // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field) + Ary any // Slice or Pointer of Slice of FieldEncoder, FieldDecoder or both (Field) } func (a Ary[LEN]) WriteTo(w io.Writer) (n int64, err error) { diff --git a/net/rcon.go b/net/rcon.go index 9aa68f5..834e94c 100644 --- a/net/rcon.go +++ b/net/rcon.go @@ -90,7 +90,7 @@ func (r *RCONConn) ReadPacket() (RequestID, Type int32, Payload string, err erro func (r *RCONConn) WritePacket(RequestID, Type int32, Payload string) error { buf := new(bytes.Buffer) - for _, v := range []interface{}{ + for _, v := range []any{ int32(4 + 4 + len(Payload) + 2), // Length RequestID, // Request ID Type, // Type diff --git a/realms/realms.go b/realms/realms.go index 23b40c5..11ea1aa 100644 --- a/realms/realms.go +++ b/realms/realms.go @@ -52,7 +52,7 @@ func New(version, user, astk, uuid string) *Realms { return r } -func (r *Realms) get(endpoint string, resp interface{}) error { +func (r *Realms) get(endpoint string, resp any) error { rawResp, err := r.c.Get(Domain + endpoint) if err != nil { return err @@ -67,7 +67,7 @@ func (r *Realms) get(endpoint string, resp interface{}) error { return nil } -func (r *Realms) post(endpoint string, payload, resp interface{}) error { +func (r *Realms) post(endpoint string, payload, resp any) error { data, err := json.Marshal(payload) if err != nil { return err diff --git a/realms/server.go b/realms/server.go index f794da7..34f43c0 100644 --- a/realms/server.go +++ b/realms/server.go @@ -23,7 +23,7 @@ type Server struct { MiniGameID *int MinigameImage *string ActiveSlot int - // Slots interface{} + // Slots any Member bool } diff --git a/save/dimension.go b/save/dimension.go index 995b4aa..4e2c801 100644 --- a/save/dimension.go +++ b/save/dimension.go @@ -38,8 +38,8 @@ type WorldGenSettings struct { } type DimensionGenerator struct { - Type string `nbt:"type"` - Generator map[string]interface{} `nbt:"generator"` + Type string `nbt:"type"` + Generator map[string]any `nbt:"generator"` } var ( @@ -47,7 +47,7 @@ var ( DefaultDimensionsGenerators = map[string]DimensionGenerator{ "minecraft:overworld": { Type: "minecraft:overworld", - Generator: map[string]interface{}{ + Generator: map[string]any{ "type": "minecraft:noise", "settings": "minecraft:overworld", "biome_source": map[string]string{ @@ -58,7 +58,7 @@ var ( }, "minecraft:the_end": { Type: "minecraft:the_end", - Generator: map[string]interface{}{ + Generator: map[string]any{ "type": "minecraft:noise", "settings": "minecraft:end", "biome_source": map[string]string{ @@ -68,7 +68,7 @@ var ( }, "minecraft:the_nether": { Type: "minecraft:the_nether", - Generator: map[string]interface{}{ + Generator: map[string]any{ "type": "minecraft:noise", "settings": "minecraft:nether", "biome_source": map[string]string{ diff --git a/save/level.go b/save/level.go index c8575c2..0046ff7 100644 --- a/save/level.go +++ b/save/level.go @@ -46,7 +46,7 @@ type LevelData struct { LastPlayed int64 LevelName string MapFeatures bool - Player map[string]interface{} + Player map[string]any Raining bool `nbt:"raining"` RainTime int32 `nbt:"rainTime"` RandomSeed int64 diff --git a/save/playerdata.go b/save/playerdata.go index f3a508d..d842bdc 100644 --- a/save/playerdata.go +++ b/save/playerdata.go @@ -72,8 +72,8 @@ type PlayerData struct { type Item struct { Count byte Slot byte - ID string `nbt:"id"` - Tag map[string]interface{} `nbt:"tag"` + ID string `nbt:"id"` + Tag map[string]any `nbt:"tag"` } func ReadPlayerData(r io.Reader) (data PlayerData, err error) { diff --git a/save/region/mca_test.go b/save/region/mca_test.go index a60a16b..85d7be2 100644 --- a/save/region/mca_test.go +++ b/save/region/mca_test.go @@ -43,7 +43,7 @@ func TestReadRegion(t *testing.T) { if err != nil { t.Error(err) } - var b interface{} + var b any _, err = nbt.NewDecoder(r).Decode(&b) if err != nil { t.Error(err) diff --git a/server/command/command.go b/server/command/command.go index 60553f9..1d79659 100644 --- a/server/command/command.go +++ b/server/command/command.go @@ -54,7 +54,7 @@ func (g *Graph) Execute(ctx context.Context, cmd string) error { } } -type ParsedData interface{} +type ParsedData any type HandlerFunc func(ctx context.Context, args []ParsedData) error diff --git a/server/internal/bvh/bvh.go b/server/internal/bvh/bvh.go index 9ae6469..0bb688c 100644 --- a/server/internal/bvh/bvh.go +++ b/server/internal/bvh/bvh.go @@ -121,7 +121,7 @@ func (t *Tree[I, B, V]) Insert(leaf B, value V) (n *Node[I, B, V]) { return } -func (t *Tree[I, B, V]) Delete(n *Node[I, B, V]) interface{} { +func (t *Tree[I, B, V]) Delete(n *Node[I, B, V]) any { if n.parent == nil { // n is the root t.root = nil @@ -204,11 +204,11 @@ type ( } ) -func (h searchHeap[I, V]) Len() int { return len(h) } -func (h searchHeap[I, V]) Less(i, j int) bool { return h[i].inheritedCost < h[j].inheritedCost } -func (h searchHeap[I, V]) Swap(i, j int) { h[i], h[j] = h[j], h[i] } -func (h *searchHeap[I, V]) Push(x interface{}) { *h = append(*h, x.(searchItem[I, V])) } -func (h *searchHeap[I, V]) Pop() interface{} { +func (h searchHeap[I, V]) Len() int { return len(h) } +func (h searchHeap[I, V]) Less(i, j int) bool { return h[i].inheritedCost < h[j].inheritedCost } +func (h searchHeap[I, V]) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h *searchHeap[I, V]) Push(x any) { *h = append(*h, x.(searchItem[I, V])) } +func (h *searchHeap[I, V]) Pop() any { old := *h n := len(old) x := old[n-1] diff --git a/yggdrasil/user/user.go b/yggdrasil/user/user.go index 913fe5a..dbc31ec 100644 --- a/yggdrasil/user/user.go +++ b/yggdrasil/user/user.go @@ -54,7 +54,7 @@ func fetchKeyPair(accessToken string) (KeyPairResp, error) { return keyPairResp, err } -func post(endpoint string, accessToken string, resp interface{}) error { +func post(endpoint string, accessToken string, resp any) error { rowResp, err := rawPost(endpoint, accessToken) if err != nil { return fmt.Errorf("request fail: %v", err) diff --git a/yggdrasil/yggdrasil.go b/yggdrasil/yggdrasil.go index 27c6bce..86788f0 100644 --- a/yggdrasil/yggdrasil.go +++ b/yggdrasil/yggdrasil.go @@ -28,7 +28,7 @@ var AuthURL = "https://authserver.mojang.com" var client = http.DefaultClient -func post(endpoint string, payload interface{}, resp interface{}) error { +func post(endpoint string, payload any, resp any) error { rowResp, err := rawPost(endpoint, payload) if err != nil { return fmt.Errorf("request fail: %v", err) @@ -43,7 +43,7 @@ func post(endpoint string, payload interface{}, resp interface{}) error { return nil } -func rawPost(endpoint string, payload interface{}) (*http.Response, error) { +func rawPost(endpoint string, payload any) (*http.Response, error) { data, err := json.Marshal(payload) if err != nil { return nil, fmt.Errorf("marshal payload fail: %v", err)