Pulcedocs
Pulce docs

Минимальный плагин

Небольшой плагин для проверки подключения и регистрации.

На этой странице

Методы плагина

Этот плагин регистрирует команду и пример контента в Init. Используйте его, чтобы проверить сборку и подключение к серверу.

minimal-plugin.go
package minimalplugin

import (
	"context"
	"fmt"

	"github.com/pulce-core/Pulce/core/command"
	"github.com/pulce-core/Pulce/core/content"
	"github.com/pulce-core/Pulce/core/gameplay"
	"github.com/pulce-core/Pulce/core/item"
	"github.com/pulce-core/Pulce/core/player"
	"github.com/pulce-core/Pulce/core/plugin"
)

type Plugin struct {
	ctx        plugin.Context
	unregister func()
}

func New() plugin.Plugin { return &Plugin{} }

func init() {
	plugin.MustRegisterFactory("minimalplugin", New)
}

func (p *Plugin) Name() string { return "minimalplugin" }

func (p *Plugin) Init(ctx plugin.Context) error {
	p.ctx = ctx

	if _, err := ctx.RegisterBlock(content.BlockSpec{Name: "minimalplugin:ruby_block"}); err != nil {
		return err
	}
	if _, err := ctx.RegisterItem(content.ItemSpec{Name: "minimalplugin:ruby", MaxCount: 64}); err != nil {
		return err
	}

	unregister, err := ctx.Commands().Register(command.Definition{
		Name: "ruby",
		Handler: p.giveRuby,
	})
	if err != nil {
		return err
	}
	p.unregister = unregister
	return nil
}

func (p *Plugin) Start(context.Context) error { return nil }

func (p *Plugin) Stop(context.Context) error {
	if p.unregister != nil {
		p.unregister()
	}
	return nil
}

func (p *Plugin) giveRuby(ctx context.Context, exec command.Execution) (command.Result, error) {
	_, err := p.ctx.Gameplay().GiveItem(gameplay.InventoryGiveRequest{
		PlayerID: player.ID(exec.Source.PlayerID),
		Stack: item.Stack{
			Item: item.State{Name: "minimalplugin:ruby"},
			Count: 1,
		},
	})
	if err != nil {
		return command.Result{}, err
	}
	return command.Result{Messages: []string{fmt.Sprintf("Gave ruby to %s.", exec.Source.Username)}}, nil
}