Solved Problem with PlayerJoinEvent

Discussion in 'Plugin Development' started by Hugo1307, Jul 23, 2018.

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



    I'm developing a plugin and I need to, if the player hasn't never played on the server, that plugin should create some strings on the configuration.
    I created this code:

        public void onPlayerLoginEvent (PlayerLoginEvent e){
            Player p = e.getPlayer();
                Configuration.contasconfig.set("Maquinas.Jogadores." + e.getPlayer().getName().toString() + ".Compradas", Integer.valueOf(0));
                Configuration.contasconfig.set("Maquinas.Jogadores." + e.getPlayer().getName().toString() + ".Criadas", Integer.valueOf(0));
    However, what is happening is the opposite. When I enter on the server with an account that haven't never played on the server the if structure is ignored and when I enter with an account that had already played on the server de if structure works.

    Someone have any idea of what is going on?
  2. Offline


    @Hugo1307, Hey.
    I'm not really sure I'll help you, but there are a couple of suggestions.

    1. Try PlayerJoinEvent, instead.
    2. (Silly, of course) Do

    if (e.getPlayer (). hasPlayedBefore ()) {
    } else {
    Your code

    Hardly I helped you, but still.
  3. Offline


    If the player has never joined before, wouldn't the config not contain their name, meaning you could just check the config? Doing this will also means that player that have joined before this plugin has been created will also have their values set.
  4. If you can, you should use UUIDs instead of player names... players can change their names, while UUIDs will stay the same for each unique account. Make sure to use uuid.toString() as well wherever you’re not adding it to a string.
  5. Offline


    @ewrs Although you thought that your suggestion won't help me, it had solved the problem.
    Thank You!

    @0ct0berBkkitPlgins I probably will follow your advice but if I use p.getDisplayName() I will get the name in-game of the player which can be changed with the command /nick for example, but if I use p.getName() I will get the account name of the player which can't be changed. Am I right ?
    Last edited: Jul 24, 2018
  6. Offline


    Of course.

    Player p = event.getPlayer();
  7. Offline


    Players are able to change their names so it's best to use UUID's instead of p.getName(), i've also heard that using player names can cause some memory leaks (especially on join/leave) or something like that.

    EDIT: Btw i see that you have a "p" variable for "e.getPlayer()" but you are only using it here:
    1. if (!p.hasPlayedBefore()) {

    but you could also use it here:
    1. Configuration.contasconfig.set("Maquinas.Jogadores." + e.getPlayer().getName().toString() + ".Compradas", Integer.valueOf(0));
    2. Configuration.contasconfig.set("Maquinas.Jogadores." + e.getPlayer().getName().toString() + ".Criadas", Integer.valueOf(0));

    instead of the "e.getPlayer()". Another thing is that you are using "e.getPlayer().getName().toString()" while i'm pretty sure you don't need ".toString()" in this case. Just wanted to let you know, anyway hope this helped.
    Last edited by a moderator: Jul 24, 2018
  8. Offline


    To clarify: Memory leaks are caused because of the name change. Memory leaks are caused when an object is stored in memory but cannot be accessed. So, when a player changes their name, all data associated with the previous name cannot be accessed, and that is what causes the memory leak.
  9. Offline


  10. Offline


    @KarimAKL Yes, I know that i don't need the toString(), I don't even know why I ever use it. Anyway, thanks for the help of everyone, the problem is solved.
    Last edited: Jul 25, 2018
  11. Offline


    Glad to know it's solved it. :)
Thread Status:
Not open for further replies.

Share This Page