Implement TransactionConfirmation event + packet

This commit is contained in:
Tom
2020-09-27 21:44:24 -07:00
parent 93cb674bd2
commit 2bdea5a0f9
8 changed files with 83 additions and 7 deletions

View File

@ -51,8 +51,9 @@ type eventBroker struct {
// total - total amount of experience received from level 0.
ExperienceChange func(bar float32, level int32, total int32) error
WindowsItem func(id byte, slots []entity.Slot) error
WindowsItemChange func(id byte, slotID int, slot entity.Slot) error
WindowsItem func(id byte, slots []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.
// At time of writing (1.16.3), difficulty values of 0, 1, 2, and 3 correspond

View File

@ -151,6 +151,8 @@ func (c *Client) handlePacket(p pk.Packet) (disconnect bool, err error) {
err = handleWindowItemsPacket(c, p)
case data.OpenWindow:
err = handleOpenWindowPacket(c, p)
case data.TransactionClientbound:
err = handleWindowConfirmationPacket(c, p)
case data.DeclareRecipes:
// handleDeclareRecipesPacket(g, reader)
@ -736,6 +738,18 @@ func handleOpenWindowPacket(c *Client, p pk.Packet) error {
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) {
var (
bar pk.Float

View File

@ -17,8 +17,8 @@ const (
playerHeight = 1.8
resetVel = 0.003
maxYawChange = 7
maxPitchChange = 5
maxYawChange = 11
maxPitchChange = 7
stepHeight = 0.6
minJumpTicks = 14

View File

@ -1,8 +1,10 @@
package entity
import (
"bytes"
"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"
pk "github.com/Tnze/go-mc/net/packet"
"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
type Slot struct {
Present bool
ItemID int32
ItemID item.ID
Count int8
NBT interface{}
}
@ -56,9 +58,11 @@ func (s *Slot) Decode(r pk.DecodeReader) error {
return err
}
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
}
s.ItemID = item.ID(itemID)
if err := (*pk.Byte)(&s.Count).Decode(r); err != nil {
return err
}
@ -69,6 +73,25 @@ func (s *Slot) Decode(r pk.DecodeReader) error {
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 {
return item.ByID[item.ID(s.ItemID)].DisplayName
}

18
data/inv/inv.go Normal file
View 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},
}

View File

@ -7,6 +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/nbt"
pk "github.com/Tnze/go-mc/net/packet"
)
@ -67,3 +68,22 @@ func (p *OpenWindow) Decode(pkt pk.Packet) error {
}
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,
)
}