How to show a message above the hotbar in a Minecraft mod
To show a short line above the hotbar, send the player a message flagged as an action bar message: player.displayClientMessage(component, true). With false it goes to chat. On a ServerPlayer it also sends the text over the network, so call it on the server. For text players can translate, use a translatable component with a lang entry; for a quick test, use a literal one.
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.
- Override use and check the level is not client side.
- Send the player the message with the action bar flag set to true.
- For real mods, use a translatable component and add the text to the lang file (en_us.json).
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 and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.19 to 1.20.1, Fabric 1.19 to 1.20.3
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.19 to 1.20.1, Fabric 1.19 to 1.20.3”Uses:
Player.displayClientMessageComponent.literal
src/main/java/com/example/examplemod/item/MessageScrollItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobSpawnType;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Message Scroll: right-click to show a line above the hotbar. Pass false instead of true to write to chat.public class MessageScrollItem extends Item { public MessageScrollItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { if (!level.isClientSide()) { player.displayClientMessage(Component.literal("Hello, " + player.getName().getString() + "!"), true); } return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); }}It’s registered like any other item, as MessageScrollItem: see a new item.
Fabric and NeoForge 1.21.2 to 1.21.11
Section titled “Fabric and NeoForge 1.21.2 to 1.21.11”Uses:
Player.displayClientMessageComponent.literal
src/main/java/com/example/examplemod/item/MessageScrollItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.EntitySpawnReason;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Message Scroll: right-click to show a line above the hotbar. Pass false instead of true to write to chat.public class MessageScrollItem extends Item { public MessageScrollItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { if (!level.isClientSide()) { player.displayClientMessage(Component.literal("Hello, " + player.getName().getString() + "!"), true); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as MessageScrollItem: see a new item.
Fabric and NeoForge 26.1 to 26.3
Section titled “Fabric and NeoForge 26.1 to 26.3”Uses:
Player.sendOverlayMessageComponent.literal
Since 26.1 displayClientMessage(text, true) is sendOverlayMessage(text) (the action bar).
src/main/java/com/example/examplemod/item/MessageScrollItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.EntitySpawnReason;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Message Scroll: right-click to show a line above the hotbar. Pass false instead of true to write to chat.public class MessageScrollItem extends Item { public MessageScrollItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { if (!level.isClientSide()) { player.sendOverlayMessage(Component.literal("Hello, " + player.getName().getString() + "!")); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as MessageScrollItem: see a new item.
Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
Player.displayClientMessageComponent.literal
src/main/java/com/example/examplemod/item/MessageScrollItem.java
package com.example.examplemod.item;
import net.minecraft.network.chat.TextComponent;import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobSpawnType;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Message Scroll: right-click to show a line above the hotbar. Pass false instead of true to write to chat.public class MessageScrollItem extends Item { public MessageScrollItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { if (!level.isClientSide()) { player.displayClientMessage(new TextComponent("Hello, " + player.getName().getString() + "!"), true); } return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); }}It’s registered like any other item, as MessageScrollItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
PlayerEntity.displayClientMessageStringTextComponent
In 1.16.5 the text is a StringTextComponent (or a TranslationTextComponent for translatable text).
src/main/java/com/example/examplemod/item/MessageScrollItem.java
package com.example.examplemod.item;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.Hand;import net.minecraft.util.ActionResult;import net.minecraft.entity.player.PlayerEntity;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.world.World;
// Message Scroll: right-click to show a line above the hotbar. Pass false instead of true to write to chat.public class MessageScrollItem extends Item { public MessageScrollItem(Item.Properties properties) { super(properties); }
@Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { if (!world.isClientSide()) { player.displayClientMessage(new StringTextComponent("Hello, " + player.getName().getString() + "!"), true); } return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide()); }}It’s registered like any other item, as MessageScrollItem: see a new item.
Fabric 1.16.5 to 1.18.2
Section titled “Fabric 1.16.5 to 1.18.2”Uses:
Player.displayClientMessageComponent.literal
Before 1.19 the text is new TextComponent(…) (new TranslatableComponent(…) for translatable text) instead of Component.literal.
src/main/java/com/example/examplemod/item/MessageScrollItem.java
package com.example.examplemod.item;
import net.minecraft.network.chat.TextComponent;import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobSpawnType;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Message Scroll: right-click to show a line above the hotbar. Pass false instead of true to write to chat.public class MessageScrollItem extends Item { public MessageScrollItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { if (!level.isClientSide()) { player.displayClientMessage(new TextComponent("Hello, " + player.getName().getString() + "!"), true); } return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); }}It’s registered like any other item, as MessageScrollItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
EntityPlayer.displayClientMessageTextComponentString
In 1.12.2 the method is player.sendStatusMessage(component, true), the text is a TextComponentString, and the lang file is en_us.lang.
src/main/java/com/example/examplemod/item/MessageScrollItem.java
package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.util.ActionResult;import net.minecraft.util.EnumActionResult;import net.minecraft.util.EnumHand;import net.minecraft.util.text.TextComponentString;import net.minecraft.world.World;
// Message Scroll: right-click to show a line above the hotbar. Pass false instead of true to write to chat.public class MessageScrollItem extends Item { @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { if (!world.isRemote) { player.sendStatusMessage(new TextComponentString("Hello, " + player.getName() + "!"), true); } return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand)); }}It’s registered like any other item, as MessageScrollItem: see a new item.
Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/items/interactions
- https://docs.fabricmc.net/1.21.1/develop/items/first-item
Related how-tos
Section titled “Related how-tos”- An item that fills the hunger bar
- An item that gives the player another item
- An item that heals you when you use it
- An item that launches you into the air
- An item that teleports you forward
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.