1. Welcome to LilyPad. Download the project, explore the forums, and create your own LilyPad network.


    If you use the software and enjoy it or have a question, or would like to contribute to the future of the software directly or through resources, please sign up and join our little community.

Run method when Bukkit-Connect connects successfully

Discussion in 'Discussion' started by Superfuzzy, Aug 10, 2014.

  1. Superfuzzy

    Superfuzzy Member

    Hello,

    I've written an own Plugin that sends some Message over LilyPad as soon as it is enabled.
    Problem: There is no connection to LilyPad yet, when the pluin is enabled. But i need to send someinformation as soon the server is connected...
    Question: is there any easier way than below to run a method as soon as the server connects successfully to LilyPad?

    My Current Solution (code below): I am using a BukkitRunnable, launched in the onEnable(), that checks every second if the server is connected (connect.isConnected()). If it is, i cancel the Thread and send the Information.
    For the case, that the server never connects (e.g. when lilypad is not running), i have a 'timeout' variable outside the runnable and the runnable itsself has a 'time' variable, that is increased by the interval every cycle.
    And when 'time' is bigger than 'timeout' I cancel the runnable and disable the plugin.

    Code (java):

    @Override
       public void onEnable() {
         instance = this;
         final Connect co = LilyPadRequestHandler.getInstance().getConnect();
         co.registerEvents(new LilyPadListener());

         final int startupTimeout = 10; //in seconds
         final int interval = 1; //in seconds
         BukkitRunnable br = new BukkitRunnable(){
           int time = 0;
           @Override
           public void run() {
             time += interval;
             if(time>(startupTimeout)){
               instance.logSevere("Time over! Could not load --> not connected to lilypad");
               instance.logSevere("Disabling plugin");
               this.cancel();
               Bukkit.getPluginManager().disablePlugin(instance);
             }
             if(co.isConnected()){
               instance.debug("Connected --> send Information");
               sendInformation();
               this.cancel();
             }
             instance.debug("Not Connected - checking again in "+interval+" seconds!");
           }
         };
         br.runTaskTimer(this, 0, 20*interval);
       }
     
    I'm sorry if this is the wrong sub but ididn't know where to put this.
    Last edited by a moderator: Aug 13, 2014

Share This Page