brigadier

package
v0.0.0-...-d63482c Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2025 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

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

func (ArgumentIdentifier) String

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) (any, 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) (any, error) {
			return brigodier.Bool, nil
		},
	}
	ByteArgumentPropertyCodec ArgumentPropertyCodec = &ArgumentPropertyCodecFuncs{
		EncodeFn: func(wr io.Writer, v any, protocol proto.Protocol) error {
			b, ok := v.(byte)
			if !ok {
				return fmt.Errorf("expected byte but got %T", v)
			}
			return util.WriteByte(wr, b)
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (any, error) {
			b, err := util.ReadByte(rd)
			return 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) (any, 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) (any, 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) (any, 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) (any, 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.(int)
				if ok {
					return util.WriteInt(wr, i)
				}
			}
			return nil
		},
		DecodeFn: func(rd io.Reader, protocol proto.Protocol) (any, error) {
			if protocol.GreaterEqual(version.Minecraft_1_19_4) {
				b, err := util.ReadInt(rd)
				return b, err
			}
			return 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) (any, 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) (any, 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) (any, 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) (any, 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) (any, 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) (any, error)
}

ArgumentPropertyCodecFuncs implements ArgumentPropertyCodec.

func (*ArgumentPropertyCodecFuncs) Decode

func (c *ArgumentPropertyCodecFuncs) Decode(rd io.Reader, protocol proto.Protocol) (any, error)

func (*ArgumentPropertyCodecFuncs) Encode

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

type ByteArgumentType

type ByteArgumentType byte

func (ByteArgumentType) Parse

func (ByteArgumentType) String

func (b ByteArgumentType) String() string

type IntArgumentType

type IntArgumentType int

func (IntArgumentType) Parse

func (IntArgumentType) String

func (b IntArgumentType) String() string

type ModArgumentProperty

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

func (*ModArgumentProperty) Parse

func (*ModArgumentProperty) String

func (m *ModArgumentProperty) String() string

type RegistryKeyArgumentType

type RegistryKeyArgumentType struct {
	Identifier string
}

func (*RegistryKeyArgumentType) Parse

func (*RegistryKeyArgumentType) String

func (r *RegistryKeyArgumentType) String() string

type ResourceKeyArgumentType

type ResourceKeyArgumentType RegistryKeyArgumentType

func (*ResourceKeyArgumentType) Parse

func (*ResourceKeyArgumentType) String

func (r *ResourceKeyArgumentType) String() string

type ResourceOrTagKeyArgumentType

type ResourceOrTagKeyArgumentType RegistryKeyArgumentType

func (*ResourceOrTagKeyArgumentType) Parse

func (*ResourceOrTagKeyArgumentType) String

Jump to

Keyboard shortcuts

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