Skip to content

Commit

Permalink
Refactor!: Optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard127 committed Oct 29, 2023
1 parent d174dfb commit a6f4dc3
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 391 deletions.
6 changes: 3 additions & 3 deletions bot/core/playerlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *PlayerList) ReadFrom(r io.Reader) (int64, error) {
p.UpdatePing(uuid.UUID(uuids[i]), int32(ping[i]))
}
case 3:
displayName := make([]pk.Option[chat.Message, *chat.Message], len(uuids))
displayName := make([]pk.Optional[chat.Message, *chat.Message], len(uuids))
for i := range displayName {
n2, _ := displayName[i].ReadFrom(r)
n1 += n2
Expand Down Expand Up @@ -106,7 +106,7 @@ func (p *PlayerList) UpdatePing(uuid uuid.UUID, ping int32) {
p.Players[uuid] = entry
}

func (p *PlayerList) UpdateDisplayName(uuid uuid.UUID, displayName pk.Option[chat.Message, *chat.Message]) {
func (p *PlayerList) UpdateDisplayName(uuid uuid.UUID, displayName pk.Optional[chat.Message, *chat.Message]) {
if _, ok := p.Players[uuid]; !ok {
return
}
Expand All @@ -121,7 +121,7 @@ type PlayerEntry struct {
Properties []pk.Property
Gamemode int32
Ping int32
DisplayName pk.Option[chat.Message, *chat.Message]
DisplayName pk.Optional[chat.Message, *chat.Message]
//Timestamp int64
/*PublicKey []byte
Signature []byte*/
Expand Down
197 changes: 20 additions & 177 deletions bot/provider/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ func JoinGame(c *Client, p pk.Packet, cancel context.CancelFunc) error {
(*pk.Boolean)(&c.Player.WorldInfo.EnableRespawnScreen),
(*pk.Boolean)(&c.Player.WorldInfo.IsDebug),
(*pk.Boolean)(&c.Player.WorldInfo.IsFlat),
pk.Opt{
If: (*pk.Boolean)(&c.Player.WorldInfo.HasDeathLocation),
pk.Optional[pk.Tuple, *pk.Tuple]{
Has: &c.Player.WorldInfo.HasDeathLocation,
Value: pk.Tuple{
(*pk.Identifier)(&c.Player.WorldInfo.DimensionName),
(*pk.Position)(&c.Player.WorldInfo.DeathPosition),
Expand Down Expand Up @@ -755,29 +755,23 @@ func PlayerInfoUpdate(c *Client, p pk.Packet, cancel context.CancelFunc) error {
return nil
}

func LookAt(client *Client, packet pk.Packet, cancel context.CancelFunc) error {
func LookAt(client *Client, p pk.Packet, cancel context.CancelFunc) error {
var (
targetEnum pk.VarInt
X, Y, Z pk.Double
target pk.VarInt
x, y, z pk.Double
isEntity pk.Boolean
entityID pk.VarInt
id pk.VarInt
entityTarget pk.VarInt
)

if err := packet.Scan(
pk.Tuple{
&targetEnum,
&X, &Y, &Z,
pk.Opt{
If: &isEntity,
Value: pk.Tuple{
&entityID,
&entityTarget,
},
if err := p.Scan(&target, &x, &y, &z, &isEntity,
pk.Optional[pk.Tuple, *pk.Tuple]{
Has: &isEntity,
Value: pk.Tuple{
&id, &entityTarget,
},
},
); err != nil {
return fmt.Errorf("unable to read LookAt packet: %w", err)
}); err != nil {
return fmt.Errorf("unable to read LookAt p: %w", err)
}

return nil
Expand Down Expand Up @@ -944,134 +938,6 @@ func DisplayScoreboard(c *Client, p pk.Packet, cancel context.CancelFunc) error
return nil
}

func EntityMetadata(c *Client, p pk.Packet, cancel context.CancelFunc) error {
var (
err error
EntityID pk.VarInt
Metadata struct {
Index pk.UnsignedByte
Type pk.VarInt
Value interface{}
}
)

if err := p.Scan(
&EntityID,
&Metadata.Index,
pk.Opt{
If: func() bool {
return Metadata.Index != 0xff
},
Value: &Metadata.Type,
},
); err != nil {
return fmt.Errorf("unable to read EntityMetadata packet: %w", err)
}
switch Metadata.Type {
case 0:
var Value pk.Byte
err = p.Scan(&Value)
case 1:
var Value pk.VarInt
err = p.Scan(&Value)
case 2:
var Value pk.Float
err = p.Scan(&Value)
case 3:
var Value pk.String
err = p.Scan(&Value)
case 4:
var Value chat.Message
err = p.Scan(&Value)
case 5:
var Value struct {
Present pk.Boolean
Value chat.Message
}
err = p.Scan(
&Value.Present,
pk.Opt{
If: Value.Present,
Value: &Value.Value,
},
)
case 6:
var Value Slot
err = p.Scan(&Value)
case 7:
var Value pk.Boolean
err = p.Scan(&Value)
case 8:
var Value pk.Position
err = p.Scan(&Value)
case 9:
var Value pk.Position
err = p.Scan(&Value)
case 10:
var Value struct {
Present pk.Boolean
Value pk.Position
}
err = p.Scan(
&Value.Present,
pk.Opt{
If: Value.Present,
Value: &Value.Value,
},
)
case 11:
var Value pk.VarInt
err = p.Scan(&Value)
case 12:
var Value struct {
Present pk.Boolean
Value pk.UUID
}
err = p.Scan(
&Value.Present,
pk.Opt{
If: Value.Present,
Value: &Value.Value,
},
)
case 13:
var Value pk.VarInt
err = p.Scan(&Value)
/*case 14:*/
case 15:
var Value struct {
ID pk.VarInt
Data pk.VarInt // TODO: This is a particle data
}
err = p.Scan(&Value.ID, &Value.Data)
case 16:
var Value pk.ByteArray // TODO: 3 floats
err = p.Scan(&Value)
case 17:
var Value pk.VarInt
err = p.Scan(&Value)
case 18:
var Value pk.VarInt
err = p.Scan(&Value)
case 19:
var Value pk.VarInt
err = p.Scan(&Value)
case 20:
var Value pk.VarInt
err = p.Scan(&Value)
/*case 21:*/
case 22:
var Value pk.VarInt
err = p.Scan(&Value)
}

if err != nil {
return fmt.Errorf("unable to read EntityMetadata packet: %w", err)
}

return nil
}

func AttachEntity(c *Client, p pk.Packet, cancel context.CancelFunc) error {
var (
entityID pk.Int
Expand Down Expand Up @@ -1341,48 +1207,25 @@ func EntityProperties(c *Client, p pk.Packet, cancel context.CancelFunc) error {

func EntityEffect(c *Client, p pk.Packet, cancel context.CancelFunc) error {
var (
entityID pk.VarInt
effectID pk.VarInt
amplifier pk.Byte
duration pk.VarInt
flags pk.Byte
factorData pk.Boolean
codec struct {
PaddingDuration int `nbt:"padding_duration"`
FactorStart float32 `nbt:"factor_start"`
FactorTarget float32 `nbt:"factor_target"`
FactorCurrent float32 `nbt:"factor_current"`
EffectChangedTimeStamp int `nbt:"effect_changed_timestamp"`
FactorPreviousFrame float32 `nbt:"factor_previous_frame"`
HadEffectLastTick bool `nbt:"had_effect_last_tick"`
}
entityID pk.VarInt
effectID pk.VarInt
amplifier pk.Byte
duration pk.VarInt
flags pk.Byte
)

if err := p.Scan(
pk.Tuple{
&entityID,
&effectID,
&amplifier,
&duration,
&flags,
pk.Opt{
If: &factorData,
Value: pk.NBT(&codec),
},
},
); err != nil {
if err := p.Scan(&entityID, &effectID, &amplifier, &duration, &flags); err != nil {
return fmt.Errorf("unable to read EntityEffect packet: %w", err)
}

if _, ok := effects.ByID[int32(effectID)]; ok {
effectStatus := effects.EffectStatus{
c.Player.EntityPlayer.ActivePotionEffects[int32(effectID)] = effects.EffectStatus{
ID: int32(effectID),
Amplifier: byte(amplifier),
Duration: int32(duration),
ShowParticles: flags&0x01 == 0x01,
ShowIcon: flags&0x04 == 0x04,
}
c.Player.EntityPlayer.ActivePotionEffects[effectStatus.ID] = effectStatus
}
return nil
}
15 changes: 4 additions & 11 deletions bot/world/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ func (m *Map) ReadFrom(r io.Reader) (int64, error) {
n1, err := (*pk.UnsignedByte)(&m.Columns).ReadFrom(r)
n += n1

n2, err := pk.Opt{
If: m.Columns > 0,
n2, err := pk.Optional[pk.Tuple, *pk.Tuple]{
Has: m.Columns > 0,
Value: pk.Tuple{
(*pk.UnsignedByte)(&m.Rows),
(*pk.Byte)(&m.X),
Expand All @@ -68,8 +68,7 @@ type MapIcon struct {
Type int32
X, Z int8
Direction int8
HasName bool
Name chat.Message
Name pk.Optional[chat.Message, *chat.Message]
}

func (m *MapIcon) ReadFrom(r io.Reader) (n int64, err error) {
Expand All @@ -78,12 +77,6 @@ func (m *MapIcon) ReadFrom(r io.Reader) (n int64, err error) {
(*pk.Byte)(&m.X),
(*pk.Byte)(&m.Z),
(*pk.Byte)(&m.Direction),
(*pk.Boolean)(&m.HasName),
pk.Opt{
If: m.HasName,
Value: pk.Tuple{
&m.Name,
},
},
&m.Name,
}.ReadFrom(r)
}
51 changes: 51 additions & 0 deletions chat/signature.go
Original file line number Diff line number Diff line change
@@ -1 +1,52 @@
package chat

import (
"crypto/rand"
"encoding/binary"
pk "github.com/Edouard127/go-mc/net/packet"
"io"
"time"
)

type SignedMessage struct {
Message string
Timestamp int64
Salt int64
HasSignature bool
Signature []byte
Count int
Ack pk.FixedBitSet
}

func SignMessage(msg Message, signature []byte) *SignedMessage {
var salt int64
binary.Read(rand.Reader, binary.BigEndian, &salt)

return &SignedMessage{
Message: msg.Text,
Timestamp: time.Now().Unix(),
Salt: salt,
HasSignature: false,
Signature: nil,
Count: 0,
Ack: pk.NewFixedBitSet(20),
}
}

func (s SignedMessage) WriteTo(w io.Writer) (int64, error) {
return pk.Tuple{
pk.String(s.Message),
pk.Long(s.Timestamp),
pk.Long(s.Salt),
pk.Boolean(s.HasSignature),
pk.Optional[pk.ByteArray, *pk.ByteArray]{
Has: s.Signature != nil,
Value: s.Signature,
},
pk.VarInt(s.Count),
pk.Optional[pk.FixedBitSet, *pk.FixedBitSet]{
Has: s.Count > 0,
Value: s.Ack,
},
}.WriteTo(w)
}
17 changes: 6 additions & 11 deletions data/slots/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,18 @@ func (s *Slot) GetIndex() int {
}

func (s *Slot) WriteTo(w io.Writer) (n int64, err error) {
var present pk.Boolean = s.ID != 0 && s.Count != 0
return pk.Tuple{
present, pk.Opt{
If: present,
Value: pk.Tuple{&s.ID, &s.Count, pk.NBT(&s.NBT)},
},
return pk.Optional[pk.Tuple, *pk.Tuple]{
Has: s.ID != 0 && s.Count != 0,
Value: pk.Tuple{&s.ID, &s.Count, pk.NBT(&s.NBT)},
}.WriteTo(w)
}

func (s *Slot) ReadFrom(r io.Reader) (n int64, err error) {
var present pk.Boolean
return pk.Tuple{
&present, pk.Opt{
If: &present,
Value: pk.Tuple{
(*pk.VarInt)(&s.ID), (*pk.Byte)(&s.Count), pk.NBT(&s.NBT),
},
pk.Optional[pk.Tuple, *pk.Tuple]{
Has: &present,
Value: pk.Tuple{(*pk.VarInt)(&s.ID), (*pk.Byte)(&s.Count), pk.NBT(&s.NBT)},
},
}.ReadFrom(r)
}
Expand Down
Loading

0 comments on commit a6f4dc3

Please sign in to comment.