Thanks. I found the method, but now it appears my chat message is not sending. My code is as follows
if (!Minecraft.getMinecraft().isSinglePlayer) {
Minecraft.getMinecraft().player.sendChatMessage("Hello, " + playerIn.getDisplayName());
}
There's no error, but the message doesn't send.
You're accessing the Minecraft class (client-only) from a class in the server package (server-only). Any code in this class is executed ONLY when you're on a server world, where you can't access the Minecraft class (it's not visible on that side). I wonder why your game doesn't crash...
The parameter to initializeConnectionToPlayer is a EntityPlayerMP. Would I use the parameter to send the chat message?
EntityPlayerMP = serverside player
EntityPlayerSP = clientside player
EntityPlayer = superclass of both
almost all player entities given in methods are an EntityPlayerMP, but you don't ahve to worry about that since they still extend EntityPlayer.
If you don't get what that means google "Java inheritance"
I think I got some usefull code for you. I tested it on minecraft forge 1.10 and it worked perfectely fine.
code:
OnPlayerJoin.java:
package insert the package you are using;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class OnPlayerJoin {
@SubscribeEvent
public void PlayerJoin(EntityJoinWorldEvent event) {
if(!(event.getEntity() instanceof EntityPlayer)) {
return;
}
EntityPlayer player = (EntityPlayer) event.getEntity();
if(!event.getEntity().worldObj.isRemote) {
player.addChatMessage(new TextComponentString("Welcome, " + player.getName()));
}
}
}
and the Main.java file:
package org.devoxx4kids.forge.mods;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
@Mod(modid = Main.MODID, version = Main.VERSION)
public class Main {
public static final String MODID = "myMods";
public static final String VERSION = "1.0";
@EventHandler
public void init(FMLInitializationEvent event) {
MinecraftForge.EVENT_BUS.register(new OnPlayerJoin());
}
}
Now, if you want a colored message, add a "color code" to your message. so if you want a message in gold, you would do this:
Hi
I'm developing a mod using the Mod Coder Pack, and I would like to be able to send a chat message welcoming players when they join.
For example, if a person joins the server, people using the client should say
<username> Welcome, (person who joined)
How can this be accomplished?
-John
So you want each player on the server to send that player a chat message?
yes, I will tweak it later so it's not so spammy.
-John
bump
-John
look at net.minecraft.server.management.PlayerList#initializeConnectionToPlayer().
Thanks. I found the method, but now it appears my chat message is not sending. My code is as follows
There's no error, but the message doesn't send.
-John
Are you using forge or just MCP? And it's not sending because you're trying to do it client-side. You need a player instance.
I am just using MCP. I thought I was using the player instance by calling Minecraft.getMinecraft().player. That's the player object, right?
-John
Thats the client player object, which doesn't work when you are trying to use it on server side I
You're accessing the Minecraft class (client-only) from a class in the server package (server-only). Any code in this class is executed ONLY when you're on a server world, where you can't access the Minecraft class (it's not visible on that side). I wonder why your game doesn't crash...
Oh, I understand now.
Where do I get the multiplayer instance of the player, since Minecraft.getMinecraft().player is the client side instance?
-John
You are usually provided with one. If not it's because you aren't dealing with a specific player. You may have to loop through a player list.
The parameter to initializeConnectionToPlayer is a EntityPlayerMP. Would I use the parameter to send the chat message?
-John
the method provides an EntityPlayerMP.
EntityPlayerMP = serverside player
EntityPlayerSP = clientside player
EntityPlayer = superclass of both
almost all player entities given in methods are an EntityPlayerMP, but you don't ahve to worry about that since they still extend EntityPlayer.
If you don't get what that means google "Java inheritance"
OP isn't using Forge
This code is for forge, I am using the MCP. Thank you, though.
-John
You should consider it however, forge is a very powerful and easy to use modding tool. It also adds compatibility between mods.
I probably will.
-John
It's alright, I'll probably end up using the code either way.
-John