Fix some warnings and typos

This commit is contained in:
Tnze
2021-02-18 14:07:30 +08:00
parent 750f87e780
commit bc9cd93066
23 changed files with 102 additions and 86 deletions

View File

@ -40,24 +40,24 @@ func (p Packet) Scan(fields ...FieldDecoder) error {
// Pack 打包一个数据包
func (p *Packet) Pack(threshold int) (pack []byte) {
data := append(VarInt(p.ID).Encode(), p.Data...)
d := append(VarInt(p.ID).Encode(), p.Data...)
if threshold > 0 { //是否启用了压缩
if len(data) > threshold { //是否需要压缩
Len := len(data)
if len(d) > threshold { //是否需要压缩
Len := len(d)
VarLen := VarInt(Len).Encode()
data = Compress(data)
d = Compress(d)
pack = append(pack, VarInt(len(VarLen)+len(data)).Encode()...)
pack = append(pack, VarInt(len(VarLen)+len(d)).Encode()...)
pack = append(pack, VarLen...)
pack = append(pack, data...)
pack = append(pack, d...)
} else {
pack = append(pack, VarInt(int32(len(data)+1)).Encode()...)
pack = append(pack, VarInt(int32(len(d)+1)).Encode()...)
pack = append(pack, 0x00)
pack = append(pack, data...)
pack = append(pack, d...)
}
} else {
pack = append(pack, VarInt(int32(len(data))).Encode()...) //len
pack = append(pack, data...)
pack = append(pack, VarInt(int32(len(d))).Encode()...) //len
pack = append(pack, d...)
}
return
@ -73,17 +73,17 @@ func RecvPacket(r DecodeReader, useZlib bool) (*Packet, error) {
return nil, fmt.Errorf("packet length too short")
}
data := make([]byte, length) // read packet content
if _, err := io.ReadFull(r, data); err != nil {
d := make([]byte, length) // read packet content
if _, err := io.ReadFull(r, d); err != nil {
return nil, fmt.Errorf("read content of packet fail: %v", err)
}
//解压数据
if useZlib {
return UnCompress(data)
return UnCompress(d)
}
buf := bytes.NewBuffer(data)
buf := bytes.NewBuffer(d)
var packetID VarInt
if err := packetID.Decode(buf); err != nil {
return nil, fmt.Errorf("read packet id fail: %v", err)

View File

@ -10,7 +10,7 @@ import (
pk "github.com/Tnze/go-mc/net/packet"
)
// ChunkData is a clientbound packet which describes a chunk.
// ChunkData is a client-bound packet which describes a chunk.
type ChunkData struct {
X, Z pk.Int
FullChunk pk.Boolean
@ -24,10 +24,10 @@ type ChunkData struct {
func (p *ChunkData) Decode(pkt pk.Packet) error {
r := bytes.NewReader(pkt.Data)
if err := p.X.Decode(r); err != nil {
return fmt.Errorf("X: %v", err)
return fmt.Errorf("decode X: %v", err)
}
if err := p.Z.Decode(r); err != nil {
return fmt.Errorf("Z: %v", err)
return fmt.Errorf("decode Z: %v", err)
}
if err := p.FullChunk.Decode(r); err != nil {
return fmt.Errorf("full chunk: %v", err)
@ -39,7 +39,7 @@ func (p *ChunkData) Decode(pkt pk.Packet) error {
return fmt.Errorf("heightmaps: %v", err)
}
// Biome data is only present for full chunks.
// Biomes data is only present for full chunks.
if p.FullChunk {
if err := p.Biomes.Decode(r); err != nil {
return fmt.Errorf("heightmaps: %v", err)
@ -60,13 +60,13 @@ type biomesData struct {
}
func (b *biomesData) Decode(r pk.DecodeReader) error {
var nobd pk.VarInt // Number of Biome Datums
if err := nobd.Decode(r); err != nil {
var BiomesDataNums pk.VarInt // Number of Biomes Data
if err := BiomesDataNums.Decode(r); err != nil {
return err
}
b.data = make([]pk.VarInt, nobd)
b.data = make([]pk.VarInt, BiomesDataNums)
for i := 0; i < int(nobd); i++ {
for i := 0; i < int(BiomesDataNums); i++ {
var d pk.VarInt
if err := d.Decode(r); err != nil {
return err

View File

@ -2,7 +2,7 @@ package ptypes
import pk "github.com/Tnze/go-mc/net/packet"
// SpawnEntity is a clientbound packet used to spawn a non-mob entity.
// SpawnEntity is a client-bound packet used to spawn a non-mob entity.
type SpawnEntity struct {
ID pk.VarInt
UUID pk.UUID
@ -19,7 +19,7 @@ func (p *SpawnEntity) Decode(pkt pk.Packet) error {
&p.Data, &p.VelX, &p.VelY, &p.VelZ)
}
// SpawnPlayer is a clientbound packet used to describe a player entering
// SpawnPlayer is a client-bound packet used to describe a player entering
// visible range.
type SpawnPlayer struct {
ID pk.VarInt
@ -32,7 +32,7 @@ func (p *SpawnPlayer) Decode(pkt pk.Packet) error {
return pkt.Scan(&p.ID, &p.UUID, &p.X, &p.Y, &p.Z, &p.Yaw, &p.Pitch)
}
// SpawnLivingEntity is a clientbound packet used to spawn a mob.
// SpawnLivingEntity is a client-bound packet used to spawn a mob.
type SpawnLivingEntity struct {
ID pk.VarInt
UUID pk.UUID
@ -49,7 +49,7 @@ func (p *SpawnLivingEntity) Decode(pkt pk.Packet) error {
&p.HeadPitch, &p.VelX, &p.VelY, &p.VelZ)
}
// EntityAnimationClientbound updates the animationf state of an entity.
// EntityAnimationClientbound updates the animation state of an entity.
type EntityAnimationClientbound struct {
ID pk.VarInt
Animation pk.UnsignedByte

View File

@ -8,7 +8,7 @@ import (
pk "github.com/Tnze/go-mc/net/packet"
)
// SoundEffect is a clientbound packet used to play a specific sound ID
// SoundEffect is a client-bound packet used to play a specific sound ID
// on the client.
type SoundEffect struct {
Sound pk.VarInt
@ -21,7 +21,7 @@ func (p *SoundEffect) Decode(pkt pk.Packet) error {
return pkt.Scan(&p.Sound, &p.Category, &p.X, &p.Y, &p.Z, &p.Volume, &p.Pitch)
}
// NamedSoundEffect is a clientbound packet used to play a sound with the
// NamedSoundEffect is a client-bound packet used to play a sound with the
// specified name on the client.
type NamedSoundEffect struct {
Sound pk.String
@ -64,11 +64,11 @@ func (p PluginData) Encode() []byte {
}
func (p *PluginData) Decode(r pk.DecodeReader) error {
data, err := ioutil.ReadAll(r)
d, err := ioutil.ReadAll(r)
if err != nil {
return err
}
*p = data
*p = d
return nil
}