Add permission to BanCommand, rework its targeting

This commit is contained in:
AnimeGitB 2022-06-25 16:12:23 +09:30 committed by Melledy
parent 1ba415c66a
commit a3b4a11863
8 changed files with 57 additions and 105 deletions

View File

@ -2,36 +2,26 @@ package emu.grasscutter.command.commands;
import java.util.List; import java.util.List;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account; import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.game.GameSession;
import static emu.grasscutter.utils.Language.translate;
@Command( @Command(
label = "ban", label = "ban",
usage = "ban <player> [time] [reason]", usage = "ban <@player> [time] [reason]",
description = "commands.ban.description", description = "commands.ban.description",
targetRequirement = Command.TargetRequirement.NONE permission = "server.ban",
targetRequirement = Command.TargetRequirement.PLAYER
) )
public final class BanCommand implements CommandHandler { public final class BanCommand implements CommandHandler {
private boolean banAccount(int uid, int time, String reason) { private boolean banAccount(Player targetPlayer, int time, String reason) {
Player player = Grasscutter.getGameServer().getPlayerByUid(uid, true); Account account = targetPlayer.getAccount();
if (player == null) {
return false;
}
Account account = player.getAccount();
if (account == null) { if (account == null) {
account = DatabaseHelper.getAccountByPlayerId(uid); return false;
if (account == null) {
return false;
}
} }
account.setBanReason(reason); account.setBanReason(reason);
@ -40,51 +30,36 @@ public final class BanCommand implements CommandHandler {
account.setBanned(true); account.setBanned(true);
account.save(); account.save();
Player banUser = Grasscutter.getGameServer().getPlayerByUid(uid); GameSession session = targetPlayer.getSession();
if (session != null) {
if (banUser != null) { session.close();
banUser.getSession().close();
} }
return true; return true;
} }
@Override @Override
public void execute(Player sender, Player targetPlayer, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
CommandHandler.sendMessage(sender, translate(sender, "commands.ban.command_usage"));
return;
}
int uid = 0;
int time = 2051190000; int time = 2051190000;
String reason = "Reason not specified."; String reason = "Reason not specified.";
if (args.size() >= 1) { switch (args.size()) {
try { case 2:
uid = Integer.parseInt(args.get(0)); reason = args.get(1); // Fall-through
} catch (NumberFormatException ignored) { case 1:
CommandHandler.sendMessage(sender, translate(sender, "commands.ban.invalid_player_id")); try {
return; 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) { if (banAccount(targetPlayer, time, reason)) {
try { CommandHandler.sendTranslatedMessage(sender, "commands.ban.success");
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"));
} else { } else {
CommandHandler.sendMessage(sender, translate(sender, "commands.ban.failure")); CommandHandler.sendTranslatedMessage(sender, "commands.ban.failure");
} }
} }
} }

View File

@ -2,37 +2,25 @@ package emu.grasscutter.command.commands;
import java.util.List; import java.util.List;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account; import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import static emu.grasscutter.utils.Language.translate;
@Command( @Command(
label = "unban", label = "unban",
usage = "unban <player>", usage = "unban <@player>",
description = "commands.unban.description", description = "commands.unban.description",
targetRequirement = Command.TargetRequirement.NONE permission = "server.ban",
targetRequirement = Command.TargetRequirement.PLAYER
) )
public final class UnBanCommand implements CommandHandler { public final class UnBanCommand implements CommandHandler {
private boolean unBanAccount(int uid) { private boolean unBanAccount(Player targetPlayer) {
Player player = Grasscutter.getGameServer().getPlayerByUid(uid, true); Account account = targetPlayer.getAccount();
if (player == null) {
return false;
}
Account account = player.getAccount();
if (account == null) { if (account == null) {
account = DatabaseHelper.getAccountByPlayerId(uid); return false;
if (account == null) {
return false;
}
} }
account.setBanReason(null); account.setBanReason(null);
@ -46,24 +34,10 @@ public final class UnBanCommand implements CommandHandler {
@Override @Override
public void execute(Player sender, Player targetPlayer, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) { if (unBanAccount(targetPlayer)) {
CommandHandler.sendMessage(sender, translate(sender, "commands.unban.command_usage")); CommandHandler.sendTranslatedMessage(sender, "commands.unban.success");
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"));
} else { } else {
CommandHandler.sendMessage(sender, translate(sender, "commands.unban.failure")); CommandHandler.sendTranslatedMessage(sender, "commands.unban.failure");
} }
} }
} }

View File

@ -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" "description": "Changes weather ID and climate type. Weather IDs can be found in WeatherExcelConfigData.json.\nClimate types: sunny, cloudy, rain, thunderstorm, snow, mist"
}, },
"ban": { "ban": {
"command_usage": "Usage: ban <playerId> [timestamp] [reason]", "command_usage": "Usage: ban <@playerId> [timestamp] [reason]",
"success": "Successful.", "success": "Successful.",
"failure": "Failed, player not found.", "failure": "Failed, player not found.",
"invalid_time": "Unable to parse timestamp.", "invalid_time": "Unable to parse timestamp.",
"invalid_player_id": "Unable to parse player ID.",
"description": "Ban a player" "description": "Ban a player"
}, },
"unban": { "unban": {
"command_usage": "Usage: unban <playerId>", "command_usage": "Usage: unban <@playerId>",
"success": "Successful.", "success": "Successful.",
"failure": "Failed, player not found.", "failure": "Failed, player not found.",
"invalid_player_id": "Unable to parse player ID.",
"description": "Unban a player" "description": "Unban a player"
} }
}, },

View File

@ -395,15 +395,13 @@
"success": "Succès.", "success": "Succès.",
"failure": "Échec, joueur introuvable.", "failure": "Échec, joueur introuvable.",
"invalid_time": "Impossible d'analyser le timestamp.", "invalid_time": "Impossible d'analyser le timestamp.",
"invalid_player_id": "Impossible d'analyser le player ID.", "command_usage": "Usage: ban <@playerId> [timestamp] [raison]"
"command_usage": "Usage: ban <playerId> [timestamp] [raison]"
}, },
"unban": { "unban": {
"description": "Retire le bannissement d'un joueur", "description": "Retire le bannissement d'un joueur",
"success": "Succès.", "success": "Succès.",
"failure": "Échec, joueur introuvable.", "failure": "Échec, joueur introuvable.",
"invalid_player_id": "Imossible d'analyser player ID.", "command_usage": "Usage: unban <@playerId>"
"command_usage": "Usage: unban <playerId>"
} }
}, },
"gacha": { "gacha": {

View File

@ -306,6 +306,19 @@
"unlocktower": { "unlocktower": {
"success": "odblokować gotowe", "success": "odblokować gotowe",
"description": "Odblokuj głęboką spiralę" "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": { "gacha": {

View File

@ -395,15 +395,13 @@
"success": "Успех.", "success": "Успех.",
"failure": "Неудача, игрок не найден.", "failure": "Неудача, игрок не найден.",
"invalid_time": "Не удалось определить промежуток времени.", "invalid_time": "Не удалось определить промежуток времени.",
"invalid_player_id": "Не удалось определить ID игрока.", "command_usage": "Применение: ban <@Id игрока> [промежуток_времени] [причина]"
"command_usage": "Применение: ban <Id игрока> [промежуток_времени] [причина]"
}, },
"unban": { "unban": {
"description": "Разбанивает игрока", "description": "Разбанивает игрока",
"success": "Успех.", "success": "Успех.",
"failure": "Неудача, игрок не найден.", "failure": "Неудача, игрок не найден.",
"invalid_player_id": "Не удалось определить ID игрока.", "command_usage": "Применение: unban <@Id_игрока>"
"command_usage": "Применение: unban <Id_игрока>"
} }
}, },
"gacha": { "gacha": {

View File

@ -390,18 +390,16 @@
"description": "更改天气ID和气候类型。天气ID可以在 WeatherExcelConfigData.json 中找到。\n气候类型sunny(晴天), cloudy(多云), rain(雨), thunderstorm(雷雨), snow(雪), mist(雾)" "description": "更改天气ID和气候类型。天气ID可以在 WeatherExcelConfigData.json 中找到。\n气候类型sunny(晴天), cloudy(多云), rain(雨), thunderstorm(雷雨), snow(雪), mist(雾)"
}, },
"ban": { "ban": {
"command_usage": "用法ban <玩家ID> [时间] [原因]", "command_usage": "用法ban <@玩家ID> [时间] [原因]",
"success": "成功封禁玩家。", "success": "成功封禁玩家。",
"failure": "封禁玩家失败,因为玩家不存在。", "failure": "封禁玩家失败,因为玩家不存在。",
"invalid_time": "无法解析时间戳。", "invalid_time": "无法解析时间戳。",
"invalid_player_id": "无法解析玩家ID。",
"description": "封禁玩家" "description": "封禁玩家"
}, },
"unban": { "unban": {
"command_usage": "用法unban <玩家ID>", "command_usage": "用法unban <@玩家ID>",
"success": "成功取消玩家的封禁。", "success": "成功取消玩家的封禁。",
"failure": "取消玩家的封禁失败,因为玩家不存在。", "failure": "取消玩家的封禁失败,因为玩家不存在。",
"invalid_player_id": "无法解析玩家ID。",
"description": "取消玩家的封禁" "description": "取消玩家的封禁"
} }
}, },

View File

@ -397,15 +397,13 @@
"success": "停權成功。", "success": "停權成功。",
"failure": "停權失敗,玩家帳號不存在。", "failure": "停權失敗,玩家帳號不存在。",
"invalid_time": "無效的時間戳。", "invalid_time": "無效的時間戳。",
"invalid_player_id": "無效的玩家ID。", "command_usage": "用法ban <@playerId> [timestamp] [reason]"
"command_usage": "用法ban <playerId> [timestamp] [reason]"
}, },
"unban": { "unban": {
"description": "撤銷停權指定玩家。", "description": "撤銷停權指定玩家。",
"success": "撤銷停權成功。", "success": "撤銷停權成功。",
"failure": "撤銷停權失敗,玩家帳號不存在。", "failure": "撤銷停權失敗,玩家帳號不存在。",
"invalid_player_id": "無效的玩家ID。", "command_usage": "用法unban <@playerId>"
"command_usage": "用法unban <playerId>"
} }
}, },
"gacha": { "gacha": {