7
7
import org .bukkit .ChatColor ;
8
8
import org .bukkit .OfflinePlayer ;
9
9
import org .bukkit .command .CommandSender ;
10
+ import org .bukkit .configuration .file .FileConfiguration ;
11
+ import org .bukkit .configuration .file .YamlConfiguration ;
10
12
import org .bukkit .plugin .RegisteredServiceProvider ;
11
13
import org .bukkit .plugin .java .JavaPlugin ;
12
14
import org .cubexmc .ecobalancer .commands .CheckAllCommand ;
25
27
import java .util .logging .SimpleFormatter ;
26
28
27
29
import java .util .zip .GZIPOutputStream ;
28
- import java . util . zip . ZipEntry ;
29
- import java . util . zip . ZipOutputStream ;
30
+
31
+ import org . cubexmc . ecobalancer . metrics . Metrics ;
30
32
31
33
public final class EcoBalancer extends JavaPlugin {
32
34
@@ -42,6 +44,7 @@ public final class EcoBalancer extends JavaPlugin {
42
44
private String scheduleType ;
43
45
private List <Integer > scheduleDaysOfWeek ;
44
46
private List <Integer > scheduleDatesOfMonth ;
47
+ private FileConfiguration langConfig ;
45
48
46
49
@ Override
47
50
public void onEnable () {
@@ -78,6 +81,13 @@ public void onEnable() {
78
81
e .printStackTrace ();
79
82
}
80
83
84
+ // metrics
85
+ int pluginId = 20269 ; // <-- Replace with the id of your plugin!
86
+ Metrics metrics = new Metrics (this , pluginId );
87
+ // Optional: Add custom charts
88
+ metrics .addCustomChart (new Metrics .SimplePie ("chart_id" , () -> "My value" ));
89
+
90
+ // getCommand("ecobalancer").setExecutor(new UtilCommand(this));
81
91
getCommand ("ecobal" ).setExecutor (new UtilCommand (this ));
82
92
getCommand ("checkall" ).setExecutor (new CheckAllCommand (this ));
83
93
getCommand ("checkplayer" ).setExecutor (new CheckPlayerCommand (this ));
@@ -87,7 +97,8 @@ public void onEnable() {
87
97
public void loadConfiguration () {
88
98
// Cancel all scheduled tasks
89
99
Bukkit .getScheduler ().cancelTasks (this );
90
-
100
+ // load language config
101
+ loadLangFile ();
91
102
// Load the new scheduling configuration
92
103
scheduleType = getConfig ().getString ("schedule.type" , "daily" );
93
104
scheduleDaysOfWeek = getConfig ().getIntegerList ("schedule.days-of-week" );
@@ -120,6 +131,25 @@ public void loadConfiguration() {
120
131
}
121
132
}
122
133
134
+ private void loadLangFile () {
135
+ File langFile = new File (getDataFolder (), "lang.yml" );
136
+ if (!langFile .exists ()) {
137
+ saveResource ("lang.yml" , false );
138
+ }
139
+ langConfig = YamlConfiguration .loadConfiguration (langFile );
140
+ }
141
+
142
+ public String getFormattedMessage (String path , Map <String , String > placeholders ) {
143
+ String message = langConfig .getString (path , "Message not found!" );
144
+ if (placeholders != null ) {
145
+ for (Map .Entry <String , String > entry : placeholders .entrySet ()) {
146
+ message = message .replace ("%" + entry .getKey () + "%" , entry .getValue ());
147
+ }
148
+ }
149
+
150
+ return ChatColor .translateAlternateColorCodes ('&' , message );
151
+ }
152
+
123
153
@ Override
124
154
public void onDisable () {
125
155
// Ensure all pending logs are flushed and the handler is closed
@@ -140,16 +170,25 @@ public void onDisable() {
140
170
141
171
// Method to compress the existing log file
142
172
private void compressExistingLogFile (File logFile ) {
143
- SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyyMMddHHmmss " );
173
+ SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd-HHmm " );
144
174
String timestamp = dateFormat .format (new Date (logFile .lastModified ()));
145
- File compressedFile = new File (logFile .getParent (), timestamp + ".gz" );
146
-
175
+ File renamedLogFile = new File (logFile .getParent (), timestamp + ".log" );
176
+ // Rename the file to include the timestamp
177
+ if (!logFile .renameTo (renamedLogFile )) {
178
+ getLogger ().severe ("Could not rename the log file." );
179
+ return ;
180
+ }
181
+ // Compress the renamed log file into a .gz file
182
+ File compressedFile = new File (renamedLogFile .getParent (), renamedLogFile .getName () + ".gz" );
147
183
try (GZIPOutputStream gzos = new GZIPOutputStream (new FileOutputStream (compressedFile ))) {
148
- Files .copy (logFile .toPath (), gzos );
184
+ Files .copy (renamedLogFile .toPath (), gzos );
149
185
} catch (IOException e ) {
150
186
getLogger ().severe ("Could not compress the log file: " + e .getMessage ());
151
187
}
152
- logFile .delete (); // 删除原始日志文件
188
+ // Delete the original (now renamed) log file after it's compressed
189
+ if (!renamedLogFile .delete ()) {
190
+ getLogger ().severe ("Could not delete the original log file after compression." );
191
+ }
153
192
}
154
193
155
194
private boolean setupEconomy () {
@@ -187,49 +226,59 @@ public void checkBalance(CommandSender sender, long currentTime, OfflinePlayer p
187
226
deductionRate = 0.0 ; // defaultRate should be defined somewhere in your class
188
227
}
189
228
229
+ Map <String , String > placeholders = new HashMap <>();
230
+ placeholders .put ("player" , player .getName ());
231
+ placeholders .put ("balance" , String .format ("%.2f" , balance ));
232
+ placeholders .put ("days_offline" , String .valueOf (daysOffline ));
233
+
234
+
190
235
if (deductBasedOnTime ) {
191
236
// 计算玩家离线天数
192
237
if (balance < 0 ) {
193
238
econ .depositPlayer (player , -1 * balance );
239
+ placeholders .put ("new_balance" , String .format ("%.2f" , econ .getBalance (player )));
194
240
if (sender != null ) {
195
- sender .sendMessage (ChatColor . YELLOW + player . getName () + ChatColor . GREEN + " 的余额置为" + String . format ( "%.2f " , balance ) + "。" );
196
- sender .sendMessage (ChatColor . GREEN + "已将余额置为" + String . format ( "%.2f " , econ . getBalance ( player )) + "。" );
241
+ sender .sendMessage (getFormattedMessage ( "messages.set_balance.reason " , placeholders ) );
242
+ sender .sendMessage (getFormattedMessage ( "messages.set_balance.balance_set " , placeholders ) );
197
243
}
198
244
if (log )
199
- fileLogger .info ("[负额度] 已将 " + player . getName () + " 的余额置为" + String . format ( "%.2f" , econ . getBalance ( player )) + "。" );
245
+ fileLogger .info (getFormattedMessage ( "logs.negative_balance" , placeholders ) );
200
246
} else {
201
247
if (daysOffline > inactiveDaysToClear ) {
202
248
// 清除超过inactiveDaysToClear天未上线的玩家
203
249
econ .withdrawPlayer (player , balance );
250
+ placeholders .put ("new_balance" , String .format ("%.2f" , econ .getBalance (player )));
204
251
if (sender != null ) {
205
- sender .sendMessage (ChatColor . YELLOW + player . getName () + ChatColor . GREEN + " 已经离线" + daysOffline + "天,极不活跃。" );
206
- sender .sendMessage (ChatColor . GREEN + "已将余额置为" + String . format ( "%.2f " , econ . getBalance ( player )) + "。" );
252
+ sender .sendMessage (getFormattedMessage ( "messages.offline_extreme.reason" , placeholders ) );
253
+ sender .sendMessage (getFormattedMessage ( "messages.offline_extreme.balance_set " , placeholders ) );
207
254
}
208
255
if (log )
209
- fileLogger .info ("[及不活跃] 已将 " + player . getName () + " 的余额置为" + String . format ( "%.2f" , econ . getBalance ( player )) + "。" );
256
+ fileLogger .info (getFormattedMessage ( "logs.inactive_extreme" , placeholders ) );
210
257
} else if (daysOffline > inactiveDaysToDeduct ) {
211
258
// 对于超过50天未上线的玩家,按税率扣除
212
259
double deduction = balance * deductionRate ;
260
+ placeholders .put ("deduction" , String .format ("%.2f" , deduction ));
213
261
econ .withdrawPlayer (player , deduction );
214
262
if (sender != null ) {
215
- sender .sendMessage (ChatColor . YELLOW + player . getName () + ChatColor . GREEN + " 已经离线" + daysOffline + "天,较不活跃。" );
216
- sender .sendMessage (ChatColor . GREEN + "已从余额中扣除 " + ChatColor . YELLOW + String . format ( "%.2f" , deduction ) + ChatColor . GREEN + "。" );
263
+ sender .sendMessage (getFormattedMessage ( "messages.offline_moderate.reason" , placeholders ) );
264
+ sender .sendMessage (getFormattedMessage ( "messages.offline_moderate.deduction_made" , placeholders ) );
217
265
}
218
266
if (log )
219
- fileLogger .info ("[较不活跃] 已从 " + player . getName () + " 的余额中扣除 " + String . format ( "%.2f" , deduction ) + "。" );
267
+ fileLogger .info (getFormattedMessage ( "logs.inactive_moderate" , placeholders ) );
220
268
} else {
221
269
if (sender != null ) {
222
- sender .sendMessage (ChatColor . YELLOW + player . getName () + ChatColor . GREEN + " 仅离线" + daysOffline + "天,较活跃。" );
270
+ sender .sendMessage (getFormattedMessage ( "messages.offline_active" , placeholders ) );
223
271
}
224
272
}
225
273
}
226
274
} else {
227
275
double deduction = balance * deductionRate ;
276
+ placeholders .put ("deduction" , String .format ("%.2f" , deduction ));
228
277
econ .withdrawPlayer (player , deduction );
229
278
if (sender != null )
230
- sender .sendMessage (ChatColor . GREEN + "已从 " + ChatColor . YELLOW + player . getName () + ChatColor . GREEN + " 的余额中扣除 " + ChatColor . YELLOW + String . format ( "%.2f" , deduction ) + ChatColor . GREEN + "。" );
279
+ sender .sendMessage (getFormattedMessage ( "messages.deduction_made" , placeholders ) );
231
280
if (log )
232
- fileLogger .info ("已从 " + player . getName () + " 的余额中扣除 " + String . format ( "%.2f" , deduction ) + "。" );
281
+ fileLogger .info (getFormattedMessage ( "logs.deduction_made" , placeholders ) );
233
282
}
234
283
}
235
284
@@ -346,11 +395,18 @@ public void run() {
346
395
checkBalance (null , currentTime , player , false );
347
396
}
348
397
index += batchSize ;
398
+
399
+ Map <String , String > placeholders = new HashMap <>();
400
+ placeholders .put ("start" , Integer .toString (start ));
401
+ placeholders .put ("end" , Integer .toString (end ));
402
+ placeholders .put ("batch" , Integer .toString (end - start ));
403
+ placeholders .put ("total_players" , Integer .toString (players .length ));
404
+
349
405
if (sender != null ) {
350
406
// Send a message to the sender after each batch
351
- Bukkit .getScheduler ().runTask (EcoBalancer .this , () -> sender .sendMessage (ChatColor . GRAY + "处理了" + ( end - start ) + "个玩家,共处理:" + end ));
407
+ Bukkit .getScheduler ().runTask (EcoBalancer .this , () -> sender .sendMessage (getFormattedMessage ( "messages.players_processing" , placeholders ) ));
352
408
} else {
353
- getLogger ().info ("处理了" + ( end - start ) + "个玩家,共处理:" + end );
409
+ getLogger ().info (getFormattedMessage ( "logs.players_processed" , placeholders ) );
354
410
}
355
411
if (index < players .length ) {
356
412
// Schedule next batch
@@ -360,10 +416,10 @@ public void run() {
360
416
// Send a message to the sender after each batch
361
417
Bukkit .getScheduler ().runTask (EcoBalancer .this , () -> {
362
418
if (sender != null ) {
363
- sender .sendMessage (ChatColor . GREEN + "所有玩家都已被洗劫。" );
419
+ sender .sendMessage (getFormattedMessage ( "messages.all_players_processed" , null ) );
364
420
} else {
365
- getLogger ().info ("所有玩家都已被洗劫。" );
366
- fileLogger .info ("处理了" + players . length + "个玩家,共处理:" + end );
421
+ getLogger ().info (getFormattedMessage ( "logs.all_players_processed" , null ) );
422
+ fileLogger .info (getFormattedMessage ( "logs.all_players_processed" , null ) );
367
423
}
368
424
});
369
425
}
0 commit comments