下面列出了org.bukkit.configuration.file.FileConfiguration#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
private static void renameKey(FileConfiguration config, String from, String to) {
final Object value = config.get(from);
if (value == null) return;
config.set(to, value);
config.set(from, null);
}
/**
* Will return object from a config with specified path. If the object does not exist in
* the config, it will add it into the config and return the default object.
*
* @param defaultValue default object
* @param config FileConfiguration instance
* @param path path to object
*/
//this method is probably the only necessary method in this util
public static Object getOrSetDefault(Object defaultValue, FileConfiguration config, String path) {
Object result;
if (config.isSet(path)) {
result = config.get(path);
} else {
result = defaultValue;
config.set(path, defaultValue);
}
return result;
}
public static FileConfiguration getConfigFullPath(File originalFile, String url) {
FileConfiguration config = new YamlConfiguration();
try {
InputStream inputStream = FallbackConfigUtil.class.getResourceAsStream(url);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
config.load(reader);
if (originalFile != null && originalFile.exists()) {
FileConfiguration configOverride = new YamlConfiguration();
configOverride.load(originalFile);
for (String key : configOverride.getKeys(true)) {
if (configOverride.get(key) instanceof ConfigurationSection) {
continue;
}
config.set(key, configOverride.get(key));
}
}
} catch (Exception e) {
if (originalFile != null) {
Civs.logger.log(Level.SEVERE, "File name: {0}", originalFile.getName());
}
if (url != null) {
Civs.logger.log(Level.SEVERE, "Resource path: {0}", url);
}
Civs.logger.log(Level.SEVERE, "Unable to load config", e);
}
return config;
}
@SuppressWarnings("unchecked")
public static Location locationDeserialize(String key, FileConfiguration config) {
if (!config.contains(key)) {
return null;
}
Object locSec = config.get(key);
if (locSec instanceof Location) {
return (Location) locSec;
}
try {
Map<String, Object> section = (Map<String, Object>) config.get(key);
if (section == null) {
return null;
}
double x = Double.valueOf(section.get("x").toString());
double y = Double.valueOf(section.get("y").toString());
double z = Double.valueOf(section.get("z").toString());
float yaw = Float.valueOf(section.get("yaw").toString());
float pitch = Float.valueOf(section.get("pitch").toString());
World world = BedwarsRel.getInstance().getServer().getWorld(section.get("world").toString());
if (world == null) {
return null;
}
return new Location(world, x, y, z, yaw, pitch);
} catch (Exception ex) {
BedwarsRel.getInstance().getBugsnag().notify(ex);
ex.printStackTrace();
}
return null;
}
/**
* 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);
}
/**
* Since CommandInitializer contains all descriptions for commands in English, the help_en.yml file
* only contains an entry for one command as to provide an example.
*/
@Test
public void shouldOnlyHaveDescriptionForOneCommand() {
// given
FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE);
// when
Object commands = configuration.get("commands");
// then
assertThat(commands, instanceOf(MemorySection.class));
assertThat(((MemorySection) commands).getKeys(false), contains("authme"));
}
private static boolean isNotInnerNode(String key, FileConfiguration configuration) {
return !(configuration.get(key) instanceof MemorySection);
}
/**
* Retrieve a property as a float from the given file configuration.
*
* @param configuration The file configuration to use
* @param path The path of the property to retrieve
*
* @return The float
*/
private static float getFloat(FileConfiguration configuration, String path) {
Object value = configuration.get(path);
// This behavior is consistent with FileConfiguration#getDouble
return (value instanceof Number) ? ((Number) value).floatValue() : 0;
}