OnJoinEvent

Discussion in 'Spigot Plugin Development' started by ChazzaPlays, May 28, 2015.

  1. Hello,
    Im trying to get my onJoinEvent Listener working however it looks correct yet wont work.
    Code (Text):
    package me.chazplays.helloworld

    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    public class MyPlayerListener implements Listener {
        public static Hello plugin;
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event){
            Player player = event.getPlayer();
            event.setJoinMessage(getConfig().getString("MOTD"));
        }

    }
     
     
  2. Did you create an instance of this class and register it?

    Also, what's even the purpose of this?
    Code (Text):
    public static Hello plugin;
     
  3. Are you registering the event?
     
  4. Idk why i put that there and all i did was make the listener file and add that along with my main file that just gathers MOTD frm the config when typing /motd
     
  5. Are you registering the event in the onEnable() (Main Class)
     
  6. No, what do i need to do on there
     
  7. do you get any errors?

    Code (Text):
    event.setJoinMessage(getConfig().getString("MOTD"));
    that should produce an error, because there is no plugin that it is referencing.
     
  8. Try reading/watching the tutorial again.
     
  9. Inkzzz

    Resource Staff

    From the words MOTD, that sounds like you're wanting to send the message to one player, if so you don't want to set the Join message to it.
     
  10. Yeah i get an error with the getconfig bit, i want it so if the Player has the perm say motd.notify they would get the MOTD set in the Config.
     
  11. Inkzzz

    Resource Staff

    Code (Text):
    @EventHandler
    public void onjoin(PlayerJoinEvent e) {
    Player p = e.getPlayer();
    if(p.hasPermission("perm") {
    p.sendMessage(getConfig().getString("String"));
    }
     
    • Informative Informative x 1
  12. Bukkit.getPlugin("PluginName").getConfig()...

    For permissions:
    event.getPlayer().hasPermission(...)
     
  13. I get http://prntscr.com/7afj1o from the getConfig() part. Also how would i link to my onEnable part too.
     
  14. In onEnable:
    Code (Text):
    Bukkit.getPluginManager().registerEvents(new MyPlayerListener(), this);
    getConfig:
    Code (Text):
    Bukkit.getPluginManager().getPlugin("PluginName").getConfig()
     
  15. Yah, the fact that it took 10 posts to get you to say there was an error is making this pretty difficult. You need to share as much information as possible as to WHY it won't work. I am not a compiler, its tough to just stare at code without errors and figure out the errors in syntax in every line.

    You need to initilize this listener in your Main class.

    So in the onEnable() method in your main class you should have

    Code (Text):
    getServer().getPluginManager().registerEvents(new MyPlayerListener(), this);
    You can also save yourself some trouble by adding this method in your Main class


    Code (Text):
    //at the top
    private Hello plugin;  

    //somewhere in your main class

    public static Hello getPlugin() {

            return plugin;

        }
    then to get what you want in your listener

    Code (Text):
    public class MyPlayerListener implements Listener {
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event){
            Player player = event.getPlayer();
            String motd = Hello.getPlugin().getConfig().getString("MOTD"));
           
           //notice I am sending the message, not setting the join message
            player.sendMessage(motd);
        }

    }
     
    • Like Like x 1
  16. Can you post your main class and your listener class? can you tell me what line 14 is in 'MyPlayerListener'? My guess is it is the Hello.getPlugin() method.

    EDIT::

    This is my fault, in your main class, in the onEnable() method you need to add

    Code (Text):
    plugin = this;
    apologies. That should fix the issue
     
    • Like Like x 1
  17. Thank you everyone for the help! Appreciated alot!

    P.S Im new to this thats why i sound like a noob
     

  18. You should definitely look through the code, and figure out what your version didn't work, and why this one did. To learn, instead of blindly copying. A lot of the stuff here is covered in many beginner tutorials, so you should have consulted other sources and tried to understand. But, I have no problem helping either way.
     
    • Like Like x 1
    • Agree Agree x 1
  19. I did watch BCBroz however ive read his useless and dang right he was. I did go through the bukkit docs but couldnt work out. Im slowly learning.