Skip to content

How to give players items when they first join (Minecraft mod)

Listen for a player joining, then give them a stack: player.getInventory().add(stack). The call returns false when the inventory is full, so drop the stack at the player’s feet in that case. To do it only once, put a tag on the player with player.addTag; tags are saved with the player and survive restarts. The join event runs on the server on every loader.

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.

  1. Register the join listener for your loader.
  2. Check that the player has no tag yet, then add the tag.
  3. Make the ItemStack and add it to the inventory; drop it when full.
  4. Join a world for the first time, then rejoin and check you get nothing more.

The example uses these values. Change them in the code.

Setting In the example What it does
item minecraft:bread The id of the item.
count 8 How many.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Uses:

  • ServerPlayConnectionEvents.JOIN
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
// The first time a player joins, they get 8 of minecraft:bread (Fabric API: ServerPlayConnectionEvents.JOIN). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> give(handler.getPlayer()));
}
private static void give(ServerPlayer player) {
if (player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside onInitialize:

WelcomeGiftKit.register();

Uses:

  • ServerPlayConnectionEvents.JOIN
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
// The first time a player joins, they get 8 of minecraft:bread (Fabric API: ServerPlayConnectionEvents.JOIN). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> give(handler.getPlayer()));
}
private static void give(ServerPlayer player) {
if (player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside onInitialize:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (NeoForge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(ResourceLocation.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (NeoForge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • ServerPlayConnectionEvents.JOIN
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
// The first time a player joins, they get 8 of minecraft:bread (Fabric API: ServerPlayConnectionEvents.JOIN). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> give(handler.getPlayer()));
}
private static void give(ServerPlayer player) {
if (player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(ResourceLocation.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside onInitialize:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (NeoForge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(ResourceLocation.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (NeoForge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • ServerPlayConnectionEvents.JOIN
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
// The first time a player joins, they get 8 of minecraft:bread (Fabric API: ServerPlayConnectionEvents.JOIN). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> give(handler.getPlayer()));
}
private static void give(ServerPlayer player) {
if (player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside onInitialize:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (NeoForge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.entityTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • ServerPlayConnectionEvents.JOIN
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
// The first time a player joins, they get 8 of minecraft:bread (Fabric API: ServerPlayConnectionEvents.JOIN). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> give(handler.getPlayer()));
}
private static void give(ServerPlayer player) {
if (player.entityTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside onInitialize:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (NeoForge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.entityTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.spawnAtLocation(player.level(), stack);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • ServerPlayConnectionEvents.JOIN
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.util.Prediction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.Identifier;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
// The first time a player joins, they get 8 of minecraft:bread (Fabric API: ServerPlayConnectionEvents.JOIN). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> give(handler.getPlayer()));
}
private static void give(ServerPlayer player) {
if (player.entityTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.getValue(Identifier.parse("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false, Prediction.SERVER_ONLY);
}
}
}

In your main mod class, inside onInitialize:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (Forge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (Forge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (NeoForge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof ServerPlayer player) || player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(BuiltInRegistries.ITEM.get(new ResourceLocation("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • ServerPlayConnectionEvents.JOIN
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
// The first time a player joins, they get 8 of minecraft:bread (Fabric API: ServerPlayConnectionEvents.JOIN). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> give(handler.getPlayer()));
}
private static void give(ServerPlayer player) {
if (player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:bread")), 8);
if (!player.getInventory().add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside onInitialize:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • PlayerInventory.add
  • Entity.addTag
  • PlayerEntity.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraft.util.ResourceLocation;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
// The first time a player joins, they get 8 of minecraft:bread (Forge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(WelcomeGiftKit::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getPlayer() instanceof ServerPlayerEntity)) {
return;
}
ServerPlayerEntity player = (ServerPlayerEntity) event.getPlayer();
if (player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(ForgeRegistries.ITEMS.getValue(new ResourceLocation("minecraft:bread")), 8);
if (!player.inventory.add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside <init>:

WelcomeGiftKit.register();

Uses:

  • ServerPlayConnectionEvents.JOIN
  • Inventory.add
  • Entity.addTag
  • Player.drop

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
// The first time a player joins, they get 8 of minecraft:bread (Fabric API: ServerPlayConnectionEvents.JOIN). A tag on the player remembers it.
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_welcome_gift";
private WelcomeGiftKit() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> give(handler.player));
}
private static void give(ServerPlayer player) {
if (player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
ItemStack stack = new ItemStack(Registry.ITEM.get(new ResourceLocation("minecraft:bread")), 8);
if (!player.inventory.add(stack)) {
player.drop(stack, false);
}
}
}

In your main mod class, inside onInitialize:

WelcomeGiftKit.register();

Uses:

  • PlayerEvent.PlayerLoggedInEvent
  • PlayerInventory.add
  • Entity.addTag
  • EntityPlayer.drop

In 1.12.2 the inventory call is player.inventory.addItemStackToInventory(stack).

src/main/java/com/example/examplemod/event/WelcomeGiftKit.java

package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
// The first time a player joins, they get 8 of minecraft:bread (Forge: PlayerEvent.PlayerLoggedInEvent). A tag on the player remembers it.
@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)
public final class WelcomeGiftKit {
private static final String TAG = "examplemod_starter_kit";
private WelcomeGiftKit() {
}
@SubscribeEvent
public static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
EntityPlayer player = event.player;
if (player.getTags().contains(TAG)) {
return;
}
player.addTag(TAG);
Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation("minecraft:bread"));
ItemStack stack = new ItemStack(item, 8);
if (!player.inventory.addItemStackToInventory(stack)) {
player.dropItem(stack, false);
}
}
}

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.