Примеры

Небольшие рабочие фрагменты для gameplay и content задач.

Выдать предмет и заполнить контейнер

Пример находит игрока, выдает именной зачарованный меч, затем находит контейнер в мире и кладет туда алмазы.

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.Result, который сервер доставит источнику команды.

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, ok := ctx.Gameplay().Players().Get(cmd.Source.PlayerID)			if !ok {				return command.Result{Messages: []string{"player not found"}}, nil			}			_, err := playerHandle.Inventory().Give(item.New("example:ruby").Count(count).Stack())			return command.Result{Messages: []string{"ruby added"}}, err		})}

Обработчик формы

Forms хранят обработчики ответа на стороне сервера и могут использовать gameplay handles внутри callback.

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, ok := ctx.Gameplay().Players().Get(fc.PlayerID)			if !ok {				return nil			}			_, err := playerHandle.Inventory().Give(item.New("iron_pickaxe").Stack())			return err		}).		OnClose(func(fc form.Context, reason form.CloseReason) error {			return ctx.Gameplay().SendMessage(gameplay.PlayerMessageRequest{				PlayerID: fc.PlayerID,				Text:     "No kit selected",			})		}))