Tho*_*eia 14 java timer scheduled-tasks
我需要一种简单的方法来每60分钟调用一次函数.我怎样才能做到这一点?我正在制作一个MineCraft bukkit插件,这就是我所拥有的:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我需要运行一个我每小时都要实现的功能,怎么样?记住:该函数将使用l1和l2.另外,我如何循环这个以获得中间的每个块?
101*_*100 27
创建一个Timer对象并给它一个TimerTask执行您想要执行的代码.
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);
Run Code Online (Sandbox Code Playgroud)
如果您hourlyTask在onPlayerInteract函数中声明,则可以访问l1和l2.要进行编译,您需要将它们标记为final.
使用Timer对象的优点是它可以处理多个TimerTask对象,每个对象都有自己的时间,延迟等.只要通过将Timer对象声明为类变量或其他东西来保持对象,您也可以启动和停止计时器..
我不知道如何让每一块都介于两者之间.
| 归档时间: |
|
| 查看次数: |
32407 次 |
| 最近记录: |