Regenerate files, moves packet id from data package

This commit is contained in:
Tnze
2021-02-18 21:29:16 +08:00
parent b4fc573118
commit ea0e0d1cae
19 changed files with 181 additions and 1379 deletions

View File

@ -9,7 +9,7 @@ import (
"github.com/Tnze/go-mc/bot/world"
"github.com/Tnze/go-mc/bot/world/entity"
"github.com/Tnze/go-mc/bot/world/entity/player"
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/net"
"github.com/Tnze/go-mc/net/packet"
pk "github.com/Tnze/go-mc/net/packet"
@ -50,7 +50,7 @@ func (c *Client) Close() error {
}
func (c *Client) SendCloseWindow(windowID byte) error {
return c.conn.WritePacket(packet.Marshal(data.CloseWindowServerbound, pk.UnsignedByte(windowID)))
return c.conn.WritePacket(packet.Marshal(packetid.CloseWindowServerbound, pk.UnsignedByte(windowID)))
}
// NewClient init and return a new Client.

View File

@ -3,6 +3,7 @@ package bot
import (
"encoding/hex"
"fmt"
"github.com/Tnze/go-mc/offline"
"github.com/Tnze/go-mc/yggdrasil"
"log"
)
@ -21,7 +22,7 @@ func ExampleClient_JoinServer_offline() {
c := NewClient()
c.Auth.Name = "Tnze" // set it's name before login.
id := OfflineUUID(c.Auth.Name) // optional, get uuid of offline mode game
id := offline.NameToUUID(c.Auth.Name) // optional, get uuid of offline mode game
c.Auth.UUID = hex.EncodeToString(id[:])
//Login
@ -77,7 +78,7 @@ func ExampleClient_JoinServer_online() {
}
func ExampleOfflineUUID() {
fmt.Println(OfflineUUID("Tnze"))
fmt.Println(offline.NameToUUID("Tnze"))
// output:
// c7b9eece-2f2e-325c-8da8-6fc8f3d0edb0

View File

@ -3,7 +3,6 @@ package bot
import (
"bytes"
"fmt"
"net"
"net/http"
"os"
"time"
@ -14,35 +13,42 @@ import (
"github.com/Tnze/go-mc/bot/world/entity"
"github.com/Tnze/go-mc/bot/world/entity/player"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/data/soundid"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/net/ptypes"
)
func (c *Client) updateServerPos(pos player.Pos) error {
func (c *Client) updateServerPos(pos player.Pos) (err error) {
prev := c.Player.Pos
c.Player.Pos = pos
switch {
case !prev.LookEqual(pos) && !prev.PosEqual(pos):
sendPlayerPositionAndLookPacket(c)
err = sendPlayerPositionAndLookPacket(c)
case !prev.PosEqual(pos):
sendPlayerPositionPacket(c)
err = sendPlayerPositionPacket(c)
case !prev.LookEqual(pos):
sendPlayerLookPacket(c)
err = sendPlayerLookPacket(c)
case prev.OnGround != pos.OnGround:
c.conn.WritePacket(
err = c.conn.WritePacket(
pk.Marshal(
data.Flying,
packetid.Flying,
pk.Boolean(pos.OnGround),
),
)
}
if err != nil {
return err
}
if c.justTeleported || time.Now().Add(-time.Second).After(c.lastPosTx) {
c.justTeleported = false
c.lastPosTx = time.Now()
sendPlayerPositionPacket(c)
err = sendPlayerPositionPacket(c)
if err != nil {
return err
}
}
if c.Events.PositionChange != nil && !prev.Equal(pos) {
@ -68,9 +74,6 @@ func (c *Client) HandleGame() error {
//Read packets
p, err := c.conn.ReadPacket()
if err != nil {
if e, ok := err.(*net.OpError); ok && e.Err.Error() != "use of closed network connection" {
fmt.Fprintf(os.Stderr, "ReadPacket error: %v\n", err)
}
return
}
c.inbound <- p
@ -126,90 +129,90 @@ func (c *Client) handlePacket(p pk.Packet) (disconnect bool, err error) {
}
}
switch data.PktID(p.ID) {
case data.Login:
switch p.ID {
case packetid.Login:
err = handleJoinGamePacket(c, p)
case data.CustomPayloadClientbound:
case packetid.CustomPayloadClientbound:
err = handlePluginPacket(c, p)
case data.Difficulty:
case packetid.Difficulty:
err = handleServerDifficultyPacket(c, p)
case data.SpawnPosition:
case packetid.SpawnPosition:
err = handleSpawnPositionPacket(c, p)
if err2 := c.Events.updateSeenPackets(seenSpawnPos); err == nil {
err = err2
}
case data.AbilitiesClientbound:
case packetid.AbilitiesClientbound:
err = handlePlayerAbilitiesPacket(c, p)
case data.UpdateHealth:
case packetid.UpdateHealth:
err = handleUpdateHealthPacket(c, p)
case data.ChatClientbound:
case packetid.ChatClientbound:
err = handleChatMessagePacket(c, p)
case data.HeldItemSlotClientbound:
case packetid.HeldItemSlotClientbound:
err = handleHeldItemPacket(c, p)
case data.WindowItems:
case packetid.WindowItems:
err = handleWindowItemsPacket(c, p)
case data.OpenWindow:
case packetid.OpenWindow:
err = handleOpenWindowPacket(c, p)
case data.TransactionClientbound:
case packetid.TransactionClientbound:
err = handleWindowConfirmationPacket(c, p)
case data.DeclareRecipes:
case packetid.DeclareRecipes:
// handleDeclareRecipesPacket(g, reader)
case data.KeepAliveClientbound:
case packetid.KeepAliveClientbound:
err = handleKeepAlivePacket(c, p)
case data.SpawnEntity:
case packetid.SpawnEntity:
err = handleSpawnEntityPacket(c, p)
case data.NamedEntitySpawn:
case packetid.NamedEntitySpawn:
err = handleSpawnPlayerPacket(c, p)
case data.SpawnEntityLiving:
case packetid.SpawnEntityLiving:
err = handleSpawnLivingEntityPacket(c, p)
case data.Animation:
case packetid.Animation:
err = handleEntityAnimationPacket(c, p)
case data.EntityStatus:
case packetid.EntityStatus:
err = handleEntityStatusPacket(c, p)
case data.EntityDestroy:
case packetid.EntityDestroy:
err = handleDestroyEntitiesPacket(c, p)
case data.RelEntityMove:
case packetid.RelEntityMove:
err = handleEntityPositionPacket(c, p)
case data.EntityMoveLook:
case packetid.EntityMoveLook:
err = handleEntityPositionLookPacket(c, p)
case data.EntityLook:
case packetid.EntityLook:
err = handleEntityLookPacket(c, p)
case data.Entity:
case packetid.Entity:
err = handleEntityMovePacket(c, p)
case data.UpdateLight:
case packetid.UpdateLight:
err = c.Events.updateSeenPackets(seenUpdateLight)
case data.MapChunk:
case packetid.MapChunk:
err = handleChunkDataPacket(c, p)
case data.BlockChange:
case packetid.BlockChange:
err = handleBlockChangePacket(c, p)
case data.MultiBlockChange:
case packetid.MultiBlockChange:
err = handleMultiBlockChangePacket(c, p)
case data.UnloadChunk:
case packetid.UnloadChunk:
err = handleUnloadChunkPacket(c, p)
case data.TileEntityData:
case packetid.TileEntityData:
err = handleTileEntityDataPacket(c, p)
case data.PositionClientbound:
case packetid.PositionClientbound:
err = handlePlayerPositionAndLookPacket(c, p)
sendPlayerPositionAndLookPacket(c) // to confirm the position
if err2 := c.Events.updateSeenPackets(seenPlayerPositionAndLook); err == nil {
err = err2
}
case data.KickDisconnect:
case packetid.KickDisconnect:
err = handleDisconnectPacket(c, p)
disconnect = true
case data.SetSlot:
case packetid.SetSlot:
err = handleSetSlotPacket(c, p)
case data.SoundEffect:
case packetid.SoundEffect:
err = handleSoundEffect(c, p)
case data.NamedSoundEffect:
case packetid.NamedSoundEffect:
err = handleNamedSoundEffect(c, p)
case data.Experience:
case packetid.Experience:
err = handleSetExperience(c, p)
}
return
@ -497,7 +500,7 @@ func handleJoinGamePacket(c *Client, p pk.Packet) error {
c.conn.WritePacket(
//PluginMessage packet (serverbound) - sending minecraft brand.
pk.Marshal(
data.CustomPayloadServerbound,
packetid.CustomPayloadServerbound,
pk.Identifier("minecraft:brand"),
pk.String(c.settings.Brand),
),
@ -572,7 +575,7 @@ func handlePlayerAbilitiesPacket(c *Client, p pk.Packet) error {
c.conn.WritePacket(
//ClientSettings packet (serverbound)
pk.Marshal(
data.Settings,
packetid.Settings,
pk.String(c.settings.Locale),
pk.Byte(c.settings.ViewDistance),
pk.VarInt(c.settings.ChatMode),
@ -690,7 +693,7 @@ func handlePlayerPositionAndLookPacket(c *Client, p pk.Packet) error {
//Confirm
return c.conn.WritePacket(pk.Marshal(
data.TeleportConfirm,
packetid.TeleportConfirm,
pk.VarInt(pkt.TeleportID),
))
}
@ -702,7 +705,7 @@ func handleKeepAlivePacket(c *Client, p pk.Packet) error {
}
//Response
return c.conn.WritePacket(pk.Marshal(
data.KeepAliveServerbound,
packetid.KeepAliveServerbound,
KeepAliveID,
))
}

View File

@ -4,7 +4,6 @@ import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
@ -17,7 +16,6 @@ import (
"github.com/Tnze/go-mc/net/CFB8"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/google/uuid"
)
// Auth includes a account
@ -27,20 +25,6 @@ type Auth struct {
AsTk string
}
// OfflineUUID return the UUID from player name in offline mode
func OfflineUUID(name string) uuid.UUID {
var version = 3
h := md5.New()
h.Reset()
h.Write([]byte("OfflinePlayer:" + name))
s := h.Sum(nil)
var id uuid.UUID
copy(id[:], s)
id[6] = (id[6] & 0x0f) | uint8((version&0xf)<<4)
id[8] = (id[8] & 0x3f) | 0x80 // RFC 4122 variant
return id
}
// 加密请求
func handleEncryptionRequest(c *Client, pack pk.Packet) error {
//创建AES对称加密密钥

View File

@ -10,7 +10,7 @@ import (
"net"
"strconv"
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/data/packetid"
mcnet "github.com/Tnze/go-mc/net"
pk "github.com/Tnze/go-mc/net/packet"
)
@ -155,7 +155,7 @@ func (c *Client) Conn() *mcnet.Conn {
func (c *Client) SendMessage(msg string) error {
return c.conn.WritePacket(
pk.Marshal(
data.ChatServerbound,
packetid.ChatServerbound,
pk.String(msg),
),
)

View File

@ -4,7 +4,7 @@ import (
"errors"
"strconv"
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/net/ptypes"
)
@ -14,7 +14,7 @@ import (
// It's just animation.
func (c *Client) SwingArm(hand int) error {
return c.conn.WritePacket(pk.Marshal(
data.ArmAnimation,
packetid.ArmAnimation,
pk.VarInt(hand),
))
}
@ -22,7 +22,7 @@ func (c *Client) SwingArm(hand int) error {
// Respawn the player when it was dead.
func (c *Client) Respawn() error {
return c.conn.WritePacket(pk.Marshal(
data.ClientCommand,
packetid.ClientCommand,
pk.VarInt(0),
))
}
@ -31,7 +31,7 @@ func (c *Client) Respawn() error {
// hand could be one of 0: main hand, 1: off hand
func (c *Client) UseItem(hand int) error {
return c.conn.WritePacket(pk.Marshal(
data.UseItem,
packetid.UseItem,
pk.VarInt(hand),
))
}
@ -43,7 +43,7 @@ func (c *Client) UseItem(hand int) error {
// and within a 4-unit radius of the player's position.
func (c *Client) UseEntity(entityID int32, hand int) error {
return c.conn.WritePacket(pk.Marshal(
data.UseEntity,
packetid.UseEntity,
pk.VarInt(entityID),
pk.VarInt(0),
pk.VarInt(hand),
@ -54,7 +54,7 @@ func (c *Client) UseEntity(entityID int32, hand int) error {
// The attack version of UseEntity. Has the same limit.
func (c *Client) AttackEntity(entityID int32, hand int) error {
return c.conn.WritePacket(pk.Marshal(
data.UseEntity,
packetid.UseEntity,
pk.VarInt(entityID),
pk.VarInt(1),
))
@ -63,7 +63,7 @@ func (c *Client) AttackEntity(entityID int32, hand int) error {
// UseEntityAt is a variety of UseEntity with target location
func (c *Client) UseEntityAt(entityID int32, x, y, z float32, hand int) error {
return c.conn.WritePacket(pk.Marshal(
data.UseEntity,
packetid.UseEntity,
pk.VarInt(entityID),
pk.VarInt(2),
pk.Float(x), pk.Float(y), pk.Float(z),
@ -78,7 +78,7 @@ func (c *Client) Chat(msg string) error {
}
return c.conn.WritePacket(pk.Marshal(
data.ChatServerbound,
packetid.ChatServerbound,
pk.String(msg),
))
}
@ -103,7 +103,7 @@ func (c *Client) PluginMessage(channel string, msg []byte) error {
// insideBlock is true when the player's head is inside of a block's collision.
func (c *Client) UseBlock(hand, locX, locY, locZ, face int, cursorX, cursorY, cursorZ float32, insideBlock bool) error {
return c.conn.WritePacket(pk.Marshal(
data.UseItem,
packetid.UseItem,
pk.VarInt(hand),
pk.Position{X: locX, Y: locY, Z: locZ},
pk.VarInt(face),
@ -120,7 +120,7 @@ func (c *Client) SelectItem(slot int) error {
}
return c.conn.WritePacket(pk.Marshal(
data.HeldItemSlotServerbound,
packetid.HeldItemSlotServerbound,
pk.Short(slot),
))
}
@ -137,14 +137,14 @@ func (c *Client) SelectItem(slot int) error {
// the server swaps the items and then change player's selected slot (cause the HeldItemChange event).
func (c *Client) PickItem(slot int) error {
return c.conn.WritePacket(pk.Marshal(
data.PickItem,
packetid.PickItem,
pk.VarInt(slot),
))
}
func (c *Client) playerAction(status, locX, locY, locZ, face int) error {
return c.conn.WritePacket(pk.Marshal(
data.BlockDig,
packetid.BlockDig,
pk.VarInt(status),
pk.Position{X: locX, Y: locY, Z: locZ},
pk.Byte(face),