Skip to content

Commit 265f37d

Browse files
committed
yue
1 parent d9480b4 commit 265f37d

35 files changed

+1181
-64
lines changed

.idea/artifacts/EcoBalancer.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import org.bukkit.ChatColor;
88
import org.bukkit.OfflinePlayer;
99
import org.bukkit.command.CommandSender;
10+
import org.bukkit.configuration.file.FileConfiguration;
11+
import org.bukkit.configuration.file.YamlConfiguration;
1012
import org.bukkit.plugin.RegisteredServiceProvider;
1113
import org.bukkit.plugin.java.JavaPlugin;
1214
import org.cubexmc.ecobalancer.commands.CheckAllCommand;
@@ -25,8 +27,8 @@
2527
import java.util.logging.SimpleFormatter;
2628

2729
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;
3032

3133
public final class EcoBalancer extends JavaPlugin {
3234

@@ -42,6 +44,7 @@ public final class EcoBalancer extends JavaPlugin {
4244
private String scheduleType;
4345
private List<Integer> scheduleDaysOfWeek;
4446
private List<Integer> scheduleDatesOfMonth;
47+
private FileConfiguration langConfig;
4548

4649
@Override
4750
public void onEnable() {
@@ -78,6 +81,13 @@ public void onEnable() {
7881
e.printStackTrace();
7982
}
8083

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));
8191
getCommand("ecobal").setExecutor(new UtilCommand(this));
8292
getCommand("checkall").setExecutor(new CheckAllCommand(this));
8393
getCommand("checkplayer").setExecutor(new CheckPlayerCommand(this));
@@ -87,7 +97,8 @@ public void onEnable() {
8797
public void loadConfiguration() {
8898
// Cancel all scheduled tasks
8999
Bukkit.getScheduler().cancelTasks(this);
90-
100+
// load language config
101+
loadLangFile();
91102
// Load the new scheduling configuration
92103
scheduleType = getConfig().getString("schedule.type", "daily");
93104
scheduleDaysOfWeek = getConfig().getIntegerList("schedule.days-of-week");
@@ -120,6 +131,25 @@ public void loadConfiguration() {
120131
}
121132
}
122133

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+
123153
@Override
124154
public void onDisable() {
125155
// Ensure all pending logs are flushed and the handler is closed
@@ -140,16 +170,25 @@ public void onDisable() {
140170

141171
// Method to compress the existing log file
142172
private void compressExistingLogFile(File logFile) {
143-
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
173+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HHmm");
144174
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");
147183
try (GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(compressedFile))) {
148-
Files.copy(logFile.toPath(), gzos);
184+
Files.copy(renamedLogFile.toPath(), gzos);
149185
} catch (IOException e) {
150186
getLogger().severe("Could not compress the log file: " + e.getMessage());
151187
}
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+
}
153192
}
154193

155194
private boolean setupEconomy() {
@@ -187,49 +226,59 @@ public void checkBalance(CommandSender sender, long currentTime, OfflinePlayer p
187226
deductionRate = 0.0; // defaultRate should be defined somewhere in your class
188227
}
189228

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+
190235
if (deductBasedOnTime) {
191236
// 计算玩家离线天数
192237
if (balance < 0) {
193238
econ.depositPlayer(player, -1 * balance);
239+
placeholders.put("new_balance", String.format("%.2f", econ.getBalance(player)));
194240
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));
197243
}
198244
if (log)
199-
fileLogger.info("[负额度] 已将 " + player.getName() + " 的余额置为" + String.format("%.2f", econ.getBalance(player)) + "。");
245+
fileLogger.info(getFormattedMessage("logs.negative_balance", placeholders));
200246
} else {
201247
if (daysOffline > inactiveDaysToClear) {
202248
// 清除超过inactiveDaysToClear天未上线的玩家
203249
econ.withdrawPlayer(player, balance);
250+
placeholders.put("new_balance", String.format("%.2f", econ.getBalance(player)));
204251
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));
207254
}
208255
if (log)
209-
fileLogger.info("[及不活跃] 已将 " + player.getName() + " 的余额置为" + String.format("%.2f", econ.getBalance(player)) + "。");
256+
fileLogger.info(getFormattedMessage("logs.inactive_extreme", placeholders));
210257
} else if (daysOffline > inactiveDaysToDeduct) {
211258
// 对于超过50天未上线的玩家,按税率扣除
212259
double deduction = balance * deductionRate;
260+
placeholders.put("deduction", String.format("%.2f", deduction));
213261
econ.withdrawPlayer(player, deduction);
214262
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));
217265
}
218266
if (log)
219-
fileLogger.info("[较不活跃] 已从 " + player.getName() + " 的余额中扣除 " + String.format("%.2f", deduction) + "。");
267+
fileLogger.info(getFormattedMessage("logs.inactive_moderate", placeholders));
220268
} else {
221269
if (sender != null) {
222-
sender.sendMessage(ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " 仅离线" + daysOffline + "天,较活跃。");
270+
sender.sendMessage(getFormattedMessage("messages.offline_active", placeholders));
223271
}
224272
}
225273
}
226274
} else {
227275
double deduction = balance * deductionRate;
276+
placeholders.put("deduction", String.format("%.2f", deduction));
228277
econ.withdrawPlayer(player, deduction);
229278
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));
231280
if (log)
232-
fileLogger.info("已从 " + player.getName() + " 的余额中扣除 " + String.format("%.2f", deduction) + "。");
281+
fileLogger.info(getFormattedMessage("logs.deduction_made", placeholders));
233282
}
234283
}
235284

@@ -346,11 +395,18 @@ public void run() {
346395
checkBalance(null, currentTime, player, false);
347396
}
348397
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+
349405
if (sender != null) {
350406
// 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)));
352408
} else {
353-
getLogger().info("处理了" + (end - start) + "个玩家,共处理:" + end);
409+
getLogger().info(getFormattedMessage("logs.players_processed", placeholders));
354410
}
355411
if (index < players.length) {
356412
// Schedule next batch
@@ -360,10 +416,10 @@ public void run() {
360416
// Send a message to the sender after each batch
361417
Bukkit.getScheduler().runTask(EcoBalancer.this, () -> {
362418
if (sender != null) {
363-
sender.sendMessage(ChatColor.GREEN + "所有玩家都已被洗劫。");
419+
sender.sendMessage(getFormattedMessage("messages.all_players_processed", null));
364420
} 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));
367423
}
368424
});
369425
}

src/main/java/org/cubexmc/ecobalancer/commands/CheckAllCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ public boolean onCommand(CommandSender sender, Command command, String s, String
2424
if (sender instanceof Player) {
2525
Player player = (Player) sender;
2626
if (!player.hasPermission("ecobalancer.admin")) {
27-
player.sendMessage(ChatColor.RED + "你没有权限使用这个命令。");
27+
player.sendMessage(plugin.getFormattedMessage("messages.no_permission", null));
2828
return true;
2929
}
3030
if (args.length == 0) {
31-
player.sendMessage(ChatColor.GREEN + "正在扫描离线玩家...");
31+
player.sendMessage(plugin.getFormattedMessage("messages.scanning_offline_players", null));
3232
plugin.checkAll(sender);
3333
}
3434
} else if (sender.hasPermission("ecobalancer.admin")) {
3535
if (args.length == 0) {
36-
sender.sendMessage(ChatColor.GREEN + "正在扫描离线玩家...");
36+
sender.sendMessage(plugin.getFormattedMessage("messages.scanning_offline_players", null));
3737
plugin.checkAll(sender);
3838
}
3939
}
4040

41+
4142
return true;
4243
}
4344
}

src/main/java/org/cubexmc/ecobalancer/commands/CheckPlayerCommand.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ public boolean onCommand(CommandSender sender, Command command, String s, String
2424
if (sender instanceof Player) {
2525
Player player = (Player) sender;
2626
if (!player.hasPermission("ecobalancer.admin")) {
27-
player.sendMessage(ChatColor.RED + "你没有权限使用这个命令。");
27+
player.sendMessage(plugin.getFormattedMessage("messages.no_permission", null));
2828
return true;
2929
}
3030
if (args.length == 0) {
31-
player.sendMessage(ChatColor.GREEN + "请输入玩家名称或使用/checkall");
31+
player.sendMessage(plugin.getFormattedMessage("messages.enter_player_name_or_use_checkall", null));
3232
} else {
3333
checkPlayer(player, args[0]);
3434
}
3535
} else if (sender.hasPermission("ecobalancer.admin")) {
3636
if (args.length == 0) {
37-
sender.sendMessage(ChatColor.GREEN + "请输入玩家名称或使用/checkall");
37+
sender.sendMessage(plugin.getFormattedMessage("messages.enter_player_name_or_use_checkall", null));
3838
} else {
3939
checkPlayer(sender, args[0]);
4040
}

src/main/java/org/cubexmc/ecobalancer/commands/UtilCommand.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import org.bukkit.command.CommandSender;
88
import org.cubexmc.ecobalancer.EcoBalancer;
99

10+
import java.util.HashMap;
11+
import java.util.Map;
12+
1013
public class UtilCommand implements CommandExecutor {
1114
EcoBalancer plugin;
1215

@@ -15,19 +18,23 @@ public UtilCommand(EcoBalancer plugin) {
1518
}
1619
@Override
1720
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
21+
1822
if (args.length > 0 && args[0].equalsIgnoreCase("reload")) {
1923
Bukkit.getScheduler().cancelTasks(plugin);
2024
plugin.reloadConfig();
2125
plugin.loadConfiguration(); // You should create this method
22-
sender.sendMessage(ChatColor.GREEN + "EcoBalancer 重载成功");
26+
sender.sendMessage(plugin.getFormattedMessage("messages.reload_success", null));
2327
return true;
2428
} else if (args.length > 0 && args[0].equalsIgnoreCase("help")) {
25-
sender.sendMessage(ChatColor.WHITE + "" + ChatColor.BOLD + "EcoBalancer 帮助:");
26-
sender.sendMessage(ChatColor.YELLOW + "/ecobal help " + ChatColor.WHITE + "- 帮助");
27-
sender.sendMessage(ChatColor.YELLOW + "/checkall " + ChatColor.WHITE + "- 检查并清洗全部玩家余额");
28-
sender.sendMessage(ChatColor.YELLOW + "/checkplayer <player> " + ChatColor.WHITE + "- 检查并清洗单个玩家余额");
29-
sender.sendMessage(ChatColor.YELLOW + "/ecobal reload " + ChatColor.WHITE + "- 重载配置文件");
30-
sender.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "来自CubeX统治阶级工具包");
29+
sender.sendMessage(plugin.getFormattedMessage("messages.help_header", null));
30+
String[] commandMessages = {
31+
plugin.getFormattedMessage("messages.commands.help", null),
32+
plugin.getFormattedMessage("messages.commands.checkall", null),
33+
plugin.getFormattedMessage("messages.commands.checkplayer", null),
34+
plugin.getFormattedMessage("messages.commands.reload", null),
35+
plugin.getFormattedMessage("messages.help_footer", null)
36+
};
37+
sender.sendMessage(String.join("\n", commandMessages));
3138
return true;
3239
}
3340
return false;

0 commit comments

Comments
 (0)