Simple message on join?

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

  1. Code (Text):
    package com.waldxn.onJoin;

    import org.bukkit.event.player.PlayerJoinEvent;

    public class onJoin {

        public void joinMessage(PlayerJoinEvent event){

            event.getPlayer().sendMessage("Hello World!");
           
        }
       
    }
     
    This is in a separate class from the main class.

    How do I register this in the main class so it actually works on player joining?
     
  2. Choco

    Moderator

    1. You need an @EventHandler annotation above your method to declare that it is listening for an event
    2. Your class must implement Listener
    3. To register it, in your #onEnable() method, call Bukkit.getPluginManager().registerEvents(new YourListeningClass(), this);

    I also recommend naming your classes in PascalCase as that is Java standards. You should take a look at the Wiki page for listening to events
     
  3. Code (Text):
    public void onEnable(){
         
            Bukkit.getPluginManager().registerEvents(new OnJoin(), this);
            joinMessage();
         
        }
    Code (Text):
    public class OnJoin implements Listener{
     
        @EventHandler
        public void joinMessage(PlayerJoinEvent event){
         
            event.getPlayer().sendMessage("Hello World");
         
        }

    }
    Still not wanting to accept it. Sorry I'm trying to learn
     
  4. Choco

    Moderator

    You need not call the #joinMessage() method. The Bukkit Event API does this for you in the background. All you have to do is create a method (and have logic to go alongside the event), and register it in the onEnable() method. What you have should work just fine with the exception of the method call in the #onEnable(). Assure that your main class extends JavaPlugin, it is set properly in the plugin.yml and that all is well in your plugin :)

    We have a bunch of Wiki pages on pretty much as many topics as you could imagine. You're welcome to take a browse
     
  5. Ok thanks for the help sir!
     
    • Friendly Friendly x 1