Merge branch 'main' of github.com:HimbeerserverDE/mt-multiserver-proxy

master
HimbeerserverDE 2022-05-14 18:56:33 +02:00
commit 7a9ff285f3
No known key found for this signature in database
GPG Key ID: A3D3E205DA0B0401
2 changed files with 58 additions and 1 deletions

52
plugin_interact.go Normal file
View File

@ -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
}

View File

@ -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,