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),

View File

@ -2,19 +2,22 @@
package main
import (
"log"
"github.com/Tnze/go-mc/bot"
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/net"
pk "github.com/Tnze/go-mc/net/packet"
"github.com/Tnze/go-mc/offline"
"github.com/google/uuid"
"log"
)
const ProtocolVersion = 578
const Threshold = 256
const MaxPlayer = 200
// Packet IDs
const (
PlayerPositionAndLookClientbound = 0x36
JoinGame = 0x26
)
func main() {
l, err := net.ListenMC(":25565")
if err != nil {
@ -58,20 +61,26 @@ func handlePlaying(conn net.Conn, protocol int32) {
}
// Write LoginSuccess packet
err = loginSuccess(conn, info.Name, info.UUID)
if err != nil {
if err = loginSuccess(conn, info.Name, info.UUID); err != nil {
log.Print("Login failed on success")
return
}
joinGame(conn)
conn.WritePacket(pk.Marshal(data.PositionClientbound,
// https://wiki.vg/Protocol#Player_Position_And_Look_.28clientbound.29
if err := joinGame(conn); err != nil {
log.Print("Login failed on joinGame")
return
}
if err := conn.WritePacket(pk.Marshal(PlayerPositionAndLookClientbound,
// https://wiki.vg/index.php?title=Protocol&oldid=16067#Player_Position_And_Look_.28clientbound.29
pk.Double(0), pk.Double(0), pk.Double(0), // XYZ
pk.Float(0), pk.Float(0), // Yaw Pitch
pk.Byte(0), // flag
pk.VarInt(0), // TP ID
))
)); err != nil {
log.Print("Login failed on sending PlayerPositionAndLookClientbound")
return
}
// Just for block this goroutine. Keep the connection
for {
if _, err := conn.ReadPacket(); err != nil {
@ -109,7 +118,7 @@ func acceptLogin(conn net.Conn) (info PlayerInfo, err error) {
log.Panic("Not Implement")
} else {
// offline-mode UUID
info.UUID = bot.OfflineUUID(info.Name)
info.UUID = offline.NameToUUID(info.Name)
}
return
@ -140,7 +149,7 @@ func loginSuccess(conn net.Conn, name string, uuid uuid.UUID) error {
}
func joinGame(conn net.Conn) error {
return conn.WritePacket(pk.Marshal(data.Login,
return conn.WritePacket(pk.Marshal(JoinGame,
pk.Int(0), // EntityID
pk.UnsignedByte(1), // Gamemode
pk.Int(0), // Dimension

View File

@ -1,7 +1,6 @@
// gen_blocks.go generates block information.
//+build ignore
// gen_blocks.go generates block information.
package main
import (
@ -131,6 +130,7 @@ func makeBlockDeclaration(blocks []Block) *ast.DeclStmt {
return out
}
//go:generate go run $GOFILE > ./packetid.go
func main() {
blocks, err := downloadInfo()
if err != nil {
@ -138,7 +138,9 @@ func main() {
os.Exit(1)
}
fmt.Println(`// Package block stores information about blocks in Minecraft.
fmt.Println(`// Code generated by gen_blocks.go; DO NOT EDIT.
// Package block stores information about blocks in Minecraft.
package block
import (

View File

@ -1,172 +0,0 @@
// This file is automatically generated by gen_packetIDs.go. DO NOT EDIT.
package data
//go:generate go run gen_packetIDs.go
// PktID represents a packet ID used in the minecraft protocol.
type PktID int32
// Valid PktID values.
const (
// Clientbound packets for connections in the login state.
EncryptionBeginClientbound PktID = 0x01
Success PktID = 0x02
Compress PktID = 0x03
LoginPluginRequest PktID = 0x04
Disconnect PktID = 0x00
// Serverbound packets for connections in the login state.
LoginStart PktID = 0x00
EncryptionBeginServerbound PktID = 0x01
LoginPluginResponse PktID = 0x02
// Clientbound packets for connections in the play state.
KickDisconnect PktID = 0x19
UnlockRecipes PktID = 0x35
Animation PktID = 0x05
WorldEvent PktID = 0x21
ScoreboardDisplayObjective PktID = 0x43
CustomPayloadClientbound PktID = 0x17
NamedSoundEffect PktID = 0x18
HeldItemSlotClientbound PktID = 0x3f
ChatClientbound PktID = 0x0e
AbilitiesClientbound PktID = 0x30
EntityDestroy PktID = 0x36
SetPassengers PktID = 0x4b
KeepAliveClientbound PktID = 0x1f
SpawnEntityExperienceOrb PktID = 0x01
OpenHorseWindow PktID = 0x1e
RemoveEntityEffect PktID = 0x37
RelEntityMove PktID = 0x27
SelectAdvancementTab PktID = 0x3c
OpenSignEntity PktID = 0x2e
Map PktID = 0x25
FacePlayer PktID = 0x33
EntityEquipment PktID = 0x47
ResourcePackSend PktID = 0x38
NbtQueryResponse PktID = 0x54
ScoreboardObjective PktID = 0x4a
StopSound PktID = 0x52
OpenWindow PktID = 0x2d
Camera PktID = 0x3e
Advancements PktID = 0x57
UpdateTime PktID = 0x4e
Login PktID = 0x24
PositionClientbound PktID = 0x34
UpdateViewPosition PktID = 0x40
EntitySoundEffect PktID = 0x50
Respawn PktID = 0x39
BlockChange PktID = 0x0b
BlockBreakAnimation PktID = 0x08
Title PktID = 0x4f
EntityTeleport PktID = 0x56
EntityEffect PktID = 0x59
TileEntityData PktID = 0x09
SpawnPosition PktID = 0x42
WorldBorder PktID = 0x3d
Experience PktID = 0x48
PlayerlistHeader PktID = 0x53
WindowItems PktID = 0x13
EntityUpdateAttributes PktID = 0x58
EntityHeadRotation PktID = 0x3a
VehicleMoveClientbound PktID = 0x2b
MapChunk PktID = 0x20
EntityLook PktID = 0x29
Teams PktID = 0x4c
UpdateViewDistance PktID = 0x41
Explosion PktID = 0x1b
MultiBlockChange PktID = 0x3b
PlayerInfo PktID = 0x32
CraftRecipeResponse PktID = 0x2f
TransactionClientbound PktID = 0x11
TradeList PktID = 0x26
CloseWindowClientbound PktID = 0x12
TabCompleteClientbound PktID = 0x0f
SetCooldown PktID = 0x16
BlockAction PktID = 0x0a
NamedEntitySpawn PktID = 0x04
SpawnEntityPainting PktID = 0x03
UpdateLight PktID = 0x23
CombatEvent PktID = 0x31
SpawnEntityLiving PktID = 0x02
ScoreboardScore PktID = 0x4d
DeclareCommands PktID = 0x10
UpdateHealth PktID = 0x49
EntityMetadata PktID = 0x44
AttachEntity PktID = 0x45
Tags PktID = 0x5b
EntityStatus PktID = 0x1a
AcknowledgePlayerDigging PktID = 0x07
Collect PktID = 0x55
WorldParticles PktID = 0x22
Entity PktID = 0x2a
UnloadChunk PktID = 0x1c
Difficulty PktID = 0x0d
CraftProgressBar PktID = 0x14
BossBar PktID = 0x0c
DeclareRecipes PktID = 0x5a
GameStateChange PktID = 0x1d
Statistics PktID = 0x06
EntityVelocity PktID = 0x46
SetSlot PktID = 0x15
OpenBook PktID = 0x2c
SoundEffect PktID = 0x51
EntityMoveLook PktID = 0x28
SpawnEntity PktID = 0x00
// Serverbound packets for connections in the play state.
EnchantItem PktID = 0x08
CustomPayloadServerbound PktID = 0x0b
SelectTrade PktID = 0x23
SetCreativeSlot PktID = 0x28
UpdateSign PktID = 0x2b
WindowClick PktID = 0x09
PositionLook PktID = 0x13
UpdateCommandBlock PktID = 0x26
QueryBlockNbt PktID = 0x01
Flying PktID = 0x15
KeepAliveServerbound PktID = 0x10
ClientCommand PktID = 0x04
BlockPlace PktID = 0x2e
EntityAction PktID = 0x1c
PositionServerbound PktID = 0x12
ResourcePackReceive PktID = 0x21
Spectate PktID = 0x2d
TeleportConfirm PktID = 0x00
GenerateStructure PktID = 0x0f
SetDifficulty PktID = 0x02
CloseWindowServerbound PktID = 0x0a
Look PktID = 0x14
AdvancementTab PktID = 0x22
SetBeaconEffect PktID = 0x24
AbilitiesServerbound PktID = 0x1a
ChatServerbound PktID = 0x03
DisplayedRecipe PktID = 0x1e
RecipeBook PktID = 0x1f
UpdateJigsawBlock PktID = 0x29
TransactionServerbound PktID = 0x07
SteerVehicle PktID = 0x1d
NameItem PktID = 0x20
PickItem PktID = 0x18
UpdateStructureBlock PktID = 0x2a
TabCompleteServerbound PktID = 0x06
HeldItemSlotServerbound PktID = 0x25
SteerBoat PktID = 0x17
Settings PktID = 0x05
UseItem PktID = 0x2f
CraftRecipeRequest PktID = 0x19
UpdateCommandBlockMinecart PktID = 0x27
BlockDig PktID = 0x1b
EditBook PktID = 0x0c
UseEntity PktID = 0x0e
VehicleMoveServerbound PktID = 0x16
ArmAnimation PktID = 0x2c
LockDifficulty PktID = 0x11
QueryEntityNbt PktID = 0x0d
// Clientbound packets used to respond to ping/status requests.
ServerInfo PktID = 0x00
PingClientbound PktID = 0x01
// Serverbound packets used to ping or read server status.
PingStart PktID = 0x00
PingServerbound PktID = 0x01
)

View File

@ -1,7 +1,6 @@
// gen_packetIDs.go generates the enumeration of packet IDs used on the wire.
//+build ignore
// gen_packetIDs.go generates the enumeration of packet IDs used on the wire.
package main
import (
@ -9,13 +8,39 @@ import (
"fmt"
"net/http"
"os"
"strings"
"text/template"
"github.com/iancoleman/strcase"
)
const (
protocolURL = "https://raw.githubusercontent.com/PrismarineJS/minecraft-data/master/data/pc/1.16.2/protocol.json"
protocolURL = "https://raw.githubusercontent.com/PrismarineJS/minecraft-data/master/data/pc/1.16.2/protocol.json"
packetidTmpl = `// This file is automatically generated by gen_packetIDs.go. DO NOT EDIT.
package packetid
// Valid PktID values.
const (
// Clientbound packets for connections in the login state.
{{range $Name, $ID := .Login.Clientbound}} {{$Name}} = {{$ID}}
{{end}}
// Serverbound packets for connections in the login state
{{range $Name, $ID := .Login.Serverbound}} {{$Name}} = {{$ID}}
{{end}}
// Clientbound packets for connections in the play state.
{{range $Name, $ID := .Play.Clientbound}} {{$Name}} = {{$ID}}
{{end}}
// Serverbound packets for connections in the play state.
{{range $Name, $ID := .Play.Serverbound}} {{$Name}} = {{$ID}}
{{end}}
// Clientbound packets used to respond to ping/status requests.
{{range $Name, $ID := .Status.Clientbound}} {{$Name}} = {{$ID}}
{{end}}
// Serverbound packets used to ping or read server status.
{{range $Name, $ID := .Status.Serverbound}} {{$Name}} = {{$ID}}
{{end}}
)
`
)
// unnest is a utility function to unpack a value from a nested map, given
@ -96,23 +121,6 @@ type protocolIDs struct {
// Handshake state has no packets
}
func (p protocolIDs) MaxLen() int {
var max int
for _, m := range []duplexMappings{p.Login, p.Play, p.Status} {
for k, _ := range m.Clientbound {
if len(k) > max {
max = len(k)
}
}
for k, _ := range m.Serverbound {
if len(k) > max {
max = len(k)
}
}
}
return max
}
func downloadInfo() (*protocolIDs, error) {
resp, err := http.Get(protocolURL)
if err != nil {
@ -141,6 +149,8 @@ func downloadInfo() (*protocolIDs, error) {
return &out, nil
}
//go:generate go run $GOFILE
//go:generate go fmt packetid.go
func main() {
pIDs, err := downloadInfo()
if err != nil {
@ -148,56 +158,16 @@ func main() {
os.Exit(1)
}
maxLen := pIDs.MaxLen()
f, err := os.Create("packetIDs.go")
f, err := os.Create("packetid.go")
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
defer f.Close()
fmt.Fprintln(f, "// This file is automatically generated by gen_packetIDs.go. DO NOT EDIT.")
fmt.Fprintln(f)
fmt.Fprintln(f, "package data")
fmt.Fprintln(f)
fmt.Fprintln(f, "//go:generate go run gen_packetIDs.go")
fmt.Fprintln(f)
fmt.Fprintln(f, "// PktID represents a packet ID used in the minecraft protocol.")
fmt.Fprintln(f, "type PktID int32")
fmt.Fprintln(f)
fmt.Fprintln(f, "// Valid PktID values.")
fmt.Fprintln(f, "const (")
fmt.Fprintln(f, " // Clientbound packets for connections in the login state.")
for k, v := range pIDs.Login.Clientbound {
fmt.Fprintf(f, " %s%s PktID = %s\n", k, strings.Repeat(" ", maxLen-len(k)), v)
tmpl := template.Must(template.New("packetIDs").Parse(packetidTmpl))
if err := tmpl.Execute(f, pIDs); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
fmt.Fprintln(f, " // Serverbound packets for connections in the login state.")
for k, v := range pIDs.Login.Serverbound {
fmt.Fprintf(f, " %s%s PktID = %s\n", k, strings.Repeat(" ", maxLen-len(k)), v)
}
fmt.Fprintln(f)
fmt.Fprintln(f, " // Clientbound packets for connections in the play state.")
for k, v := range pIDs.Play.Clientbound {
fmt.Fprintf(f, " %s%s PktID = %s\n", k, strings.Repeat(" ", maxLen-len(k)), v)
}
fmt.Fprintln(f, " // Serverbound packets for connections in the play state.")
for k, v := range pIDs.Play.Serverbound {
fmt.Fprintf(f, " %s%s PktID = %s\n", k, strings.Repeat(" ", maxLen-len(k)), v)
}
fmt.Fprintln(f)
fmt.Fprintln(f, " // Clientbound packets used to respond to ping/status requests.")
for k, v := range pIDs.Status.Clientbound {
fmt.Fprintf(f, " %s%s PktID = %s\n", k, strings.Repeat(" ", maxLen-len(k)), v)
}
fmt.Fprintln(f, " // Serverbound packets used to ping or read server status.")
for k, v := range pIDs.Status.Serverbound {
fmt.Fprintf(f, " %s%s PktID = %s\n", k, strings.Repeat(" ", maxLen-len(k)), v)
}
fmt.Fprintln(f)
fmt.Fprintln(f, ")")
}

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
// gen_soundIDs.go generates the enumeration of sound IDs.
//+build ignore
//go:generate go run $GOFILE
// gen_soundIDs.go generates the enumeration of sound IDs.
package main
import (
@ -13,7 +13,7 @@ import (
)
const (
protocolURL = "https://pokechu22.github.io/Burger/1.16.4.json"
protocolURL = "https://pokechu22.github.io/Burger/1.16.5.json"
)
type sound struct {
@ -35,7 +35,7 @@ func main() {
}
defer f.Close()
fmt.Fprintln(f, "// This file is automatically generated by gen_soundIDs.go. DO NOT EDIT.")
fmt.Fprintln(f, "// Code generated by gen_soundIDs.go. DO NOT EDIT.")
fmt.Fprintln(f)
fmt.Fprintln(f, "package data")
fmt.Fprintln(f)

1
go.mod
View File

@ -5,5 +5,6 @@ go 1.13
require (
github.com/beefsack/go-astar v0.0.0-20200827232313-4ecf9e304482
github.com/google/uuid v1.1.1
github.com/iancoleman/strcase v0.1.3
github.com/mattn/go-colorable v0.1.8
)

2
go.sum
View File

@ -2,6 +2,8 @@ github.com/beefsack/go-astar v0.0.0-20200827232313-4ecf9e304482 h1:p4g4uok3+r6Tg
github.com/beefsack/go-astar v0.0.0-20200827232313-4ecf9e304482/go.mod h1:Cu3t5VeqE8kXjUBeNXWQprfuaP5UCIc5ggGjgMx9KFc=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/iancoleman/strcase v0.1.3 h1:dJBk1m2/qjL1twPLf68JND55vvivMupZ4wIzE8CTdBw=
github.com/iancoleman/strcase v0.1.3/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5NA7kMEAOgSE=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=

View File

@ -5,8 +5,6 @@ import (
"compress/zlib"
"fmt"
"io"
"github.com/Tnze/go-mc/data"
)
// Packet define a net data package
@ -16,8 +14,8 @@ type Packet struct {
}
//Marshal generate Packet with the ID and Fields
func Marshal(id data.PktID, fields ...FieldEncoder) (pk Packet) {
pk.ID = int32(id)
func Marshal(id int32, fields ...FieldEncoder) (pk Packet) {
pk.ID = id
for _, v := range fields {
pk.Data = append(pk.Data, v.Encode()...)

View File

@ -7,7 +7,7 @@ import (
"github.com/Tnze/go-mc/bot/world/entity"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/data/packetid"
"github.com/Tnze/go-mc/nbt"
pk "github.com/Tnze/go-mc/net/packet"
)
@ -81,7 +81,7 @@ func (p *ConfirmTransaction) Decode(pkt pk.Packet) error {
func (p ConfirmTransaction) Encode() pk.Packet {
return pk.Marshal(
data.TransactionServerbound,
packetid.TransactionServerbound,
p.WindowID,
p.ActionID,
p.Accepted,

View File

@ -4,7 +4,7 @@ import (
"io/ioutil"
"github.com/Tnze/go-mc/chat"
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
)
@ -84,7 +84,7 @@ func (p *PluginMessage) Decode(pkt pk.Packet) error {
func (p *PluginMessage) Encode() pk.Packet {
return pk.Marshal(
data.CustomPayloadServerbound,
packetid.CustomPayloadServerbound,
p.Channel,
p.Data,
)

View File

@ -2,7 +2,7 @@
package ptypes
import (
"github.com/Tnze/go-mc/data"
"github.com/Tnze/go-mc/data/packetid"
pk "github.com/Tnze/go-mc/net/packet"
)
@ -45,13 +45,10 @@ type PositionAndLookServerbound struct {
func (p PositionAndLookServerbound) Encode() pk.Packet {
return pk.Marshal(
data.PositionLook,
pk.Double(p.X),
pk.Double(p.Y),
pk.Double(p.Z),
pk.Float(p.Yaw),
pk.Float(p.Pitch),
pk.Boolean(p.OnGround),
packetid.PositionLook,
p.X, p.Y, p.Z,
p.Yaw, p.Pitch,
p.OnGround,
)
}
@ -63,11 +60,9 @@ type Position struct {
func (p Position) Encode() pk.Packet {
return pk.Marshal(
data.PositionServerbound,
pk.Double(p.X),
pk.Double(p.Y),
pk.Double(p.Z),
pk.Boolean(p.OnGround),
packetid.PositionServerbound,
p.X, p.Y, p.Z,
p.OnGround,
)
}
@ -79,9 +74,8 @@ type Look struct {
func (p Look) Encode() pk.Packet {
return pk.Marshal(
data.Look,
pk.Float(p.Yaw),
pk.Float(p.Pitch),
pk.Boolean(p.OnGround),
packetid.Look,
p.Yaw, p.Pitch,
p.OnGround,
)
}

20
offline/uuid.go Normal file
View File

@ -0,0 +1,20 @@
package offline
import (
"crypto/md5"
"github.com/google/uuid"
)
// NameToUUID return the UUID from player name in offline mode
func NameToUUID(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
}