Skip to content

Commit

Permalink
Dont require the @ConfigSerializable annotation for StorageConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
TBlueF committed Feb 10, 2025
1 parent 526d587 commit 5d3f98c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
package de.bluecolored.bluemap.common.config;

import com.flowpowered.math.vector.Vector2i;
import de.bluecolored.bluemap.common.config.storage.StorageConfig;
import de.bluecolored.bluemap.common.config.typeserializer.KeyTypeSerializer;
import de.bluecolored.bluemap.common.config.typeserializer.ObjectMapperSerializer;
import de.bluecolored.bluemap.common.config.typeserializer.Vector2iTypeSerializer;
import de.bluecolored.bluemap.core.BlueMap;
import de.bluecolored.bluemap.core.util.Key;
import de.bluecolored.bluenbt.TypeToken;
import org.spongepowered.configurate.ConfigurateException;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.loader.AbstractConfigurationLoader;
Expand Down Expand Up @@ -159,6 +162,10 @@ private ConfigurationLoader<? extends ConfigurationNode> getLoader(Path path){
.defaultOptions(o -> o.serializers(b -> {
b.register(Vector2i.class, new Vector2iTypeSerializer());
b.register(Key.class, new KeyTypeSerializer());

// try parse any StorageConfig type, even without the @ConfigSerializable annotation
b.register(type -> TypeToken.of(type).is(StorageConfig.class), new ObjectMapperSerializer());

}))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of BlueMap, licensed under the MIT License (MIT).
*
* Copyright (c) Blue (Lukas Rieger) <https://bluecolored.de>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package de.bluecolored.bluemap.common.config.typeserializer;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.objectmapping.ObjectMapper;
import org.spongepowered.configurate.serialize.SerializationException;
import org.spongepowered.configurate.serialize.TypeSerializer;

import java.lang.reflect.Type;

public class ObjectMapperSerializer implements TypeSerializer<Object> {

@Override
public Object deserialize(Type type, ConfigurationNode node) throws SerializationException {
return ObjectMapper.factory().get(type).load(node);
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public void serialize(Type type, @Nullable Object obj, ConfigurationNode node) throws SerializationException {
ObjectMapper mapper = ObjectMapper.factory().get(type);
mapper.save(obj, node);
}

}

0 comments on commit 5d3f98c

Please sign in to comment.