From f909ed405e1b983599f41fbad0dfe7c4382bc685 Mon Sep 17 00:00:00 2001 From: Tnze Date: Sat, 8 Jun 2019 14:17:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=B9=B6=E6=94=B9=E6=AD=A3?= =?UTF-8?q?=E6=94=BE=E7=BD=AE=E3=80=81=E6=8C=96=E6=8E=98=E6=96=B9=E5=9D=97?= =?UTF-8?q?=E7=9A=84API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot/ingame.go | 7 +++---- bot/motion.go | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/bot/ingame.go b/bot/ingame.go index cab9ed9..47635bd 100644 --- a/bot/ingame.go +++ b/bot/ingame.go @@ -3,7 +3,6 @@ package bot import ( "bytes" "errors" - "io" "io/ioutil" // "math" @@ -200,13 +199,13 @@ func handleSetSlotPacket(c *Client, p pk.Packet) error { switch int8(windowID) { case 0: //if window ID is 0, it will only change the hotbar - if slotI < 32 || slotI > 44 { - return errors.New("server set slot error") + if slotI < 36 || slotI > 45 { + return fmt.Errorf("slot %d out of range for window %d", slotI, windowID) } fallthrough case -2: //or if it's -2, server can change any slot without animation if slotI < 0 || slotI > 45 { - return errors.New("server set slot out of range") + return fmt.Errorf("slot %d out of range for window %d", slotI, windowID) } c.Inventory[slotI] = slot } diff --git a/bot/motion.go b/bot/motion.go index 0a155aa..8d2f5c1 100644 --- a/bot/motion.go +++ b/bot/motion.go @@ -2,6 +2,8 @@ package bot import ( "errors" + "strconv" + "github.com/Tnze/go-mc/data" pk "github.com/Tnze/go-mc/net/packet" ) @@ -90,7 +92,7 @@ func (c *Client) PluginMessage(channal string, msg []byte) error { )) } -// PlaceBlock is used to place a block. +// UseBlock is used to place or use a block. // hand is the hand from which the block is placed; 0: main hand, 1: off hand. // face is the face on which the block is placed. // @@ -100,21 +102,22 @@ func (c *Client) PluginMessage(channal string, msg []byte) error { // cursorZ, from 0 to 1 increasing from north to south. // // insideBlock is true when the player's head is inside of a block's collision. -func (c *Client) PlaceBlock(hand, locX, locY, locZ, face int, cursorX, cursorY, cursorZ float32, insideBlock bool) error { +func (c *Client) UseBlock(hand, locX, locY, locZ, face int, cursorX, cursorY, cursorZ float32, insideBlock bool) error { return c.conn.WritePacket(pk.Marshal( data.PlayerBlockPlacement, pk.VarInt(hand), - pk.Position{locX, locY, locZ}, + pk.Position{X: locX, Y: locY, Z: locZ}, pk.VarInt(face), pk.Float(cursorX), pk.Float(cursorY), pk.Float(cursorZ), pk.Boolean(insideBlock), )) } -// ChangeHeldItem used to change the slot selection in hotbar. -func (c *Client) ChangeHeldItem(slot int) error { +// SelectItem used to change the slot selection in hotbar. +// slot should from 0 to 8 +func (c *Client) SelectItem(slot int) error { if slot < 0 || slot > 8 { - return errors.New("invalid slot") + return errors.New("invalid slot: " + strconv.Itoa(slot)) } return c.conn.WritePacket(pk.Marshal( @@ -144,12 +147,14 @@ func (c *Client) playerAction(status, locX, locY, locZ, face int) error { return c.conn.WritePacket(pk.Marshal( data.PlayerDigging, pk.VarInt(status), - pk.Position{locX, locY, locZ}, - pk.VarInt(face), + pk.Position{X: locX, Y: locY, Z: locZ}, + pk.Byte(face), )) } // Dig used to start, end or cancel a digging +// status is 0 for start digging, 1 for cancel and 2 if client think it done. +// To digging a block without cancel, use status 0 and 2 once each. func (c *Client) Dig(status, locX, locY, locZ, face int) error { return c.playerAction(status, locX, locY, locZ, face) } @@ -164,12 +169,12 @@ func (c *Client) DropItem() error { return c.playerAction(4, 0, 0, 0, 0) } -// UseItemEnd used to finish UseItem, like eating food, pulling back bows +// UseItemEnd used to finish UseItem, like eating food, pulling back bows. func (c *Client) UseItemEnd() error { return c.playerAction(5, 0, 0, 0, 0) } -// SwapItem used to swap the items in hands +// SwapItem used to swap the items in hands. func (c *Client) SwapItem() error { return c.playerAction(6, 0, 0, 0, 0) }