requested changes
parent
308b7ae083
commit
562f688965
|
@ -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 Interaction(handler.Type) == WildCard || Interaction(cmd.Action) == handler.Type {
|
||||
if handler.Handler(cc, cmd) {
|
||||
handled = true
|
||||
}
|
||||
}
|
||||
if cmd.Action == handler.Type {
|
||||
handle(handler.Handler(cc, cmd))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return handled
|
||||
|
|
10
process.go
10
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 {
|
||||
|
|
Loading…
Reference in New Issue