Stuck on executing code on playerjoin or playerquit event

Discussion in 'Plugin Development' started by MathijsNL, Mar 12, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    MathijsNL

    Hello there, i got the following plugin written (got the code from robinvandervliet, he quit the development, credits go to him)


    Code:
    package nl.zorpcraft.info.PlayerToFile;
     
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
     
     
     
    public class PlayerToFilePlayerListener implements Listener
    {
     
        @EventHandler(priority = EventPriority.MONITOR)
        public void onPlayerJoin(final PlayerJoinEvent event)
     
        {
               
     
            }
         
             
     
     
     
        @EventHandler(priority = EventPriority.MONITOR)
        public void onPlayerQuit(final PlayerQuitEvent event)
        {
                {
             
                     
                        }
                }
        }
     
     
    


    But i got some code to run in the plugin itself:


    Code:
    public void run()
        {
            run1();
            run2();
        }
     
        public void run1()
        {
            try
            {
    etc etc

    What do i need to put into the listener so that my code gets executed every time a player joins or quits?

    The plugin does its job on reloading the server, but I just cant find how this should work in the listener, basically what i want is:

    Code:
    public void onPlayerJoin(final PlayerJoinEvent event)
     
    {
    Run the code from the plugin
     
    }
    
    Anyone can help me with that?
     
  2. Offline

    dillyg10

    Huh? As long as you register the listner.. it will always be listening for the event.. is that the question you are asking?
     
  3. Offline

    MathijsNL

    No no that was not the question. The thing is, when a player joins, the code for that event will be executed, and not the code in the plugin itself. So to make things a bit more clear:
    You got 2 .java files: Plugin.java and PluginListener.java, and the code from plugin java needs to be executed, but there needs to be some code in the PluginListener.

    Does that make sense?

    I just started with java, but the problem is ( to make it even more clear):
    Imagine you got 2 java files
    1 listener and 1 plugin file.
    the listener noticed a player joined, and then nothing happens. I need to call the run() from the main java class.

    Im sure someone must be able to help with this. It must be some really stupid simple line of code.

    might be something like call mainjavafile.run or something?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
  4. Offline

    anerach

    Could you post the 2 files with all their content please?
     
  5. Offline

    ZachBora

    your Plugin.java needs to extend JavaPlugin class, from there it you should have stuff working.
     
  6. Offline

    MathijsNL

    Ok, im not getting any further. I forgot to mention that with the old code it did work!

    Code:
    plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
            {
                public void run()
                {
                    plugin.run();
                }
            }, 20);
        }
    but for some reason this piece of code broke
     
  7. Offline

    Taien

    Code:
    public class BlahListener implements Listener
    {
        public BlahListener(YourPlugin plugin)
        {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event)
        {
            //do stuff
        }
    }
     
  8. Offline

    MathijsNL

    Will try that when I get home. thanks for the reply

    Can't upload the 2 files yet, cause there is code in it I didnt get permission for from the original author.

    This is the old playercount plugin that broke with 1.2, im fixing it right now :)

    Fixed, please mark this thread as solved, final code is:

    Code:
    public class PlayerToFilePlayerListener implements Listener
    {
     
        public static PlayerToFile plugin;
            public PlayerToFilePlayerListener(PlayerToFile instance)
            {
                plugin = instance;
            }
        @EventHandler(priority = EventPriority.MONITOR)
        public void onPlayerJoin(final PlayerJoinEvent event)
     
        {
            plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
            {
                public void run()
                {
                    plugin.run();
                }
            }, 20);
        }
    thanks for all your helpful input!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 24, 2016
Thread Status:
Not open for further replies.

Share This Page