-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxy.go
327 lines (287 loc) · 7.59 KB
/
proxy.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package main
import (
"context"
"fmt"
"math"
"math/rand"
"os"
"strconv"
"strings"
"time"
"simple-proxy/command"
"simple-proxy/game"
"simple-proxy/luckperms"
"simple-proxy/minimessage"
"simple-proxy/packet"
"simple-proxy/redisdb"
"simple-proxy/webhook"
"go.minekube.com/gate/pkg/edition/java/ping"
"go.minekube.com/gate/pkg/edition/java/proto/state"
"go.minekube.com/gate/pkg/edition/java/proto/version"
"go.minekube.com/gate/pkg/edition/java/proxy/tablist"
"github.com/robinbraemer/event"
"go.minekube.com/common/minecraft/color"
. "go.minekube.com/common/minecraft/component"
"go.minekube.com/gate/cmd/gate"
"go.minekube.com/gate/pkg/edition/java/proxy"
)
var discordWebhookURL string
var purple, _ = color.Make(color.LightPurple)
var gold, _ = color.Make(color.Gold)
var aqua, _ = color.Make(color.Aqua)
var ip, _ = color.Hex("#266ee0")
func main() {
const discordEnv = "DISCORD_WEBHOOK_URL"
discordWebhookURL = os.Getenv(discordEnv)
if discordWebhookURL == "" {
_, _ = fmt.Fprintln(os.Stderr, discordEnv)
os.Exit(1)
}
redisdb.RedisClient = redisdb.InitRedis()
state.Play.ClientBound.Register(&packet.EntitySoundEffect{}, &state.PacketMapping{
ID: 0x5D,
Protocol: version.Minecraft_1_19_3.Protocol,
})
proxy.Plugins = append(proxy.Plugins, proxy.Plugin{
Name: "SimpleProxy",
Init: func(ctx context.Context, proxy *proxy.Proxy) error {
return newSimpleProxy(proxy).init()
},
})
// Execute Gate entrypoint and block until shutdown.
// We could also run gate.Start if we don't need Gate's command-line.
gate.Execute()
}
type SimpleProxy struct {
*proxy.Proxy
}
func newSimpleProxy(proxy *proxy.Proxy) *SimpleProxy {
return &SimpleProxy{
Proxy: proxy,
}
}
func (p *SimpleProxy) init() error {
command.RegisterCommands(p.Proxy)
p.registerSubscribers()
packet.EntityStore.Subscribe(p.Event())
game.RegisterPubSub(p.Proxy)
return nil
}
// Register event subscribers
func (p *SimpleProxy) registerSubscribers() {
// Send message on server switch.
event.Subscribe(p.Event(), 0, p.onServerLogin)
event.Subscribe(p.Event(), 0, p.onServerDisconnect)
event.Subscribe(p.Event(), 0, p.onChat)
// Change the MOTD response.
event.Subscribe(p.Event(), 0, pingHandler(p.Proxy))
}
var randomJoinMessages = []string{
"How's the wife?",
"How's the kids?",
"What's the weather like?",
"Back so soon?",
"A good day for EmortalMC",
"Another great day for procrastination",
"Great to see you!",
"[Server] Back in 5 minutes",
"Salutations, <username>",
"Act busy, <username> is here",
"Not you again...",
"I hope you brought pizza",
"I hope you brought friends",
"I hope you aren't using Lunar",
"Welcome back, we missed you",
"You finally arrived!",
"Marathon again?",
}
func (p *SimpleProxy) onServerLogin(e *proxy.ServerPostConnectEvent) {
if e.PreviousServer() == nil {
refreshTablist(p.Proxy)
webhook.PlayerJoined(e.Player(), p.PlayerCount(), discordWebhookURL)
collectResult := luckperms.CollectData(e.Player())
if collectResult != nil {
e.Player().SendMessage(&Text{
Content: "Failed to collect your LuckPerms data!",
})
fmt.Printf("failed to collect %s's LuckPerms data %s\n", e.Player().Username(), collectResult)
}
thereAre := "There are now"
plrs := "players"
if p.PlayerCount() == 1 {
plrs = "player"
thereAre = "There is now"
}
e.Player().SendMessage(&Text{
Extra: []Component{
&Text{
S: Style{Color: color.Gray},
Content: "Welcome to ",
},
minimessage.Gradient("EmortalMC", Style{Bold: True}, *gold, *purple),
&Text{
S: Style{Color: color.Gray},
Content: fmt.Sprintf("! %s ", thereAre),
},
&Text{
S: Style{Color: color.Yellow},
Content: strconv.Itoa(p.PlayerCount()),
},
&Text{
S: Style{Color: color.Gray},
Content: fmt.Sprintf(" %s online", plrs),
},
},
})
randomMessage := minimessage.Gradient(strings.Replace(randomJoinMessages[rand.Intn(len(randomJoinMessages))], "<username>", e.Player().Username(), 1), Style{}, *gold, *purple)
ctx, cancel := context.WithCancel(e.Player().Context())
i := 0
go tick(ctx, 2*time.Second, func() {
if i > 4 {
defer cancel()
return
}
i += 1
e.Player().SendActionBar(randomMessage)
})
}
}
func (p *SimpleProxy) onServerDisconnect(e *proxy.DisconnectEvent) {
refreshTablist(p.Proxy)
webhook.PlayerLeft(e.Player(), p.PlayerCount(), discordWebhookURL)
}
func (p *SimpleProxy) onPreJoin(e *proxy.LoginEvent) {
for _, a := range command.BanMap {
if a == e.Player().ID() {
e.Deny(&Text{
Content: "get band",
})
break
}
}
}
func (p *SimpleProxy) onChat(e *proxy.PlayerChatEvent) {
e.SetAllowed(false)
components := []Component{
luckperms.DisplayName(e.Player()),
&Text{
S: Style{Color: color.Gray},
Content: ": ",
},
&Text{
S: Style{Color: color.White},
Content: e.Message(),
},
}
for _, plr := range p.Players() {
go plr.SendMessage(&Text{
Extra: components,
})
}
}
func refreshTablist(p *proxy.Proxy) {
for _, plr := range p.Players() {
go tablist.SendHeaderFooter(plr,
&Text{
Extra: []Component{
&Text{
S: Style{Color: gold},
Content: "┌ ",
},
&Text{
S: Style{Color: purple},
Content: "┐ \n",
},
minimessage.Gradient("EmortalMC\n", Style{Bold: True}, *gold, *purple),
},
},
&Text{
Extra: []Component{
&Text{
S: Style{Color: color.Gray},
Content: fmt.Sprintf(" \n%d online", p.PlayerCount()),
},
&Text{
S: Style{Color: ip},
Content: "\nᴍᴄ.ᴇᴍᴏʀᴛᴀʟ.ᴅᴇᴠ",
},
&Text{
S: Style{Color: purple},
Content: "\n└ ",
},
&Text{
S: Style{Color: gold},
Content: "┘ ",
},
},
},
)
}
}
func pingHandler(p *proxy.Proxy) func(evt *proxy.PingEvent) {
messages := []string{
"coolest server to ever exist",
"better than hypixel",
"you should join",
"stop scrolling, click here!",
"Lunar client users: Beware!",
"using 3 server softwares!",
"gradient lover",
"emortal is watching",
"Chuck Norris joined and said it was pretty good",
"private lobbies when?",
}
return func(e *proxy.PingEvent) {
randomMessage := messages[rand.Intn(len(messages))]
motd := &Text{
Extra: []Component{
&Text{
Content: "▓▒░ ",
S: Style{Color: color.LightPurple},
},
&Text{
Content: "⚡ ",
S: Style{Color: color.LightPurple, Bold: True},
},
minimessage.Gradient("EmortalMC", Style{Bold: True}, *gold, *purple),
&Text{
Content: " ⚡",
S: Style{Color: color.Gold, Bold: True},
},
&Text{
Content: " ░▒▓",
S: Style{Color: color.Gold},
},
&Text{
Content: "\n" + randomMessage,
S: Style{Color: color.Yellow},
},
},
}
evt := e.Ping()
evt.Description = motd
evt.Players.Max = evt.Players.Online + 1
sampleCount := int(math.Min(float64(p.PlayerCount()), 10))
samples := make([]ping.SamplePlayer, sampleCount)
for i, plr := range p.Players() {
if i >= sampleCount {
break
}
samples[i] = ping.SamplePlayer{Name: plr.Username(), ID: plr.ID()}
}
evt.Players.Sample = samples
}
}
// tick runs a function every interval until the context is cancelled.
func tick(ctx context.Context, interval time.Duration, fn func()) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for true {
select {
case <-ticker.C:
fn()
case <-ctx.Done():
return
}
}
}