Skip to content

Commit 5d3f98c

Browse files
committed
Dont require the @ConfigSerializable annotation for StorageConfigs
1 parent 526d587 commit 5d3f98c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

common/src/main/java/de/bluecolored/bluemap/common/config/ConfigManager.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
package de.bluecolored.bluemap.common.config;
2626

2727
import com.flowpowered.math.vector.Vector2i;
28+
import de.bluecolored.bluemap.common.config.storage.StorageConfig;
2829
import de.bluecolored.bluemap.common.config.typeserializer.KeyTypeSerializer;
30+
import de.bluecolored.bluemap.common.config.typeserializer.ObjectMapperSerializer;
2931
import de.bluecolored.bluemap.common.config.typeserializer.Vector2iTypeSerializer;
3032
import de.bluecolored.bluemap.core.BlueMap;
3133
import de.bluecolored.bluemap.core.util.Key;
34+
import de.bluecolored.bluenbt.TypeToken;
3235
import org.spongepowered.configurate.ConfigurateException;
3336
import org.spongepowered.configurate.ConfigurationNode;
3437
import org.spongepowered.configurate.loader.AbstractConfigurationLoader;
@@ -159,6 +162,10 @@ private ConfigurationLoader<? extends ConfigurationNode> getLoader(Path path){
159162
.defaultOptions(o -> o.serializers(b -> {
160163
b.register(Vector2i.class, new Vector2iTypeSerializer());
161164
b.register(Key.class, new KeyTypeSerializer());
165+
166+
// try parse any StorageConfig type, even without the @ConfigSerializable annotation
167+
b.register(type -> TypeToken.of(type).is(StorageConfig.class), new ObjectMapperSerializer());
168+
162169
}))
163170
.build();
164171
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of BlueMap, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package de.bluecolored.bluemap.common.config.typeserializer;
26+
27+
import org.checkerframework.checker.nullness.qual.Nullable;
28+
import org.spongepowered.configurate.ConfigurationNode;
29+
import org.spongepowered.configurate.objectmapping.ObjectMapper;
30+
import org.spongepowered.configurate.serialize.SerializationException;
31+
import org.spongepowered.configurate.serialize.TypeSerializer;
32+
33+
import java.lang.reflect.Type;
34+
35+
public class ObjectMapperSerializer implements TypeSerializer<Object> {
36+
37+
@Override
38+
public Object deserialize(Type type, ConfigurationNode node) throws SerializationException {
39+
return ObjectMapper.factory().get(type).load(node);
40+
}
41+
42+
@Override
43+
@SuppressWarnings({"unchecked", "rawtypes"})
44+
public void serialize(Type type, @Nullable Object obj, ConfigurationNode node) throws SerializationException {
45+
ObjectMapper mapper = ObjectMapper.factory().get(type);
46+
mapper.save(obj, node);
47+
}
48+
49+
}

0 commit comments

Comments
 (0)