github.com/df-mc/dragonfly@v0.9.13/server/cmd/register.go (about) 1 package cmd 2 3 import "sync" 4 5 // commands holds a list of registered commands indexed by their name. 6 var commands sync.Map 7 8 // Register registers a command with its name and all aliases that it has. Any command with the same name or 9 // aliases will be overwritten. 10 func Register(command Command) { 11 commands.Store(command.name, command) 12 for _, alias := range command.aliases { 13 commands.Store(alias, command) 14 } 15 } 16 17 // ByAlias looks up a command by an alias. If found, the command and true are returned. If not, the returned 18 // command is nil and the bool is false. 19 func ByAlias(alias string) (Command, bool) { 20 command, ok := commands.Load(alias) 21 if !ok { 22 return Command{}, false 23 } 24 return command.(Command), ok 25 } 26 27 // Commands returns a map of all registered commands indexed by the alias they were registered with. 28 func Commands() map[string]Command { 29 cmd := make(map[string]Command) 30 commands.Range(func(key, value any) bool { 31 cmd[key.(string)] = value.(Command) 32 return true 33 }) 34 return cmd 35 }