Skip to content

Go: Commands

Functions

ExecuteCommand

Go
func ExecuteCommand(cmd string) ([]CommandOutput, error)

ExecuteCommand runs cmd as the console and returns what it printed. Server thread only. An error means the level is not ready, or the host cannot run commands.

  • Parameters:
    • cmd : string
  • Return type: ([]CommandOutput, error)
  • Slots: execute_command

RegisterCommand

Go
func RegisterCommand(name, description string, permission Permission, fn func(*Invocation)) error

RegisterCommand registers /name, whose text after the name reaches fn as Args. Call it from Enable, on the server thread. A command stays registered while the server runs, since Bedrock cannot unregister one; the host mutes it while the mod is disabled.

  • Parameters:
    • name : string
    • description : string
    • permission : Permission
    • fn : func(*Invocation)
  • Return type: error
  • Slots: register_command

NewOverload

Go
func NewOverload() *Overload

NewOverload begins an overload.

  • Return type: *Overload

NewCommand

Go
func NewCommand(name, description string, permission Permission) *CommandBuilder

NewCommand begins declaring /name.

  • Parameters:
    • name : string
    • description : string
    • permission : Permission
  • Return type: *CommandBuilder

RegisterCommandEnum

Go
func RegisterCommandEnum(name string, values []EnumValue) error

RegisterCommandEnum registers a fixed enum for RequiredEnum and OptionalEnum.

RegisterSoftEnum

Go
func RegisterSoftEnum(name string, values []string) error

RegisterSoftEnum registers an enum whose values can change while the server runs.

UpdateSoftEnum

Go
func UpdateSoftEnum(name string, op SoftEnumOp, values []string) error

UpdateSoftEnum replaces, adds to or removes from the values of a soft enum.

CommandCall

Go
1
2
3
4
5
6
7
type CommandCall struct {
    // Overload is the index of the overload that parsed.
    Overload int
    Args     *Nbt
    Origin   CommandOrigin
    // unexported fields
}

CommandCall is one run of a command declared with CommandBuilder.

CommandCall.Arg

Go
func (c *CommandCall) Arg(name string) (*Nbt, bool)

Arg is a named argument, and false for an optional one left out.

  • Parameters:
    • name : string
  • Return type: (*Nbt, bool)

CommandCall.ArgString

Go
func (c *CommandCall) ArgString(name string) (string, bool)

ArgString is a named argument as text.

  • Parameters:
    • name : string
  • Return type: (string, bool)

CommandCall.ArgInt

Go
func (c *CommandCall) ArgInt(name string) (int64, bool)

ArgInt is a named integer argument.

  • Parameters:
    • name : string
  • Return type: (int64, bool)

CommandCall.ArgFloat

Go
func (c *CommandCall) ArgFloat(name string) (float64, bool)

ArgFloat is a named number argument.

  • Parameters:
    • name : string
  • Return type: (float64, bool)

CommandCall.IsConsole

Go
func (c *CommandCall) IsConsole() bool

IsConsole reports whether the dedicated server console ran the command.

  • Return type: bool

CommandCall.Success

Go
func (c *CommandCall) Success(msg string)

Success sends a line of output.

  • Parameters:
    • msg : string

CommandCall.Error

Go
func (c *CommandCall) Error(msg string)

Error sends a line of error output.

  • Parameters:
    • msg : string

Overload

Go
1
2
3
type Overload struct {
    // unexported fields
}

Overload is one way a command parses: its parameters in order.

Overload.Required

Go
func (o *Overload) Required(name string, t ParamType) *Overload

Required adds a parameter that must be given.

  • Parameters:
    • name : string
    • t : ParamType
  • Return type: *Overload

Overload.Optional

Go
func (o *Overload) Optional(name string, t ParamType) *Overload

Optional adds a parameter that may be left out; Arg answers false for it then.

  • Parameters:
    • name : string
    • t : ParamType
  • Return type: *Overload

Overload.RequiredEnum

Go
func (o *Overload) RequiredEnum(name string, t ParamType, enum string) *Overload

RequiredEnum adds an enum parameter, which RegisterCommandEnum or RegisterSoftEnum registered first.

  • Parameters:
    • name : string
    • t : ParamType
    • enum : string
  • Return type: *Overload

Overload.OptionalEnum

Go
func (o *Overload) OptionalEnum(name string, t ParamType, enum string) *Overload

OptionalEnum adds an enum parameter that may be left out.

  • Parameters:
    • name : string
    • t : ParamType
    • enum : string
  • Return type: *Overload

Overload.Text

Go
func (o *Overload) Text(name, literal string) *Overload

Text adds a literal word, the shape /scoreboard objectives add is made of. The word comes back among the arguments under name, so a handler reads it like any other parameter. Sibling words taking the same arguments are one enum instead (contract section 6.1).

  • Parameters:
    • name : string
    • literal : string
  • Return type: *Overload

Invocation

Go
1
2
3
4
5
6
7
type Invocation struct {
    // Args is the raw text after the command name, possibly empty.
    Args string
    // Origin is the display name of whoever ran it: a player's name, or "Server".
    Origin string
    // unexported fields
}

Invocation is one run of a command. Success and Error may be called any number of times during the callback and not after it returns.

Invocation.Success

Go
func (i *Invocation) Success(msg string)

Success sends a line of output.

  • Parameters:
    • msg : string

Invocation.Error

Go
func (i *Invocation) Error(msg string)

Error sends a line of error output.

  • Parameters:
    • msg : string

CommandBuilder

Go
1
2
3
type CommandBuilder struct {
    // unexported fields
}

CommandBuilder declares a command with typed overloads. Each overload must parse a different input: two that accept the same words leave the engine to pick one.

CommandBuilder.Overload

Go
func (b *CommandBuilder) Overload(o *Overload) *CommandBuilder

Overload adds one way the command parses.

  • Parameters:
    • o : *Overload
  • Return type: *CommandBuilder

CommandBuilder.Register

Go
func (b *CommandBuilder) Register(fn func(*CommandCall)) error

Register registers the command. fn runs on the server thread for every run; a payload the binding cannot parse is reported to whoever ran the command and fn does not run.

CommandOutput

Go
1
2
3
4
type CommandOutput struct {
    Success bool
    Text    string
}

CommandOutput is one line a command printed.

Permission

Go
type Permission int32

Permission is who may run a command; it mirrors CommandPermissionLevel.

Name Value Description
PermissionAny 0
PermissionGameDirectors 1
PermissionAdmin 2
PermissionHost 3
PermissionOwner 4

ParamType

Go
type ParamType string

ParamType is the type of one command parameter, the kind words of register_command_ex.

Name Value Description
ParamInt "int"
ParamBool "bool"
ParamFloat "float"
ParamString "string"
ParamEnum "enum"
ParamSoftEnum "soft_enum"
ParamActor "actor"
ParamPlayer "player"
ParamBlockPos "block_pos"
ParamVec3 "vec3"
ParamRawText "raw_text"
ParamMessage "message"
ParamJSON "json"
ParamItem "item"
ParamBlockName "block_name"
ParamEffect "effect"
ParamActorType "actor_type"
ParamCommand "command"
ParamRelativeFloat "relative_float"
ParamFilePath "file_path"

CommandOrigin

Go
1
2
3
4
5
6
7
8
type CommandOrigin struct {
    Name string
    // Kind is the CommandOriginType: 0 a player, 7 the server console, -1 not said.
    Kind    int32
    Dim     int32
    X, Y, Z float64
    HasPos  bool
}

CommandOrigin is who ran a command and where.

EnumValue

Go
1
2
3
4
type EnumValue struct {
    Name  string
    Value int64
}

EnumValue is one value of a command enum.

SoftEnumOp

Go
type SoftEnumOp int32

SoftEnumOp is how UpdateSoftEnum changes the values.

Name Value Description
SoftEnumSet 0
SoftEnumAdd 1
SoftEnumRemove 2