When I start TimerTask and then I finish, and then start again it shows this error, why? this error:
[16:30:02] [Client thread/INFO] [MacroSK]: KEY_L
[16:30:06] [Client thread/INFO] [MacroSK]: KEY_O
[16:30:08] [Client thread/INFO] [MacroSK]: KEY_L
[16:30:08] [Client thread/ERROR] [FML]: Exception caught during firing event net.minecraftforge.fml.common.gameevent.TickEvent$ClientTickEvent@1bc14017:
java.lang.IllegalStateException: Task already scheduled or cancelled
at java.util.Timer.sched(Unknown Source) ~[?:1.8.0_161]
at java.util.Timer.scheduleAtFixedRate(Unknown Source) ~[?:1.8.0_161]
at com.skelletonx.MacroSK.handler.KeybindHandler.Shopmsg(KeybindHandler.java:53) ~[KeybindHandler.class:?]
at com.skelletonx.MacroSK.handler.KeybindHandler.onTickEvent(KeybindHandler.java:63) ~[KeybindHandler.class:?]
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_KeybindHandler_onTickEvent_ClientTickEvent.invoke(.dynamic) ~[?:?]
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) ~[ASMEventHandler.class:?]
at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:140) [EventBus.class:?]
at net.minecraftforge.fml.common.FMLCommonHandler.onPostClientTick(FMLCommonHandler.java:371) [FMLCommonHandler.class:?]
at net.minecraft.client.Minecraft.func_71407_l(Minecraft.java:2152) [bsu.class:?]
at net.minecraft.client.Minecraft.func_71411_J(Minecraft.java:1028) [bsu.class:?]
at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:345) [bsu.class:?]
at net.minecraft.client.main.Main.main(SourceFile:120) [Main.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_161]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_161]
at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_161]
at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.12.jar:?]
my code :
Timer timer = new Timer();
final TimerTask ts = new TimerTask(){
@Override
public void run() {
Minecraft.getMinecraft().thePlayer.sendChatMessage(Ini.getIni("Loja"));
}
};
int delaymsg = Integer.parseInt(Ini.getIni("DelayL"));
public void Shopmsg() {
timer.scheduleAtFixedRate(ts,1000*delaymsg,1000*delaymsg);
}
@SideOnly(Side.CLIENT)
@SubscribeEvent(priority=EventPriority.NORMAL, receiveCanceled=true)
public void onTickEvent(TickEvent.ClientTickEvent event) {
if(event.phase == TickEvent.Phase.END) {
if (mc.inGameHasFocus) {
if (keyBindings[0].isPressed()) {
LogHelper.info("KEY_L");
Shopmsg();
}
if(keyBindings[1].isPressed()) {
LogHelper.info("KEY_O");
ts.cancel();
}
}
}
}
how can I do that when I finish TaskTime, I can start again? if not stop, stop and restart the tasktime is it possible to pause and resume?
TimerTask can only be scheduled by a Timer once.
Continuing working with Timer, you would need to recreate a new TimerTask ts each time in ShopMsg().
It's worth saying though that Timer has been superseded by ScheduledExecutorService these days. This is generally more flexible...
E.g. to create the executor (in place of Timer):
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
Then to schedule the loop:
ScheduledFuture<?> future = scheduledExecutorService.scheduleAtFixedRate(
() -> Minecraft.getMinecraft().thePlayer.sendChatMessage(Ini.getIni("Loja")),
delaymsg,
delaymsg,
TimeUnit.SECONDS);
And to stop that schedule:
future.cancel(false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With