brigadier

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HasMinIntFlag byte = 0x01
	HasMaxIntFlag byte = 0x02

	MinInt = math.MinInt32
	MaxInt = math.MaxInt32

	MinFloat64 = -math.MaxFloat64
	MaxFloat64 = math.MaxFloat64
)

Variables

This section is empty.

Functions

func Decode

func Decode(rd io.Reader) (brigodier.ArgumentType, error)

func Encode

func Encode(wr io.Writer, argType brigodier.ArgumentType) error

Types

type ArgumentPropertyCodec

type ArgumentPropertyCodec interface {
	Encode(wr io.Writer, v interface{}) error
	Decode(rd io.Reader) (interface{}, error)
}
var (
	EmptyArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{}

	BoolArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v interface{}) error {
			b, ok := v.(bool)
			if !ok {
				return fmt.Errorf("execpted bool but got %T", v)
			}
			return util.WriteBool(wr, b)
		},
		DecodeFn: func(rd io.Reader) (interface{}, error) {
			return util.ReadBool(rd)
		},
	}
	ByteArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v interface{}) error {
			b, ok := v.(byte)
			if !ok {
				return fmt.Errorf("execpted byte but got %T", v)
			}
			return util.WriteByte(wr, b)
		},
		DecodeFn: func(rd io.Reader) (interface{}, error) {
			return util.ReadByte(rd)
		},
	}
	StringArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v interface{}) error {
			t, ok := v.(brigodier.StringType)
			if !ok {
				return fmt.Errorf("expected brigodier.StringType but got %T", v)
			}
			switch t {
			case brigodier.SingleWord, brigodier.QuotablePhase, brigodier.GreedyPhrase:
				return util.WriteVarInt(wr, int(t))
			default:
				return fmt.Errorf("invalid string argument type %d", t)
			}
		},
		DecodeFn: func(rd io.Reader) (interface{}, error) {
			t, err := util.ReadVarInt(rd)
			if err != nil {
				return nil, err
			}
			switch t {
			case 0, 1, 2:
				return brigodier.StringType(t), nil
			default:
				return nil, fmt.Errorf("invalid string argument type %d", t)
			}
		},
	}

	Float64ArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v interface{}) error {
			i, ok := v.(*brigodier.Float64ArgumentType)
			if !ok {
				return fmt.Errorf("expected *brigodier.Float64ArgumentType but got %T", v)
			}
			hasMin := i.Min != MinFloat64
			hasMax := i.Max != MaxFloat64
			flag := flags(hasMin, hasMax)

			err := util.WriteByte(wr, flag)
			if err != nil {
				return err
			}
			if hasMin {
				err = util.WriteFloat64(wr, i.Min)
				if err != nil {
					return err
				}
			}
			if hasMax {
				err = util.WriteFloat64(wr, i.Max)
			}
			return err
		},
		DecodeFn: func(rd io.Reader) (interface{}, error) {
			flags, err := util.ReadByte(rd)
			if err != nil {
				return nil, err
			}
			min := MinFloat64
			max := MaxFloat64
			if flags&HasMinIntFlag != 0 {
				min, err = util.ReadFloat64(rd)
				if err != nil {
					return nil, err
				}
			}
			if flags&HasMaxIntFlag != 0 {
				max, err = util.ReadFloat64(rd)
				if err != nil {
					return nil, err
				}
			}
			return &brigodier.Float64ArgumentType{Min: min, Max: max}, nil
		},
	}

	IntArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v interface{}) error {
			i, ok := v.(*brigodier.IntegerArgumentType)
			if !ok {
				return fmt.Errorf("expected *brigodier.IntegerArgumentType but got %T", v)
			}
			hasMin := i.Min != MinInt
			hasMax := i.Max != MaxInt
			flag := flags(hasMin, hasMax)

			err := util.WriteByte(wr, flag)
			if err != nil {
				return err
			}
			if hasMin {
				err = util.WriteInt(wr, i.Min)
				if err != nil {
					return err
				}
			}
			if hasMax {
				err = util.WriteInt(wr, i.Max)
			}
			return err
		},
		DecodeFn: func(rd io.Reader) (interface{}, error) {
			flags, err := util.ReadByte(rd)
			if err != nil {
				return nil, err
			}
			min := MinInt
			max := MaxInt
			if flags&HasMinIntFlag != 0 {
				min, err = util.ReadInt(rd)
				if err != nil {
					return nil, err
				}
			}
			if flags&HasMaxIntFlag != 0 {
				max, err = util.ReadInt(rd)
				if err != nil {
					return nil, err
				}
			}
			return &brigodier.IntegerArgumentType{Min: min, Max: max}, nil
		},
	}
)

type ArgumentPropertyCodecFuncs

type ArgumentPropertyCodecFuncs struct {
	EncodeFn func(wr io.Writer, v interface{}) error
	DecodeFn func(rd io.Reader) (interface{}, error)
}

ArgumentPropertyCodecFuncs implements ArgumentPropertyCodec.

func (*ArgumentPropertyCodecFuncs) Decode

func (c *ArgumentPropertyCodecFuncs) Decode(rd io.Reader) (interface{}, error)

func (*ArgumentPropertyCodecFuncs) Encode

func (c *ArgumentPropertyCodecFuncs) Encode(wr io.Writer, v interface{}) error

type PassthroughProperty

type PassthroughProperty struct {
	Identifier string
	Codec      ArgumentPropertyCodec
	Result     interface{}
}

func (*PassthroughProperty) Parse

func (p *PassthroughProperty) Parse(*brigodier.StringReader) (interface{}, error)

Parse is unsupported.

func (*PassthroughProperty) String

func (p *PassthroughProperty) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL