[SPIGOT-6234] enum classes don't serialize properly when implementing ConfigurationSerializable Created: 10/Nov/20 Updated: 17/Nov/20 Resolved: 17/Nov/20 |
|
| Status: | Resolved |
| Project: | Spigot |
| Component/s: | None |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Bug | Priority: | Minor |
| Reporter: | Jan Boerman | Assignee: | Unassigned |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | None | ||
| Environment: |
Java 15, Windows 10 |
||
| Attachments: |
|
| Version: | This server is running CraftBukkit version git-Spigot-a19903d-009f0ba (MC: 1.16.4) (Implementing API version 1.16.4-R0.1-SNAPSHOT) |
| Guidelines Read: | Yes |
| Description |
|
enumerations that implement org.bukkit.configuration.serialization.ConfigurationSerializable aren't properly serialized to disk. Given a configurationserializable class like this: @SerializableAs("Hello") enum Hello implements ConfigurationSerializable { FOO, BAR; @Override public Map<String, Object> serialize() { return Collections.singletonMap("variant", name()); } public static Hello deserialize(Map<String, Object> map) { return Hello.valueOf((String) map.get("variant")); } } and an onEnable like this:
@Override
public void onEnable() {
ConfigurationSerialization.registerClass(Hello.class, "Hello");
File dataFolder = getDataFolder();
dataFolder.mkdirs();
File saveFile = new File(dataFolder, "test.yml");
try {
if (!saveFile.exists()) saveFile.createNewFile();
YamlConfiguration yamlConfiguration = new YamlConfiguration();
yamlConfiguration.set("hello", Hello.FOO);
yamlConfiguration.save(saveFile);
yamlConfiguration = YamlConfiguration.loadConfiguration(saveFile);
Hello hello = (Hello) yamlConfiguration.get("hello");
assert hello == Hello.FOO;
} catch (IOException e) {
getLogger().log(Level.SEVERE, "could not save or load test.yml", e);
}
}
, it is expected that the contents of the test.yml savefile looks as follows: hello: ==: Hello variant: 'FOO' but instead all that's there is: hello: !!com.example.Hello 'FOO' |
| Comments |
| Comment by Jan Boerman [ 10/Nov/20 ] |
|
After some digging, I think the problem is that the RepresentEnum from SafeRepresenter takes priority over bukkit's RepresentConfigurationSerializable. |