Commit 104e04f7 authored by AnimeGitB's avatar AnimeGitB Committed by Luke H-W
Browse files

Change commands to use new usage message method.

parent 2cfbe781
...@@ -90,6 +90,7 @@ class JsonHelpers: ...@@ -90,6 +90,7 @@ class JsonHelpers:
class LanguageManager: class LanguageManager:
TRANSLATION_KEY = re.compile(r'[Tt]ranslate.*"(\w+\.[\w\.]+)"') TRANSLATION_KEY = re.compile(r'[Tt]ranslate.*"(\w+\.[\w\.]+)"')
POTENTIAL_KEY = re.compile(r'"(\w+\.[\w\.]+)"') POTENTIAL_KEY = re.compile(r'"(\w+\.[\w\.]+)"')
COMMAND_LABEL = re.compile(r'@Command\s*\([\W\w]*?label\s*=\s*"(\w+)"', re.MULTILINE) # [\W\w] is a cheeky way to match everything including \n
def __init__(self): def __init__(self):
self.load_jsons() self.load_jsons()
...@@ -122,6 +123,8 @@ class LanguageManager: ...@@ -122,6 +123,8 @@ class LanguageManager:
used.add(k) used.add(k)
for k in self.POTENTIAL_KEY.findall(data): for k in self.POTENTIAL_KEY.findall(data):
potential.add(k) potential.add(k)
for label in self.COMMAND_LABEL.findall(data):
used.add(f'commands.{label}.description')
return used | (potential & expected_keys) return used | (potential & expected_keys)
def _lint_report_language(self, lang: str, keys: set, flattened: dict, primary_language_flattened: dict) -> None: def _lint_report_language(self, lang: str, keys: set, flattened: dict, primary_language_flattened: dict) -> None:
......
...@@ -163,7 +163,7 @@ public final class DefaultAuthenticators { ...@@ -163,7 +163,7 @@ public final class DefaultAuthenticators {
} else { } else {
successfulLogin = false; successfulLogin = false;
loggerMessage = translate("messages.dispatch.account.login_password_storage_error", address); loggerMessage = translate("messages.dispatch.account.login_password_storage_error", address);
responseMessage = translate("password_storage_error"); responseMessage = translate("messages.dispatch.account.password_storage_error");
} }
} else { } else {
loggerMessage = translate("messages.dispatch.account.account_login_exist_error", address); loggerMessage = translate("messages.dispatch.account.account_login_exist_error", address);
......
...@@ -68,7 +68,6 @@ public interface CommandHandler { ...@@ -68,7 +68,6 @@ public interface CommandHandler {
default String getDescriptionString(Player player) { default String getDescriptionString(Player player) {
Command annotation = this.getClass().getAnnotation(Command.class); Command annotation = this.getClass().getAnnotation(Command.class);
String key = "commands.%s.description".formatted(annotation.label()); String key = "commands.%s.description".formatted(annotation.label());
// TODO: fallback to "commands.generic.no_description_specified"
return translate(player, key); return translate(player, key);
} }
......
...@@ -35,6 +35,7 @@ public final class CommandMap { ...@@ -35,6 +35,7 @@ public final class CommandMap {
*/ */
public CommandMap registerCommand(String label, CommandHandler command) { public CommandMap registerCommand(String label, CommandHandler command) {
Grasscutter.getLogger().debug("Registered command: " + label); Grasscutter.getLogger().debug("Registered command: " + label);
label = label.toLowerCase();
// Get command data. // Get command data.
Command annotation = command.getClass().getAnnotation(Command.class); Command annotation = command.getClass().getAnnotation(Command.class);
...@@ -203,7 +204,7 @@ public final class CommandMap { ...@@ -203,7 +204,7 @@ public final class CommandMap {
// Parse message. // Parse message.
String[] split = rawMessage.split(" "); String[] split = rawMessage.split(" ");
List<String> args = new LinkedList<>(Arrays.asList(split)); List<String> args = new LinkedList<>(Arrays.asList(split));
String label = args.remove(0); String label = args.remove(0).toLowerCase();
String playerId = (player == null) ? consoleId : player.getAccount().getId(); String playerId = (player == null) ? consoleId : player.getAccount().getId();
// Check for special cases - currently only target command. // Check for special cases - currently only target command.
......
...@@ -30,7 +30,7 @@ public final class AccountCommand implements CommandHandler { ...@@ -30,7 +30,7 @@ public final class AccountCommand implements CommandHandler {
} }
if (args.size() < 2) { if (args.size() < 2) {
CommandHandler.sendTranslatedMessage(sender, "commands.account.command_usage"); sendUsageMessage(sender);
return; return;
} }
...@@ -39,7 +39,7 @@ public final class AccountCommand implements CommandHandler { ...@@ -39,7 +39,7 @@ public final class AccountCommand implements CommandHandler {
switch (action) { switch (action) {
default: default:
CommandHandler.sendTranslatedMessage(sender, "commands.account.command_usage"); sendUsageMessage(sender);
return; return;
case "create": case "create":
int uid = 0; int uid = 0;
......
...@@ -23,14 +23,14 @@ public final class AnnounceCommand implements CommandHandler { ...@@ -23,14 +23,14 @@ public final class AnnounceCommand implements CommandHandler {
public void execute(Player sender, Player targetPlayer, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
var manager = Grasscutter.getGameServer().getAnnouncementSystem(); var manager = Grasscutter.getGameServer().getAnnouncementSystem();
if (args.size() < 1) { if (args.size() < 1) {
CommandHandler.sendTranslatedMessage(sender, "commands.announce.command_usage"); sendUsageMessage(sender);
return; return;
} }
switch (args.get(0)) { switch (args.get(0)) {
case "tpl": case "tpl":
if (args.size() < 2) { if (args.size() < 2) {
CommandHandler.sendTranslatedMessage(sender, "commands.announce.command_usage"); sendUsageMessage(sender);
return; return;
} }
...@@ -52,7 +52,7 @@ public final class AnnounceCommand implements CommandHandler { ...@@ -52,7 +52,7 @@ public final class AnnounceCommand implements CommandHandler {
case "revoke": case "revoke":
if (args.size() < 2) { if (args.size() < 2) {
CommandHandler.sendTranslatedMessage(sender, "commands.announce.command_usage"); sendUsageMessage(sender);
return; return;
} }
......
...@@ -83,7 +83,7 @@ public final class ClearCommand implements CommandHandler { ...@@ -83,7 +83,7 @@ public final class ClearCommand implements CommandHandler {
} }
if (args.size() < 1) { if (args.size() < 1) {
CommandHandler.sendTranslatedMessage(sender, "commands.clear.command_usage"); sendUsageMessage(sender);
return; return;
} }
......
...@@ -7,8 +7,6 @@ import emu.grasscutter.game.player.Player; ...@@ -7,8 +7,6 @@ import emu.grasscutter.game.player.Player;
import java.util.List; import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "coop", usage = {"[<host UID>]"}, permission = "server.coop", permissionTargeted = "server.coop.others") @Command(label = "coop", usage = {"[<host UID>]"}, permission = "server.coop", permissionTargeted = "server.coop.others")
public final class CoopCommand implements CommandHandler { public final class CoopCommand implements CommandHandler {
...@@ -17,25 +15,26 @@ public final class CoopCommand implements CommandHandler { ...@@ -17,25 +15,26 @@ public final class CoopCommand implements CommandHandler {
Player host = sender; Player host = sender;
switch (args.size()) { switch (args.size()) {
case 0: // Summon target to self case 0: // Summon target to self
CommandHandler.sendMessage(sender, translate(sender, "commands.coop.usage")); if (sender == null) { // Console doesn't have a self to summon to
if (sender == null) // Console doesn't have a self to summon to sendUsageMessage(sender);
return; return;
}
break; break;
case 1: // Summon target to argument case 1: // Summon target to argument
try { try {
int hostId = Integer.parseInt(args.get(0)); int hostId = Integer.parseInt(args.get(0));
host = Grasscutter.getGameServer().getPlayerByUid(hostId); host = Grasscutter.getGameServer().getPlayerByUid(hostId);
if (host == null) { if (host == null) {
CommandHandler.sendMessage(sender, translate(sender, "commands.execution.player_offline_error")); CommandHandler.sendTranslatedMessage(sender, "commands.execution.player_offline_error");
return; return;
} }
break; break;
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, translate(sender, "commands.generic.invalid.uid")); CommandHandler.sendTranslatedMessage(sender, "commands.generic.invalid.uid");
return; return;
} }
default: default:
CommandHandler.sendMessage(sender, translate(sender, "commands.coop.usage")); sendUsageMessage(sender);
return; return;
} }
...@@ -45,6 +44,6 @@ public final class CoopCommand implements CommandHandler { ...@@ -45,6 +44,6 @@ public final class CoopCommand implements CommandHandler {
} }
host.getServer().getMultiplayerSystem().applyEnterMp(targetPlayer, host.getUid()); host.getServer().getMultiplayerSystem().applyEnterMp(targetPlayer, host.getUid());
targetPlayer.getServer().getMultiplayerSystem().applyEnterMpReply(host, targetPlayer.getUid(), true); targetPlayer.getServer().getMultiplayerSystem().applyEnterMpReply(host, targetPlayer.getUid(), true);
CommandHandler.sendMessage(sender, translate(sender, "commands.coop.success", targetPlayer.getNickname(), host.getNickname())); CommandHandler.sendTranslatedMessage(sender, "commands.coop.success", targetPlayer.getNickname(), host.getNickname());
} }
} }
...@@ -14,7 +14,7 @@ public final class EnterDungeonCommand implements CommandHandler { ...@@ -14,7 +14,7 @@ public final class EnterDungeonCommand 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 (args.size() < 1) {
CommandHandler.sendMessage(sender, translate(sender, "commands.enter_dungeon.usage")); sendUsageMessage(sender);
return; return;
} }
...@@ -33,7 +33,7 @@ public final class EnterDungeonCommand implements CommandHandler { ...@@ -33,7 +33,7 @@ public final class EnterDungeonCommand implements CommandHandler {
CommandHandler.sendMessage(sender, translate(sender, "commands.enter_dungeon.changed", dungeonId)); CommandHandler.sendMessage(sender, translate(sender, "commands.enter_dungeon.changed", dungeonId));
} }
} catch (Exception e) { } catch (Exception e) {
CommandHandler.sendMessage(sender, translate(sender, "commands.enter_dungeon.usage")); sendUsageMessage(sender);
} }
} }
} }
...@@ -67,7 +67,7 @@ public final class GiveCommand implements CommandHandler { ...@@ -67,7 +67,7 @@ public final class GiveCommand implements CommandHandler {
public GiveAllType giveAllType = GiveAllType.NONE; public GiveAllType giveAllType = GiveAllType.NONE;
}; };
private static GiveItemParameters parseArgs(Player sender, List<String> args) throws IllegalArgumentException { private GiveItemParameters parseArgs(Player sender, List<String> args) throws IllegalArgumentException {
GiveItemParameters param = new GiveItemParameters(); GiveItemParameters param = new GiveItemParameters();
// Extract any tagged arguments (e.g. "lv90", "x100", "r5") // Extract any tagged arguments (e.g. "lv90", "x100", "r5")
...@@ -99,7 +99,7 @@ public final class GiveCommand implements CommandHandler { ...@@ -99,7 +99,7 @@ public final class GiveCommand implements CommandHandler {
// At this point, first remaining argument MUST be itemId/avatarId // At this point, first remaining argument MUST be itemId/avatarId
if (args.size() < 1) { if (args.size() < 1) {
CommandHandler.sendTranslatedMessage(sender, "commands.give.usage"); // Reachable if someone does `/give lv90` or similar sendUsageMessage(sender); // Reachable if someone does `/give lv90` or similar
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
String id = args.remove(0); String id = args.remove(0);
...@@ -169,7 +169,7 @@ public final class GiveCommand implements CommandHandler { ...@@ -169,7 +169,7 @@ public final class GiveCommand implements CommandHandler {
throw e; throw e;
} }
} else { } else {
CommandHandler.sendTranslatedMessage(sender, "commands.give.usage"); sendUsageMessage(sender);
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
} }
...@@ -180,7 +180,7 @@ public final class GiveCommand implements CommandHandler { ...@@ -180,7 +180,7 @@ public final class GiveCommand 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) { // *No args* if (args.size() < 1) { // *No args*
CommandHandler.sendTranslatedMessage(sender, "commands.give.usage"); sendUsageMessage(sender);
return; return;
} }
try { try {
......
...@@ -59,8 +59,9 @@ public final class HelpCommand implements CommandHandler { ...@@ -59,8 +59,9 @@ public final class HelpCommand implements CommandHandler {
commands_no_permission.add(createCommand(player, command, args)); commands_no_permission.add(createCommand(player, command, args));
} }
} }
CommandHandler.sendTranslatedMessage(player, "commands.help.available_commands");
} else { } else {
String command_str = args.remove(0); String command_str = args.remove(0).toLowerCase();
CommandHandler command = handlers.get(command_str); CommandHandler command = handlers.get(command_str);
if (command == null) { if (command == null) {
CommandHandler.sendTranslatedMessage(player, "commands.generic.command_exist_error"); CommandHandler.sendTranslatedMessage(player, "commands.generic.command_exist_error");
......
...@@ -25,7 +25,7 @@ public final class KillAllCommand implements CommandHandler { ...@@ -25,7 +25,7 @@ public final class KillAllCommand implements CommandHandler {
scene = targetPlayer.getWorld().getSceneById(Integer.parseInt(args.get(0))); scene = targetPlayer.getWorld().getSceneById(Integer.parseInt(args.get(0)));
break; break;
default: default:
CommandHandler.sendMessage(sender, translate(sender, "commands.killall.usage")); sendUsageMessage(sender);
return; return;
} }
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
......
...@@ -18,11 +18,6 @@ public final class KillCharacterCommand implements CommandHandler { ...@@ -18,11 +18,6 @@ public final class KillCharacterCommand 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.isEmpty()) {
CommandHandler.sendMessage(sender, translate(sender, "commands.killCharacter.usage"));
return;
}
EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity(); EntityAvatar entity = targetPlayer.getTeamManager().getCurrentAvatarEntity();
entity.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 0f); entity.setFightProperty(FightProperty.FIGHT_PROP_CUR_HP, 0f);
// Packets // Packets
......
...@@ -17,7 +17,7 @@ public final class PermissionCommand implements CommandHandler { ...@@ -17,7 +17,7 @@ public final class PermissionCommand 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() != 2) { if (args.size() != 2) {
CommandHandler.sendMessage(sender, translate(sender, "commands.permission.usage")); sendUsageMessage(sender);
return; return;
} }
...@@ -37,7 +37,7 @@ public final class PermissionCommand implements CommandHandler { ...@@ -37,7 +37,7 @@ public final class PermissionCommand implements CommandHandler {
switch (action) { switch (action) {
default: default:
CommandHandler.sendMessage(sender, translate(sender, "commands.permission.usage")); sendUsageMessage(sender);
break; break;
case "add": case "add":
if (account.addPermission(permission)) { if (account.addPermission(permission)) {
......
...@@ -15,7 +15,7 @@ public final class QuestCommand implements CommandHandler { ...@@ -15,7 +15,7 @@ public final class QuestCommand 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() != 2) { if (args.size() != 2) {
CommandHandler.sendMessage(sender, translate(sender, "commands.quest.usage")); sendUsageMessage(sender);
return; return;
} }
...@@ -53,7 +53,7 @@ public final class QuestCommand implements CommandHandler { ...@@ -53,7 +53,7 @@ public final class QuestCommand implements CommandHandler {
CommandHandler.sendMessage(sender, translate(sender, "commands.quest.finished", questId)); CommandHandler.sendMessage(sender, translate(sender, "commands.quest.finished", questId));
} }
default -> { default -> {
CommandHandler.sendMessage(sender, translate(sender, "commands.quest.usage")); sendUsageMessage(sender);
} }
} }
} }
......
...@@ -14,11 +14,6 @@ public final class ResetShopLimitCommand implements CommandHandler { ...@@ -14,11 +14,6 @@ public final class ResetShopLimitCommand 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.isEmpty()) {
CommandHandler.sendMessage(sender, translate(sender, "commands.resetShopLimit.usage"));
return;
}
targetPlayer.getShopLimit().forEach(x -> x.setNextRefreshTime(0)); targetPlayer.getShopLimit().forEach(x -> x.setNextRefreshTime(0));
targetPlayer.save(); targetPlayer.save();
CommandHandler.sendMessage(sender, translate(sender, "commands.resetShopLimit.success")); CommandHandler.sendMessage(sender, translate(sender, "commands.resetShopLimit.success"));
......
...@@ -43,7 +43,7 @@ public final class SendMailCommand implements CommandHandler { ...@@ -43,7 +43,7 @@ public final class SendMailCommand implements CommandHandler {
MailBuilder mailBuilder; MailBuilder mailBuilder;
switch (args.get(0).toLowerCase()) { switch (args.get(0).toLowerCase()) {
case "help" -> { case "help" -> {
CommandHandler.sendMessage(sender, translate(sender, "commands.sendMail.usage")); sendUsageMessage(sender);
return; return;
} }
case "all" -> mailBuilder = new MailBuilder(true, new Mail()); case "all" -> mailBuilder = new MailBuilder(true, new Mail());
...@@ -150,7 +150,7 @@ public final class SendMailCommand implements CommandHandler { ...@@ -150,7 +150,7 @@ public final class SendMailCommand implements CommandHandler {
} }
break; break;
default: // *No args* default: // *No args*
CommandHandler.sendMessage(sender, translate(sender, "commands.give.usage")); CommandHandler.sendTranslatedMessage(sender, "commands.sendMail.give_usage");
return; return;
} }
mailBuilder.mail.itemList.add(new Mail.MailItem(item, amount, lvl)); mailBuilder.mail.itemList.add(new Mail.MailItem(item, amount, lvl));
......
...@@ -8,8 +8,6 @@ import emu.grasscutter.game.player.Player; ...@@ -8,8 +8,6 @@ import emu.grasscutter.game.player.Player;
import java.util.List; import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command( @Command(
label = "sendMessage", label = "sendMessage",
aliases = {"say", "sendservmsg", "sendservermessage", "b", "broadcast"}, aliases = {"say", "sendservmsg", "sendservermessage", "b", "broadcast"},
...@@ -22,7 +20,7 @@ public final class SendMessageCommand implements CommandHandler { ...@@ -22,7 +20,7 @@ public final class SendMessageCommand 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() == 0) { if (args.size() == 0) {
CommandHandler.sendMessage(sender, translate(sender, "commands.sendMessage.usage")); sendUsageMessage(sender);
return; return;
} }
...@@ -35,6 +33,6 @@ public final class SendMessageCommand implements CommandHandler { ...@@ -35,6 +33,6 @@ public final class SendMessageCommand implements CommandHandler {
} else { } else {
CommandHandler.sendMessage(targetPlayer, message); CommandHandler.sendMessage(targetPlayer, message);
} }
CommandHandler.sendMessage(sender, translate(sender, "commands.sendMessage.success")); CommandHandler.sendTranslatedMessage(sender, "commands.sendMessage.success");
} }
} }
...@@ -22,7 +22,7 @@ public final class SetFetterLevelCommand implements CommandHandler { ...@@ -22,7 +22,7 @@ public final class SetFetterLevelCommand 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 (args.size() != 1) {
CommandHandler.sendMessage(sender, translate(sender, "commands.setFetterLevel.usage")); sendUsageMessage(sender);
return; return;
} }
......
...@@ -10,7 +10,7 @@ import emu.grasscutter.game.player.Player; ...@@ -10,7 +10,7 @@ import emu.grasscutter.game.player.Player;
import emu.grasscutter.game.props.PlayerProperty; import emu.grasscutter.game.props.PlayerProperty;
import emu.grasscutter.game.tower.TowerLevelRecord; import emu.grasscutter.game.tower.TowerLevelRecord;
@Command(label = "setProp", aliases = {"prop"}, usage = {"<prop> <value>"}, permission = "player.setprop", permissionTargeted = "player.setprop.others") @Command(label = "setProp", aliases = {"prop"}, usage = {"<prop> <value>"}, permission = "player.setprop", permissionTargeted = "player.setprop.others")
public final class SetPropCommand implements CommandHandler { public final class SetPropCommand implements CommandHandler {
static enum PseudoProp { static enum PseudoProp {
NONE, NONE,
...@@ -95,7 +95,7 @@ public final class SetPropCommand implements CommandHandler { ...@@ -95,7 +95,7 @@ public final class SetPropCommand 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() != 2) { if (args.size() != 2) {
CommandHandler.sendTranslatedMessage(sender, "commands.setProp.usage"); sendUsageMessage(sender);
return; return;
} }
String propStr = args.get(0).toLowerCase(); String propStr = args.get(0).toLowerCase();
...@@ -103,7 +103,7 @@ public final class SetPropCommand implements CommandHandler { ...@@ -103,7 +103,7 @@ public final class SetPropCommand implements CommandHandler {
int value; int value;
if (!props.containsKey(propStr)) { if (!props.containsKey(propStr)) {
CommandHandler.sendTranslatedMessage(sender, "commands.setProp.usage"); sendUsageMessage(sender);
return; return;
} }
try { try {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment