timer already canceled

as u can see in the file, i am scheduling two tasks using TimerTask, and in the first line of the code in each task, u will see that i am returning if huntEnded is true, and while that works fine, the reason i am doing it like that is because, when i set the huntEnded to true and try to do timer.cancel() i just get a Timer already canceled, and i dont understand, why? i am not even trying to cancel while the task is being executed (sorry but i cant provide any error messages, i had this issue a while ago and replicating the thing will be painful but it basically just said timer already cancelled and pointed to the line where i have the timer.cancel())
13 Replies
JavaBot
JavaBot7mo ago
This post has been reserved for your question.
Hey @MoonSouhayl! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
MoonSouhayl
MoonSouhaylOP7mo ago
timer.schedule(new TimerTask() {
@Override
public void run() {
if(huntEnded) return;

if(!PokeHunt.config.isCoolDownEnabled()) {
startNewHunt();
} else {

if (PokeHunt.config.isIndividualHunts() && PokeHunt.config.isSendHuntEndMessage()) {

Objects.requireNonNull(PokeHunt.server.getPlayerList().getPlayer(owner)).sendSystemMessage(
Component.literal(
Utils.formatPlaceholders(PokeHunt.language.getEndedHuntMessage(), null, pokemon)));

} else if (PokeHunt.config.isSendHuntEndMessage()) {

Utils.broadcastMessage(
Utils.formatPlaceholders(PokeHunt.language.getEndedHuntMessage(), null, pokemon));

}

huntEnded = true;
addToEndTime(PokeHunt.config.getCoolDown(strRarity)*60*1000);
timer.schedule(new TimerTask() {
@Override
public void run() {
startNewHunt();
}
}, PokeHunt.config.getCoolDown(strRarity)*60*1000);
}
}
}, duration);



timer.schedule(new TimerTask() {
@Override
public void run() {
if(huntEnded) return;

startNewHunt();
if(!isOwnerOnline()) {
huntEnd(Cause.OWNER_OFFLINE);
}
}
}, duration + PokeHunt.config.getCoolDown(strRarity)*60*1000 + PokeHunt.config.getSafetyDelay);
timer.schedule(new TimerTask() {
@Override
public void run() {
if(huntEnded) return;

if(!PokeHunt.config.isCoolDownEnabled()) {
startNewHunt();
} else {

if (PokeHunt.config.isIndividualHunts() && PokeHunt.config.isSendHuntEndMessage()) {

Objects.requireNonNull(PokeHunt.server.getPlayerList().getPlayer(owner)).sendSystemMessage(
Component.literal(
Utils.formatPlaceholders(PokeHunt.language.getEndedHuntMessage(), null, pokemon)));

} else if (PokeHunt.config.isSendHuntEndMessage()) {

Utils.broadcastMessage(
Utils.formatPlaceholders(PokeHunt.language.getEndedHuntMessage(), null, pokemon));

}

huntEnded = true;
addToEndTime(PokeHunt.config.getCoolDown(strRarity)*60*1000);
timer.schedule(new TimerTask() {
@Override
public void run() {
startNewHunt();
}
}, PokeHunt.config.getCoolDown(strRarity)*60*1000);
}
}
}, duration);



timer.schedule(new TimerTask() {
@Override
public void run() {
if(huntEnded) return;

startNewHunt();
if(!isOwnerOnline()) {
huntEnd(Cause.OWNER_OFFLINE);
}
}
}, duration + PokeHunt.config.getCoolDown(strRarity)*60*1000 + PokeHunt.config.getSafetyDelay);
dan1st
dan1st7mo ago
How are you calling cancel?
MoonSouhayl
MoonSouhaylOP7mo ago
it was smth like this
public void setCaught() {
timer.cancel();
huntCaught = true;
}
public void setCaught() {
timer.cancel();
huntCaught = true;
}
if thats what ur asking about
dan1st
dan1st7mo ago
so you attempt to cancel the whole timer? not a specific task?
MoonSouhayl
MoonSouhaylOP7mo ago
yep...
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
dan1st
dan1st7mo ago
tbh I don't like Timer, I prefer ScheduledExecutorService lol
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
MoonSouhayl
MoonSouhaylOP7mo ago
i meaaaaaan
dan1st
dan1st7mo ago
it isn't part of JavaFX
MoonSouhayl
MoonSouhaylOP7mo ago
im using Timer cause i wrote Time and intelliJ gave it to me gonna take a look at ScheduledExecutorService
JavaBot
JavaBot7mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server