Skip to content

Modify command implementation to use a Command interface #10

@densestvoid

Description

@densestvoid

Currently, commands are being defined with functions that typically end up calling processCmdToEvent(Arr). As the functions are private methods, there are no options for commands to be defined for mods and datapacks in other packages while still using this package. A fork would be required.

Consider instead of defining a function for each command, define a struct fro each command that satisfies a Command interface which is passes to a Command function, which handles stringing the command to pass to the console, and reading/handling the events associated with that command. For example:

type Command interface {
	Command() string
	Events() []events.Event
}

type ExperienceAdd struct {
	Target string
	XP int32
	XPType ExperienceType
}

func (c ExperienceAdd) Command() string {
	return fmt.Sprintf(experience add %s &d %s, c.target, c.xp, c.xpType)
}

func (c ExperienceAdd) Events() []events.Event {
	return []events.Event{
		events.ExperienceAdd,
		events.NoPlayerFound.
	}
}

func (w *Wrapper) ExecuteCommand(c Command) error {
	ev, err := w.processCmdToEvent(c.Command(), time.Second, c.Events()...)
	if err != nil {
		return err
	}
	if ev.Is(events.NoPlayerFoundEvent) {
		return ErrPlayerNotFound
	}
	return nil
}

Please note this is not a real implementation, I just adapted the ExperienceAdd function to what it could look like in this Command interface format.

If this is an idea you are open to, I would be happy to work with you on a full implementation before switching from the current function approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions