JoinLoginEvent

Discussion in 'Spigot Plugin Development' started by Tomdaarom, Apr 18, 2017.

  1. Hello, I have a little problem, my code doesn't work. When I join, It is supposed to teleport me to a location and send me a message but it doesn't. I don't get any errors in the console

    This is my code :
    Code (Text):
    package me.officialnl.main;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin{
       
        public void onEnable(){
            getLogger().info("BizzCraft First Join plugin enabled");
            getServer().getPluginManager().registerEvents((Listener) this, this);
        }
       
        public void onDisable(){
            getLogger().info("BizzCraft First Join plugin disabled");
        }
       
        private void teleportInWorld(Player p, int x, int y, int z){
            p.teleport(new Location(p.getWorld(), x, y, z));
        }
       
        @EventHandler
        public void PlayerJoin(PlayerLoginEvent event) {
        Player p = event.getPlayer();
        if(!p.hasPlayedBefore()) {
        teleportInWorld(p, 149,33,170);
        p.sendMessage("You have to accept the /rules before you can play");
        }
      }
       
        public boolean onCommand(CommandSender s, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("acceptrules")){
                if (!(s instanceof Player)){
                    getLogger().info("Only players are allowed to use this command");
                    return true;
                }
                Player p = (Player) s;
                p.performCommand("help");
                p.sendMessage(ChatColor.GREEN + "You accepted the rules");
                }
            return false;
            }
        }
     
  2. You're not technically in the game at that point. Change where you call the teleport method to a bukkitrunnable that is delayed by a tick or two. Then have the runnable teleport you


    Sent from my iPhone using Tapatalk
     
  3. Hello,

    As said above, the reason why it doesn't work is because you're still technically in the middle of joining the server. @dNiym suggests using a delayed task; while this would work well in most cases, I suggest using PlayerJoinEvent instead of PlayerLoginEvent (do not mix these two up!). This would be a safer way of fixing the problem, and wouldn't require any additional code - just change PlayerLoginEvent to PlayerJoinEvent.
     
  4. I changed it and it still isn't working
     
  5. Then I recommend you try the following:

    1) Make a debug statement for Player#hasPlayedBefore(). Print the value it returns and see if it's true or false.
    2) Try what was said above: use a scheduled task (I'd go with 10 ticks, then go down if it works).
     
  6. Change it to PlayerJoinEvent.

    Should work :p

    I would also advise making a method that does the teleportation, creating the location then teleporting them

    For one of my upcoming premiums I have a PlayerJoinEvent, I create the location and then teleport the player to it
     
    #6 24763, Apr 18, 2017
    Last edited by a moderator: Apr 20, 2017
  7. Try adding the INT's to the config and reaching them from there
     
  8. Try to remove the cast on registerEvents method and have your class implement Listener, instead.
     
  9. I implemented Listener and it is working now, thank you.