Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove newday timer option #7509

Merged
merged 3 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1575,11 +1575,6 @@ public enum ConfigNodes {
"# The time each \"day\", when taxes will be collected.",
"# Only used when less than day_interval. Default is 12h (midday).",
"# If day_interval is set to something like 20m, the new_day_time is not used, day_interval will be used instead."),
PLUGIN_NEWDAY_USES_JAVA_TIMER(
"plugin.day_timer.uses_java_timer",
"false",
"",
"# If enabled (disabled by default), a Java Timer will be used for scheduling new days, which is more accurate than Towny's Bukkit timer task but uses an extra thread."),
PLUGIN_NEWDAY_DELETE_0_PLOT_TOWNS(
"plugin.day_timer.delete_0_plot_towns",
"false",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3754,10 +3754,6 @@ public static void removeReloadListener(@NotNull NamespacedKey key) {
CONFIG_RELOAD_LISTENERS.remove(key);
}

public static boolean doesNewDayUseTimer() {
return getBoolean(ConfigNodes.PLUGIN_NEWDAY_USES_JAVA_TIMER);
}

public static List<String> getTownUnkickableRanks() {
return getStrArr(ConfigNodes.GTOWN_SETTINGS_UNKICKABLE_RANKS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -899,10 +899,12 @@ private void parseAdminMySQLDump(CommandSender sender) throws TownyException {
throw new TownyException(Translatable.of("msg_err_mysql_not_being_used"));
}

private void parseAdminNewDay(CommandSender sender) throws NoPermissionException, TownyException {
private void parseAdminNewDay(CommandSender sender) throws TownyException {
checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_NEWDAY.getNode());
if (NewDayScheduler.isNewDayScheduled())
throw new TownyException(Translatable.of("msg_newday_already_scheduled_soon"));
if (NewDayScheduler.isNewDayRunning())
throw new TownyException(Translatable.of("msg_newday_already_running"));

// Turn the daily timer scheduler on if it wasn't running already.
if (!NewDayScheduler.isNewDaySchedulerRunning())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.bukkit.entity.Player;

public class DailyTimerTask extends TownyTimerTask {
private static final Object NEW_DAY_LOCK = new Object();

private double totalTownUpkeep = 0.0;
private double totalNationUpkeep = 0.0;
Expand All @@ -46,6 +47,12 @@ public DailyTimerTask(Towny plugin) {

@Override
public void run() {
synchronized (NEW_DAY_LOCK) {
doNewDay();
}
}

public void doNewDay() {

long start = System.currentTimeMillis();
totalTownUpkeep = 0.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler;
import com.palmergames.util.TimeMgmt;
import org.bukkit.NamespacedKey;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

public class NewDayScheduler extends TownyTimerTask {
Expand All @@ -30,7 +28,6 @@ public NewDayScheduler(Towny plugin) {
});
}

private static Timer newDayTimer;
private static ScheduledTask scheduleTask = null;
private static ScheduledTask newDayTask = null;

Expand All @@ -41,28 +38,16 @@ public void run() {

newDayInterval = TownySettings.getDayInterval();

if (!TownySettings.doesNewDayUseTimer()) {
long secondsUntilNextNewDay = TimeMgmt.townyTime();

// If the next new day is less than 2 minutes away, schedule the new day.
if (plugin.getScheduler() instanceof FoliaTaskScheduler || secondsUntilNextNewDay < TimeUnit.MINUTES.toSeconds(2)) {
TownyMessaging.sendDebugMsg("New Day time finalized for: " + TimeMgmt.formatCountdownTime(secondsUntilNextNewDay) + " from now.");
scheduleUpComingNewDay(secondsUntilNextNewDay);
// Else the new day scheduler will run again at half the secondsUntilNextNewDay, to check again.
} else {
scheduleTask = plugin.getScheduler().runLater(new NewDayScheduler(plugin), (secondsUntilNextNewDay / 2) * 20);
TownyMessaging.sendDebugMsg("Re-evaluation of New Day time scheduled for: " + TimeMgmt.formatCountdownTime(secondsUntilNextNewDay / 2) + " from now.");
}
} else {
TownyMessaging.sendDebugMsg("Starting new new day scheduler timer.");
long secondsUntilNextNewDay = TimeMgmt.townyTime();

newDayTimer = new Timer("towny-new-day-scheduler", true);
newDayTimer.schedule(new TimerTask() {
@Override
public void run() {
newDay();
}
}, TimeUnit.SECONDS.toMillis(TimeMgmt.townyTime()), TimeUnit.SECONDS.toMillis(TownySettings.getDayInterval()));
// If the next new day is less than 2 minutes away, schedule the new day.
if (plugin.getScheduler() instanceof FoliaTaskScheduler || secondsUntilNextNewDay < TimeUnit.MINUTES.toSeconds(2)) {
TownyMessaging.sendDebugMsg("New Day time finalized for: " + TimeMgmt.formatCountdownTime(secondsUntilNextNewDay) + " from now.");
scheduleUpComingNewDay(secondsUntilNextNewDay);
// Else the new day scheduler will run again at half the secondsUntilNextNewDay, to check again.
} else {
scheduleTask = plugin.getScheduler().runLater(new NewDayScheduler(plugin), (secondsUntilNextNewDay / 2) * 20);
TownyMessaging.sendDebugMsg("Re-evaluation of New Day time scheduled for: " + TimeMgmt.formatCountdownTime(secondsUntilNextNewDay / 2) + " from now.");
}
}

Expand All @@ -71,26 +56,22 @@ public void run() {
* @param secondsUntilNextNewDay long seconds until the next task.
*/
private void scheduleUpComingNewDay(long secondsUntilNextNewDay) {
plugin.getScheduler().runAsyncLater(() -> TownyEconomyHandler.economyExecutor().execute(new DailyTimerTask(plugin)), secondsUntilNextNewDay * 20);
newDayTask = plugin.getScheduler().runAsyncLater(() -> TownyEconomyHandler.economyExecutor().execute(new DailyTimerTask(plugin)), secondsUntilNextNewDay * 20);
}

public static boolean isNewDaySchedulerRunning() {
if (TownySettings.doesNewDayUseTimer())
return newDayTimer != null;

return scheduleTask != null && !scheduleTask.isCancelled();
}

public static boolean isNewDayScheduled() {
return newDayTask != null && !newDayTask.isCancelled();
}

public static boolean isNewDayRunning() {
return newDayTask != null && newDayTask.isCurrentlyRunning();
}

public static void cancelScheduledNewDay() {
if (newDayTimer != null) {
newDayTimer.cancel();
newDayTimer = null;
}

if (scheduleTask != null) {
scheduleTask.cancel();
scheduleTask = null;
Expand Down
1 change: 1 addition & 0 deletions Towny/src/main/resources/lang/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,7 @@ msg_you_must_wait_x_seconds_before_renaming_your_town: 'You must wait %s seconds

#Added in 1.141
msg_newday_already_scheduled_soon: 'A new day task is already scheduled to happen soon.'
msg_newday_already_running: 'A new day task is already currently running.'

#Added in 0.142

Expand Down
Loading