mt-multiserver-proxy/plugin_interact.go

53 lines
1.1 KiB
Go
Raw Permalink Normal View History

2022-05-10 09:28:34 -07:00
package proxy
import (
"sync"
"github.com/anon55555/mt"
)
// A InteractionHandler holds information on how to handle a Minetest Interaction.
type InteractionHandler struct {
2022-05-11 02:17:37 -07:00
Type Interaction
Handler func(*ClientConn, *mt.ToSrvInteract) bool
2022-05-10 09:28:34 -07:00
}
2022-05-11 02:17:37 -07:00
type Interaction uint8
const (
Dig Interaction = iota
StopDigging
Dug
Place
Use
Activate
2022-05-11 11:52:20 -07:00
AnyInteraction = 255
2022-05-11 02:17:37 -07:00
)
2022-05-10 09:28:34 -07:00
var interactionHandlers []InteractionHandler
var interactionHandlerMu sync.RWMutex
var interactionHandlerOnce sync.Once
// RegisterInteractionHandler adds a new InteractionHandler.
func RegisterInteractionHandler(handler InteractionHandler) {
2022-05-11 02:17:37 -07:00
interactionHandlerMu.Lock()
defer interactionHandlerMu.Unlock()
2022-05-10 09:28:34 -07:00
interactionHandlers = append(interactionHandlers, handler)
}
func handleInteraction(cmd *mt.ToSrvInteract, cc *ClientConn) bool {
handled := false
for _, handler := range interactionHandlers {
2022-05-11 11:52:20 -07:00
interaction := Interaction(handler.Type)
if interaction == AnyInteraction || interaction == handler.Type {
2022-05-11 02:17:37 -07:00
if handler.Handler(cc, cmd) {
handled = true
}
2022-05-10 09:28:34 -07:00
}
}
return handled
}