Implement TransactionConfirmation event + packet
This commit is contained in:
@ -53,6 +53,7 @@ type eventBroker struct {
|
|||||||
|
|
||||||
WindowsItem func(id byte, slots []entity.Slot) error
|
WindowsItem func(id byte, slots []entity.Slot) error
|
||||||
WindowsItemChange func(id byte, slotID int, slot entity.Slot) error
|
WindowsItemChange func(id byte, slotID int, slot entity.Slot) error
|
||||||
|
WindowConfirmation func(pkt ptypes.ConfirmTransaction) error
|
||||||
|
|
||||||
// ServerDifficultyChange is called whenever the gamemode of the server changes.
|
// ServerDifficultyChange is called whenever the gamemode of the server changes.
|
||||||
// At time of writing (1.16.3), difficulty values of 0, 1, 2, and 3 correspond
|
// At time of writing (1.16.3), difficulty values of 0, 1, 2, and 3 correspond
|
||||||
|
@ -151,6 +151,8 @@ func (c *Client) handlePacket(p pk.Packet) (disconnect bool, err error) {
|
|||||||
err = handleWindowItemsPacket(c, p)
|
err = handleWindowItemsPacket(c, p)
|
||||||
case data.OpenWindow:
|
case data.OpenWindow:
|
||||||
err = handleOpenWindowPacket(c, p)
|
err = handleOpenWindowPacket(c, p)
|
||||||
|
case data.TransactionClientbound:
|
||||||
|
err = handleWindowConfirmationPacket(c, p)
|
||||||
|
|
||||||
case data.DeclareRecipes:
|
case data.DeclareRecipes:
|
||||||
// handleDeclareRecipesPacket(g, reader)
|
// handleDeclareRecipesPacket(g, reader)
|
||||||
@ -736,6 +738,18 @@ func handleOpenWindowPacket(c *Client, p pk.Packet) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleWindowConfirmationPacket(c *Client, p pk.Packet) error {
|
||||||
|
var pkt ptypes.ConfirmTransaction
|
||||||
|
if err := pkt.Decode(p); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.Events.WindowConfirmation != nil {
|
||||||
|
return c.Events.WindowConfirmation(pkt)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func handleSetExperience(c *Client, p pk.Packet) (err error) {
|
func handleSetExperience(c *Client, p pk.Packet) (err error) {
|
||||||
var (
|
var (
|
||||||
bar pk.Float
|
bar pk.Float
|
||||||
|
@ -17,8 +17,8 @@ const (
|
|||||||
playerHeight = 1.8
|
playerHeight = 1.8
|
||||||
resetVel = 0.003
|
resetVel = 0.003
|
||||||
|
|
||||||
maxYawChange = 7
|
maxYawChange = 11
|
||||||
maxPitchChange = 5
|
maxPitchChange = 7
|
||||||
|
|
||||||
stepHeight = 0.6
|
stepHeight = 0.6
|
||||||
minJumpTicks = 14
|
minJumpTicks = 14
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
package entity
|
package entity
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
|
||||||
"github.com/Tnze/go-mc/data/entity"
|
"github.com/Tnze/go-mc/data/entity"
|
||||||
item "github.com/Tnze/go-mc/data/items"
|
item "github.com/Tnze/go-mc/data/item"
|
||||||
"github.com/Tnze/go-mc/nbt"
|
"github.com/Tnze/go-mc/nbt"
|
||||||
pk "github.com/Tnze/go-mc/net/packet"
|
pk "github.com/Tnze/go-mc/net/packet"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -45,7 +47,7 @@ type Entity struct {
|
|||||||
// The Slot data structure is how Minecraft represents an item and its associated data in the Minecraft Protocol
|
// The Slot data structure is how Minecraft represents an item and its associated data in the Minecraft Protocol
|
||||||
type Slot struct {
|
type Slot struct {
|
||||||
Present bool
|
Present bool
|
||||||
ItemID int32
|
ItemID item.ID
|
||||||
Count int8
|
Count int8
|
||||||
NBT interface{}
|
NBT interface{}
|
||||||
}
|
}
|
||||||
@ -56,9 +58,11 @@ func (s *Slot) Decode(r pk.DecodeReader) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if s.Present {
|
if s.Present {
|
||||||
if err := (*pk.VarInt)(&s.ItemID).Decode(r); err != nil {
|
var itemID pk.VarInt
|
||||||
|
if err := itemID.Decode(r); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
s.ItemID = item.ID(itemID)
|
||||||
if err := (*pk.Byte)(&s.Count).Decode(r); err != nil {
|
if err := (*pk.Byte)(&s.Count).Decode(r); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -69,6 +73,25 @@ func (s *Slot) Decode(r pk.DecodeReader) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s Slot) Encode() []byte {
|
||||||
|
if !s.Present {
|
||||||
|
return pk.Boolean(false).Encode()
|
||||||
|
}
|
||||||
|
|
||||||
|
var b bytes.Buffer
|
||||||
|
b.Write(pk.Boolean(true).Encode())
|
||||||
|
b.Write(pk.VarInt(s.ItemID).Encode())
|
||||||
|
b.Write(pk.Byte(s.Count).Encode())
|
||||||
|
|
||||||
|
if s.NBT != nil {
|
||||||
|
nbt.NewEncoder(&b).Encode(s.NBT)
|
||||||
|
} else {
|
||||||
|
b.Write([]byte{nbt.TagEnd})
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
func (s Slot) String() string {
|
func (s Slot) String() string {
|
||||||
return item.ByID[item.ID(s.ItemID)].DisplayName
|
return item.ByID[item.ID(s.ItemID)].DisplayName
|
||||||
}
|
}
|
||||||
|
18
data/inv/inv.go
Normal file
18
data/inv/inv.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
// Package inv maps window types to inventory slot information.
|
||||||
|
package inv
|
||||||
|
|
||||||
|
type Info struct {
|
||||||
|
Name string
|
||||||
|
Start, End int // Player inventory
|
||||||
|
Slots int
|
||||||
|
}
|
||||||
|
|
||||||
|
var ByType = map[int]Info{
|
||||||
|
-1: Info{Name: "inventory", Start: 9, End: 44, Slots: 46},
|
||||||
|
0: Info{Name: "generic_9x1", Start: 1 * 9, End: 1*9 + 35, Slots: 1*9 + 36},
|
||||||
|
1: Info{Name: "generic_9x2", Start: 2 * 9, End: 2*9 + 35, Slots: 2*9 + 36},
|
||||||
|
2: Info{Name: "generic_9x3", Start: 3 * 9, End: 3*9 + 35, Slots: 3*9 + 36},
|
||||||
|
3: Info{Name: "generic_9x4", Start: 4 * 9, End: 4*9 + 35, Slots: 4*9 + 36},
|
||||||
|
4: Info{Name: "generic_9x5", Start: 5 * 9, End: 5*9 + 35, Slots: 5*9 + 36},
|
||||||
|
5: Info{Name: "generic_9x6", Start: 6 * 9, End: 6*9 + 35, Slots: 6*9 + 36},
|
||||||
|
}
|
@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
"github.com/Tnze/go-mc/bot/world/entity"
|
"github.com/Tnze/go-mc/bot/world/entity"
|
||||||
"github.com/Tnze/go-mc/chat"
|
"github.com/Tnze/go-mc/chat"
|
||||||
|
"github.com/Tnze/go-mc/data"
|
||||||
"github.com/Tnze/go-mc/nbt"
|
"github.com/Tnze/go-mc/nbt"
|
||||||
pk "github.com/Tnze/go-mc/net/packet"
|
pk "github.com/Tnze/go-mc/net/packet"
|
||||||
)
|
)
|
||||||
@ -67,3 +68,22 @@ func (p *OpenWindow) Decode(pkt pk.Packet) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ConfirmTransaction struct {
|
||||||
|
WindowID pk.Byte
|
||||||
|
ActionID pk.Short
|
||||||
|
Accepted pk.Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *ConfirmTransaction) Decode(pkt pk.Packet) error {
|
||||||
|
return pkt.Scan(&p.WindowID, &p.ActionID, &p.Accepted)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p ConfirmTransaction) Encode() pk.Packet {
|
||||||
|
return pk.Marshal(
|
||||||
|
data.TransactionServerbound,
|
||||||
|
p.WindowID,
|
||||||
|
p.ActionID,
|
||||||
|
p.Accepted,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user