aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--plugin_interact.go46
-rw-r--r--process.go10
2 files changed, 25 insertions, 31 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
diff --git a/process.go b/process.go
index b576143..806dacd 100644
--- a/process.go
+++ b/process.go
@@ -438,13 +438,13 @@ func (cc *ClientConn) process(pkt mt.Pkt) {
return
}
- if handleInteraction(cmd, cc) { // if return true: already handled
- return
- }
-
if _, ok := cmd.Pointed.(*mt.PointedAO); ok {
srv.swapAOID(&cmd.Pointed.(*mt.PointedAO).ID)
}
+
+ if handleInteraction(cmd, cc) { // if return true: already handled
+ return
+ }
case *mt.ToSrvChatMsg:
done := make(chan struct{})
@@ -741,7 +741,7 @@ func (sc *ServerConn) process(pkt mt.Pkt) {
return
case *mt.ToCltMediaPush:
prepend(sc.mediaPool, &cmd.Filename)
-
+
var exit bool
for _, f := range clt.media {
if f.name == cmd.Filename {