Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function every hour? Also, how can I loop this?

I need a simple way to call a function every 60 minutes. How can I do this? I'm making a MineCraft bukkit plugin, and this is what I have:

package com.webs.playsoulcraft.plazmotech.java.MineRegen;

import java.util.logging.Logger;

import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;


public class Main extends JavaPlugin{

    public final Logger log = Logger.getLogger("Minecraft");


    @Override
    public void onEnable() {
        this.log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        this.log.info("Plaz's Mine Regen is now enabled!");
        this.log.info("Copyright 2012 Plazmotech Co. All rights reserved.");
        this.log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }

    @Override 
    public void onDisable() {
        this.log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        this.log.info("Plaz's Mine Regen is now disabled!");
        this.log.info("Copyright 2012 Plazmotech Co. All rights reserved.");
        this.log.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }

    public void onPlayerInteract(PlayerInteractEvent event) {
        final Action action = event.getAction();
        if (action == Action.LEFT_CLICK_BLOCK) {
            Location l1 = event.getClickedBlock().getLocation();
        } else if (action == Action.RIGHT_CLICK_BLOCK) {
            Location l2 = event.getClickedBlock().getLocation();
        }
    }
}

I need to run a function I will implement every hour, how? Remember: The function will use l1, and l2. Also, how can I loop this to get every block inbetween?

like image 544
Thor Correia Avatar asked May 25 '12 03:05

Thor Correia


People also ask

How do you call a function every hour?

var now = new Date(); var delay = 60 * 60 * 1000; // 1 hour in msec var start = delay - (now. getMinutes() * 60 + now. getSeconds()) * 1000 + now. getMilliseconds(); setTimeout(function doSomething() { // do the operation // ...

How do you call a function again and again?

Answer: Use the JavaScript setInterval() method You can use the JavaScript setInterval() method to execute a function repeatedly after a certain time period. The setInterval() method requires two parameters first one is typically a function or an expression and the other is time delay in milliseconds.


2 Answers

Create a Timer object and give it a TimerTask that performs the code you'd like to perform.

Timer timer = new Timer ();
TimerTask hourlyTask = new TimerTask () {
    @Override
    public void run () {
        // your code here...
    }
};

// schedule the task to run starting now and then every hour...
timer.schedule (hourlyTask, 0l, 1000*60*60);

If you declare hourlyTask within your onPlayerInteract function, then you can access l1 and l2. To make that compile, you will need to mark both of them as final.

The advantage of using a Timer object is that it can handle multiple TimerTask objects, each with their own timing, delay, etc. You can also start and stop the timers as long as you hold on to the Timer object by declaring it as a class variable or something.

I don't know how to get every block in between.

like image 199
101100 Avatar answered Oct 19 '22 04:10

101100


Create a thread that will run forever and wakes up every hour to execute your data.

Thread t = new Thread() {
    @Override
    public void run() {
        while(true) {
            try {
                Thread.sleep(1000*60*60);
                //your code here...
            } catch (InterruptedException ie) {
            }
        }
    }
};
t.start();
like image 20
Luiggi Mendoza Avatar answered Oct 19 '22 04:10

Luiggi Mendoza