update to 1.16 and removes usage of "io/ioutil"

This commit is contained in:
Tnze
2021-02-25 12:43:39 +08:00
parent 178c34eacb
commit 9b6f640843
10 changed files with 34 additions and 21 deletions

View File

@ -10,7 +10,7 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
@ -176,7 +176,7 @@ func loginAuth(AsTk, name, UUID string, shareSecret []byte, er encryptionRequest
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
body, _ := io.ReadAll(resp.Body)
if resp.Status != "204 No Content" {
return fmt.Errorf("auth fail: %s", string(body))
}

View File

@ -7,7 +7,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
@ -117,7 +116,7 @@ func pack(f, o string) {
checkerr(err)
defer r.Close()
mcc, err := ioutil.ReadFile(f)
mcc, err := os.ReadFile(f)
checkerr(err)
rx, rz := region.In(x, z)

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/Tnze/go-mc
go 1.13
go 1.16
require (
github.com/beefsack/go-astar v0.0.0-20200827232313-4ecf9e304482

View File

@ -3,7 +3,7 @@ package nbt
import (
"bytes"
"compress/gzip"
"io/ioutil"
"io"
"reflect"
"testing"
)
@ -225,7 +225,7 @@ func TestMarshal_bigTest(t *testing.T) {
}
rd, _ := gzip.NewReader(bytes.NewReader(bigTestData[:]))
want, err := ioutil.ReadAll(rd)
want, err := io.ReadAll(rd)
if err != nil {
t.Error(err)
}

View File

@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"reflect"
)
@ -382,7 +381,7 @@ func (d *Decoder) rawRead(tagType byte) error {
return err
}
if _, err = io.CopyN(ioutil.Discard, d.r, int64(aryLen)); err != nil {
if _, err = io.CopyN(io.Discard, d.r, int64(aryLen)); err != nil {
return err
}
case TagIntArray:

View File

@ -101,21 +101,21 @@ func UnCompress(data []byte) (*Packet, error) {
return nil, err
}
uncompressData := make([]byte, sizeUncompressed)
uncompressedData := make([]byte, sizeUncompressed)
if sizeUncompressed != 0 { // != 0 means compressed, let's decompress
r, err := zlib.NewReader(reader)
if err != nil {
return nil, fmt.Errorf("decompress fail: %v", err)
}
defer r.Close()
_, err = io.ReadFull(r, uncompressData)
_, err = io.ReadFull(r, uncompressedData)
if err != nil {
return nil, fmt.Errorf("decompress fail: %v", err)
}
} else {
uncompressData = data[1:]
uncompressedData = data[1:]
}
buf := bytes.NewBuffer(uncompressData)
buf := bytes.NewBuffer(uncompressedData)
var packetID VarInt
if err := packetID.Decode(buf); err != nil {
return nil, fmt.Errorf("read packet id fail: %v", err)
@ -130,7 +130,11 @@ func UnCompress(data []byte) (*Packet, error) {
func Compress(data []byte) []byte {
var b bytes.Buffer
w := zlib.NewWriter(&b)
w.Write(data)
w.Close()
if _, err := w.Write(data); err != nil {
panic(err)
}
if err := w.Close(); err != nil {
panic(err)
}
return b.Bytes()
}

View File

@ -1,6 +1,7 @@
package packet
import (
"bytes"
"errors"
"io"
"math"
@ -76,6 +77,7 @@ type (
UUID uuid.UUID
//NBT encode a value as Named Binary Tag
//Tips: define your own struct and implement pk.Field for better performance
NBT struct {
V interface{}
}
@ -407,6 +409,15 @@ func (d *Double) Decode(r DecodeReader) error {
return nil
}
// Encode a NBT
func (n NBT) Encode() []byte {
var buf bytes.Buffer
if err := nbt.NewEncoder(&buf).Encode(n.V); err != nil {
panic(err)
}
return buf.Bytes()
}
// Decode a NBT
func (n NBT) Decode(r DecodeReader) error {
return nbt.NewDecoder(r).Decode(n.V)

View File

@ -1,7 +1,7 @@
package ptypes
import (
"io/ioutil"
"io"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data/packetid"
@ -64,7 +64,7 @@ func (p PluginData) Encode() []byte {
}
func (p *PluginData) Decode(r pk.DecodeReader) error {
d, err := ioutil.ReadAll(r)
d, err := io.ReadAll(r)
if err != nil {
return err
}

View File

@ -1,6 +1,6 @@
package realms
import "io/ioutil"
import "io"
// Available returns whether the user can access the Minecraft Realms service
func (r *Realms) Available() (ok bool, err error) {
@ -19,7 +19,7 @@ func (r *Realms) Compatible() (string, error) {
}
defer resp.Body.Close()
rp, err := ioutil.ReadAll(resp.Body)
rp, err := io.ReadAll(resp.Body)
return string(rp), err
}

View File

@ -2,7 +2,7 @@ package yggdrasil
import (
"fmt"
"io/ioutil"
"io"
)
// Validate checks if an accessToken is usable for authentication with a Minecraft server.
@ -28,7 +28,7 @@ func (a *Access) Invalidate() error {
defer resp.Body.Close()
if resp.StatusCode != 204 {
content, _ := ioutil.ReadAll(resp.Body)
content, _ := io.ReadAll(resp.Body)
return fmt.Errorf("invalidate error: %v: %s", resp.Status, content)
}