Bukkit Runnable

Discussion in 'Spigot Plugin Development' started by BiastGaming, Jun 8, 2016.

  1. Hey,
    Can anyone tell me how I would add 1 to an integer every minute. Since I am using different classes hwo would I register the runnable in main class and how would I add the int.
     
  2. Create a repeating task to run every minute (20 ticks=1 second, 20*60) and when it runs add 1 to that int.
     
    • Informative Informative x 1
  3. public static int A = 1;

    And A DelayedTask Running
    A++
     
  4. why make that static?
     
  5. Code (Text):
        private int yourInt = 0;
        public void start(){
            new BukkitRunnable() {
                @Override
                public void run() {
                    yourInt++;
                }
            }.runTaskTimer(SpigotPlugin.getInstance(), 0 /* delay before timer starts */, 20*60 /* 20 ticks is 1 second, so 60 seconds */);
        }
    sorry for spoonfeed, feeling lazy this morning hehe xd
     
  6. Code (Text):
    public static int integer = 0;
        Bukkit.getScheduler().scheduleSyncRepeatingTask(pl, new Runnable() {
            public void run() {
                integer++;
            }
        }, 1200, integer);
     
     
  7. Don't teach other people java wrong.
     
    • Agree Agree x 3
  8. I'm Make this static for acess on main and other classes :) For future Problems :)
     
  9. if is WRONG say the certain code...
     
  10. Maybe like this:

    Code (Text):
    int YourInt = 1;
    Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    public void run(){
    YourInt++;
    }
    }, 1200, YourInt);

    // 1200 means 1 minute, it is in ticks & 20 ticks is 1 second
     
  11. You abuse the java conventions.
    1. Do you know, what "static" is? No, you don't. static isn't just have a easier access to the field/method or whatever. It is not a good idea, to work with static in this example
    2. Attributes should be ALWAYS private (Ok, it has exceptions). In java, you need to work with Getter/Setter.
     
  12. Code (Text):
    int integer = 0;
    Code (Text):
    Bukkit.getScheduler().scheduleSyncRepeatingTask(pl, new Runnable() {
            public void run() {
                integer++
            }
        }, 1200L, integer);
     
    20L = 1 Second
    20*60 = 1200L
    and scheduleSyncRepeatingTask Return to integer , if integer = 0; its add 1 to integer and repeat
    if int = 1 add a 1 to integer and repeat :)
     
  13. Done?
     
  14. Only if you learned java ;)
     
  15. please, use BukkitRunnable() instead Runnable()
     
  16. Thanks To Say Me :)
     
  17. Only use BukkitRunnables if the task must be able to cancel itself. There's no real reason to use BukkitRunnables aside that.
     
    • Agree Agree x 1
  18. Code (Text):

    public class MyTask extends BukkitRunnable {
        private int counter;

        public MyTask() {
            counter = 0;
        }

        @Override
        public void run() {
            counter++;
        }

        public int getCount() {
            return counter;
        }
    }
     
    Code (Text):

    public class MyPlugin extends JavaPlugin {
        private MyTask myTask;

        @Override
        public void onEnable() {
            getTask().runTaskTimer(this, 0, 1200); // 1200 ticks = 1 minute

            getServer().getPluginManager().registerEvents(new MyListener(this), this);
        }

        @Override
        public void onDisable() {
            getTask().cancel();
            myTask = null;
        }

        public MyTask getTask() {
            if (myTask == null) {
                myTask = new MyTask();
            }
            return myTask;
        }
    }
     
    Code (Text):

    public class MyListener implements Listener {
        private MyPlugin plugin;

        public MyListener(MyPlugin plugin) {
            this.plugin = plugin;
        }

        @EventHandler
        public void onRightClickAir(PlayerInteractEvent event) {
            if (event.getHand() == EquipmentSlot.HAND) {
                return; // ignore all interacts except main hand
            }

            if (event.getAction() != Action.RIGHT_CLICK_AIR) {
                return; // ignore anything not a right click air
            }

            event.getPlayer().sendMessage("Counter: " + plugin.getTask().getCount());
        }
    }
     
    Just some stupid example to show how it all works without any static abuse. Every time a user right clicks air the counter will show in their chat how many minutes have passed since the plugin was loaded.

    @PeixeArtz this is mainly for you since you seem to think static is the only acceptable answer. You need to learn Java before giving advice, man. ;)
     
    #18 BillyGalbreath, Jun 8, 2016
    Last edited: Jun 8, 2016
    • Agree Agree x 1
  19. ok but how would I start the timer with a command. I wasn't sure if I can put the runnable in an executor class as well. Or do what else. I prefer not to be spoon fed code as I want to learn.
     
  20. You can start it where ever you like. Just call runTaskTimer() on the BukkitRunnable object to start it, and cancel() to stop it.