commit
c3fe13d096
|
@ -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 {
|
if _, ok := cmd.Pointed.(*mt.PointedAO); ok {
|
||||||
srv.swapAOID(&cmd.Pointed.(*mt.PointedAO).ID)
|
srv.swapAOID(&cmd.Pointed.(*mt.PointedAO).ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if handleInteraction(cmd, cc) { // if return true: already handled
|
||||||
|
return
|
||||||
|
}
|
||||||
case *mt.ToSrvChatMsg:
|
case *mt.ToSrvChatMsg:
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
|
||||||
|
@ -736,6 +740,8 @@ func (sc *ServerConn) process(pkt mt.Pkt) {
|
||||||
|
|
||||||
return
|
return
|
||||||
case *mt.ToCltMediaPush:
|
case *mt.ToCltMediaPush:
|
||||||
|
prepend(sc.mediaPool, &cmd.Filename)
|
||||||
|
|
||||||
var exit bool
|
var exit bool
|
||||||
for _, f := range clt.media {
|
for _, f := range clt.media {
|
||||||
if f.name == cmd.Filename {
|
if f.name == cmd.Filename {
|
||||||
|
@ -748,7 +754,6 @@ func (sc *ServerConn) process(pkt mt.Pkt) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
prepend(sc.mediaPool, &cmd.Filename)
|
|
||||||
if cmd.ShouldCache {
|
if cmd.ShouldCache {
|
||||||
cacheMedia(mediaFile{
|
cacheMedia(mediaFile{
|
||||||
name: cmd.Filename,
|
name: cmd.Filename,
|
||||||
|
|
Loading…
Reference in New Issue