[Help] PlayerJoinEvent

Discussion in 'Plugin Development' started by milkymilkway, Aug 12, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    milkymilkway

    Okay So I have it where I want a player to join the server and their name be red above their head. Now this works with a command. But I rather it just work from joining the server. Any ideas on why it isn't working?

    Code:
    package me.milkymilkway.color;
     
    import net.minecraft.server.EntityPlayer;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Color extends JavaPlugin implements Listener{
    public void onEnable(){
    }
     
    public void inDisable(){
       
    }
     
    @EventHandler
    public void onJoinEvent(PlayerJoinEvent event){
        Player p = null;
        String name = "milkymilkway";
        EntityPlayer changingName = ((CraftPlayer) p).getHandle();
        if(event.getPlayer().getName() == name || p.getName() == name){
            changingName.name = ChatColor.RED+name;
          }
        }
    }
     
  2. Offline

    Phil2812

    Why is your player p = null? Shouldn't it be Player p = event.getPlayer() ?
    At least you seem to try to convert that null reference to a craftplayer afterwards?
     
  3. Offline

    JjPwN1

    Also, make public void inDisable(){ into

    Code:
    public void onDisable(){
     
  4. Offline

    milkymilkway

    No because when I do that the rest of the code doesnt work.
     
  5. Offline

    Turtlegasm

    What's the command that's working?
     
  6. Offline

    dark navi

    You're manipulating a null variable, if for some reason it doesn't throw an NPE, it wont manipulate the player who joined the server.
     
  7. Offline

    milkymilkway

    Actually the rest of the code does work. Just my name isnt red. So it still isnt working.

    This is my new code. And it still wont work

    Code:
    package me.milkymilkway.color;
     
    import net.minecraft.server.EntityPlayer;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Color extends JavaPlugin implements Listener{
    public void onEnable(){
    }
     
    public void onDisable(){
       
    }
     
    @EventHandler
    public void onJoinEvent(PlayerJoinEvent event){
        Player p = event.getPlayer();
        String name = "milkymilkway";
        EntityPlayer changingName = ((CraftPlayer) p).getHandle();
        if(event.getPlayer().getName() == name || p.getName() == name){
            changingName.name = ChatColor.RED+name;
          }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  8. Offline

    dark navi

    What do you mean, it "doesn't work?" Does it throw any errors?

    Also, this check ("if(event.getPlayer().getName() == name || p.getName() == name)") is redundant.
     
  9. Offline

    milkymilkway

    I get no errors. Its just suppost to check if the player who joins the server is "milkymilkway" and then if it is make his name red above his head. And this works through a command. Just not onJoinEvent which I want it to do on an onJoinEvent So I dont need to use a command.
     
  10. Offline

    dark navi

  11. Try this:

    Code:
    @EventHandler
    public void onJoinEvent(PlayerJoinEvent event){
        Player p = event.getPlayer();
        String name = "milkymilkyway";
        if(p.getName() == name){
            p.setDisplayName(ChatColor.RED+name);
        }
    }
     
  12. Offline

    dark navi


    This will only set their name in chat to red.
     
  13. Ah, you're right. I'm completely off :/.

    Are you registering your listener?
     
    dark navi likes this.
  14. Offline

    Jogy34

    Weird no one caught this. You can't compare Strings in java with the == opperator. You have to do something like this:
    Code:
    if(p.getName().equals(name))
    {
     
    }
    //or
    if(p.getName().equalsIgnoreCase(name))
    {
     
    }
    
     
  15. Offline

    milkymilkway

    Ill see thanks. man Thats probably it.

    Nope it still didnt work man

    This is my code now

    Code:
    package me.milkymilkway.color;
     
    import net.minecraft.server.EntityPlayer;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.craftbukkit.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Color extends JavaPlugin implements Listener{
    public void onEnable(){
    }
     
    public void onDisable(){
     
     }
     
    @EventHandler
    public void onJoinEvent(PlayerJoinEvent event){
        Player p = event.getPlayer();
        String name = "milkymilkway";
        EntityPlayer changingName = ((CraftPlayer) p).getHandle();
        if(p.getName().equalsIgnoreCase(name)){
            changingName.name = ChatColor.RED+name;
           }
        }
    }
    
    But still doesn't work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  16. Offline

    MrPmiguelP

    How about, register your event... i.e:
    Code:
    PluginManager pm = getServer().getPluginManager();
    // then
    pm.registerEvents(this, this);
    
    Also, put that in your onEnable()... Sorry if I'm wrong, but I'm really tired. 4:28 here
     
  17. Offline

    milkymilkway

    Ok Ill try =-)
     
  18. Offline

    toothplck1

    This would be because you have to edit the packets being sent to players to change the color of the name above your head. Calling up the name of a player and then changing it locally in the function does nothing at all. you can however change your TAB list display name color via: player.setPlayerListName(ChatColor.RED+player.getName());
     
  19. Offline

    ThatBox

    Make sure you do that in your onEnable()
     
Thread Status:
Not open for further replies.

Share This Page