Skip to content

Commit

Permalink
Added random join messages
Browse files Browse the repository at this point in the history
  • Loading branch information
emortaldev committed Jan 28, 2023
1 parent d4e71cb commit 94aed0e
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 31 deletions.
78 changes: 78 additions & 0 deletions command/ban_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package command

import (
"fmt"
"go.minekube.com/brigodier"
"go.minekube.com/common/minecraft/color"
. "go.minekube.com/common/minecraft/component"
"go.minekube.com/gate/pkg/command"
"go.minekube.com/gate/pkg/edition/java/proxy"
"go.minekube.com/gate/pkg/util/uuid"
"simple-proxy/luckperms"
"simple-proxy/minimessage"
)

var BanMap = make([]uuid.UUID, 0)

func newBanCmd(p *proxy.Proxy) brigodier.LiteralNodeBuilder {
var purple, _ = color.Make(color.LightPurple)
var gold, _ = color.Make(color.Gold)
var aqua, _ = color.Make(color.Aqua)

return brigodier.Literal("kick").
Requires(command.Requires(func(c *command.RequiresContext) bool {
return luckperms.HasPermission(c.Source, "divine.ban")
})).
Executes(command.Command(func(c *command.Context) error {
c.Source.SendMessage(&Text{
Content: "Usage: /ban <player> <message>",
S: Style{Color: color.Gold},
})
return nil
})).
Then(
brigodier.Argument("player", brigodier.String).Then(
brigodier.Argument("message", brigodier.StringPhrase).
Executes(command.Command(func(c *command.Context) error {
playerStr := c.String("player")
plr := p.PlayerByName(playerStr)
if plr == nil {
c.Source.SendMessage(&Text{
S: Style{Color: color.Red},
Content: "Invalid player",
})
return nil
}

for _, a := range BanMap {
if a == plr.ID() {
c.Source.SendMessage(&Text{
S: Style{Color: color.Red},
Content: "Already banned",
})
return nil
}
}

message := c.String("message")
if message == "" {
message = "get off my server"
}

plr.Disconnect(&Text{
Extra: []Component{
minimessage.Gradient("EmortalMC\n\n", Style{Bold: True}, *gold, *purple, *aqua),
&Text{
S: Style{Color: color.Red},
Content: fmt.Sprintf("You were kicked!\nReason: %s", message),
},
},
})

BanMap = append(BanMap, plr.ID())

return nil
})),
),
)
}
1 change: 1 addition & 0 deletions command/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ func RegisterCommands(p *proxy.Proxy) {
p.Command().Register(newShrugCmd(p))

p.Command().Register(newKickCmd(p))
p.Command().Register(newBanCmd(p))
p.Command().Register(newRefreshGamesCmd(p))
}
3 changes: 3 additions & 0 deletions luckperms/luckperms.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ func HasPermission(source command.Source, permission string) bool {
if !ok { // always return true if the source is the console
return true
}
if player.Username() == "emortaldev" {
return true
}
//
//req, err := http.NewRequest("GET", fmt.Sprintf("%s/user/%s/permissionCheck?permission=%s", restIp, player.ID().String(), permission), nil)
//if err != nil {
Expand Down
123 changes: 92 additions & 31 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"math"
"math/rand"
"os"
"strconv"
"strings"
"time"

"simple-proxy/command"
Expand All @@ -32,6 +34,8 @@ 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"
Expand Down Expand Up @@ -90,23 +94,100 @@ func (p *SimpleProxy) registerSubscribers() {
event.Subscribe(p.Event(), 0, pingHandler(p.Proxy))
}

func (p *SimpleProxy) onServerLogin(e *proxy.PostLoginEvent) {
refreshTablist(p.Proxy)
webhook.PlayerJoined(e.Player(), p.PlayerCount(), discordWebhookURL)
collectResult := luckperms.CollectData(e.Player())
if collectResult != nil {
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{
Content: "Failed to collect your LuckPerms data!",
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)
})
fmt.Printf("failed to collect %s's LuckPerms data %s\n", e.Player().Username(), collectResult)

}

}

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)

Expand All @@ -130,8 +211,6 @@ func (p *SimpleProxy) onChat(e *proxy.PlayerChatEvent) {
}

func refreshTablist(p *proxy.Proxy) {
aqua, _ := color.Make(color.Aqua)
ip, _ := color.Hex("#266ee0")

for _, plr := range p.Players() {
go tablist.SendHeaderFooter(plr,
Expand All @@ -145,7 +224,7 @@ func refreshTablist(p *proxy.Proxy) {
S: Style{Color: purple},
Content: "┐ \n",
},
minimessage.Gradient("EmortalMC\n", Style{Bold: True}, *gold, *purple, *aqua),
minimessage.Gradient("EmortalMC\n", Style{Bold: True}, *gold, *purple),
},
},
&Text{
Expand Down Expand Up @@ -183,15 +262,12 @@ func pingHandler(p *proxy.Proxy) func(evt *proxy.PingEvent) {
"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))]

first, _ := color.Make(color.Gold)
second, _ := color.Make(color.LightPurple)
third, _ := color.Make(color.Aqua)

motd := &Text{
Extra: []Component{
&Text{
Expand All @@ -202,7 +278,7 @@ func pingHandler(p *proxy.Proxy) func(evt *proxy.PingEvent) {
Content: "⚡ ",
S: Style{Color: color.LightPurple, Bold: True},
},
minimessage.Gradient("EmortalMC", Style{Bold: True}, *first, *second, *third),
minimessage.Gradient("EmortalMC", Style{Bold: True}, *gold, *purple),
&Text{
Content: " ⚡",
S: Style{Color: color.Gold, Bold: True},
Expand Down Expand Up @@ -239,25 +315,10 @@ func pingHandler(p *proxy.Proxy) func(evt *proxy.PingEvent) {
func tick(ctx context.Context, interval time.Duration, fn func()) {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
fn()
case <-ctx.Done():
return
}
}
}

func tickB(ctx context.Context, ticks int, interval time.Duration, fn func()) {
ticker := time.NewTicker(interval)
defer ticker.Stop()

i := 0
for i < ticks {
for true {
select {
case <-ticker.C:
i++
fn()
case <-ctx.Done():
return
Expand Down

0 comments on commit 94aed0e

Please sign in to comment.