diff --git a/session/handler.go b/session/handler.go index 0dc6c53..4bea5b5 100644 --- a/session/handler.go +++ b/session/handler.go @@ -5,7 +5,6 @@ import ( "context" "errors" "fmt" - "slices" "time" packet2 "github.com/cooldogedev/spectrum/server/packet" @@ -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) diff --git a/session/session.go b/session/session.go index 1777817..70675a6 100644 --- a/session/session.go +++ b/session/session.go @@ -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. @@ -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 @@ -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. diff --git a/util/opts.go b/util/opts.go index 641fc34..fc88702 100644 --- a/util/opts.go +++ b/util/opts.go @@ -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{