如何每小时调用一次函数?另外,我怎样才能循环这个? [英] How to call function every hour? Also, how can I loop this?

查看:49
本文介绍了如何每小时调用一次函数?另外,我怎样才能循环这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种简单的方法来每 60 分钟调用一次函数.我怎样才能做到这一点?我正在制作一个 MineCraft bukkit 插件,这就是我所拥有的:

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();
        }
    }
}

我需要运行一个我将每小时执行一次的函数,怎么做?请记住:该函数将使用 l1 和 l2.另外,我怎样才能循环这个以获得中间的每个块?

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?

推荐答案

创建一个 Timer 对象并给它一个 TimerTask 来执行你想要执行的代码.

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);

如果您在 onPlayerInteract 函数中声明了 hourlyTask,则您可以访问 l1l2.要进行编译,您需要将它们都标记为 final.

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.

使用 Timer 对象的好处是它可以处理多个 TimerTask 对象,每个对象都有自己的计时、延迟等.你也可以启动和停止只要你通过将 Timer 对象声明为类变量或其他东西来保持它的计时器.

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.

这篇关于如何每小时调用一次函数?另外,我怎样才能循环这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆