aboutsummaryrefslogtreecommitdiff
path: root/plugin_interact.go
blob: ce7897253ea23b8c09ad83ed8df70bd598d175c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package proxy

import (
	"sync"

	"github.com/HimbeerserverDE/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
}