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

@ -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
}