player.hasPlayedBefore() doesn´t work

Discussion in 'Plugin Development' started by MiBB3L, May 2, 2020.

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

    MiBB3L

    Code:
            if(p.hasPlayedBefore()) {
                p.teleport(location);
                p.getInventory().clear();
                p.getInventory().addItem(compass);
                e.setJoinMessage("§7[§a+§7] §" + PermissionsEx.getUser(p).getPrefix() + " §7» "  + p.getName());
              
                p.setScoreboard(board);
                p.setGameMode(GameMode.ADVENTURE);
              
            }else {
              
            p.teleport(location);
            p.getInventory().clear();
            p.getInventory().addItem(compass);
            e.setJoinMessage("§7[§a+§7] §" + PermissionsEx.getUser(p).getPrefix() + " §7» "  + p.getName() + " §6(spielt das erste Mal auf SkyEpic.de)");
            p.setScoreboard(board);
            p.setGameMode(GameMode.ADVENTURE);
                  
            }
    So.. here is my code, the user gets teleported to the location when he had played before. But if he doesn´t he gets not teleported (but the other join message get sent). The compass item and the gamemode do not get executed too, the socreboard gets set.
     
  2. Have you tried debugging? You should probably print out things like where the location is the result p.hasPlayedBefore().

    Also, unless you aren't showing us some code that changes the location or compass, you could probably simplify your code. There is no reason as far as I know to write the same code twice for both true and false cases in the if statement when they will be executing the same thing (regardless of p.hasPlayedBefore() being true or false).

    Code:
    p.teleport(location);
    p.getInventory().clear();
    p.getInventory().addItem(compass);
    p.setScoreboard(board);
    p.setGameMode(GameMode.ADVENTURE);
    
    if(p.hasPlayedBefore()) {
    e.setJoinMessage("§7[§a+§7] §" + PermissionsEx.getUser(p).getPrefix() + " §7» " + p.getName());
    }else {
    e.setJoinMessage("§7[§a+§7] §" + PermissionsEx.getUser(p).getPrefix() + " §7» " + p.getName() + " §6(spielt das erste Mal auf SkyEpic.de)");
    }
    
     
  3. Offline

    MiBB3L

    Doesn´t work...heres is my full PlayerJoinEvent:
    Code:
    @EventHandler(priority = EventPriority.HIGHEST)
        public void onFirstJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            FileConfiguration config = Main.getPlugin().getConfig();
            ItemStack compass = new ItemStack(Material.COMPASS, 1);
            ItemMeta compassmeta = compass.getItemMeta();
            compassmeta.setDisplayName("§3Navigator");
            compass.setItemMeta(compassmeta);
              
            World world = Bukkit.getWorld(config.getString("Spawn.World"));
            double x = config.getDouble("Spawn.X");
            double y = config.getDouble("Spawn.Y");
            double z = config.getDouble("Spawn.Z");
            float yaw = (float) config.getDouble("Spawn.Yaw");
            float pitch = (float) config.getDouble("Spawn.Pitch");
            Location location = new Location(world, x, y, z, yaw, pitch);
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
            Objective objective = board.registerNewObjective("abcd", "abcd");
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
            objective.setDisplayName("§6§lSkyEpic.de");
            objective.getScore(" ").setScore(5);
            objective.getScore("§b Dein Rang:").setScore(4);
            objective.getScore(" §" + PermissionsEx.getUser(p).getPrefix()).setScore(3);
            objective.getScore(" ").setScore(2);
            objective.getScore(" §bOnline:").setScore(1);
            objective.getScore(" §f" + Bukkit.getOnlinePlayers().size() + "§f/" + Bukkit.getMaxPlayers() + " Spieler").setScore(0);
    //        objective.getScore(" ").setScore(6);
    //        objective.getScore("§bMoney:").setScore(5);
            p.teleport(location);
            p.getInventory().clear();
            p.getInventory().addItem(compass);
            p.setScoreboard(board);
            p.setGameMode(GameMode.ADVENTURE);
            p.setLevel(0);
          
            if(p.hasPlayedBefore()) {
                e.setJoinMessage("§7[§a+§7] §" + PermissionsEx.getUser(p).getPrefix() + " §7» "  + p.getName());
            }else {
            p.setGameMode(GameMode.ADVENTURE);
            }
        }
     
  4. Do you get any errors, or is the join message simply not changing from the default format?
     
  5. Offline

    MiBB3L

    The Join Message is changing. I don´t get any errors but it says that the player is logging at the world of the location, but I don´t get teleportet. When I first join I get teleportet in the world "world" but in the console it says that Iam logging at the world "SkyEpicLobby" but that is not the case. The location is in the world "SkyEpicLobby".
     
  6. Is the issue that the player is not teleported to the right location? If so, maybe you should debug and print the x,y,z, yaw,pitch,world values and see if they lineup with what you need them to. If not, it may be an issue with your config setup.
     
  7. Offline

    MiBB3L

    Ive printed out the Location and there is everything right with it:
    Location{world=CraftWorld{name=SkyEpicLobby},x=0.5,y=51.0,z=0.5,pitch=-9.750026,yaw=1.1999958}
    The player still gets not teleported when he is joining the first time on the server. He gets teleported to a normal generated minecraft world called "world". Could this be because I don´t have a Spawn-Plugin. I don´t have one because I want my Players to respawn at the world they´re died in and not on the server spawn.

    @timtower do you have any ideas? In the past you often helped me.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 3, 2020
  8. Online

    timtower Administrator Administrator Moderator

    @MiBB3L How are you testing this?
    How are you making sure that the player hasn't played before?
     
  9. Offline

    MiBB3L

    Im doing it with:
    if(p.hasPlayedBefore()) {

    This works well I can test it with that. But the problem is that if the player haven´t ever played before, he gets not teleported to the location.
     
  10. Online

    timtower Administrator Administrator Moderator

    @MiBB3L And how are you making sure that they haven't played before? As in: how do you do that when testing?
    You can't have 100+ accounts for that.
     
  11. Offline

    MiBB3L

    I´m deleting the playerdata and stats in "world". That works. I did that in multiple Plugins and it never caused any problems except of now. I mean the join message changes for new user but the teleport to the location only works for user that played before on the server.
     
  12. Online

    timtower Administrator Administrator Moderator

    @MiBB3L Then why not remove parts to outside the check? You got a bunch of double code there.
     
  13. Offline

    MiBB3L

    I did that and it didn´t work. Now I only check the hasPlayedBefore for the JoinMessage. I tested it without the hasPlayedBefore and only the Join Event (without the other Join Message) and this didn´t work too.
     
Thread Status:
Not open for further replies.

Share This Page