Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
oq-x committed Sep 18, 2024
1 parent 05aea2b commit b3f3420
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
11 changes: 8 additions & 3 deletions server/session/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ type Broadcast struct {
dummies []Session
sessions_mu sync.RWMutex

EventManager EventManager

prev_msgs_mu sync.Mutex
previousMessages []play.PreviousMessage
}

func NewBroadcast(dummies ...Session) *Broadcast {
return &Broadcast{
sessions: make(map[uuid.UUID]Session),
dummies: dummies,
sessions: make(map[uuid.UUID]Session),
dummies: dummies,
EventManager: Default,
}
}

Expand Down Expand Up @@ -222,7 +225,6 @@ func (b *Broadcast) RemovePlayer(session Session) {
// when a new player joins the server
func (b *Broadcast) AddPlayer(session Session) {
b.sessions_mu.Lock()
defer b.sessions_mu.Unlock()

newListed, newGameMode, newLatency := session.Listed(), int32(session.Player().GameMode()), int32(session.Latency())

Expand Down Expand Up @@ -280,6 +282,9 @@ func (b *Broadcast) AddPlayer(session Session) {

session.PlayerInfoUpdate(toPlayerPk)
b.sessions[session.UUID()] = session

b.sessions_mu.Unlock()
b.EventManager.OnSessionAdd.call(session)
}

func (b *Broadcast) SpawnPlayer(session Session) {
Expand Down
12 changes: 12 additions & 0 deletions server/session/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ func (b *Broadcast) DisguisedChatMessage(session Session, content text.TextCompo
ses.DisguisedChatMessage(content, session, "minecraft:chat")
}
}

func (b *Broadcast) SystemChatMessage(content text.TextComponent) {
b.sessions_mu.RLock()
defer b.sessions_mu.RUnlock()

for _, ses := range b.sessions {
ses.SystemMessage(content)
}
for _, ses := range b.dummies {
ses.SystemMessage(content)
}
}
65 changes: 65 additions & 0 deletions server/session/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package session

func NewEvent[T any](handlers ...func(T)) Event[T] {
return Event[T]{handlers}
}

type Event[T any] struct {
handlers []func(T)
}

// Append adds handlers for the event. They will be handled last
func (e Event[T]) Append(handlers ...func(T)) {
e.handlers = append(e.handlers, handlers...)
}

// Prepend adds handlers for the event. They will be handled first
func (e Event[T]) Prepend(handlers ...func(T)) {
e.handlers = append(handlers, e.handlers...)
}

// Shift removes the first i handlers from the event
func (e Event[T]) Shift(i int) {
e.handlers = e.handlers[i:]
}

// Pop removes the last i handlers
func (e Event[T]) Pop(i int) {
e.handlers = e.handlers[i:]
}

// Override replaces all of the handlers of the event with the new handlers
func (e Event[T]) Override(handlers ...func(T)) {
e.handlers = handlers
}

// Chan creates a new channel handler
func (e Event[T]) Chan() <-chan T {
c := make(chan T)
e.Append(func(t T) {
c <- t
})

return c
}

// Await creates a new channel handler and waits for the event to be emitted
func (e Event[T]) Await() {
<-e.Chan()
}

func (e Event[T]) call(v T) {
for _, handler := range e.handlers {
handler(v)
}
}

type EventManager struct {
OnSessionAdd Event[Session]
OnSessionRemove Event[Session]
}

// Default is the default event manager
var Default = EventManager{
OnSessionAdd: NewEvent(onSessionAdd),
}
7 changes: 7 additions & 0 deletions server/session/onSessionAdd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package session

import "github.com/zeppelinmc/zeppelin/protocol/text"

func onSessionAdd(s Session) {
s.Broadcast().SystemChatMessage(text.Unmarshalf('&', "&e%s joined the game", s.Username()))
}
1 change: 1 addition & 0 deletions util/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var colors = map[string]*color.Color{
"aqua": color.New(color.FgHiCyan),
"red": color.New(color.FgHiRed),
"light_purple": color.New(color.FgHiMagenta),
"yellow": color.New(color.FgHiYellow),
"white": color.New(color.FgHiWhite),
}

Expand Down

0 comments on commit b3f3420

Please sign in to comment.