-
Notifications
You must be signed in to change notification settings - Fork 557
Expand file tree
/
Copy pathHelpTranslationGenerator.java
More file actions
127 lines (111 loc) · 5.36 KB
/
HelpTranslationGenerator.java
File metadata and controls
127 lines (111 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package fr.xephi.authme.service;
import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.command.CommandArgumentDescription;
import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.CommandInitializer;
import fr.xephi.authme.command.help.HelpMessage;
import fr.xephi.authme.command.help.HelpMessagesService;
import fr.xephi.authme.command.help.HelpSection;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.message.MessagePathHelper;
import fr.xephi.authme.permission.DefaultPermission;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Generates the full command structure for the help translation and saves it to the current help file,
* preserving already existing entries.
*/
public class HelpTranslationGenerator {
private final CommandInitializer commandInitializer;
private final HelpMessagesService helpMessagesService;
private final Settings settings;
private final File dataFolder;
@Inject
HelpTranslationGenerator(CommandInitializer commandInitializer, HelpMessagesService helpMessagesService,
Settings settings, @DataFolder File dataFolder) {
this.commandInitializer = commandInitializer;
this.helpMessagesService = helpMessagesService;
this.settings = settings;
this.dataFolder = dataFolder;
}
/**
* Updates the help file to contain entries for all commands.
*
* @return the help file that has been updated
* @throws IOException if the help file cannot be written to
*/
public File updateHelpFile() throws IOException {
String languageCode = settings.getProperty(PluginSettings.MESSAGES_LANGUAGE);
File helpFile = new File(dataFolder, MessagePathHelper.createHelpMessageFilePath(languageCode));
Map<String, Object> helpEntries = generateHelpMessageEntries();
String helpEntriesYaml = exportToYaml(helpEntries);
Files.write(helpFile.toPath(), helpEntriesYaml.getBytes(), StandardOpenOption.TRUNCATE_EXISTING);
return helpFile;
}
private static String exportToYaml(Map<String, Object> helpEntries) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setAllowUnicode(true);
return new Yaml(options).dump(helpEntries);
}
/**
* Generates entries for a complete help text file.
*
* @return help text entries to save
*/
private Map<String, Object> generateHelpMessageEntries() {
Map<String, Object> messageEntries = new LinkedHashMap<>(HelpMessage.values().length);
for (HelpMessage message : HelpMessage.values()) {
messageEntries.put(message.getEntryKey(), helpMessagesService.getMessage(message));
}
Map<String, String> defaultPermissions = new LinkedHashMap<>();
for (DefaultPermission defaultPermission : DefaultPermission.values()) {
defaultPermissions.put(HelpMessagesService.getDefaultPermissionsSubPath(defaultPermission),
helpMessagesService.getMessage(defaultPermission));
}
messageEntries.put("defaultPermissions", defaultPermissions);
Map<String, String> sectionEntries = new LinkedHashMap<>(HelpSection.values().length);
for (HelpSection section : HelpSection.values()) {
sectionEntries.put(section.getEntryKey(), helpMessagesService.getMessage(section));
}
Map<String, Object> commandEntries = new LinkedHashMap<>();
for (CommandDescription command : commandInitializer.getCommands()) {
generateCommandEntries(command, commandEntries);
}
return ImmutableMap.of(
"common", messageEntries,
"section", sectionEntries,
"commands", commandEntries);
}
/**
* Adds YAML entries for the provided command its children to the given map.
*
* @param command the command to process (including its children)
* @param commandEntries the map to add the generated entries to
*/
private void generateCommandEntries(CommandDescription command, Map<String, Object> commandEntries) {
CommandDescription translatedCommand = helpMessagesService.buildLocalizedDescription(command);
Map<String, Object> commandData = new LinkedHashMap<>();
commandData.put("description", translatedCommand.getDescription());
commandData.put("detailedDescription", translatedCommand.getDetailedDescription());
int i = 1;
for (CommandArgumentDescription argument : translatedCommand.getArguments()) {
Map<String, String> argumentData = new LinkedHashMap<>(2);
argumentData.put("label", argument.getName());
argumentData.put("description", argument.getDescription());
commandData.put("arg" + i, argumentData);
++i;
}
commandEntries.put(HelpMessagesService.getCommandSubPath(translatedCommand), commandData);
translatedCommand.getChildren().forEach(child -> generateCommandEntries(child, commandEntries));
}
}