aboutsummaryrefslogtreecommitdiff
path: root/plugin_interact.go
diff options
context:
space:
mode:
authorRiley <riley@e926.de>2022-05-11 11:17:37 +0200
committerRiley <riley@e926.de>2022-05-11 11:17:37 +0200
commit562f6889652d87fd31f440204c6a5b564e7ccc33 (patch)
treee6a3f120b514f04ad7c95292957513903dffcd69 /plugin_interact.go
parent308b7ae083264c2d3a6411335f8e60921173c370 (diff)
requested changes
Diffstat (limited to 'plugin_interact.go')
-rw-r--r--plugin_interact.go46
1 files changed, 20 insertions, 26 deletions
diff --git a/plugin_interact.go b/plugin_interact.go
index 6b4b146..f835c25 100644
--- a/plugin_interact.go
+++ b/plugin_interact.go
@@ -8,49 +8,43 @@ import (
// A InteractionHandler holds information on how to handle a Minetest Interaction.
type InteractionHandler struct {
- Type mt.Interaction // can be 255 to register on all interactions
- Handler func(*ClientConn, *mt.ToSrvInteract) bool
+ Type Interaction
+ Handler func(*ClientConn, *mt.ToSrvInteract) bool
}
+type Interaction uint8
+
+const (
+ Dig Interaction = iota
+ StopDigging
+ Dug
+ Place
+ Use
+ Activate
+ WildCard = 255
+)
+
var interactionHandlers []InteractionHandler
var interactionHandlerMu sync.RWMutex
var interactionHandlerOnce sync.Once
// RegisterInteractionHandler adds a new InteractionHandler.
func RegisterInteractionHandler(handler InteractionHandler) {
- initInteractionHandlers()
-
- chatCmdsMu.Lock()
- defer chatCmdsMu.Unlock()
+ interactionHandlerMu.Lock()
+ defer interactionHandlerMu.Unlock()
interactionHandlers = append(interactionHandlers, handler)
}
-func initInteractionHandlers() {
- chatCmdsOnce.Do(func() {
- interactionHandlerMu.Lock()
- defer interactionHandlerMu.Unlock()
-
- interactionHandlers = make([]InteractionHandler, 0)
- })
-}
-
func handleInteraction(cmd *mt.ToSrvInteract, cc *ClientConn) bool {
handled := false
- handle := func(cond bool) {
- if(cond) {
- handled = true
- }
- }
for _, handler := range interactionHandlers {
- if handler.Type == 255 {
- handle(handler.Handler(cc, cmd))
- }
- if cmd.Action == handler.Type {
- handle(handler.Handler(cc, cmd))
+ if Interaction(handler.Type) == WildCard || Interaction(cmd.Action) == handler.Type {
+ if handler.Handler(cc, cmd) {
+ handled = true
+ }
}
-
}
return handled