How to do something when a player joins in a Minecraft mod
When a player has joined a server, the loader fires an event your mod can listen to: Fabric API’s ServerPlayConnectionEvents.JOIN, or PlayerEvent.PlayerLoggedInEvent on the game event bus on Forge and NeoForge. It runs on the server, with the player ready to receive messages.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Register a listener for the join event when your mod starts.
- In it, take the player (a ServerPlayer).
- Do what you need: send a message, give an item, read their data.
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| message | Welcome to the server! |
The message it shows. |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric 1.19 to 1.21.1, Fabric 1.21.2 to 26.3
Section titled “Fabric 1.19 to 1.21.1, Fabric 1.21.2 to 26.3”Uses:
ServerPlayConnectionEvents.JOINServerGamePacketListenerImpl.getPlayerServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;import net.minecraft.network.chat.Component;import net.minecraft.server.level.ServerPlayer;
// Runs on the server when a player has joined and can receive messages (Fabric API: ServerPlayConnectionEvents.JOIN).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's onInitialize. public static void register() { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { ServerPlayer player = handler.getPlayer(); player.sendSystemMessage(Component.literal("Welcome to the server!")); }); }}In your main mod class, inside onInitialize:
WelcomeJoinEvents.register();NeoForge 1.20.2 to 1.21.1, NeoForge 26.1 to 26.3
Section titled “NeoForge 1.20.2 to 1.21.1, NeoForge 26.1 to 26.3”Uses:
PlayerEvent.PlayerLoggedInEventNeoForge.EVENT_BUSPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// Runs on the server when a player has logged in (NeoForge's game event bus: PlayerEvent.PlayerLoggedInEvent).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(WelcomeJoinEvents::onLoggedIn); }
private static void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { event.getEntity().sendSystemMessage(Component.literal("Welcome to the server!")); }}In your main mod class, inside <init>:
WelcomeJoinEvents.register();NeoForge 1.21.2 to 1.21.11
Section titled “NeoForge 1.21.2 to 1.21.11”Uses:
PlayerEvent.PlayerLoggedInEventNeoForge.EVENT_BUSPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// Runs on the server when a player has logged in (NeoForge's game event bus: PlayerEvent.PlayerLoggedInEvent).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(WelcomeJoinEvents::onLoggedIn); }
private static void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { event.getEntity().displayClientMessage(Component.literal("Welcome to the server!"), false); }}In your main mod class, inside <init>:
WelcomeJoinEvents.register();Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
PlayerEvent.PlayerLoggedInEventMinecraftForge.EVENT_BUSPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.minecraft.Util;import net.minecraft.network.chat.TextComponent;import net.minecraft.network.chat.Component;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerEvent;
// Runs on the server when a player has logged in (Forge's game event bus: PlayerEvent.PlayerLoggedInEvent).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(WelcomeJoinEvents::onLoggedIn); }
private static void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { event.getEntity().sendMessage(new TextComponent("Welcome to the server!"), Util.NIL_UUID); }}In your main mod class, inside <init>:
WelcomeJoinEvents.register();Forge 1.19 to 1.20.1
Section titled “Forge 1.19 to 1.20.1”Uses:
PlayerEvent.PlayerLoggedInEventMinecraftForge.EVENT_BUSPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerEvent;
// Runs on the server when a player has logged in (Forge's game event bus: PlayerEvent.PlayerLoggedInEvent).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(WelcomeJoinEvents::onLoggedIn); }
private static void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { event.getEntity().sendSystemMessage(Component.literal("Welcome to the server!")); }}In your main mod class, inside <init>:
WelcomeJoinEvents.register();NeoForge 1.20.1
Section titled “NeoForge 1.20.1”Uses:
PlayerEvent.PlayerLoggedInEventMinecraftForge.EVENT_BUSPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.minecraft.network.chat.Component;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerEvent;
// Runs on the server when a player has logged in (NeoForge's game event bus: PlayerEvent.PlayerLoggedInEvent).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(WelcomeJoinEvents::onLoggedIn); }
private static void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { event.getEntity().sendSystemMessage(Component.literal("Welcome to the server!")); }}In your main mod class, inside <init>:
WelcomeJoinEvents.register();Fabric 1.18 to 1.18.2
Section titled “Fabric 1.18 to 1.18.2”Uses:
ServerPlayConnectionEvents.JOINServerGamePacketListenerImpl.getPlayerServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.minecraft.Util;import net.minecraft.network.chat.TextComponent;import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;import net.minecraft.network.chat.Component;import net.minecraft.server.level.ServerPlayer;
// Runs on the server when a player has joined and can receive messages (Fabric API: ServerPlayConnectionEvents.JOIN).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's onInitialize. public static void register() { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { ServerPlayer player = handler.getPlayer(); player.sendMessage(new TextComponent("Welcome to the server!"), Util.NIL_UUID); }); }}In your main mod class, inside onInitialize:
WelcomeJoinEvents.register();Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
PlayerEvent.PlayerLoggedInEventMinecraftForge.EVENT_BUSPlayerEntity.sendMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.Util;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerEvent;
// Runs on the server when a player has logged in (Forge's game event bus: PlayerEvent.PlayerLoggedInEvent).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(WelcomeJoinEvents::onLoggedIn); }
private static void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { event.getPlayer().sendMessage(new StringTextComponent("Welcome to the server!"), Util.NIL_UUID); }}In your main mod class, inside <init>:
WelcomeJoinEvents.register();Fabric 1.16.5 to 1.17.1
Section titled “Fabric 1.16.5 to 1.17.1”Uses:
ServerPlayConnectionEvents.JOINServerGamePacketListenerImpl.getPlayerServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import net.minecraft.Util;import net.minecraft.network.chat.TextComponent;import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;import net.minecraft.network.chat.Component;import net.minecraft.server.level.ServerPlayer;
// Runs on the server when a player has joined and can receive messages (Fabric API: ServerPlayConnectionEvents.JOIN).public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
// Called from the mod's onInitialize. public static void register() { ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> { ServerPlayer player = handler.player; player.sendMessage(new TextComponent("Welcome to the server!"), Util.NIL_UUID); }); }}In your main mod class, inside onInitialize:
WelcomeJoinEvents.register();Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
PlayerEvent.PlayerLoggedInEventMinecraftForge.EVENT_BUSEntityPlayer.sendMessage
src/main/java/com/example/examplemod/event/WelcomeJoinEvents.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.util.text.TextComponentString;import net.minecraftforge.fml.common.Mod;import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;import net.minecraftforge.fml.common.gameevent.PlayerEvent;
// Runs on the server when a player has logged in (Forge: PlayerEvent.PlayerLoggedInEvent).@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)public final class WelcomeJoinEvents { private WelcomeJoinEvents() { }
@SubscribeEvent public static void onLoggedIn(PlayerEvent.PlayerLoggedInEvent event) { event.player.sendMessage(new TextComponentString("Welcome to the server!")); }}Sources
Section titled “Sources”- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/networking/v1/ServerPlayConnectionEvents.html
- https://docs.neoforged.net/docs/1.21.1/concepts/events
Related how-tos
Section titled “Related how-tos”- Give players items when they first join
- React to chat messages
- React when a player breaks a block
- React when a player right-clicks a block
- React when something takes damage
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.