From a3b4a11863eaa5464fad7535a9266c08419c79fd Mon Sep 17 00:00:00 2001 From: AnimeGitB Date: Sat, 25 Jun 2022 16:12:23 +0930 Subject: [PATCH] Add permission to BanCommand, rework its targeting --- .../command/commands/BanCommand.java | 75 +++++++------------ .../command/commands/UnBanCommand.java | 44 +++-------- src/main/resources/languages/en-US.json | 6 +- src/main/resources/languages/fr-FR.json | 6 +- src/main/resources/languages/pl-PL.json | 13 ++++ src/main/resources/languages/ru-RU.json | 6 +- src/main/resources/languages/zh-CN.json | 6 +- src/main/resources/languages/zh-TW.json | 6 +- 8 files changed, 57 insertions(+), 105 deletions(-) diff --git a/src/main/java/emu/grasscutter/command/commands/BanCommand.java b/src/main/java/emu/grasscutter/command/commands/BanCommand.java index da3615b7d..6b4dd9766 100644 --- a/src/main/java/emu/grasscutter/command/commands/BanCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/BanCommand.java @@ -2,36 +2,26 @@ package emu.grasscutter.command.commands; import java.util.List; -import emu.grasscutter.Grasscutter; import emu.grasscutter.command.Command; import emu.grasscutter.command.CommandHandler; -import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.game.Account; import emu.grasscutter.game.player.Player; - -import static emu.grasscutter.utils.Language.translate; +import emu.grasscutter.server.game.GameSession; @Command( label = "ban", - usage = "ban [time] [reason]", + usage = "ban <@player> [time] [reason]", description = "commands.ban.description", - targetRequirement = Command.TargetRequirement.NONE + permission = "server.ban", + targetRequirement = Command.TargetRequirement.PLAYER ) public final class BanCommand implements CommandHandler { - private boolean banAccount(int uid, int time, String reason) { - Player player = Grasscutter.getGameServer().getPlayerByUid(uid, true); + private boolean banAccount(Player targetPlayer, int time, String reason) { + Account account = targetPlayer.getAccount(); - if (player == null) { - return false; - } - - Account account = player.getAccount(); if (account == null) { - account = DatabaseHelper.getAccountByPlayerId(uid); - if (account == null) { - return false; - } + return false; } account.setBanReason(reason); @@ -40,51 +30,36 @@ public final class BanCommand implements CommandHandler { account.setBanned(true); account.save(); - Player banUser = Grasscutter.getGameServer().getPlayerByUid(uid); - - if (banUser != null) { - banUser.getSession().close(); + GameSession session = targetPlayer.getSession(); + if (session != null) { + session.close(); } return true; } @Override public void execute(Player sender, Player targetPlayer, List args) { - if (args.size() < 1) { - CommandHandler.sendMessage(sender, translate(sender, "commands.ban.command_usage")); - return; - } - - int uid = 0; int time = 2051190000; String reason = "Reason not specified."; - if (args.size() >= 1) { - try { - uid = Integer.parseInt(args.get(0)); - } catch (NumberFormatException ignored) { - CommandHandler.sendMessage(sender, translate(sender, "commands.ban.invalid_player_id")); - return; - } + switch (args.size()) { + case 2: + reason = args.get(1); // Fall-through + case 1: + try { + time = Integer.parseInt(args.get(0)); + } catch (NumberFormatException ignored) { + CommandHandler.sendTranslatedMessage(sender, "commands.ban.invalid_time"); + return; + } // Fall-through, unimportant + default: + break; } - if (args.size() >= 2) { - try { - time = Integer.parseInt(args.get(1)); - } catch (NumberFormatException ignored) { - CommandHandler.sendMessage(sender, translate(sender, "commands.ban.invalid_time")); - return; - } - } - - if (args.size() >= 3) { - reason = args.get(2); - } - - if (banAccount(uid, time, reason)) { - CommandHandler.sendMessage(sender, translate(sender, "commands.ban.success")); + if (banAccount(targetPlayer, time, reason)) { + CommandHandler.sendTranslatedMessage(sender, "commands.ban.success"); } else { - CommandHandler.sendMessage(sender, translate(sender, "commands.ban.failure")); + CommandHandler.sendTranslatedMessage(sender, "commands.ban.failure"); } } } \ No newline at end of file diff --git a/src/main/java/emu/grasscutter/command/commands/UnBanCommand.java b/src/main/java/emu/grasscutter/command/commands/UnBanCommand.java index 0b3dc7d8d..3dafa4efc 100644 --- a/src/main/java/emu/grasscutter/command/commands/UnBanCommand.java +++ b/src/main/java/emu/grasscutter/command/commands/UnBanCommand.java @@ -2,37 +2,25 @@ package emu.grasscutter.command.commands; import java.util.List; -import emu.grasscutter.Grasscutter; import emu.grasscutter.command.Command; import emu.grasscutter.command.CommandHandler; -import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.game.Account; import emu.grasscutter.game.player.Player; -import static emu.grasscutter.utils.Language.translate; - @Command( label = "unban", - usage = "unban ", + usage = "unban <@player>", description = "commands.unban.description", - targetRequirement = Command.TargetRequirement.NONE + permission = "server.ban", + targetRequirement = Command.TargetRequirement.PLAYER ) public final class UnBanCommand implements CommandHandler { - private boolean unBanAccount(int uid) { - Player player = Grasscutter.getGameServer().getPlayerByUid(uid, true); - - if (player == null) { - return false; - } - - Account account = player.getAccount(); + private boolean unBanAccount(Player targetPlayer) { + Account account = targetPlayer.getAccount(); if (account == null) { - account = DatabaseHelper.getAccountByPlayerId(uid); - if (account == null) { - return false; - } + return false; } account.setBanReason(null); @@ -46,24 +34,10 @@ public final class UnBanCommand implements CommandHandler { @Override public void execute(Player sender, Player targetPlayer, List args) { - if (args.size() < 1) { - CommandHandler.sendMessage(sender, translate(sender, "commands.unban.command_usage")); - return; - } - - int uid = 0; - - try { - uid = Integer.parseInt(args.get(0)); - } catch (NumberFormatException ignored) { - CommandHandler.sendMessage(sender, translate(sender, "commands.unban.invalid_player_id")); - return; - } - - if (unBanAccount(uid)) { - CommandHandler.sendMessage(sender, translate(sender, "commands.unban.success")); + if (unBanAccount(targetPlayer)) { + CommandHandler.sendTranslatedMessage(sender, "commands.unban.success"); } else { - CommandHandler.sendMessage(sender, translate(sender, "commands.unban.failure")); + CommandHandler.sendTranslatedMessage(sender, "commands.unban.failure"); } } } \ No newline at end of file diff --git a/src/main/resources/languages/en-US.json b/src/main/resources/languages/en-US.json index a5a50ec8e..4c6b2e377 100644 --- a/src/main/resources/languages/en-US.json +++ b/src/main/resources/languages/en-US.json @@ -390,18 +390,16 @@ "description": "Changes weather ID and climate type. Weather IDs can be found in WeatherExcelConfigData.json.\nClimate types: sunny, cloudy, rain, thunderstorm, snow, mist" }, "ban": { - "command_usage": "Usage: ban [timestamp] [reason]", + "command_usage": "Usage: ban <@playerId> [timestamp] [reason]", "success": "Successful.", "failure": "Failed, player not found.", "invalid_time": "Unable to parse timestamp.", - "invalid_player_id": "Unable to parse player ID.", "description": "Ban a player" }, "unban": { - "command_usage": "Usage: unban ", + "command_usage": "Usage: unban <@playerId>", "success": "Successful.", "failure": "Failed, player not found.", - "invalid_player_id": "Unable to parse player ID.", "description": "Unban a player" } }, diff --git a/src/main/resources/languages/fr-FR.json b/src/main/resources/languages/fr-FR.json index 955bbe1b4..12f4c0072 100644 --- a/src/main/resources/languages/fr-FR.json +++ b/src/main/resources/languages/fr-FR.json @@ -395,15 +395,13 @@ "success": "Succès.", "failure": "Échec, joueur introuvable.", "invalid_time": "Impossible d'analyser le timestamp.", - "invalid_player_id": "Impossible d'analyser le player ID.", - "command_usage": "Usage: ban [timestamp] [raison]" + "command_usage": "Usage: ban <@playerId> [timestamp] [raison]" }, "unban": { "description": "Retire le bannissement d'un joueur", "success": "Succès.", "failure": "Échec, joueur introuvable.", - "invalid_player_id": "Imossible d'analyser player ID.", - "command_usage": "Usage: unban " + "command_usage": "Usage: unban <@playerId>" } }, "gacha": { diff --git a/src/main/resources/languages/pl-PL.json b/src/main/resources/languages/pl-PL.json index e2fcfbae8..b90755dcf 100644 --- a/src/main/resources/languages/pl-PL.json +++ b/src/main/resources/languages/pl-PL.json @@ -306,6 +306,19 @@ "unlocktower": { "success": "odblokować gotowe", "description": "Odblokuj głęboką spiralę" + }, + "ban": { + "command_usage": "Usage: ban <@playerId> [timestamp] [reason]", + "success": "Successful.", + "failure": "Failed, player not found.", + "invalid_time": "Unable to parse timestamp.", + "description": "Ban a player" + }, + "unban": { + "command_usage": "Usage: unban <@playerId>", + "success": "Successful.", + "failure": "Failed, player not found.", + "description": "Unban a player" } }, "gacha": { diff --git a/src/main/resources/languages/ru-RU.json b/src/main/resources/languages/ru-RU.json index eea174c89..f099396f2 100644 --- a/src/main/resources/languages/ru-RU.json +++ b/src/main/resources/languages/ru-RU.json @@ -395,15 +395,13 @@ "success": "Успех.", "failure": "Неудача, игрок не найден.", "invalid_time": "Не удалось определить промежуток времени.", - "invalid_player_id": "Не удалось определить ID игрока.", - "command_usage": "Применение: ban [промежуток_времени] [причина]" + "command_usage": "Применение: ban <@Id игрока> [промежуток_времени] [причина]" }, "unban": { "description": "Разбанивает игрока", "success": "Успех.", "failure": "Неудача, игрок не найден.", - "invalid_player_id": "Не удалось определить ID игрока.", - "command_usage": "Применение: unban " + "command_usage": "Применение: unban <@Id_игрока>" } }, "gacha": { diff --git a/src/main/resources/languages/zh-CN.json b/src/main/resources/languages/zh-CN.json index 608bd7c7f..2bfb93f56 100644 --- a/src/main/resources/languages/zh-CN.json +++ b/src/main/resources/languages/zh-CN.json @@ -390,18 +390,16 @@ "description": "更改天气ID和气候类型。天气ID可以在 WeatherExcelConfigData.json 中找到。\n气候类型:sunny(晴天), cloudy(多云), rain(雨), thunderstorm(雷雨), snow(雪), mist(雾)" }, "ban": { - "command_usage": "用法:ban <玩家ID> [时间] [原因]", + "command_usage": "用法:ban <@玩家ID> [时间] [原因]", "success": "成功封禁玩家。", "failure": "封禁玩家失败,因为玩家不存在。", "invalid_time": "无法解析时间戳。", - "invalid_player_id": "无法解析玩家ID。", "description": "封禁玩家" }, "unban": { - "command_usage": "用法:unban <玩家ID>", + "command_usage": "用法:unban <@玩家ID>", "success": "成功取消玩家的封禁。", "failure": "取消玩家的封禁失败,因为玩家不存在。", - "invalid_player_id": "无法解析玩家ID。", "description": "取消玩家的封禁" } }, diff --git a/src/main/resources/languages/zh-TW.json b/src/main/resources/languages/zh-TW.json index f834d1669..7b86c09b9 100644 --- a/src/main/resources/languages/zh-TW.json +++ b/src/main/resources/languages/zh-TW.json @@ -397,15 +397,13 @@ "success": "停權成功。", "failure": "停權失敗,玩家帳號不存在。", "invalid_time": "無效的時間戳。", - "invalid_player_id": "無效的玩家ID。", - "command_usage": "用法:ban [timestamp] [reason]" + "command_usage": "用法:ban <@playerId> [timestamp] [reason]" }, "unban": { "description": "撤銷停權指定玩家。", "success": "撤銷停權成功。", "failure": "撤銷停權失敗,玩家帳號不存在。", - "invalid_player_id": "無效的玩家ID。", - "command_usage": "用法:unban " + "command_usage": "用法:unban <@playerId>" } }, "gacha": {