diff options
Diffstat (limited to 'plugin_interact.go')
-rw-r--r-- | plugin_interact.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/plugin_interact.go b/plugin_interact.go new file mode 100644 index 0000000..f3d00d9 --- /dev/null +++ b/plugin_interact.go @@ -0,0 +1,52 @@ +package proxy + +import ( + "sync" + + "github.com/anon55555/mt" +) + +// A InteractionHandler holds information on how to handle a Minetest Interaction. +type InteractionHandler struct { + Type Interaction + Handler func(*ClientConn, *mt.ToSrvInteract) bool +} + +type Interaction uint8 + +const ( + Dig Interaction = iota + StopDigging + Dug + Place + Use + Activate + AnyInteraction = 255 +) + +var interactionHandlers []InteractionHandler +var interactionHandlerMu sync.RWMutex +var interactionHandlerOnce sync.Once + +// RegisterInteractionHandler adds a new InteractionHandler. +func RegisterInteractionHandler(handler InteractionHandler) { + interactionHandlerMu.Lock() + defer interactionHandlerMu.Unlock() + + interactionHandlers = append(interactionHandlers, handler) +} + +func handleInteraction(cmd *mt.ToSrvInteract, cc *ClientConn) bool { + handled := false + + for _, handler := range interactionHandlers { + interaction := Interaction(handler.Type) + if interaction == AnyInteraction || interaction == handler.Type { + if handler.Handler(cc, cmd) { + handled = true + } + } + } + + return handled +} |