geforkt von Mirrors/Paper
Fixed issues with loading YamlConfigurations with typed keys
By: Nathan Adams <dinnerbone@dinnerbone.com>
Dieser Commit ist enthalten in:
Ursprung
98583f6462
Commit
2ec416e8e0
@ -4,6 +4,7 @@ import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
@ -55,11 +56,14 @@ public class YamlConfiguration extends FileConfiguration {
|
||||
throw new IllegalArgumentException("Contents cannot be null");
|
||||
}
|
||||
|
||||
Map<String, Object> input;
|
||||
try {
|
||||
input = (Map<String, Object>)yaml.load(contents);
|
||||
} catch (Throwable ex) {
|
||||
throw new InvalidConfigurationException("Specified contents is not a valid Configuration", ex);
|
||||
Map<Object, Object> input = (Map<Object, Object>)yaml.load(contents);
|
||||
int size = (input == null) ? 0 : input.size();
|
||||
Map<String, Object> result = new LinkedHashMap<String, Object>(size);
|
||||
|
||||
if (size > 0) {
|
||||
for (Map.Entry<Object, Object> entry : input.entrySet()) {
|
||||
result.put(entry.getKey().toString(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
String header = parseHeader(contents);
|
||||
@ -68,7 +72,7 @@ public class YamlConfiguration extends FileConfiguration {
|
||||
options().header(header);
|
||||
}
|
||||
|
||||
deserializeValues(input, this);
|
||||
deserializeValues(result, this);
|
||||
}
|
||||
|
||||
protected void deserializeValues(Map<String, Object> input, ConfigurationSection section) throws InvalidConfigurationException {
|
||||
@ -80,12 +84,14 @@ public class YamlConfiguration extends FileConfiguration {
|
||||
Object value = entry.getValue();
|
||||
|
||||
if (value instanceof Map) {
|
||||
Map<String, Object> subvalues;
|
||||
|
||||
try {
|
||||
subvalues = (Map<String, Object>) value;
|
||||
} catch (ClassCastException ex) {
|
||||
throw new InvalidConfigurationException("Map found where type is not <String, Object>", ex);
|
||||
Map<Object, Object> subinput = (Map<Object, Object>)value;
|
||||
int size = (subinput == null) ? 0 : subinput.size();
|
||||
Map<String, Object> subvalues = new LinkedHashMap<String, Object>(size);
|
||||
|
||||
if (size > 0) {
|
||||
for (Map.Entry<Object, Object> subentry : subinput.entrySet()) {
|
||||
subvalues.put(subentry.getKey().toString(), subentry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
if (subvalues.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
|
||||
|
@ -1,7 +1,6 @@
|
||||
package org.bukkit.configuration;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -22,9 +21,10 @@ public abstract class ConfigurationSectionTest {
|
||||
section.set("subsection.subkey2", true);
|
||||
section.set("subsection.subsubsection.key", true);
|
||||
section.set("key2", true);
|
||||
section.set("42", true);
|
||||
|
||||
assertArrayEquals(new String[] {"key", "subsection", "key2"}, section.getKeys(false).toArray());
|
||||
assertArrayEquals(new String[] {"key", "subsection", "subsection.subkey", "subsection.subkey2", "subsection.subsubsection", "subsection.subsubsection.key", "key2"}, section.getKeys(true).toArray());
|
||||
assertArrayEquals(new String[] {"key", "subsection", "key2", "42"}, section.getKeys(false).toArray());
|
||||
assertArrayEquals(new String[] {"key", "subsection", "subsection.subkey", "subsection.subkey2", "subsection.subsubsection", "subsection.subsubsection.key", "key2", "42"}, section.getKeys(true).toArray());
|
||||
assertArrayEquals(new String[] {"subkey", "subkey2", "subsubsection", "subsubsection.key"}, section.getConfigurationSection("subsection").getKeys(true).toArray());
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ public abstract class ConfigurationTest {
|
||||
result.put("false-boolean", false);
|
||||
result.put("vector", new Vector(12345.67, 64, -12345.6789));
|
||||
result.put("list", Arrays.asList(1, 2, 3, 4, 5));
|
||||
result.put("42", "The Answer");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -36,7 +36,8 @@ public class YamlConfigurationTest extends FileConfigurationTest {
|
||||
"- 2\n" +
|
||||
"- 3\n" +
|
||||
"- 4\n" +
|
||||
"- 5\n";
|
||||
"- 5\n" +
|
||||
"'42': The Answer\n";
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren