brigadier

package
v0.31.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

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

Variables

Functions

func Decode

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

func Encode

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

Types

type ArgumentIdentifier added in v0.19.0

type ArgumentIdentifier struct {
	// contains filtered or unexported fields
}

func (ArgumentIdentifier) String added in v0.19.0

func (a ArgumentIdentifier) String() string

type ArgumentPropertyCodec

type ArgumentPropertyCodec interface {
	Encode(wr io.Writer, v any, protocol proto.Protocol) error
	Decode(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error)
}
var (
	EmptyArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{}

	BoolArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			_, ok := v.(*brigodier.BoolArgumentType)
			if !ok {
				return fmt.Errorf("expected *brigodier.BoolArgumentType but got %T", v)
			}
			return nil
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			return brigodier.Bool, nil
		},
	}
	ByteArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			b, ok := v.(ByteArgumentType)
			if !ok {
				return fmt.Errorf("expected byte but got %T", v)
			}
			return util.WriteByte(wr, byte(b))
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			b, err := util.ReadByte(rd)
			return ByteArgumentType(b), err
		},
	}
	StringArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) 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, protocol proto.Protocol) (brigodier.ArgumentType, 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)
			}
		},
	}
	RegistryKeyArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			i, ok := v.(*RegistryKeyArgumentType)
			if !ok {
				return fmt.Errorf("expected *RegistryKeyArgumentType but got %T", v)
			}
			return util.WriteString(wr, i.Identifier)
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			id, err := util.ReadString(rd)
			if err != nil {
				return nil, err
			}
			return &RegistryKeyArgumentType{Identifier: id}, nil
		},
	}
	ResourceOrTagKeyArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			i, ok := v.(*ResourceOrTagKeyArgumentType)
			if !ok {
				return fmt.Errorf("expected *RegistryKeyArgumentType but got %T", v)
			}
			return util.WriteString(wr, i.Identifier)
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			id, err := util.ReadString(rd)
			if err != nil {
				return nil, err
			}
			return &ResourceOrTagKeyArgumentType{Identifier: id}, nil
		},
	}
	ResourceKeyArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			i, ok := v.(*ResourceKeyArgumentType)
			if !ok {
				return fmt.Errorf("expected *ResourceKeyArgumentType but got %T", v)
			}
			return util.WriteString(wr, i.Identifier)
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			id, err := util.ReadString(rd)
			if err != nil {
				return nil, err
			}
			return &ResourceKeyArgumentType{Identifier: id}, nil
		},
	}
	TimeArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			if protocol.GreaterEqual(version.Minecraft_1_19_4) {
				i, ok := v.(IntArgumentType)
				if ok {
					return util.WriteInt(wr, int(i))
				}
			}
			return nil
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			if protocol.GreaterEqual(version.Minecraft_1_19_4) {
				b, err := util.ReadInt(rd)
				return IntArgumentType(b), err
			}
			return IntArgumentType(0), nil
		},
	}

	Float64ArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			i, ok := v.(*brigodier.Float64ArgumentType)
			if !ok {
				return fmt.Errorf("expected *brigodier.Float64ArgumentType but got %T", v)
			}
			hasMin := i.Min != brigodier.MinFloat64
			hasMax := i.Max != brigodier.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, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			flags, err := util.ReadByte(rd)
			if err != nil {
				return nil, err
			}
			min := brigodier.MinFloat64
			max := brigodier.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
		},
	}
	Float32ArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			i, ok := v.(*brigodier.Float32ArgumentType)
			if !ok {
				return fmt.Errorf("expected *brigodier.Float32ArgumentType but got %T", v)
			}
			hasMin := i.Min != brigodier.MinFloat32
			hasMax := i.Max != brigodier.MaxFloat32
			flag := flags(hasMin, hasMax)

			err := util.WriteByte(wr, flag)
			if err != nil {
				return err
			}
			if hasMin {
				err = util.WriteFloat32(wr, i.Min)
				if err != nil {
					return err
				}
			}
			if hasMax {
				err = util.WriteFloat32(wr, i.Max)
			}
			return err
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			flags, err := util.ReadByte(rd)
			if err != nil {
				return nil, err
			}
			min := float32(brigodier.MinFloat32)
			max := float32(brigodier.MaxFloat32)
			if flags&HasMinIntFlag != 0 {
				min, err = util.ReadFloat32(rd)
				if err != nil {
					return nil, err
				}
			}
			if flags&HasMaxIntFlag != 0 {
				max, err = util.ReadFloat32(rd)
				if err != nil {
					return nil, err
				}
			}
			return &brigodier.Float32ArgumentType{Min: min, Max: max}, nil
		},
	}

	Int32ArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			i, ok := v.(*brigodier.Int32ArgumentType)
			if !ok {
				return fmt.Errorf("expected *brigodier.Int32ArgumentType but got %T", v)
			}
			hasMin := i.Min != brigodier.MinInt32
			hasMax := i.Max != brigodier.MaxInt32
			flag := flags(hasMin, hasMax)

			err := util.WriteByte(wr, flag)
			if err != nil {
				return err
			}
			if hasMin {
				err = util.WriteInt32(wr, i.Min)
				if err != nil {
					return err
				}
			}
			if hasMax {
				err = util.WriteInt32(wr, i.Max)
			}
			return err
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			flags, err := util.ReadByte(rd)
			if err != nil {
				return nil, err
			}
			min := int32(brigodier.MinInt32)
			max := int32(brigodier.MaxInt32)
			if flags&HasMinIntFlag != 0 {
				min, err = util.ReadInt32(rd)
				if err != nil {
					return nil, err
				}
			}
			if flags&HasMaxIntFlag != 0 {
				max, err = util.ReadInt32(rd)
				if err != nil {
					return nil, err
				}
			}
			return &brigodier.Int32ArgumentType{Min: min, Max: max}, nil
		},
	}
	Int64ArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			i, ok := v.(*brigodier.Int64ArgumentType)
			if !ok {
				return fmt.Errorf("expected *brigodier.Int64ArgumentType but got %T", v)
			}
			hasMin := i.Min != brigodier.MinInt64
			hasMax := i.Max != brigodier.MaxInt64
			flag := flags(hasMin, hasMax)

			err := util.WriteByte(wr, flag)
			if err != nil {
				return err
			}
			if hasMin {
				err = util.WriteInt64(wr, i.Min)
				if err != nil {
					return err
				}
			}
			if hasMax {
				err = util.WriteInt64(wr, i.Max)
			}
			return err
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			flags, err := util.ReadByte(rd)
			if err != nil {
				return nil, err
			}
			min := int64(brigodier.MinInt64)
			max := int64(brigodier.MaxInt64)
			if flags&HasMinIntFlag != 0 {
				min, err = util.ReadInt64(rd)
				if err != nil {
					return nil, err
				}
			}
			if flags&HasMaxIntFlag != 0 {
				max, err = util.ReadInt64(rd)
				if err != nil {
					return nil, err
				}
			}
			return &brigodier.Int64ArgumentType{Min: min, Max: max}, nil
		},
	}
	ModArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {

			return fmt.Errorf("unsupported operation")
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error) {
			var identifier *ArgumentIdentifier
			if protocol.GreaterEqual(version.Minecraft_1_19) {
				idx, err := util.ReadVarInt(rd)
				if err != nil {
					return nil, err
				}
				var suffix string
				if idx < 0 {
					suffix = fmt.Sprintf("n%d", -idx)
				} else {
					suffix = strconv.Itoa(idx)
				}
				identifier, err = newArgIdentifier("crossstitch:identified_"+suffix, versionSet{
					version: protocol,
					id:      idx,
				})
				if err != nil {
					return nil, err
				}
			} else {
				id, err := util.ReadString(rd)
				if err != nil {
					return nil, err
				}
				identifier, err = newArgIdentifier(id)
				if err != nil {
					return nil, err
				}
			}

			extraData, err := util.ReadBytes(rd)
			if err != nil {
				return nil, err
			}
			return &ModArgumentProperty{
				Identifier: identifier,
				Data:       extraData,
			}, nil
		},
	}
)

type ArgumentPropertyCodecFuncs

type ArgumentPropertyCodecFuncs struct {
	EncodeFn func(wr io.Writer, v any, protocol proto.Protocol) error
	DecodeFn func(rd io.Reader, protocol proto.Protocol) (brigodier.ArgumentType, error)
}

ArgumentPropertyCodecFuncs implements ArgumentPropertyCodec.

func (*ArgumentPropertyCodecFuncs) Decode

func (*ArgumentPropertyCodecFuncs) Encode

func (c *ArgumentPropertyCodecFuncs) Encode(wr io.Writer, v any, protocol proto.Protocol) error

type ByteArgumentType added in v0.19.0

type ByteArgumentType byte

func (ByteArgumentType) Parse added in v0.19.0

func (ByteArgumentType) String added in v0.19.0

func (b ByteArgumentType) String() string

type IntArgumentType added in v0.27.0

type IntArgumentType int

func (IntArgumentType) Parse added in v0.27.0

func (IntArgumentType) String added in v0.27.0

func (b IntArgumentType) String() string

type ModArgumentProperty added in v0.19.0

type ModArgumentProperty struct {
	Identifier *ArgumentIdentifier
	Data       []byte
}

func (*ModArgumentProperty) Parse added in v0.19.0

func (*ModArgumentProperty) String added in v0.19.0

func (m *ModArgumentProperty) String() string

type RegistryKeyArgumentType added in v0.18.2

type RegistryKeyArgumentType struct {
	Identifier string
}

func (*RegistryKeyArgumentType) Parse added in v0.18.2

func (*RegistryKeyArgumentType) String added in v0.18.2

func (r *RegistryKeyArgumentType) String() string

type ResourceKeyArgumentType added in v0.22.0

type ResourceKeyArgumentType RegistryKeyArgumentType

func (*ResourceKeyArgumentType) Parse added in v0.22.0

func (*ResourceKeyArgumentType) String added in v0.22.0

func (r *ResourceKeyArgumentType) String() string

type ResourceOrTagKeyArgumentType added in v0.22.0

type ResourceOrTagKeyArgumentType RegistryKeyArgumentType

func (*ResourceOrTagKeyArgumentType) Parse added in v0.22.0

func (*ResourceOrTagKeyArgumentType) String added in v0.22.0

Jump to

Keyboard shortcuts

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