Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions session/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"slices"
"time"

packet2 "github.com/cooldogedev/spectrum/server/packet"
Expand Down Expand Up @@ -142,7 +141,7 @@ func handleClientPacket(s *Session, header *packet.Header, pool packet.Pool, shi
return errors.New("failed to decode header")
}

if !slices.Contains(s.opts.ClientDecode, header.PacketID) {
if _, ok := s.clientDecode[header.PacketID]; !ok {
s.processor.ProcessClientEncoded(ctx, &payload)
if !ctx.Cancelled() {
return s.Server().Write(payload)
Expand Down
5 changes: 5 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type Session struct {
latency atomic.Int64
transferring atomic.Bool
once sync.Once

clientDecode map[uint32]struct{}
}

// NewSession creates a new Session instance using the provided minecraft.Conn.
Expand All @@ -61,6 +63,8 @@ func NewSession(client *minecraft.Conn, logger *slog.Logger, registry *Registry,
animation: &animation.Dimension{},
processor: NopProcessor{},
tracker: newTracker(),

clientDecode: opts.ClientDecodeAsMap(),
}
s.ctx, s.cancelFunc = context.WithCancelCause(client.Context())
return s
Expand Down Expand Up @@ -234,6 +238,7 @@ func (s *Session) Opts() util.Opts {
// SetOpts updates the session options.
func (s *Session) SetOpts(opts util.Opts) {
s.opts = opts
s.clientDecode = opts.ClientDecodeAsMap()
}

// Processor returns the current processor.
Expand Down
9 changes: 9 additions & 0 deletions util/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ type Opts struct {
Token string `yaml:"token"`
}

// ClientDecodeAsMap converts the ClientDecode slice to a map for faster lookups.
func (opts *Opts) ClientDecodeAsMap() map[uint32]struct{} {
m := make(map[uint32]struct{}, len(opts.ClientDecode))
for _, id := range opts.ClientDecode {
m[id] = struct{}{}
}
return m
}

// DefaultOpts returns the default configuration options for Spectrum.
func DefaultOpts() *Opts {
return &Opts{
Expand Down