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())
#timer already canceled
22 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @fickle crown! Please use
/closeor theClose Postbutton 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.
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);
How are you calling cancel?
it was smth like this
public void setCaught() {
timer.cancel();
huntCaught = true;
}
if thats what ur asking about
yep...
u rly shouldnt be trying to weave tasks together like that ..........
and u shouldnt use anonomous classes like that
public class PokeHunt {
private static final int COOLDOWN_DURATION = 60*1000;
private void scheduleNextHunt() {
timer.schedule(
this::startNewHunt,
strRarity * COOLDOWN_DURATION,
duration);
}
}
u dont need anonomous classes in modern java
since TimerTask only has 1 method its automatically a functional interface and u can use it that way
also, ur method calls should be neater so they readable like this
tbh I don't like Timer, I prefer ScheduledExecutorService lol
and replace the magic numbers
same
but Timer is specifically for javaFX iirc
so by using it u end up in the GUI event queue, which is a benefit
i meaaaaaan
it isn't part of JavaFX