Skip to content

Commit

Permalink
feat: add dump command messages to messages.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia committed Jul 15, 2024
1 parent 0c29606 commit 075427a
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 58 deletions.
119 changes: 61 additions & 58 deletions core/src/main/java/com/rexcantor64/triton/commands/DebugCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,13 @@ public void handleCommand(CommandEvent event) throws NoPermissionException, Unsu
args = Arrays.copyOfRange(args, 1, args.length);
} else if (sender.getUUID() != null && event.getPlatform().isProxy()) {
if (args.length < 1) {
// TODO get from messages.yml
sendMessage(event, "You must provide a target platform: " + getSubcommandList(TargetPlatform.values()));
sendMessage(event, "debug.target-platform.missing", getSubcommandList(TargetPlatform.values()));
return;
}

val targetPlatform = getSubcommandFromName(TargetPlatform.values(), args[0]);
if (!targetPlatform.isPresent()) {
// TODO get from messages.yml
sendMessage(event, "Invalid target platform. Available: " + getSubcommandList(Subcommand.values()));
sendMessage(event, "debug.target-platform.invalid", args[0], getSubcommandList(TargetPlatform.values()));
return;
}

Expand All @@ -56,15 +54,13 @@ public void handleCommand(CommandEvent event) throws NoPermissionException, Unsu
event = event.toBuilder().args(args).build();

if (args.length < 1) {
// TODO get from messages.yml
sendMessage(event, "You must provide a subcommand: " + getSubcommandList(Subcommand.values()));
sendMessage(event, "debug.subcommand.missing", getSubcommandList(Subcommand.values()));
return;
}

val subcommand = getSubcommandFromName(Subcommand.values(), args[0]);
if (!subcommand.isPresent()) {
// TODO get from messages.yml
sendMessage(event, "Invalid subcommand. Available: " + getSubcommandList(Subcommand.values()));
sendMessage(event, "debug.subcommand.invalid", args[0], getSubcommandList(Subcommand.values()));
return;
}

Expand All @@ -84,77 +80,82 @@ public void handleDumpCommand(CommandEvent event) {
val args = event.getArgs();

if (args.length < 2) {
// TODO get from messages.yml
sendMessage(event, "You must provide a dump subcommand: " + getSubcommandList(DumpSubcommand.values()));
sendMessage(event, "debug.dump.action.missing", getSubcommandList(DumpAction.values()));
return;
}

val subcommand = getSubcommandFromName(DumpSubcommand.values(), args[1]);
if (!subcommand.isPresent()) {
// TODO get from messages.yml
sendMessage(event, "Invalid dump subcommand. Available: " + getSubcommandList(DumpSubcommand.values()));
val action = getSubcommandFromName(DumpAction.values(), args[1]);
if (!action.isPresent()) {
sendMessage(event, "debug.dump.action.invalid", args[1], getSubcommandList(DumpAction.values()));
return;
}

val dumpManager = Triton.get().getDumpManager();
switch (subcommand.get()) {
switch (action.get()) {
case ADD:
case REMOVE:
if (args.length < 3) {
// TODO get from messages.yml
sendMessage(event, "You must provide a player, 'me' or 'all'");
sendMessage(event, "debug.dump.player.missing");
return;
}
val playerStr = args[2];
List<FeatureSyntax> types = new ArrayList<>();
List<String> typeNames = new ArrayList<>();
if (args.length >= 4) {
for (int i = 3; i < args.length; ++i) {
val type = dumpManager.getAvailableTypes().get(args[i]);
val typeName = args[i].toLowerCase();
val type = dumpManager.getAvailableTypes().get(typeName);
if (type == null) {
// TODO get from messages.yml
sendMessage(event, "Type " + args[i] + " not found");
String allTypes = String.join(", ", dumpManager.getAvailableTypes().keySet());
sendMessage(event, "debug.dump.type.not-found", args[i], allTypes);
return;
}
types.add(type);
typeNames.add(typeName);
}
} else {
types.addAll(dumpManager.getAvailableTypes().values());
typeNames.addAll(dumpManager.getAvailableTypes().keySet());
}

val typeNamesStr = String.join(", ", typeNames);

if (playerStr.equalsIgnoreCase("all")) {
if (subcommand.get() == DumpSubcommand.ADD) {
if (action.get() == DumpAction.ADD) {
dumpManager.enableForEveryone(types);
sendMessage(event, "debug.dump.success.add.all", typeNamesStr);
} else {
dumpManager.disableForEveryone(types);
sendMessage(event, "debug.dump.success.remove.all", typeNamesStr);
}
} else {
UUID player;
if (playerStr.equalsIgnoreCase("me")) {
if (sender.getUUID() == null) {
sendMessage(event, "Only players can use 'me'");
sendMessage(event, "debug.dump.player.me-not-player");
return;
}
player = sender.getUUID();
} else {
val uuid = Triton.get().getPlayerUUIDFromString(playerStr);
if (uuid == null) {
// TODO get from messages.yml
sendMessage(event, "Can't find player " + playerStr);
sendMessage(event, "debug.dump.player.not-found", playerStr);
return;
}
player = uuid;
}
if (subcommand.get() == DumpSubcommand.ADD) {
if (action.get() == DumpAction.ADD) {
dumpManager.enableForPlayer(player, types);
sendMessage(event, "debug.dump.success.add.player", typeNamesStr, player);
} else {
dumpManager.disableForPlayer(player, types);
sendMessage(event, "debug.dump.success.remove.player", typeNamesStr, player);
}
}
// TODO get from messages.yml
sendMessage(event, "Success");
break;
case CLEAR:
dumpManager.disable();
sendMessage(event, "Disabled dumping for everyone");
sendMessage(event, "debug.dump.clear");
break;
}
}
Expand All @@ -164,8 +165,7 @@ public void handleLoadCommand(CommandEvent event) {
val args = event.getArgs();

if (args.length < 2) {
// TODO get from messages.yml
sendMessage(event, "You must provide a dump name");
sendMessage(event, "debug.load.dump-file.missing");
return;
}

Expand All @@ -174,18 +174,22 @@ public void handleLoadCommand(CommandEvent event) {
int startLine = 0;
int endLine = Integer.MAX_VALUE;

try {
if (args.length >= 3) {
if (args.length >= 3) {
try {
startLine = Integer.parseInt(args[2]);
endLine = startLine + 1;
} catch (NumberFormatException e) {
sendMessage(event, "debug.load.line-number.invalid", args[2]);
return;
}
if (args.length >= 4) {
}
if (args.length >= 4) {
try {
endLine = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sendMessage(event, "debug.load.line-number.invalid", args[3]);
return;
}
} catch (NumberFormatException e) {
// TODO get from messages.yml
sendMessage(event, "Invalid number");
return;
}

try {
Expand All @@ -195,15 +199,12 @@ public void handleLoadCommand(CommandEvent event) {
sender.sendMessage(message);
}

// TODO get from messages.yml
sendMessage(event, "Sent " + messages.size() + " messages!");
sendMessage(event, "debug.load.success.sent", messages.size());
} catch (IOException e) {
// TODO get from messages.yml
sendMessage(event, "Failed to open dump file: " + e.getMessage());
sendMessage(event, "debug.load.dump-file.not-found", dumpName, e.getMessage());
Triton.get().getLogger().logError(e, "Failed to open dump file");
} catch (JsonParseException e) {
// TODO get from messages.yml
sendMessage(event, "Invalid message JSON " + e.getMessage());
sendMessage(event, "debug.load.dump-file.invalid-json", e.getMessage());
Triton.get().getLogger().logError(e, "Failed to parse message JSON while loading dump");
}
}
Expand Down Expand Up @@ -238,9 +239,9 @@ public List<String> handleTabCompletion(CommandEvent event) throws NoPermissionE
switch (subcommand.get()) {
case DUMP:
if (args.length == 2) {
return autocompleteEnum(args[1], DumpSubcommand.values());
return autocompleteEnum(args[1], DumpAction.values());
}
val dumpSubcommand = getSubcommandFromName(DumpSubcommand.values(), args[1]);
val dumpSubcommand = getSubcommandFromName(DumpAction.values(), args[1]);
if (!dumpSubcommand.isPresent()) {
return Collections.emptyList();
}
Expand All @@ -255,14 +256,11 @@ public List<String> handleTabCompletion(CommandEvent event) throws NoPermissionE
.collect(Collectors.toList());
}

if (args.length == 4) {
val argLower = args[3].toLowerCase();
return Triton.get().getDumpManager().getAvailableTypes().keySet().stream()
.map(String::toLowerCase)
.filter(value -> value.startsWith(argLower))
.collect(Collectors.toList());
}
break;
val argLower = args[args.length - 1].toLowerCase();
return Triton.get().getDumpManager().getAvailableTypes().keySet().stream()
.map(String::toLowerCase)
.filter(value -> value.startsWith(argLower))
.collect(Collectors.toList());
case CLEAR:
break;
}
Expand All @@ -286,11 +284,16 @@ private <T extends Enum<T>> List<String> autocompleteEnum(String arg, T[] enumVa
/**
* Wrapper to include platform in the message sent
*
* @param event The command event being handled
* @param message The message to send
* @param event The command event being handled
* @param code The code of the message to send
* @param args The arguments of the message
*/
private void sendMessage(CommandEvent event, String message) {
event.getSender().sendMessage("[Triton @ " + event.getPlatform() + "] " + message);
private void sendMessage(CommandEvent event, String code, Object... args) {
event.getSender().sendMessageFormatted(
"debug.prefix",
event.getPlatform(),
Triton.get().getMessagesConfig().getMessage(code, args)
);
}

private enum TargetPlatform {
Expand All @@ -303,7 +306,7 @@ private enum Subcommand {
LOAD
}

private enum DumpSubcommand {
private enum DumpAction {
ADD,
REMOVE,
CLEAR
Expand Down
37 changes: 37 additions & 0 deletions core/src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ command:
database: "Uploads/downloads translations to/from a database"
info: "Shows information about the plugin (version, author, storage, etc)"
loglevel: "See or change Triton's log level temporarily"
debug: "Debugging tools (use upon request from the developer)"
success:
getflag: "&bYou received the &7%1&b flag!"
selector: "&bYour language has been changed to &7%1"
Expand Down Expand Up @@ -65,6 +66,42 @@ other:
selector-gui-currentpage: "&bPage &3%1&b of &3%2"
selected: "&bCurrently selected"
database-loading: "&bPerforming operation..."
debug:
prefix: "&b[Triton @ &l%1&b] &r%2"
target-platform:
missing: "&cYou must provide one of the following target platforms: &4%1"
invalid: "&cInvalid target platform '&4%1&c'. Available platforms: &4%2"
subcommand:
missing: "&cYou must provide one of the following subcommands: &4%1"
invalid: "&cInvalid subcommand '&4%1&c'. Available subcommands: &4%2"
dump:
action:
missing: "&cYou must provide one of the following actions for dump: &4%1"
invalid: "&cInvalid action '&4%1&c'. Available actions for dump: &4%2"
player:
missing: "&cYou must provide a player name, a UUID, 'me' or 'all'"
me-not-player: "&cOnly players can use 'me'"
not-found: "&cPlayer '&4%1&c' could not be found"
type:
not-found: "&cType '&4%1&c' not found. Available types: &4%2"
success:
add:
player: "&aEnabled dumping (&6%1&a) for player with UUID &6%2"
all: "&aEnabled dumping (&6%1&a) for all players"
remove:
player: "&aDisabled dumping (&6%1&a) for player with UUID &6%2"
all: "&aDisabled dumping (&6%1&a) for all players"
clear: "&aDisabled dumping for everyone"
load:
dump-file:
missing: "&cYou must provide a dump name (name of file under plugins/Triton/dumps)"
not-found: "&cCannot open dump file '&4%1&c': &4%2"
invalid-json: "&cFailed to parse message JSON in dump file: &4%1"
line-number:
invalid: "&cProvided line number '&4%1&c' is not a valid number"
success:
sent: '&aSend &6%1&a message(s)!'

info-command:
- "&8-------[ &b&lTriton&8 ]-------"
- "&bVersion: &7%1"
Expand Down

0 comments on commit 075427a

Please sign in to comment.