Register to a simple event - HevelMc/CraftWithPy GitHub Wiki

Someone joined the server !

Introduction

Tired of the boring yellow "user joined the game" message? Let's personalize it! In this tutorial, we are going to edit the join and leave messages with colors and more.

The code

# -*- coding: iso-8859-1 -*-
from core import register_event

class login_messages:
    def on_enable(self):
        register_event("PlayerJoinEvent", self.on_join_event)
        register_event("PlayerQuitEvent", self.on_quit_event)

    def on_join_event(self, event):
        event.setJoinMessage("§8[§2+§8] §a" + str(event.getPlayer().getName()) + " §7joined the server !")

    def on_quit_event(self, event):
        event.setQuitMessage("§8[§4-§8] §c" + str(event.getPlayer().getName()) + " §7left the server !")

Output

Explanations

  • Line 2 is the import for the register_event function
  • Line 6 is the event registration : The first argument is the event name (see Spigot Docs for the list of events). The second argument is the function to call when the event is triggered.
  • Line 7: Here we call the event.setJoinMessage() function to replace the original message. You should now be familiar with the rest of the code.