Understanding how to call a BukkitRunnable Class

Discussion in 'Spigot Plugin Development' started by fieldforensics, May 20, 2017.

  1. I've never attempted to call a BukkitRunnable class before, but the basic idea is that I want to allow code within an external class loop every tick. I need to start this timer in the onEnable() method in the Main class. I'm having trouble however figuring out how to do this.

    Code (Text):
        TPS tps;
        public Main(TPS tps){
            this.tps = tps;
        }
       
        public int time1;
        public int time2;

        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
            registerEvents(this, new onDamage(this));
            tps.runTaskTimer((Plugin) tps, 0, 1); //this is giving me issues :/
            Timer();
            TimeChecker();
            getLogger().info("Enabled! =D");
            getLogger().info("*** If you run into any problems please be sure to check to make sure all our dependecies are installed! ***");
            }
     
    Code (Text):
    public class TPS extends BukkitRunnable
    {        
        @SuppressWarnings("unused")
        private Main plugin;
       
        public TPS(Main plugin){
        this.plugin = plugin;

        }
        private int tps = 0;
        long sec;
        long currentSec;
        int  ticks;
        int  delay;

            @Override
            public void run()
            {
                sec = (System.currentTimeMillis() / 1000);          
                if(currentSec == sec)
                {              
                    ticks++;
                }
                else       {
                   
                    currentSec = sec;
                    tps = (tps == 0 ? ticks : ((tps + ticks) / 2));
                    ticks = 0;
                   
                    if((++delay % 300) == 0)
                    {                  
                        delay = 0;                  
                        Bukkit.broadcastMessage("TPS: " + tps);
                    }
                }
            }

    }

     
    ~Geekles o_O
     
  2. You need to actually instantiate the TPS object. And I didn't think plugins could have non-default constructors...

    so in onEnable, try this.tps = new TPS(); then register that instance.
     
  3. I'm not really understanding what you mean =)
    But if you want to call a class, the run through it, Use:
    Code (Text):
    Bukkit.getPlugin().getClass("Example");
    // Or you can do the Example below,
    // But make sure that you call "Plugin"
    plugin.getClass("Example");
     
  4. You need to instantiate. Just write
    tps = new TPS();
    tps.runTaskTimer...




    Sent from my iPhone using Tapatalk
     
  5. Not 100% what you meant by that tbh. I tried this though, was this similar to what you were suggesting I do?
    Code (Text):
        TPS tps;

        public int time1;
        public int time2;
       
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
            registerEvents(this, new onDamage(this));
            this.tps = new TPS(this);
            tps.runTaskTimer(tps, 0, 1); //java no likey (error)
           
            Timer();
            TimeChecker();
            getLogger().info("Enabled! =D");
            getLogger().info("*** If you run into any problems please be sure to check to make sure all our dependecies are installed! ***");
            }
     
    Code (Text):
        @SuppressWarnings("unused")
        private Main plugin;
       
       
        public TPS(Main instance) {
            this.plugin = instance;
        }
       
    ~Geekles
     
  6. The first argument has to be your plugin. Change tps to this.
     
  7. Oh.. okay thanks. It seems to kinda work however I never get a broadcast displaying the server's TPS therefore I feel like something is missing however everything else works so it seems to call the BukkitSchedular class just fine.

    MAIN CLASS:
    Code (Text):
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
            registerEvents(this, new onDamage(this));
            this.tps = new TPS(this);
            tps.runTaskTimer(this, 0, 1);
       
            Timer();
            TimeChecker();
            getLogger().info("Enabled! =D");
            getLogger().info("*** If you run into any problems please be sure to check to make sure all our dependecies are installed! ***");
            }
     
    Somehow I need to set it so that every tick it loops the code in the external class it broadcasts the TPS which doesn't appear to do.
    ~Geekles
     
  8. This task will only run once. You need to schedule it as a repeating task.
     
  9. How would that be done? Do I need to set a repeating task in the TPS class itself or when I call it do I use some other method?
    ~Geekles
     
  10. Sorry, my mistake. The task is correct. Your problem is that you basically do nothing in your run method in the TPS class.

    Does your TPS class even extend BukkitRunnable?
     
  11. Yes, here's the TPS class.

    Code (Text):
    public class TPS extends BukkitRunnable
    {

             
        @SuppressWarnings("unused")
        private Main plugin;
       
       
        public TPS(Main instance) {
            this.plugin = instance;
       
       
       
     
        }
        private int tps = 0;
        long sec;
        long currentSec;
        int  ticks;
        int  delay;

            @Override
            public void run()
            {
                sec = (System.currentTimeMillis() / 1000);          
                if(currentSec == sec)
                {              
                    ticks++;
                }
                else       {
                   
                    currentSec = sec;
                    tps = (tps == 0 ? ticks : ((tps + ticks) / 2));
                    ticks = 0;
                   
                    if((++delay % 300) == 0)
                    {                  
                        delay = 0;                  
                        Bukkit.broadcastMessage("TPS: " + tps);
                    }
                }
            }

    }

     


    }
     
  12. Well, from the looks of it, that shoudnt work how you think it would. You have this code:

    Code (Text):
                if(currentSec == sec)
                {            
                    ticks++;
                }
    then you have an else statement setting currentSec = sec
    meaning that whats happening is the else statement is being ran once, then ticks are just adding and adding... etc.
    The message would never be broadcasted because, as the

    Code (Text):
                    if((++delay % 300) == 0)
                    {                
                        delay = 0;                
                        Bukkit.broadcastMessage("TPS: " + tps);
                    }
    block is only being ran once, the modulus of delay and 300 will never equal to 0. Also, come to think of it, how does ++delay even work when you havn't initialised delay to equal a value?
     
  13. Okay got it working with this:
    Code (Text):


    public int tps = 0;
    long sec;
    long currentSec;
    int  ticks;
    int  delay;

       @Override
       public void run()
       {
           sec = (System.currentTimeMillis() / 1000);
         
           if(currentSec == sec)
           {          
               ticks++;
           }
           else
           {
             
               currentSec = sec;
               tps = (tps == 0 ? ticks : ((tps + ticks) / 2));
               ticks = 0;
             
               if((++delay % 10) == 0)
               {

                 
                   delay = 0;
                 
                   Bukkit.broadcastMessage("TPS: " + tps);
               }
           }
       }




    }

     
     
    This code would display the TPS of the server every so often as 18 or 19. Now I'm checking to be sure the TPS is never below or equal to 15 and not above or equal to 25. If so it stops all schedular tasks in the Main class temporarily until the lagChecker has checked four times in a row that the TPS of the server has returned anywhere from at least 18 up until 22

    However I seem to have trouble getting the other one to work properly. I did pretty much the same thing as I did for calculating the tps of the server in the other class.
    Code (Text):
    public class LagChecker extends BukkitRunnable{
     
        private Main plugin;
     
        public LagChecker(Main instance) {
            this.plugin = instance;
        }
     
        private int counter = 0;

        @Override
        public void run()
        {
            Bukkit.broadcastMessage("OOHHHHHH!!!!!");
            if(Main.lagCheck == true){
                Bukkit.broadcastMessage("LAG CHECK IS TRUE!");
                if(!(plugin.getTPS() >= 23 || plugin.getTPS() <= 17)){
                    counter++;
                    if(counter >= 4){
                        Main.lagCheck = false;
                        plugin.Timer();
                        plugin.TimeChecker();
                        counter = 0;
                    }
                }
            }
        }
    }
    [/CODE
    [/SPOILER]
    [SPOILER="Main Class"]
    [CODE]    TPS tps;
        LagChecker lag;

        public static Boolean lagCheck = false;
        public int time1;
        public int time2;
     
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
            registerEvents(this, new onDamage(this));
            this.tps = new TPS(this);
            this.lag = new LagChecker(this);
            tps.runTaskTimer(this, 0, 0);
            lag.runTaskTimer(this, 0, 20);
            Timer();
            TimeChecker();
            getLogger().info("Enabled! =D");
            getLogger().info("*** If you run into any problems please be sure to check to make sure all our dependecies are installed! ***");
            }
     
     
  14. You can remove the this.lag etc, just change it to lag. No need to have 'this' before it. TPS can also never go above 20. Also why are you running the tps runnable task timer every 0 ticks? Not needed.


    Personally I would create a startRunnables and stopRunnables method. I would then call the startRunnable method in the onEnable and the stopRunnable method when I want to stop them. Instead of making a Boolean...
    Code (Text):
    public static void startRunnables {
        tps.runTaskTimer //etc
        lagChecker.runTaskTimer // etc
    }
    Code (Text):
    public static void stopRunnables {
       tps.cancel();
       lagChecker.cancel();
    }

    Sent from my iPhone using Tapatalk
     
  15. Yeah idk why I still left it at 0. I was testing something earlier I guess. Also I know TPS should probably never go higher then 20 however I've noticed that my test server has been at 22 before. So I assumed it was apparently possible to be higher then 20 ticks per second.
     
  16. Well you're doing something wrong. If you do /tps (built-in Spigot command) you should never see a value over 20.0
     
  17. Didn't make as much sense to me as it does to you guys, but I'll just have it then check for a value lower then 17 or 18. However I would like to know how to get my second BukkitRunnable class working since it appears that it still doesn't do anything once the server starts up. And as for the rest of the plugin, appears to not function properly due to the error with the second BukkitRunnable class (LagChecker). No errors come up in console.
     

Share This Page