Skip to content

Commit ba227d6

Browse files
committed
scheduler bugs
1 parent 858c30c commit ba227d6

File tree

7 files changed

+74
-76
lines changed

7 files changed

+74
-76
lines changed

src/main/java/org/cubexmc/ecobalancer/EcoBalancer.java

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public final class EcoBalancer extends JavaPlugin {
4848
private String scheduleType;
4949
private List<Integer> scheduleDaysOfWeek;
5050
private List<Integer> scheduleDatesOfMonth;
51+
private String checkTime;
5152
private FileConfiguration langConfig;
5253
private boolean taxAccount;
5354
private String taxAccountName;
@@ -125,24 +126,12 @@ public void loadConfiguration() {
125126
loadLangFile();
126127
messagePrefix = langConfig.getString("prefix", "&7[&6EcoBalancer&7]&r");
127128
// Load the new scheduling configuration
128-
scheduleType = getConfig().getString("schedule.type", "daily");
129-
scheduleDaysOfWeek = getConfig().getIntegerList("schedule.days-of-week");
130-
scheduleDatesOfMonth = getConfig().getIntegerList("schedule.dates-of-month");
131-
String checkTime = getConfig().getString("check-time", "01:00"); // 读取配置
132-
int hourOfDay = Integer.parseInt(checkTime.split(":")[0]);
133-
int minute = Integer.parseInt(checkTime.split(":")[1]);
129+
scheduleType = getConfig().getString("check-schedule.type", "daily");
130+
scheduleDaysOfWeek = getConfig().getIntegerList("check-schedule.days-of-week");
131+
scheduleDatesOfMonth = getConfig().getIntegerList("check-schedule.dates-of-month");
132+
checkTime = getConfig().getString("check-time", "01:00"); // 读取配置
134133
// Determine which scheduling method to use based on the type
135-
switch (scheduleType.toLowerCase()) {
136-
case "weekly":
137-
scheduleWeeklyChecks();
138-
break;
139-
case "monthly":
140-
scheduleMonthlyChecks();
141-
break;
142-
default:
143-
scheduleDailyChecks(hourOfDay, minute);
144-
break;
145-
}
134+
scheduleCheck(calculateNextDelay());
146135
// deduction setting
147136
deductBasedOnTime = getConfig().getBoolean("deduct-based-on-time", false);
148137
inactiveDaysToDeduct = getConfig().getInt("inactive-days-to-deduct", 50);
@@ -305,99 +294,108 @@ private void sendMessage(CommandSender sender, String path, Map<String, String>
305294
if (isLog) for (String str : message.split("\n")) fileLogger.info(str);
306295
}
307296

308-
private long calculateInitialDailyDelay(int hourOfDay, int minute) {
297+
private long calculateNextDelay() {
309298
Calendar now = Calendar.getInstance();
299+
300+
// 选择最近的一个执行时间
301+
switch (scheduleType) {
302+
case "daily":
303+
return calculateDelayForDaily(now);
304+
case "weekly":
305+
return calculateDelayForWeekly(now);
306+
case "monthly":
307+
return calculateDelayForMonthly(now);
308+
default:
309+
return calculateDelayForDaily(now);
310+
}
311+
}
312+
313+
private long calculateDelayForDaily(Calendar now) {
314+
int hourOfDay = Integer.parseInt(checkTime.split(":")[0]);
315+
int minute = Integer.parseInt(checkTime.split(":")[1]);
316+
310317
Calendar nextCheck = (Calendar) now.clone();
311318
nextCheck.set(Calendar.HOUR_OF_DAY, hourOfDay);
312319
nextCheck.set(Calendar.MINUTE, minute);
313320
nextCheck.set(Calendar.SECOND, 0);
314321
nextCheck.set(Calendar.MILLISECOND, 0);
315322

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

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

324-
private long calculateInitialWeeklyDelay() {
325-
Calendar now = Calendar.getInstance();
331+
private long calculateDelayForWeekly(Calendar now) {
326332
int today = now.get(Calendar.DAY_OF_WEEK);
333+
if (scheduleDaysOfWeek.contains(today)) {
334+
// 如果今天是执行日,检查当前时间是否已过计划执行时间
335+
long delayForToday = calculateDelayForDaily(now);
336+
if (delayForToday > 0) {
337+
// 如果还没到计划时间,返回今天的延迟
338+
return delayForToday;
339+
}
340+
}
341+
327342
int daysUntilNextCheck = scheduleDaysOfWeek.stream()
328343
.sorted()
329-
.filter(dayOfWeek -> dayOfWeek >= today)
344+
.filter(dayOfWeek -> dayOfWeek > today)
345+
.map(dayOfWeek -> dayOfWeek - today)
330346
.findFirst()
331-
.orElse(scheduleDaysOfWeek.get(0)) - today;
347+
.orElse(7 + scheduleDaysOfWeek.get(0) - today);
332348

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

338352
Calendar nextCheck = (Calendar) now.clone();
339353
nextCheck.add(Calendar.DAY_OF_WEEK, daysUntilNextCheck);
340-
nextCheck.set(Calendar.HOUR_OF_DAY, 0);
341-
nextCheck.set(Calendar.MINUTE, 0);
354+
nextCheck.set(Calendar.HOUR_OF_DAY, hourOfDay);
355+
nextCheck.set(Calendar.MINUTE, minute);
342356
nextCheck.set(Calendar.SECOND, 0);
343357
nextCheck.set(Calendar.MILLISECOND, 0);
344358

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

348-
private long calculateInitialMonthlyDelay() {
349-
Calendar now = Calendar.getInstance();
362+
private long calculateDelayForMonthly(Calendar now) {
350363
int dayOfMonth = now.get(Calendar.DAY_OF_MONTH);
364+
if (scheduleDatesOfMonth.contains(dayOfMonth)) {
365+
// 如果今天是执行日,检查当前时间是否已过计划执行时间
366+
long delayForToday = calculateDelayForDaily(now);
367+
if (delayForToday > 0) {
368+
// 如果还没到计划时间,返回今天的延迟
369+
return delayForToday;
370+
}
371+
}
351372
int daysUntilNextCheck = scheduleDatesOfMonth.stream()
352-
.filter(date -> date >= dayOfMonth)
373+
.filter(date -> date > dayOfMonth)
374+
.map(date -> date - dayOfMonth)
353375
.findFirst()
354-
.orElse(scheduleDatesOfMonth.get(0)) - dayOfMonth;
376+
.orElse(scheduleDatesOfMonth.get(0) + now.getActualMaximum(Calendar.DAY_OF_MONTH) - dayOfMonth);
355377

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

361381
Calendar nextCheck = (Calendar) now.clone();
362382
nextCheck.add(Calendar.DAY_OF_MONTH, daysUntilNextCheck);
363-
nextCheck.set(Calendar.HOUR_OF_DAY, 0);
364-
nextCheck.set(Calendar.MINUTE, 0);
383+
nextCheck.set(Calendar.HOUR_OF_DAY, hourOfDay);
384+
nextCheck.set(Calendar.MINUTE, minute);
365385
nextCheck.set(Calendar.SECOND, 0);
366386
nextCheck.set(Calendar.MILLISECOND, 0);
367387

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

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

376-
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
377-
// Your daily check code here
378-
checkAll(null); // Replace null with the actual sender, if available
379-
}, initialDelay, ticksPerDay); // Run task every day
380-
}
381-
private void scheduleWeeklyChecks() {
382-
long ticksPerWeek = 20L * 60 * 60 * 24 * 7;
383-
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
384-
Calendar now = Calendar.getInstance();
385-
int today = now.get(Calendar.DAY_OF_WEEK);
386-
if (scheduleDaysOfWeek.contains(today)) {
387-
checkAll(null); // Replace null with the actual sender, if available
388-
}
389-
}, calculateInitialWeeklyDelay(), ticksPerWeek); // Adjust delay for weekly
390-
}
392+
private void scheduleCheck(long delay) {
393+
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
394+
checkAll(null); // 运行任务
391395

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

403401
public void checkAll(CommandSender sender) {

src/main/resources/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
check-time: "20:00" # 格式为 HH:mm # Time format is HH:mm
22
check-schedule:
3-
type: 'weekly' # 选项: 'daily', 'weekly', 'monthly' # Options: 'daily', 'weekly', 'monthly'
4-
days-of-week: [1, 3, 5] # 周一, 周三, 周五 (6 = 周六, 7 = 周日) # Monday, Wednesday, Friday (6 = Saturday, 7 = Sunday)
5-
dates-of-month: [1] # 每月一号 # 1st day of each month
3+
type: 'weekly' # 选项: 'daily', 'weekly', 'monthly' # Options: 'daily', 'weekly', 'monthly'
4+
days-of-week: [2, 4, 6] # 周一, 周三, 周五 (7 = 周六, 1 = 周日) # Monday, Wednesday, Friday (7 = Saturday, 1 = Sunday)
5+
dates-of-month: [1] # 每月一号 # 1st day of each month
66
deduct-based-on-time: true
77
# 下面两个选项仅在 deduct-based-on-time 为 true 时生效 # The following two options only take effect when deduct-based-on-time is true
88
inactive-days-to-deduct: 50 # 未上线扣款开始的天数 # Days inactive before starting deductions

target/classes/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
check-time: "20:00" # 格式为 HH:mm # Time format is HH:mm
22
check-schedule:
3-
type: 'weekly' # 选项: 'daily', 'weekly', 'monthly' # Options: 'daily', 'weekly', 'monthly'
4-
days-of-week: [1, 3, 5] # 周一, 周三, 周五 (6 = 周六, 7 = 周日) # Monday, Wednesday, Friday (6 = Saturday, 7 = Sunday)
5-
dates-of-month: [1] # 每月一号 # 1st day of each month
3+
type: 'weekly' # 选项: 'daily', 'weekly', 'monthly' # Options: 'daily', 'weekly', 'monthly'
4+
days-of-week: [2, 4, 6] # 周一, 周三, 周五 (7 = 周六, 1 = 周日) # Monday, Wednesday, Friday (7 = Saturday, 1 = Sunday)
5+
dates-of-month: [1] # 每月一号 # 1st day of each month
66
deduct-based-on-time: true
77
# 下面两个选项仅在 deduct-based-on-time 为 true 时生效 # The following two options only take effect when deduct-based-on-time is true
88
inactive-days-to-deduct: 50 # 未上线扣款开始的天数 # Days inactive before starting deductions
Binary file not shown.
Binary file not shown.
-75 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)