Solved PlayerJoinEvent

Discussion in 'Plugin Help/Development/Requests' started by MinecraftChickenNL, Apr 2, 2016.

Thread Status:
Not open for further replies.
  1. Hello!

    I am developing a plugin that uses a PlayerJoinEvent, but for some reason it doesn't work.
    Can somebody please look at this?

    Main class:
    Code:
    public class Main extends JavaPlugin {
        private PluginManager pm;
        private PlayerListener playerListener = new PlayerListener(this);
    
        @Override
        public void onEnable() {
            pm = getServer().getPluginManager();
           
            pm.registerEvents(playerListener, this);
        }
    }
    
    PlayerListener class:
    Code:
    public class PlayerListener implements Listener {
        public Main plugin;
    
        public PlayerListener(Main instance) {
            plugin = instance;
        }
       
        @EventHandler
        //onPlayerJoin Listener
        public void onPlayerJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            p.sendMessage("bla");
            plugin.getLogger().info("Blabla");
       }
    }
    
    It looks good to me. Am I missing something?
    Because it does not work at all.
     
  2. Online

    timtower Moderator Moderator

  3. Online

    timtower Moderator Moderator

    @MinecraftChickenNL Then initialize the listener class in the onEnable, and not before that.
     
  4. Offline

    xaanit

    Code:
      public void onEnable() {
          PluginManager  pm = getServer().getPluginManager();
         
            pm.registerEvents(new PlayerListener(), this);
        }
    }
    There's no real reason to make a private variable for both the PlayerListener and the PluginManager, unless there is one I'm not aware of. I never did. If what I said doesn't work, then yea the error log will help.
     
  5. Online

    timtower Moderator Moderator

    @xaanit Saving it could be a habit.
    Or it does more than just handing the event.
     
  6. Offline

    xaanit

    @timtower
    "Saving it could be a habit"
    If it's a bad habit, it should be broken. I used to make everything static. *shudders*. Now it could be a fine habit, I'm not as experienced as, say, you.

    "Or it does more than just handling the event."
    Possibly, in this case it doesn't so my way is valid but yes I didn't really think of that. Even with my way you could use a command sender, another listener, etc... Just gave a possible fix in the way that I always coded.
     
  7. Online

    timtower Moderator Moderator

    @xaanit Saving:
    Saving also gives a way to clean it up when you are done with it. And it could give more insight in what is used in the plugin.
    Doing more:
    In this case: no, it doesn't do more, not visible at least.

    Your way will do fine, just responded on the text below ;)
     
  8. I unfortunatly can't try that today because it is a little too late for that xd

    Anyways, what I wanted to say was that I am using other eventlisteners in the same class that do work,
    So that makes this even more weird..

    Edit:
    I treid that.
    Still no errors and it still doesn't work.
    Other eventlisteners in the same class do work.. As if the join method doesn't even exist


    Should I try placing the method in my main class?

    EDIT:
    It also didn't work in the main class. I really don't know any reason why it isn't working xd
     
    Last edited: Apr 3, 2016
  9. Well, I reinstalled my testing server, and now I at least got an error.

    Code:
    [19:48:25 ERROR]: Error occurred while enabling McCPlugin v3.7.PRE (Is it up to
    date?)
    java.lang.NullPointerException
            at me.maarten.McCPlugin.PlayerListener.<init>(PlayerListener.java:26) ~[
    ?:?]
            at me.maarten.McCPlugin.Main.onEnable(Main.java:28) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[s
    pigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:340) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:405) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.jav
    a:357) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.
    java:317) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:4
    14) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:3
    78) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:3
    33) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.jav
    a:263) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java
    :525) [spigot.jar:git-Spigot-e4d4710-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_73]
     
  10. Online

    timtower Moderator Moderator

    @MinecraftChickenNL PlayerListener.java:26
    Now I don't know what is on line 26 but it is probably something bad.
     
  11. There is nothing on that line..

    Code:
    package me.maarten.McCPlugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerChatEvent;
    import org.bukkit.scheduler.BukkitScheduler;
    
    import mkremins.fanciful.FancyMessage;
    
    import static org.bukkit.ChatColor.*;
    
    @SuppressWarnings("deprecation")
    public class PlayerListener implements Listener {
        public Main plugin;
    
        public PlayerListener(Main instance) {
            plugin = instance;
        }
       
       
        final String ServerName = GOLD + "[" + DARK_RED + "MinecraftChicken" + GOLD + "]: ";
        FileConfiguration config = plugin.getConfig();
       
        @EventHandler
        //stuff
    
     
  12. Online

    timtower Moderator Moderator

    @MinecraftChickenNL Then it is line 25, you can't call getConfig before your constructor is called.
     
  13. @timtower
    Could you explain that a bit further for me?
    I'm still learning Java a bit..
     
  14. Online

    timtower Moderator Moderator

  15. Thank you so much! :)
     
Thread Status:
Not open for further replies.

Share This Page