Examples
Small working snippets for gameplay and content tasks.
On this page
Give Item And Fill Container
This example resolves a player, gives a named enchanted sword, then resolves a world container and inserts diamonds.
example.go
playerHandle, ok := ctx.Gameplay().Players().ByName("Steve")
if !ok {
return fmt.Errorf("player not found")
}
target, ok := ctx.Gameplay().Players().Resolve("Steve") // ID, name, XUID, or UUID
_ = target
_ = ok
_, err := playerHandle.Inventory().Give(item.New("diamond_sword").
Enchant(item.Sharpness(5)).
NameTag("Arena Blade").
Stack())
worldHandle, ok := ctx.Gameplay().Worlds().ByName("Hub")
if !ok {
return fmt.Errorf("world not found")
}
container := worldHandle.ContainerAt(world.Pos{X: 10, Y: 64, Z: 10}, 27)
_, err = container.Give(item.New("diamond").Count(8).Stack())Command With Typed Argument
Commands can parse typed arguments and return a structured command.Result for delivery.
givegem.go
func NewGiveGemCommand(ctx plugin.Context) command.Command {
return command.New("givegem").
Permission("example.ruby").
Argument("count", command.TypeInt, command.Default(1), command.Min(1), command.Max(64)).
Executor(func(_ context.Context, cmd command.Context) (command.Result, error) {
if cmd.Source.PlayerID == "" {
return command.Result{Messages: []string{"only players can use this command"}}, nil
}
count := cmd.Int("count")
playerHandle, err := cmd.Player()
if err != nil {
return command.Result{}, err
}
_, err = playerHandle.Inventory().Give(item.New("example:ruby").Count(count).Stack())
return command.Result{Messages: []string{"ruby added"}}, err
})
}Form Callback
Forms keep callbacks in core state and can use gameplay handles in response handlers.
forms.go
_, err := playerHandle.SendForm(
form.Simple("Kit selector").
Content("Choose a starter kit").
Button("Miner", func(fc form.Context, response form.SimpleResponse) error {
playerHandle, err := fc.Player()
if err != nil {
return err
}
_, err = playerHandle.Inventory().Give(item.New("iron_pickaxe").Stack())
return err
}).
OnClose(func(fc form.Context, reason form.CloseReason) error {
playerHandle, err := fc.Player()
if err != nil {
return err
}
return playerHandle.SendMessage("No kit selected")
}))