diff options
author | Riley <49880817+DerZombiiie@users.noreply.github.com> | 2022-05-12 09:33:51 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-12 09:33:51 +0200 |
commit | c3fe13d0963b73a813e3bbe508de9a274c704fc0 (patch) | |
tree | f33866cb9105e26b09e35a5a4cea6df970a308d8 | |
parent | 0e92b7ba4495b6a4e41ab3faf8052ef03888a6c7 (diff) | |
parent | 559ccaf2536375e02a924eeef754dda5697a2b28 (diff) |
Merge pull request #103 from ev2-1/plugin_interact
Plugin interact
-rw-r--r-- | plugin_interact.go | 52 | ||||
-rw-r--r-- | process.go | 7 |
2 files changed, 58 insertions, 1 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 +} @@ -441,6 +441,10 @@ func (cc *ClientConn) process(pkt mt.Pkt) { 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{}) @@ -736,6 +740,8 @@ 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 { @@ -748,7 +754,6 @@ func (sc *ServerConn) process(pkt mt.Pkt) { break } - prepend(sc.mediaPool, &cmd.Filename) if cmd.ShouldCache { cacheMedia(mediaFile{ name: cmd.Filename, |