use of org.bukkit.configuration.MemorySection in project TokenManager by RealizedMC.
the class AbstractConfiguration method convert.
protected FileConfiguration convert(final Converter converter) throws IOException {
plugin.getLogger().info(String.format(CONVERT_START, name));
final Map<String, Object> oldValues = new HashMap<>();
for (final String key : configuration.getKeys(true)) {
if (key.equals("config-version")) {
continue;
}
final Object value = configuration.get(key);
if (value instanceof MemorySection) {
continue;
}
oldValues.put(key, value);
}
if (converter != null) {
converter.renamedKeys().forEach((old, changed) -> {
final Object previous = oldValues.get(old);
if (previous != null) {
oldValues.remove(old);
oldValues.put(changed, previous);
}
});
}
final String newName = name.replace(".yml", "") + "-" + System.currentTimeMillis() + ".yml";
final File copied = Files.copy(file.toPath(), new File(plugin.getDataFolder(), newName).toPath()).toFile();
plugin.getLogger().info(String.format(CONVERT_SAVE, copied.getName()));
plugin.saveResource(name, true);
// Loads comments of the new configuration file
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
final Multimap<String, List<String>> comments = LinkedListMultimap.create();
final List<String> currentComments = new ArrayList<>();
String line;
Matcher matcher;
while ((line = reader.readLine()) != null) {
if ((matcher = KEY_PATTERN.matcher(line)).find() && !COMMENT_PATTERN.matcher(line).matches()) {
comments.put(matcher.group(2), Lists.newArrayList(currentComments));
currentComments.clear();
} else if (COMMENT_PATTERN.matcher(line).matches()) {
currentComments.add(line);
}
}
configuration = YamlConfiguration.loadConfiguration(file);
configuration.options().header(null);
// Transfer values from the old configuration
for (Map.Entry<String, Object> entry : oldValues.entrySet()) {
final Object previous;
if ((previous = configuration.get(entry.getKey())) != null && entry.getValue().getClass().isInstance(previous)) {
configuration.set(entry.getKey(), entry.getValue());
}
}
final List<String> commentlessData = Lists.newArrayList(configuration.saveToString().split("\n"));
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
for (final String data : commentlessData) {
matcher = KEY_PATTERN.matcher(data);
if (matcher.find()) {
final String key = matcher.group(2);
final Collection<List<String>> result = comments.get(key);
if (result != null) {
final List<List<String>> commentData = Lists.newArrayList(result);
if (!commentData.isEmpty()) {
for (final String comment : commentData.get(0)) {
writer.write(comment);
writer.newLine();
}
commentData.remove(0);
comments.replaceValues(key, commentData);
}
}
}
writer.write(data);
if (commentlessData.indexOf(data) + 1 < commentlessData.size()) {
writer.newLine();
} else if (!currentComments.isEmpty()) {
writer.newLine();
}
}
// Handles comments at the end of the file without any key
for (final String comment : currentComments) {
writer.write(comment);
if (currentComments.indexOf(comment) + 1 < currentComments.size()) {
writer.newLine();
}
}
writer.flush();
}
plugin.getLogger().info(CONVERT_DONE);
}
return configuration;
}
use of org.bukkit.configuration.MemorySection in project AuthMeReloaded by AuthMe.
the class PermissionConsistencyTest method getPermissionsFromPluginYmlFile.
/**
* Returns all permission entries from the plugin.yml file.
*
* @return map with the permission entries by permission node
*/
private static Map<String, PermissionDefinition> getPermissionsFromPluginYmlFile() {
FileConfiguration pluginFile = YamlConfiguration.loadConfiguration(getJarFile("/plugin.yml"));
MemorySection permsList = (MemorySection) pluginFile.get("permissions");
Map<String, PermissionDefinition> permissions = new HashMap<>();
addChildren(permsList, permissions);
return ImmutableMap.copyOf(permissions);
}
use of org.bukkit.configuration.MemorySection in project AuthMeReloaded by AuthMe.
the class CommandConsistencyTest method getLabelsFromPluginFile.
/**
* Reads plugin.yml and returns the defined commands by main label and aliases.
*
* @return collection of all labels and their aliases
*/
@SuppressWarnings("unchecked")
private static Map<String, List<String>> getLabelsFromPluginFile() {
FileConfiguration pluginFile = YamlConfiguration.loadConfiguration(getJarFile("/plugin.yml"));
MemorySection commandList = (MemorySection) pluginFile.get("commands");
Map<String, Object> commandDefinitions = commandList.getValues(false);
Map<String, List<String>> commandLabels = new HashMap<>();
for (Map.Entry<String, Object> commandDefinition : commandDefinitions.entrySet()) {
MemorySection definition = (MemorySection) commandDefinition.getValue();
List<String> alternativeLabels = definition.get("aliases") == null ? Collections.EMPTY_LIST : (List<String>) definition.get("aliases");
commandLabels.put(commandDefinition.getKey(), alternativeLabels);
}
return commandLabels;
}
use of org.bukkit.configuration.MemorySection in project AuthMeReloaded by AuthMe.
the class HelpTranslationGeneratorIntegrationTest method checkDescription.
private static void checkDescription(Object memorySection, String description, String detailedDescription) {
if (memorySection instanceof MemorySection) {
MemorySection memSection = (MemorySection) memorySection;
assertThat(memSection.getString("description"), equalTo(description));
assertThat(memSection.getString("detailedDescription"), equalTo(detailedDescription));
} else {
fail("Expected MemorySection, got '" + memorySection + "'");
}
}
use of org.bukkit.configuration.MemorySection in project Glowstone by GlowstoneMC.
the class GlowServer method enablePlugins.
/**
* Enable all plugins of the given load order type.
*
* @param type The type of plugin to enable.
*/
private void enablePlugins(PluginLoadOrder type) {
if (type == PluginLoadOrder.STARTUP) {
helpMap.clear();
helpMap.loadConfig(config.getConfigFile(Key.HELP_FILE));
}
// load all the plugins
Plugin[] plugins = pluginManager.getPlugins();
for (Plugin plugin : plugins) {
if (!plugin.isEnabled() && plugin.getDescription().getLoad() == type) {
List<Permission> perms = plugin.getDescription().getPermissions();
for (Permission perm : perms) {
try {
pluginManager.addPermission(perm);
} catch (IllegalArgumentException ex) {
ConsoleMessages.Warn.Plugin.PERMISSION_DUPLICATE.log(ex, plugin.getDescription().getFullName(), perm.getName());
}
}
try {
pluginManager.enablePlugin(plugin);
} catch (Throwable ex) {
ConsoleMessages.Error.Plugin.LOADING.log(ex, plugin.getDescription().getFullName());
}
}
}
if (type == PluginLoadOrder.POSTWORLD) {
commandMap.setFallbackCommands();
commandMap.registerServerAliases();
DefaultPermissions.registerCorePermissions();
// Default permissions
this.permissionRoot = DefaultPermissions.registerPermission("minecraft", "Gives the user the ability to use all " + "Minecraft utilities and commands");
this.permissionRootCommand = DefaultPermissions.registerPermission("minecraft.command", "Gives the user the ability to use " + "all Minecraft commands", permissionRoot);
DefaultPermissions.registerPermission("minecraft.command.tell", "Allows the user to send a " + "private message", PermissionDefault.TRUE, permissionRootCommand);
permissionRootCommand.recalculatePermissibles();
permissionRoot.recalculatePermissibles();
helpMap.initializeCommands();
helpMap.amendTopics(config.getConfigFile(Key.HELP_FILE));
// load permissions.yml
ConfigurationSection permConfig = config.getConfigFile(Key.PERMISSIONS_FILE);
Map<String, Map<String, Object>> data = new HashMap<>();
permConfig.getValues(false).forEach((key, value) -> data.put(key, ((MemorySection) value).getValues(false)));
List<Permission> perms = Permission.loadPermissions(data, ConsoleMessages.Error.Permission.INVALID.get(), PermissionDefault.OP);
for (Permission perm : perms) {
try {
pluginManager.addPermission(perm);
} catch (IllegalArgumentException ex) {
ConsoleMessages.Warn.Permission.DUPLICATE.log(ex, perm.getName());
}
}
}
}
Aggregations