如何取消定时任务并重新运行



首先,这是一个Minecraft Forge mod,但我相信你不需要知道Forge就可以解决我的问题。

我只是简单地使用Keybind";K〃;安排3个不同的定时器,然后我点击";L";以取消它们再次运行。没有问题,直到我开始";K〃;(再次(再次启动计时器,但我得到了

"java.lang.IollegalStateException:计时器已取消"日志中的错误。

我用来管理定时器的代码:

public class KeyBind {
public static Boolean Status = false;
Timer timer = new Timer();

@SubscribeEvent
public void onKeyPressed(InputEvent.KeyInputEvent event) throws InterruptedException {
if (ForgeMain.keybinding.isKeyDown()) {
if(Status == false) {
EntityPlayerSP entityPlayerSP = (Minecraft.getMinecraft()).thePlayer;
entityPlayerSP.addChatComponentMessage((IChatComponent) new ChatComponentText(EnumChatFormatting.YELLOW + "You have successfully ran the NetherWart Macro!"));
Status = true;
timer.schedule(new Left(), 100, 38000);
timer.schedule(new Forward(), 33100, 38000);
timer.schedule(new Right(), 35100, 38000);
}
}if(ForgeMain.keybinding1.isKeyDown()){
if(Status == true) {
Status = false;
System.out.println(Status.booleanValue());
timer.cancel();
timer.purge();
}
}
}

这是我的一个计时器(它们都以相同的方式工作,差异是移动和移动时间的方向(:

public class Right extends TimerTask {
@Override
public void run() {
int left = Minecraft.getMinecraft().gameSettings.keyBindRight.getKeyCode();
long oldTime = System.currentTimeMillis();
while (System.currentTimeMillis() - oldTime < 33000) {
KeyBinding.setKeyBindState(left, true);
}
KeyBinding.setKeyBindState(left, false);
}

}

编辑:

好吧,我学会了我现在必须创建另一个计时器:

@SubscribeEvent
public void onKeyPressed(InputEvent.KeyInputEvent event) throws InterruptedException {
if (ForgeMain.keybinding.isKeyDown()) {
if(Status == false) {
EntityPlayerSP entityPlayerSP = (Minecraft.getMinecraft()).thePlayer;
entityPlayerSP.addChatComponentMessage((IChatComponent) new ChatComponentText(EnumChatFormatting.YELLOW + "You have successfully ran the NetherWart Macro!"));
Status = true;
Timer timer = new Timer();
timer.schedule(new Left(), 100, 38000);
timer.schedule(new Forward(), 33100, 38000);
timer.schedule(new Right(), 35100, 38000);
}
}if(ForgeMain.keybinding1.isKeyDown()){
if(Status == true) {    
Status = false;
System.out.println(Status.booleanValue());
timer.cancel();
timer.purge();
}
}
}

}

但现在我不知道如何取消计时器(在代码timer.cancel((中;ofc不起作用。(

timer.cancel()终止计时器,因此它不能再用于安排新任务。取消计时器后,您将需要实例化一个新的计时器。

public class KeyBind {
public static Boolean Status = false;
Timer timer = new Timer();
@SubscribeEvent
public void onKeyPressed(InputEvent.KeyInputEvent event) throws InterruptedException {
if (ForgeMain.keybinding.isKeyDown()) {
if(Status == false) {
EntityPlayerSP entityPlayerSP = (Minecraft.getMinecraft()).thePlayer;
entityPlayerSP.addChatComponentMessage((IChatComponent) new ChatComponentText(EnumChatFormatting.YELLOW + "You have successfully ran the NetherWart Macro!"));
Status = true;
timer.schedule(new Left(), 100, 38000);
timer.schedule(new Forward(), 33100, 38000);
timer.schedule(new Right(), 35100, 38000);
}
}if(ForgeMain.keybinding1.isKeyDown()){
if(Status == true) {
Status = false;
System.out.println(Status.booleanValue());
timer.cancel();
timer = new Timer();
}
}
}
}

最新更新