-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessage_manager.go
119 lines (105 loc) · 2.84 KB
/
message_manager.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"io"
"strings"
)
type MessageManager struct {
/* listeners is an array of functions that
* tell the manager how to listen for specific
* text and what to do if we receive it.
* Each listener returns true if the input was
* "consumed" (i.e. - Don't send to any more
* listeners)
*/
listeners []func(inp *Message) bool
/* tempListeners is an array of functions that
* work the same as 'listeners', but these are
* just temporary and higher priority than
* 'listeners'
*/
tempListeners []func(inp *Message) bool
/* finishedTempListener is an index of a
* tempListener to be removed
*/
finishedTempListener int
output io.WriteCloser
}
var Listeners []func(inp *Message) bool
var TempListeners []func(inp *Message) bool
func (mm MessageManager) Tell(user string, what string, color string) {
valid_color := false
switch color {
case "black", "dark_blue", "dark_green", "dark_aqua",
"dark_red", "dark_purple", "gold", "gray", "dark_gray",
"blue", "green", "aqua", "red", "light_purple", "yellow",
"white":
valid_color = true
}
if !valid_color {
color = "white"
}
strings.Replace(what, "\n", "", -1)
o := "tellraw " + user + " {\"text\":\"" + what + "\",\"color\":\"" + color + "\"}"
mm.Output(o)
}
func (mm MessageManager) TellRaw(user string, what string) {
o := "tellraw " + user + " {\"text\":\"" + what + "\"}"
mm.Output(o)
}
func (mm MessageManager) Output(o string) {
if !strings.HasSuffix("\n", o) {
o = o + "\n"
}
mm.output.Write([]byte(o))
fmt.Printf("%s", o)
}
func (mm MessageManager) ProcessMessage(m *Message) bool {
// Now run the message through all of mm's tempListeners
for i := range TempListeners {
// Pop the listener off of the stack
consumed := TempListeners[i](m)
if consumed {
// When a temp listener is consumed, we delete it
RemoveTempListener(i)
return true
}
}
// and run the message through all of mm's listeners
for i := range Listeners {
consumed := Listeners[i](m)
if consumed {
return true
}
}
// Message not consumed, return false
return false
}
func AddListener(l func(*Message) bool) {
Listeners = append(Listeners, l)
}
func AddTempListener(l func(*Message) bool) {
TempListeners = append(TempListeners, l)
}
func RemoveListener(i int) {
if i > -1 && i < len(Listeners) {
t := append(Listeners[:i], Listeners[i+1:]...)
Listeners = make(([]func(*Message) bool), len(t))
copy(Listeners, t)
}
}
func RemoveTempListener(i int) {
if i > -1 && i < len(TempListeners) {
t := append(TempListeners[:i], TempListeners[i+1:]...)
TempListeners = make(([]func(*Message) bool), len(t))
copy(TempListeners, t)
}
}
func NewManager(o io.WriteCloser) MessageManager {
mm := MessageManager{
// listeners: make(([]func(*Message) bool), 0, 10),
// tempListeners: make(([]func(*Message) bool), 0, 10),
output: o,
}
return mm
}