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