The 'PlayerJoinEvent' won't work!

Discussion in 'Plugin Development' started by DeFlavLemon, Dec 31, 2016.

Thread Status:
Not open for further replies.
  1. Hello, as the title suggests, I'm trying to use a player join event so that when a player joins, they will do certain things, here's my code:
    Code:
    package com.deflavlemon.hub;
    
    import org.bukkit.Bukkit;
    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.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Hub extends JavaPlugin {
    
        public void onEnable() {
            Bukkit.getServer().getLogger().info("Enabled the Hub plugin!");
        }
       
        public void onDisable() {
            Bukkit.getServer().getLogger().info("Disabled the Hub plugin!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandlabel, String[] args) {
            Player player = (Player) sender;
           
            if (cmd.getName().equalsIgnoreCase("hub")) {       
                sender.sendMessage(ChatColor.GREEN + "You have been teleported to the hub!");
                player.setHealth(20);
                player.setFoodLevel(20);
               
                Location location = player.getLocation();
               
                location.setX(28.5);
                location.setY(65);
                location.setZ(55.5);
                player.teleport(location);
            }
            return false;
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event, Player player) {
           
            player.setHealth(20);
            player.setFoodLevel(20);
           
            Location location = player.getLocation();
           
            location.setX(28.5);
            location.setY(65);
            location.setZ(55.5);
            player.teleport(location);
        }
    }
    Any help is greatly appreciated! :)
     
  2. Online

    timtower Administrator Administrator Moderator

    dart2112 likes this.
  3. Just tried that but I can't seem to figure out how it works, could you explain in depth? Also, thanks for the quick reply. :)
     
  4. Offline

    Jakeeeee

  5. Offline

    Asparock

    To register the event you need to put :
    Code:
    getServer().getPluginManager().registerEvents(this, this);
    
    On your onEnable method for example
     
  6. thanks!
     
  7. Offline

    Asparock

    You're welcome !

    Don't forget to mark this as solved ! :)
     
  8. Offline

    InstanceofDeath

    He also forgot to implement the Interface Listener
     
  9. @bwfcwalshy
    Well, you do need to do it when it's a Listener, but you don't need to implement CommandExecutor in the main class.
     
  10. Offline

    dart2112

    please mark as solved!
     
Thread Status:
Not open for further replies.

Share This Page