Get player on PlayerJoinEvent

Discussion in 'Plugin Development' started by Typ, Jul 17, 2016.

Thread Status:
Not open for further replies.
  1. Hi,
    It thought you could get the Player with Player p = e.getPlayer(); But this isn't so:

    Heres the Code:
    TerraSurvival.java
    Code:
    package net.OPT_developer.de;
    
    import net.OPT_developer.de.Listener.PlayerJoinEvent;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.io.File;
    import java.io.IOException;
    
    /**
    * Created by mrose on 17.07.2016.
    */
    public class TerraSurvival extends JavaPlugin
    {
        // variables
        public String prefix = "§e[Survival]";
        private ConsoleCommandSender console = getServer().getConsoleSender();
    
        // methods
        @Override
        public void onEnable()
        {
            registerListener();
            createConfig();
            console.sendMessage(prefix + "§e aktiviert");
        }
    
        @Override
        public void onDisable()
        {
    
        }
    
    
        public void registerListener()
        {
            getServer().getPluginManager().registerEvents(new PlayerJoinEvent(this), this);
        }
    
        public void createConfig()
        {
            File ordner = new File("plugins/TerraSurvival/player-datas");
            File playerhomefile = new File("plugins/TerraSurvival/player-datas/player-datas.yml");
    
            if (!ordner.exists())
            {
                ordner.mkdir();
            }
    
            if (!playerhomefile.exists())
            {
                try
                {
                    playerhomefile.createNewFile();
                } catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
    
            YamlConfiguration cfg = YamlConfiguration.loadConfiguration(playerhomefile);
        }
    }
    
    
    And here is PlayerJoinEvent.java
    Code:
    package net.OPT_developer.de.Listener;
    
    import net.OPT_developer.de.TerraSurvival;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    
    /**
    * Created by mrose on 17.07.2016.
    */
    public class PlayerJoinEvent implements Listener
    {
        TerraSurvival plugin;
        public PlayerJoinEvent(TerraSurvival plugin)
        {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            this.plugin = plugin;
        }
    
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e)
        {
            Player p = e.getPlayer();
        }
    }
    

    I'll add a screenshot, where you can see, that there is no e.getPlayer(); avaible.

    Hopefully someone find the error!

    Greetings Typ
     
  2. @Typ You're using your class not Bukkits, it's recommended you don't call your classes the same as ones you are going to use.
     
    I Al Istannen likes this.
  3. Offline

    I Al Istannen

    @Typ
    And don't use "§" directly in your code. It can be screwed up between different file encodings (as it is an unicode char) and it may change in the future. Use the ChatColor.COLOR or make a method called "color", which takes a String and returns "ChatColor#translateAlternateColorCodes('&', <String>)". The you can just call this method and use "&" as the color char.

    "console.sendMessage(prefix + "§e aktiviert");"
    Bukkit already logs this for you and colors spam the log. I would delete this line.

    "ordner.mkdir();"
    Not needed in this case, but you can use "mkdirs()", to create the folder and all parent ones.

    "public String prefix = "§e[Survival]";"
    Don't make this public, use a getter, or if you really want it public, make it final. Otherwise all classes will be able to change the prefix, which is probably not wanted.
     
  4. Thank you two very much!
    It's working fine, now :)
     
  5. Offline

    SantaClawz69

    Please set as solved so others know :)
     
Thread Status:
Not open for further replies.

Share This Page