geforkt von Mirrors/Paper
SPIGOT-6234: enum classes don't serialize properly when implementing ConfigurationSerializable
By: md_5 <git@md-5.net>
Dieser Commit ist enthalten in:
Ursprung
39f6cd7ffa
Commit
139ca6a269
@ -14,6 +14,9 @@ public class YamlRepresenter extends Representer {
|
||||
public YamlRepresenter() {
|
||||
this.multiRepresenters.put(ConfigurationSection.class, new RepresentConfigurationSection());
|
||||
this.multiRepresenters.put(ConfigurationSerializable.class, new RepresentConfigurationSerializable());
|
||||
// SPIGOT-6234: We could just switch YamlConstructor to extend Constructor rather than SafeConstructor, however there is a very small risk of issues with plugins treating config as untrusted input
|
||||
// So instead we will just allow future plugins to have their enums extend ConfigurationSerializable
|
||||
this.multiRepresenters.remove(Enum.class);
|
||||
}
|
||||
|
||||
private class RepresentConfigurationSection extends RepresentMap {
|
||||
|
@ -2,11 +2,13 @@ package org.bukkit.configuration;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.junit.Test;
|
||||
@ -592,9 +594,18 @@ public abstract class ConfigurationSectionTest {
|
||||
assertFalse(section.isConfigurationSection("doesntExist"));
|
||||
}
|
||||
|
||||
public enum TestEnum {
|
||||
public enum TestEnum implements ConfigurationSerializable {
|
||||
HELLO,
|
||||
WORLD,
|
||||
BANANAS
|
||||
BANANAS;
|
||||
|
||||
@Override
|
||||
public Map<String, Object> serialize() {
|
||||
return Collections.singletonMap("variant", this.name());
|
||||
}
|
||||
|
||||
public static TestEnum deserialize(Map<String, Object> map) {
|
||||
return TestEnum.valueOf((String) map.get("variant"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,16 @@ import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerialization;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.junit.Test;
|
||||
|
||||
public abstract class ConfigurationTest {
|
||||
|
||||
static {
|
||||
ConfigurationSerialization.registerClass(ConfigurationSectionTest.TestEnum.class);
|
||||
}
|
||||
|
||||
public abstract Configuration getConfig();
|
||||
|
||||
public Map<String, Object> getTestValues() {
|
||||
@ -24,6 +30,7 @@ public abstract class ConfigurationTest {
|
||||
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");
|
||||
result.put("enum", ConfigurationSectionTest.TestEnum.BANANAS);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -38,7 +38,10 @@ public class YamlConfigurationTest extends FileConfigurationTest {
|
||||
+ "- 3\n"
|
||||
+ "- 4\n"
|
||||
+ "- 5\n"
|
||||
+ "'42': The Answer\n";
|
||||
+ "'42': The Answer\n"
|
||||
+ "enum:\n"
|
||||
+ " ==: org.bukkit.configuration.ConfigurationSectionTest$TestEnum\n"
|
||||
+ " variant: BANANAS\n";
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren