@@ -48,6 +48,7 @@ public final class EcoBalancer extends JavaPlugin {
48
48
private String scheduleType ;
49
49
private List <Integer > scheduleDaysOfWeek ;
50
50
private List <Integer > scheduleDatesOfMonth ;
51
+ private String checkTime ;
51
52
private FileConfiguration langConfig ;
52
53
private boolean taxAccount ;
53
54
private String taxAccountName ;
@@ -125,24 +126,12 @@ public void loadConfiguration() {
125
126
loadLangFile ();
126
127
messagePrefix = langConfig .getString ("prefix" , "&7[&6EcoBalancer&7]&r" );
127
128
// 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" ); // 读取配置
134
133
// 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 ());
146
135
// deduction setting
147
136
deductBasedOnTime = getConfig ().getBoolean ("deduct-based-on-time" , false );
148
137
inactiveDaysToDeduct = getConfig ().getInt ("inactive-days-to-deduct" , 50 );
@@ -305,99 +294,108 @@ private void sendMessage(CommandSender sender, String path, Map<String, String>
305
294
if (isLog ) for (String str : message .split ("\n " )) fileLogger .info (str );
306
295
}
307
296
308
- private long calculateInitialDailyDelay ( int hourOfDay , int minute ) {
297
+ private long calculateNextDelay ( ) {
309
298
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
+
310
317
Calendar nextCheck = (Calendar ) now .clone ();
311
318
nextCheck .set (Calendar .HOUR_OF_DAY , hourOfDay );
312
319
nextCheck .set (Calendar .MINUTE , minute );
313
320
nextCheck .set (Calendar .SECOND , 0 );
314
321
nextCheck .set (Calendar .MILLISECOND , 0 );
315
322
316
- // If the next check time is before the current time, add one day
323
+ // 如果下一个检查时间在现在之前,添加一天
317
324
if (nextCheck .before (now )) {
318
325
nextCheck .add (Calendar .DAY_OF_MONTH , 1 );
319
326
}
320
327
321
- return (nextCheck .getTimeInMillis () - now .getTimeInMillis ()) / 50 ; // Ticks until the next check
328
+ return (nextCheck .getTimeInMillis () - now .getTimeInMillis ()) / 50 ; // 返回ticks
322
329
}
323
330
324
- private long calculateInitialWeeklyDelay () {
325
- Calendar now = Calendar .getInstance ();
331
+ private long calculateDelayForWeekly (Calendar now ) {
326
332
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
+
327
342
int daysUntilNextCheck = scheduleDaysOfWeek .stream ()
328
343
.sorted ()
329
- .filter (dayOfWeek -> dayOfWeek >= today )
344
+ .filter (dayOfWeek -> dayOfWeek > today )
345
+ .map (dayOfWeek -> dayOfWeek - today )
330
346
.findFirst ()
331
- .orElse (scheduleDaysOfWeek .get (0 )) - today ;
347
+ .orElse (7 + scheduleDaysOfWeek .get (0 ) - today ) ;
332
348
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 ]);
337
351
338
352
Calendar nextCheck = (Calendar ) now .clone ();
339
353
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 );
342
356
nextCheck .set (Calendar .SECOND , 0 );
343
357
nextCheck .set (Calendar .MILLISECOND , 0 );
344
358
345
- return (nextCheck .getTimeInMillis () - now .getTimeInMillis ()) / 50 ; // Ticks until the next check
359
+ return (nextCheck .getTimeInMillis () - now .getTimeInMillis ()) / 50 ; // 返回ticks
346
360
}
347
361
348
- private long calculateInitialMonthlyDelay () {
349
- Calendar now = Calendar .getInstance ();
362
+ private long calculateDelayForMonthly (Calendar now ) {
350
363
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
+ }
351
372
int daysUntilNextCheck = scheduleDatesOfMonth .stream ()
352
- .filter (date -> date >= dayOfMonth )
373
+ .filter (date -> date > dayOfMonth )
374
+ .map (date -> date - dayOfMonth )
353
375
.findFirst ()
354
- .orElse (scheduleDatesOfMonth .get (0 )) - dayOfMonth ;
376
+ .orElse (scheduleDatesOfMonth .get (0 ) + now . getActualMaximum ( Calendar . DAY_OF_MONTH ) - dayOfMonth ) ;
355
377
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 ]);
360
380
361
381
Calendar nextCheck = (Calendar ) now .clone ();
362
382
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 );
365
385
nextCheck .set (Calendar .SECOND , 0 );
366
386
nextCheck .set (Calendar .MILLISECOND , 0 );
367
387
368
- return (nextCheck .getTimeInMillis () - now .getTimeInMillis ()) / 50 ; // Ticks until the next check
388
+ return (nextCheck .getTimeInMillis () - now .getTimeInMillis ()) / 50 ; // 返回ticks
369
389
}
370
390
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 ;
375
391
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 ); // 运行任务
391
395
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 );
401
399
}
402
400
403
401
public void checkAll (CommandSender sender ) {
0 commit comments