测试并改正放置、挖掘方块的API

This commit is contained in:
Tnze
2019-06-08 14:17:26 +08:00
parent d53f77dbae
commit f909ed405e
2 changed files with 18 additions and 14 deletions

View File

@ -3,7 +3,6 @@ package bot
import ( import (
"bytes" "bytes"
"errors" "errors"
"io"
"io/ioutil" "io/ioutil"
// "math" // "math"
@ -200,13 +199,13 @@ func handleSetSlotPacket(c *Client, p pk.Packet) error {
switch int8(windowID) { switch int8(windowID) {
case 0: //if window ID is 0, it will only change the hotbar case 0: //if window ID is 0, it will only change the hotbar
if slotI < 32 || slotI > 44 { if slotI < 36 || slotI > 45 {
return errors.New("server set slot error") return fmt.Errorf("slot %d out of range for window %d", slotI, windowID)
} }
fallthrough fallthrough
case -2: //or if it's -2, server can change any slot without animation case -2: //or if it's -2, server can change any slot without animation
if slotI < 0 || slotI > 45 { 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 c.Inventory[slotI] = slot
} }

View File

@ -2,6 +2,8 @@ package bot
import ( import (
"errors" "errors"
"strconv"
"github.com/Tnze/go-mc/data" "github.com/Tnze/go-mc/data"
pk "github.com/Tnze/go-mc/net/packet" 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. // 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. // 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. // 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. // 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( return c.conn.WritePacket(pk.Marshal(
data.PlayerBlockPlacement, data.PlayerBlockPlacement,
pk.VarInt(hand), pk.VarInt(hand),
pk.Position{locX, locY, locZ}, pk.Position{X: locX, Y: locY, Z: locZ},
pk.VarInt(face), pk.VarInt(face),
pk.Float(cursorX), pk.Float(cursorY), pk.Float(cursorZ), pk.Float(cursorX), pk.Float(cursorY), pk.Float(cursorZ),
pk.Boolean(insideBlock), pk.Boolean(insideBlock),
)) ))
} }
// ChangeHeldItem used to change the slot selection in hotbar. // SelectItem used to change the slot selection in hotbar.
func (c *Client) ChangeHeldItem(slot int) error { // slot should from 0 to 8
func (c *Client) SelectItem(slot int) error {
if slot < 0 || slot > 8 { if slot < 0 || slot > 8 {
return errors.New("invalid slot") return errors.New("invalid slot: " + strconv.Itoa(slot))
} }
return c.conn.WritePacket(pk.Marshal( 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( return c.conn.WritePacket(pk.Marshal(
data.PlayerDigging, data.PlayerDigging,
pk.VarInt(status), pk.VarInt(status),
pk.Position{locX, locY, locZ}, pk.Position{X: locX, Y: locY, Z: locZ},
pk.VarInt(face), pk.Byte(face),
)) ))
} }
// Dig used to start, end or cancel a digging // 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 { func (c *Client) Dig(status, locX, locY, locZ, face int) error {
return c.playerAction(status, locX, locY, locZ, face) 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) 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 { func (c *Client) UseItemEnd() error {
return c.playerAction(5, 0, 0, 0, 0) 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 { func (c *Client) SwapItem() error {
return c.playerAction(6, 0, 0, 0, 0) return c.playerAction(6, 0, 0, 0, 0)
} }