Handling events
Unturned Plugin Development #3
MCrow • 5 months ago
MCrow • 5 months ago
This time we're gonna learn how to handle events in the plugin. We will create a simple plugin that will send a message to the chat when a player joins the server.
Events are actions that occur in the game, such as player joining the server, player dying, player chatting, etc. In RocketMod, events are handled by creating event handlers. Event handlers are methods that are called when the event occurs. In this guide, we will handle the PlayerConnected
event, which is called when a player joins the server.
Start by deleting Logger.Log("Hello, Unturned!");
line from the previous guide.
Open ExamplePlugin.cs
file in the Example
project. Add the following method to the ExamplePlugin
class:
public void OnPlayerConnected(UnturnedPlayer player)
{
UnturnedChat.Say($"{player.DisplayName} joined the server!");
}
If you paste the code, Visual Studio will automatically add using directives for UnturnedPlayer
and UnturnedChat
classes. If it doesn't, add them manually or press Ctrl + .
on the class name and select using Rocket.Unturned;
from the list.
using Rocket.Unturned.Player;
using Rocket.Unturned.Chat;
Alernatively, you can right click on the class name and select Quick Actions and Refactorings
→ using Rocket.Unturned;
.
In the Load
method of the ExamplePlugin
class, add the following line of code to register the event handler:
U.Events.OnPlayerConnected += OnPlayerConnected;
This line of code tells RocketMod to call the OnPlayerConnected
method when a player joins the server.
You will need to add using directive for U
class:
using Rocket.Unturned;
In the Unload
method of the ExamplePlugin
class, add the following line of code to dispose the event handler:
U.Events.OnPlayerConnected -= OnPlayerConnected;
It is important to dispose event handlers when the plugin is unloaded, so the plugin won't send messages to the console when it was unloaded using /rocket unload Example
command.
The end result should look like this:
using Rocket.Core.Logging;
using Rocket.Core.Plugins;
using Rocket.Unturned;
using Rocket.Unturned.Chat;
using Rocket.Unturned.Player;
namespace RestoreMonarchy.Example
{
public class ExamplePlugin : RocketPlugin<ExampleConfiguration>
{
protected override void Load()
{
U.Events.OnPlayerConnected += OnPlayerConnected;
Logger.Log($"{Name} {Assembly.GetName().Version.ToString(3)} has been loaded!");
}
protected override void Unload()
{
U.Events.OnPlayerConnected -= OnPlayerConnected;
Logger.Log($"{Name} has been unloaded!");
}
private void OnPlayerConnected(UnturnedPlayer player)
{
UnturnedChat.Say($"{player.DisplayName} joined the server!");
}
}
}
Press Ctrl + Shift + B
to compile the plugin. If there are no errors, you will see Build succeeded
in the output window. Copy the Example.dll
file to the Rocket
→ Plugins
folder on your Unturned server. Start your Unturned server. Join the server and you should see the message PlayerName joined the server!
in the chat.
Modify the plugin to send a message to the chat when a player dies. You will need to handle the PlayerDeath
event
UnturnedPlayerEvents.OnPlayerDeath += OnPlayerDeath;
and create a method OnPlayerDeath
that sends a message to the chat.
private void OnPlayerDeath(UnturnedPlayer player, EDeathCause cause, ELimb limb, CSteamID murderer)
{
// Send chat message here
}
EDeathCasue
, ELimb
and CSteamID
will require adding SDG.Unturned
and Steamworks
using directives.