Skip to content

Commit

Permalink
yue
Browse files Browse the repository at this point in the history
  • Loading branch information
angushushu committed Nov 12, 2023
1 parent d9480b4 commit 265f37d
Show file tree
Hide file tree
Showing 35 changed files with 1,181 additions and 64 deletions.
6 changes: 6 additions & 0 deletions .idea/artifacts/EcoBalancer.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 81 additions & 25 deletions src/main/java/org/cubexmc/ecobalancer/EcoBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.cubexmc.ecobalancer.commands.CheckAllCommand;
Expand All @@ -25,8 +27,8 @@
import java.util.logging.SimpleFormatter;

import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.cubexmc.ecobalancer.metrics.Metrics;

public final class EcoBalancer extends JavaPlugin {

Expand All @@ -42,6 +44,7 @@ public final class EcoBalancer extends JavaPlugin {
private String scheduleType;
private List<Integer> scheduleDaysOfWeek;
private List<Integer> scheduleDatesOfMonth;
private FileConfiguration langConfig;

@Override
public void onEnable() {
Expand Down Expand Up @@ -78,6 +81,13 @@ public void onEnable() {
e.printStackTrace();
}

// metrics
int pluginId = 20269; // <-- Replace with the id of your plugin!
Metrics metrics = new Metrics(this, pluginId);
// Optional: Add custom charts
metrics.addCustomChart(new Metrics.SimplePie("chart_id", () -> "My value"));

// getCommand("ecobalancer").setExecutor(new UtilCommand(this));
getCommand("ecobal").setExecutor(new UtilCommand(this));
getCommand("checkall").setExecutor(new CheckAllCommand(this));
getCommand("checkplayer").setExecutor(new CheckPlayerCommand(this));
Expand All @@ -87,7 +97,8 @@ public void onEnable() {
public void loadConfiguration() {
// Cancel all scheduled tasks
Bukkit.getScheduler().cancelTasks(this);

// load language config
loadLangFile();
// Load the new scheduling configuration
scheduleType = getConfig().getString("schedule.type", "daily");
scheduleDaysOfWeek = getConfig().getIntegerList("schedule.days-of-week");
Expand Down Expand Up @@ -120,6 +131,25 @@ public void loadConfiguration() {
}
}

private void loadLangFile() {
File langFile = new File(getDataFolder(), "lang.yml");
if (!langFile.exists()) {
saveResource("lang.yml", false);
}
langConfig = YamlConfiguration.loadConfiguration(langFile);
}

public String getFormattedMessage(String path, Map<String, String> placeholders) {
String message = langConfig.getString(path, "Message not found!");
if (placeholders != null) {
for (Map.Entry<String, String> entry : placeholders.entrySet()) {
message = message.replace("%" + entry.getKey() + "%", entry.getValue());
}
}

return ChatColor.translateAlternateColorCodes('&', message);
}

@Override
public void onDisable() {
// Ensure all pending logs are flushed and the handler is closed
Expand All @@ -140,16 +170,25 @@ public void onDisable() {

// Method to compress the existing log file
private void compressExistingLogFile(File logFile) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HHmm");
String timestamp = dateFormat.format(new Date(logFile.lastModified()));
File compressedFile = new File(logFile.getParent(), timestamp + ".gz");

File renamedLogFile = new File(logFile.getParent(), timestamp + ".log");
// Rename the file to include the timestamp
if (!logFile.renameTo(renamedLogFile)) {
getLogger().severe("Could not rename the log file.");
return;
}
// Compress the renamed log file into a .gz file
File compressedFile = new File(renamedLogFile.getParent(), renamedLogFile.getName() + ".gz");
try (GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(compressedFile))) {
Files.copy(logFile.toPath(), gzos);
Files.copy(renamedLogFile.toPath(), gzos);
} catch (IOException e) {
getLogger().severe("Could not compress the log file: " + e.getMessage());
}
logFile.delete(); // 删除原始日志文件
// Delete the original (now renamed) log file after it's compressed
if (!renamedLogFile.delete()) {
getLogger().severe("Could not delete the original log file after compression.");
}
}

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

Map<String, String> placeholders = new HashMap<>();
placeholders.put("player", player.getName());
placeholders.put("balance", String.format("%.2f", balance));
placeholders.put("days_offline", String.valueOf(daysOffline));


if (deductBasedOnTime) {
// 计算玩家离线天数
if (balance < 0) {
econ.depositPlayer(player, -1 * balance);
placeholders.put("new_balance", String.format("%.2f", econ.getBalance(player)));
if (sender != null) {
sender.sendMessage(ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " 的余额置为" + String.format("%.2f", balance) + "。");
sender.sendMessage(ChatColor.GREEN + "已将余额置为" + String.format("%.2f", econ.getBalance(player)) + "。");
sender.sendMessage(getFormattedMessage("messages.set_balance.reason", placeholders));
sender.sendMessage(getFormattedMessage("messages.set_balance.balance_set", placeholders));
}
if (log)
fileLogger.info("[负额度] 已将 " + player.getName() + " 的余额置为" + String.format("%.2f", econ.getBalance(player)) + "。");
fileLogger.info(getFormattedMessage("logs.negative_balance", placeholders));
} else {
if (daysOffline > inactiveDaysToClear) {
// 清除超过inactiveDaysToClear天未上线的玩家
econ.withdrawPlayer(player, balance);
placeholders.put("new_balance", String.format("%.2f", econ.getBalance(player)));
if (sender != null) {
sender.sendMessage(ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " 已经离线" + daysOffline + "天,极不活跃。");
sender.sendMessage(ChatColor.GREEN + "已将余额置为" + String.format("%.2f", econ.getBalance(player)) + "。");
sender.sendMessage(getFormattedMessage("messages.offline_extreme.reason", placeholders));
sender.sendMessage(getFormattedMessage("messages.offline_extreme.balance_set", placeholders));
}
if (log)
fileLogger.info("[及不活跃] 已将 " + player.getName() + " 的余额置为" + String.format("%.2f", econ.getBalance(player)) + "。");
fileLogger.info(getFormattedMessage("logs.inactive_extreme", placeholders));
} else if (daysOffline > inactiveDaysToDeduct) {
// 对于超过50天未上线的玩家,按税率扣除
double deduction = balance * deductionRate;
placeholders.put("deduction", String.format("%.2f", deduction));
econ.withdrawPlayer(player, deduction);
if (sender != null) {
sender.sendMessage(ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " 已经离线" + daysOffline + "天,较不活跃。");
sender.sendMessage(ChatColor.GREEN + "已从余额中扣除 " + ChatColor.YELLOW + String.format("%.2f", deduction) + ChatColor.GREEN + "。");
sender.sendMessage(getFormattedMessage("messages.offline_moderate.reason", placeholders));
sender.sendMessage(getFormattedMessage("messages.offline_moderate.deduction_made", placeholders));
}
if (log)
fileLogger.info("[较不活跃] 已从 " + player.getName() + " 的余额中扣除 " + String.format("%.2f", deduction) + "。");
fileLogger.info(getFormattedMessage("logs.inactive_moderate", placeholders));
} else {
if (sender != null) {
sender.sendMessage(ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " 仅离线" + daysOffline + "天,较活跃。");
sender.sendMessage(getFormattedMessage("messages.offline_active", placeholders));
}
}
}
} else {
double deduction = balance * deductionRate;
placeholders.put("deduction", String.format("%.2f", deduction));
econ.withdrawPlayer(player, deduction);
if (sender != null)
sender.sendMessage(ChatColor.GREEN + "已从 " + ChatColor.YELLOW + player.getName() + ChatColor.GREEN + " 的余额中扣除 " + ChatColor.YELLOW + String.format("%.2f", deduction) + ChatColor.GREEN + "。");
sender.sendMessage(getFormattedMessage("messages.deduction_made", placeholders));
if (log)
fileLogger.info("已从 " + player.getName() + " 的余额中扣除 " + String.format("%.2f", deduction) + "。");
fileLogger.info(getFormattedMessage("logs.deduction_made", placeholders));
}
}

Expand Down Expand Up @@ -346,11 +395,18 @@ public void run() {
checkBalance(null, currentTime, player, false);
}
index += batchSize;

Map<String, String> placeholders = new HashMap<>();
placeholders.put("start", Integer.toString(start));
placeholders.put("end", Integer.toString(end));
placeholders.put("batch", Integer.toString(end - start));
placeholders.put("total_players", Integer.toString(players.length));

if (sender != null) {
// Send a message to the sender after each batch
Bukkit.getScheduler().runTask(EcoBalancer.this, () -> sender.sendMessage(ChatColor.GRAY + "处理了" + (end - start) + "个玩家,共处理:" + end));
Bukkit.getScheduler().runTask(EcoBalancer.this, () -> sender.sendMessage(getFormattedMessage("messages.players_processing", placeholders)));
} else {
getLogger().info("处理了" + (end - start) + "个玩家,共处理:" + end);
getLogger().info(getFormattedMessage("logs.players_processed", placeholders));
}
if (index < players.length) {
// Schedule next batch
Expand All @@ -360,10 +416,10 @@ public void run() {
// Send a message to the sender after each batch
Bukkit.getScheduler().runTask(EcoBalancer.this, () -> {
if (sender != null) {
sender.sendMessage(ChatColor.GREEN + "所有玩家都已被洗劫。");
sender.sendMessage(getFormattedMessage("messages.all_players_processed", null));
} else {
getLogger().info("所有玩家都已被洗劫。");
fileLogger.info("处理了" + players.length + "个玩家,共处理:" + end);
getLogger().info(getFormattedMessage("logs.all_players_processed", null));
fileLogger.info(getFormattedMessage("logs.all_players_processed", null));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ public boolean onCommand(CommandSender sender, Command command, String s, String
if (sender instanceof Player) {
Player player = (Player) sender;
if (!player.hasPermission("ecobalancer.admin")) {
player.sendMessage(ChatColor.RED + "你没有权限使用这个命令。");
player.sendMessage(plugin.getFormattedMessage("messages.no_permission", null));
return true;
}
if (args.length == 0) {
player.sendMessage(ChatColor.GREEN + "正在扫描离线玩家...");
player.sendMessage(plugin.getFormattedMessage("messages.scanning_offline_players", null));
plugin.checkAll(sender);
}
} else if (sender.hasPermission("ecobalancer.admin")) {
if (args.length == 0) {
sender.sendMessage(ChatColor.GREEN + "正在扫描离线玩家...");
sender.sendMessage(plugin.getFormattedMessage("messages.scanning_offline_players", null));
plugin.checkAll(sender);
}
}


return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public boolean onCommand(CommandSender sender, Command command, String s, String
if (sender instanceof Player) {
Player player = (Player) sender;
if (!player.hasPermission("ecobalancer.admin")) {
player.sendMessage(ChatColor.RED + "你没有权限使用这个命令。");
player.sendMessage(plugin.getFormattedMessage("messages.no_permission", null));
return true;
}
if (args.length == 0) {
player.sendMessage(ChatColor.GREEN + "请输入玩家名称或使用/checkall");
player.sendMessage(plugin.getFormattedMessage("messages.enter_player_name_or_use_checkall", null));
} else {
checkPlayer(player, args[0]);
}
} else if (sender.hasPermission("ecobalancer.admin")) {
if (args.length == 0) {
sender.sendMessage(ChatColor.GREEN + "请输入玩家名称或使用/checkall");
sender.sendMessage(plugin.getFormattedMessage("messages.enter_player_name_or_use_checkall", null));
} else {
checkPlayer(sender, args[0]);
}
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/org/cubexmc/ecobalancer/commands/UtilCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import org.bukkit.command.CommandSender;
import org.cubexmc.ecobalancer.EcoBalancer;

import java.util.HashMap;
import java.util.Map;

public class UtilCommand implements CommandExecutor {
EcoBalancer plugin;

Expand All @@ -15,19 +18,23 @@ public UtilCommand(EcoBalancer plugin) {
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

if (args.length > 0 && args[0].equalsIgnoreCase("reload")) {
Bukkit.getScheduler().cancelTasks(plugin);
plugin.reloadConfig();
plugin.loadConfiguration(); // You should create this method
sender.sendMessage(ChatColor.GREEN + "EcoBalancer 重载成功");
sender.sendMessage(plugin.getFormattedMessage("messages.reload_success", null));
return true;
} else if (args.length > 0 && args[0].equalsIgnoreCase("help")) {
sender.sendMessage(ChatColor.WHITE + "" + ChatColor.BOLD + "EcoBalancer 帮助:");
sender.sendMessage(ChatColor.YELLOW + "/ecobal help " + ChatColor.WHITE + "- 帮助");
sender.sendMessage(ChatColor.YELLOW + "/checkall " + ChatColor.WHITE + "- 检查并清洗全部玩家余额");
sender.sendMessage(ChatColor.YELLOW + "/checkplayer <player> " + ChatColor.WHITE + "- 检查并清洗单个玩家余额");
sender.sendMessage(ChatColor.YELLOW + "/ecobal reload " + ChatColor.WHITE + "- 重载配置文件");
sender.sendMessage(ChatColor.GRAY + "" + ChatColor.ITALIC + "来自CubeX统治阶级工具包");
sender.sendMessage(plugin.getFormattedMessage("messages.help_header", null));
String[] commandMessages = {
plugin.getFormattedMessage("messages.commands.help", null),
plugin.getFormattedMessage("messages.commands.checkall", null),
plugin.getFormattedMessage("messages.commands.checkplayer", null),
plugin.getFormattedMessage("messages.commands.reload", null),
plugin.getFormattedMessage("messages.help_footer", null)
};
sender.sendMessage(String.join("\n", commandMessages));
return true;
}
return false;
Expand Down
Loading

0 comments on commit 265f37d

Please sign in to comment.