Solved player.hasPlayedBefore() help

Discussion in 'Spigot Plugin Development' started by R3dThunderr, Jun 13, 2017.

  1. Code (Text):
    @EventHandler
        public void onJoin(PlayerLoginEvent e) {
            Player p = e.getPlayer();
         
            if (p.hasPlayedBefore() == false) {
                Log.info("Has not played before!");
                String path = "spongeTime." + p.getUniqueId().toString();
                Main.getPlugin().getConfig().addDefault(path, 0);
                Main.getPlugin().saveConfig();
                return;
            } else {
                Log.info("Has played before");
                Integer value = Main.getPlugin().getConfig().getInt("spongeTime."+p.getUniqueId().toString());
                cooldown.put(p, value);
                Log.info(Integer.toString(value));
            }
        }
     
        @EventHandler
        public void onLeave (PlayerQuitEvent e) {
            Player p = e.getPlayer();
            UUID uuid = p.getUniqueId();
         
            String path = "SpongeCooldownRemaining." + uuid;
            Main.getPlugin().getConfig().set(path, cooldown.get(p));
            Main.getPlugin().saveConfig();
         
            Log.info("Saving the config at " + Integer.toString(cooldown.get(p)));
        }
    cooldown is a hashmap which decreases by 1 every second.


    The Problem: Every time a player joins the server, the console outputs "Has not played before!"
    I don't quite understand... I've used hasPlayedBefore in the past and it's all worked out fine.
    NOTE: Items/location of the player is the same between login attempts.
    Even when the player has played before, my code is thinking that they haven't...?
     
    #1 R3dThunderr, Jun 13, 2017
    Last edited: Jun 13, 2017
  2. Wild guess, try using the PlayerPreLoginEvent, which is fired I assume before the player logs in, and before Player#hasPlayedBefore() can be set to true, because if they login, they have played before. Also, as a means of simplifying your if statement so you look fancier, you can just do if (!p.hasPlayedBefore()) {
     
  3. Not sure how to use that event exactly. Been poking around with it for the last couple minutes and I keep getting errors.

    What baffles me is that in another plugin I wrote, it works.
    Code (Text):
    @EventHandler
        public void onJoin(PlayerLoginEvent e) {
            Player p = e.getPlayer();
         
            if (p.hasPlayedBefore() == false) {
                String path = "Credits." + p.getUniqueId().toString();
                Main.getPlugin().getConfig().addDefault(path, 250);
                Main.getPlugin().saveConfig();
            }
        }
    That plugin is coded for 1.8.8, and works flawlessly.



    Why would it be any different for 1.12 from 1.8? And, it's telling me they haven't played before, not that they have.

    Thx for help, I'm really confused lol
     
  4. There was probably some code changes from 1.8 to your current version. Just replace PlayerLoginEvent with PlayerPreLoginEvent, and see how that goes. If it doesn't work, I'm not sure what else to suggest.
     
    • Like Like x 1
  5. Code (Text):
    @EventHandler
        public void onJoin(PlayerPreLoginEvent e) {
            Player p = Bukkit.getPlayer(e.getUniqueId());
           
            if (p.hasPlayedBefore() == false) { //this is line 58 (where the null pointer exception error is)
                Log.info("Has not played before!");
                String path = "spongeTime." + p.getUniqueId().toString();
                Main.getPlugin().getConfig().addDefault(path, 0);
                Main.getPlugin().saveConfig();
                return;
            } else {
                Log.info("Has played before");
                Integer value = Main.getPlugin().getConfig().getInt("spongeTime."+p.getUniqueId().toString());
                cooldown.put(p, value);
                Log.info(Integer.toString(value));
            }
        }
    Here is what I've got. (You can't use e.getPlayer, so I made an alternative which doesn't give any compiling errors)

    However, I get this error in console everytime I log in.
    [20:50:20] [Server thread/ERROR]: Could not pass event PlayerPreLoginEvent to Survival v1.0
    org.bukkit.event.EventException: null
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:499) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at net.minecraft.server.v1_12_R1.LoginListener$LoginHandler$1.evaluate(LoginListener.java:281) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at net.minecraft.server.v1_12_R1.LoginListener$LoginHandler$1.evaluate(LoginListener.java:1) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at org.bukkit.craftbukkit.v1_12_R1.util.Waitable.run(Waitable.java:24) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:757) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_112]
    Caused by: java.lang.NullPointerException
    at org.FortressWars.general.SpongeRide.onJoin(SpongeRide.java:58) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_112]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_112]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_112]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.jar:git-Spigot-cd6ba67-2a2d6d6]
    ... 11 more
    [20:50:20] [User Authenticator #4/INFO]: UUID of player R3dThunderr is 90121240-1d2e-472e-a4eb-1fd5914444aa
    [20:50:20] [Server thread/INFO]: R3dThunderr[/ip-address(removed)] logged in with entity id 2935 at ([world]117.15232014627745, 63.0, 304.52818840126145)
     
  6. Try this
    Code (Text):


    if (!p.hasPlayedBefore() && p != null)  {

    //code

    }

     
    If that didn't work then I will see more into this problem of yours.
     
    • Like Like x 1
  7. Firestar311

    Supporter

    Use the OfflinePlayer object. The Player object only works of they are actually logged into the game.
     
    • Like Like x 1
  8. Ok, thank you everyone!

    Here is the current state of things in case anyone is wondering. Otherwise, simply go on your way and bless your soul.

    -The path to the integer cooldown in the config.yml was set to two different things for the leave and join events (this was only a problem once everything else was fixed)
    -Changed the hashmap containing <Player, Integer> (integer = cooldown) to <String, Integer> (string = playername, integer = cooldown). This is to accommodate how I need to switch from player to offlineplayer and back again. It simply gives more flexibility. Although my first post's code would have worked in 1.8, it doesn't seem like it will work in 1.12.
    Anyway, thanks for your help and patience!