Skip to content

Commit

Permalink
scheduler bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
angushushu committed Nov 30, 2023
1 parent 858c30c commit ba227d6
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 76 deletions.
138 changes: 68 additions & 70 deletions src/main/java/org/cubexmc/ecobalancer/EcoBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public final class EcoBalancer extends JavaPlugin {
private String scheduleType;
private List<Integer> scheduleDaysOfWeek;
private List<Integer> scheduleDatesOfMonth;
private String checkTime;
private FileConfiguration langConfig;
private boolean taxAccount;
private String taxAccountName;
Expand Down Expand Up @@ -125,24 +126,12 @@ public void loadConfiguration() {
loadLangFile();
messagePrefix = langConfig.getString("prefix", "&7[&6EcoBalancer&7]&r");
// Load the new scheduling configuration
scheduleType = getConfig().getString("schedule.type", "daily");
scheduleDaysOfWeek = getConfig().getIntegerList("schedule.days-of-week");
scheduleDatesOfMonth = getConfig().getIntegerList("schedule.dates-of-month");
String checkTime = getConfig().getString("check-time", "01:00"); // 读取配置
int hourOfDay = Integer.parseInt(checkTime.split(":")[0]);
int minute = Integer.parseInt(checkTime.split(":")[1]);
scheduleType = getConfig().getString("check-schedule.type", "daily");
scheduleDaysOfWeek = getConfig().getIntegerList("check-schedule.days-of-week");
scheduleDatesOfMonth = getConfig().getIntegerList("check-schedule.dates-of-month");
checkTime = getConfig().getString("check-time", "01:00"); // 读取配置
// Determine which scheduling method to use based on the type
switch (scheduleType.toLowerCase()) {
case "weekly":
scheduleWeeklyChecks();
break;
case "monthly":
scheduleMonthlyChecks();
break;
default:
scheduleDailyChecks(hourOfDay, minute);
break;
}
scheduleCheck(calculateNextDelay());
// deduction setting
deductBasedOnTime = getConfig().getBoolean("deduct-based-on-time", false);
inactiveDaysToDeduct = getConfig().getInt("inactive-days-to-deduct", 50);
Expand Down Expand Up @@ -305,99 +294,108 @@ private void sendMessage(CommandSender sender, String path, Map<String, String>
if (isLog) for (String str : message.split("\n")) fileLogger.info(str);
}

private long calculateInitialDailyDelay(int hourOfDay, int minute) {
private long calculateNextDelay() {
Calendar now = Calendar.getInstance();

// 选择最近的一个执行时间
switch (scheduleType) {
case "daily":
return calculateDelayForDaily(now);
case "weekly":
return calculateDelayForWeekly(now);
case "monthly":
return calculateDelayForMonthly(now);
default:
return calculateDelayForDaily(now);
}
}

private long calculateDelayForDaily(Calendar now) {
int hourOfDay = Integer.parseInt(checkTime.split(":")[0]);
int minute = Integer.parseInt(checkTime.split(":")[1]);

Calendar nextCheck = (Calendar) now.clone();
nextCheck.set(Calendar.HOUR_OF_DAY, hourOfDay);
nextCheck.set(Calendar.MINUTE, minute);
nextCheck.set(Calendar.SECOND, 0);
nextCheck.set(Calendar.MILLISECOND, 0);

// If the next check time is before the current time, add one day
// 如果下一个检查时间在现在之前,添加一天
if (nextCheck.before(now)) {
nextCheck.add(Calendar.DAY_OF_MONTH, 1);
}

return (nextCheck.getTimeInMillis() - now.getTimeInMillis()) / 50; // Ticks until the next check
return (nextCheck.getTimeInMillis() - now.getTimeInMillis()) / 50; // 返回ticks
}

private long calculateInitialWeeklyDelay() {
Calendar now = Calendar.getInstance();
private long calculateDelayForWeekly(Calendar now) {
int today = now.get(Calendar.DAY_OF_WEEK);
if (scheduleDaysOfWeek.contains(today)) {
// 如果今天是执行日,检查当前时间是否已过计划执行时间
long delayForToday = calculateDelayForDaily(now);
if (delayForToday > 0) {
// 如果还没到计划时间,返回今天的延迟
return delayForToday;
}
}

int daysUntilNextCheck = scheduleDaysOfWeek.stream()
.sorted()
.filter(dayOfWeek -> dayOfWeek >= today)
.filter(dayOfWeek -> dayOfWeek > today)
.map(dayOfWeek -> dayOfWeek - today)
.findFirst()
.orElse(scheduleDaysOfWeek.get(0)) - today;
.orElse(7 + scheduleDaysOfWeek.get(0) - today);

// If the next check date is in the next week
if (daysUntilNextCheck < 0) {
daysUntilNextCheck += 7; // days left in the current week
}
int hourOfDay = Integer.parseInt(checkTime.split(":")[0]);
int minute = Integer.parseInt(checkTime.split(":")[1]);

Calendar nextCheck = (Calendar) now.clone();
nextCheck.add(Calendar.DAY_OF_WEEK, daysUntilNextCheck);
nextCheck.set(Calendar.HOUR_OF_DAY, 0);
nextCheck.set(Calendar.MINUTE, 0);
nextCheck.set(Calendar.HOUR_OF_DAY, hourOfDay);
nextCheck.set(Calendar.MINUTE, minute);
nextCheck.set(Calendar.SECOND, 0);
nextCheck.set(Calendar.MILLISECOND, 0);

return (nextCheck.getTimeInMillis() - now.getTimeInMillis()) / 50; // Ticks until the next check
return (nextCheck.getTimeInMillis() - now.getTimeInMillis()) / 50; // 返回ticks
}

private long calculateInitialMonthlyDelay() {
Calendar now = Calendar.getInstance();
private long calculateDelayForMonthly(Calendar now) {
int dayOfMonth = now.get(Calendar.DAY_OF_MONTH);
if (scheduleDatesOfMonth.contains(dayOfMonth)) {
// 如果今天是执行日,检查当前时间是否已过计划执行时间
long delayForToday = calculateDelayForDaily(now);
if (delayForToday > 0) {
// 如果还没到计划时间,返回今天的延迟
return delayForToday;
}
}
int daysUntilNextCheck = scheduleDatesOfMonth.stream()
.filter(date -> date >= dayOfMonth)
.filter(date -> date > dayOfMonth)
.map(date -> date - dayOfMonth)
.findFirst()
.orElse(scheduleDatesOfMonth.get(0)) - dayOfMonth;
.orElse(scheduleDatesOfMonth.get(0) + now.getActualMaximum(Calendar.DAY_OF_MONTH) - dayOfMonth);

// If the next check date is in the next month
if (daysUntilNextCheck < 0) {
daysUntilNextCheck += now.getActualMaximum(Calendar.DAY_OF_MONTH); // days left in the current month
}
int hourOfDay = Integer.parseInt(checkTime.split(":")[0]);
int minute = Integer.parseInt(checkTime.split(":")[1]);

Calendar nextCheck = (Calendar) now.clone();
nextCheck.add(Calendar.DAY_OF_MONTH, daysUntilNextCheck);
nextCheck.set(Calendar.HOUR_OF_DAY, 0);
nextCheck.set(Calendar.MINUTE, 0);
nextCheck.set(Calendar.HOUR_OF_DAY, hourOfDay);
nextCheck.set(Calendar.MINUTE, minute);
nextCheck.set(Calendar.SECOND, 0);
nextCheck.set(Calendar.MILLISECOND, 0);

return (nextCheck.getTimeInMillis() - now.getTimeInMillis()) / 50; // Ticks until the next check
return (nextCheck.getTimeInMillis() - now.getTimeInMillis()) / 50; // 返回ticks
}

private void scheduleDailyChecks(int hourOfDay, int minute) {
// Calculate initial delay
long initialDelay = calculateInitialDailyDelay(hourOfDay, minute);
long ticksPerDay = 20L * 60 * 60 * 24;

Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
// Your daily check code here
checkAll(null); // Replace null with the actual sender, if available
}, initialDelay, ticksPerDay); // Run task every day
}
private void scheduleWeeklyChecks() {
long ticksPerWeek = 20L * 60 * 60 * 24 * 7;
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
Calendar now = Calendar.getInstance();
int today = now.get(Calendar.DAY_OF_WEEK);
if (scheduleDaysOfWeek.contains(today)) {
checkAll(null); // Replace null with the actual sender, if available
}
}, calculateInitialWeeklyDelay(), ticksPerWeek); // Adjust delay for weekly
}
private void scheduleCheck(long delay) {
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
checkAll(null); // 运行任务

private void scheduleMonthlyChecks() {
long ticksPerMonth = 20L * 60 * 60 * 24 * 30; // Rough approximation
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
Calendar now = Calendar.getInstance();
int today = now.get(Calendar.DATE);
if (scheduleDatesOfMonth.contains(today)) {
checkAll(null); // Replace null with the actual sender, if available
}
}, calculateInitialMonthlyDelay(), ticksPerMonth); // Adjust for monthly
// 任务完成后,计划下一个任务
scheduleCheck(calculateNextDelay());
}, delay);
}

public void checkAll(CommandSender sender) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
check-time: "20:00" # 格式为 HH:mm # Time format is HH:mm
check-schedule:
type: 'weekly' # 选项: 'daily', 'weekly', 'monthly' # Options: 'daily', 'weekly', 'monthly'
days-of-week: [1, 3, 5] # 周一, 周三, 周五 (6 = 周六, 7 = 周日) # Monday, Wednesday, Friday (6 = Saturday, 7 = Sunday)
dates-of-month: [1] # 每月一号 # 1st day of each month
type: 'weekly' # 选项: 'daily', 'weekly', 'monthly' # Options: 'daily', 'weekly', 'monthly'
days-of-week: [2, 4, 6] # 周一, 周三, 周五 (7 = 周六, 1 = 周日) # Monday, Wednesday, Friday (7 = Saturday, 1 = Sunday)
dates-of-month: [1] # 每月一号 # 1st day of each month
deduct-based-on-time: true
# 下面两个选项仅在 deduct-based-on-time 为 true 时生效 # The following two options only take effect when deduct-based-on-time is true
inactive-days-to-deduct: 50 # 未上线扣款开始的天数 # Days inactive before starting deductions
Expand Down
Binary file not shown.
6 changes: 3 additions & 3 deletions target/classes/config.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
check-time: "20:00" # 格式为 HH:mm # Time format is HH:mm
check-schedule:
type: 'weekly' # 选项: 'daily', 'weekly', 'monthly' # Options: 'daily', 'weekly', 'monthly'
days-of-week: [1, 3, 5] # 周一, 周三, 周五 (6 = 周六, 7 = 周日) # Monday, Wednesday, Friday (6 = Saturday, 7 = Sunday)
dates-of-month: [1] # 每月一号 # 1st day of each month
type: 'weekly' # 选项: 'daily', 'weekly', 'monthly' # Options: 'daily', 'weekly', 'monthly'
days-of-week: [2, 4, 6] # 周一, 周三, 周五 (7 = 周六, 1 = 周日) # Monday, Wednesday, Friday (7 = Saturday, 1 = Sunday)
dates-of-month: [1] # 每月一号 # 1st day of each month
deduct-based-on-time: true
# 下面两个选项仅在 deduct-based-on-time 为 true 时生效 # The following two options only take effect when deduct-based-on-time is true
inactive-days-to-deduct: 50 # 未上线扣款开始的天数 # Days inactive before starting deductions
Expand Down
Binary file not shown.
Binary file modified target/classes/org/cubexmc/ecobalancer/EcoBalancer.class
Binary file not shown.
Binary file modified target/original-EcoBalancer-1.0-SNAPSHOT.jar
Binary file not shown.

0 comments on commit ba227d6

Please sign in to comment.