Fix serializers

This commit is contained in:
Draycia committed 2020-09-25 01:45:36 -07:00
1 parent 935e1bbf0e
commit 3c196e59fb
4 files changed
+164 -5

No files matched your search

@@ -0,0 +1,57 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2020 KyoriPowered
*
* 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 net.draycia.carbon.common.config;
import java.lang.reflect.Type;
import java.util.function.Predicate;
import net.kyori.adventure.key.InvalidKeyException;
import net.kyori.adventure.key.Key;
import org.spongepowered.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.configurate.serialize.ScalarSerializer;
public final class KeySerializer extends ScalarSerializer<Key> {
public static final KeySerializer INSTANCE = new KeySerializer();
private KeySerializer() {
super(Key.class);
}
@Override
public Key deserialize(final Type type, final Object obj) throws ObjectMappingException {
if (!(obj instanceof CharSequence)) {
throw new ObjectMappingException("Invalid type presented to serializer: " + type);
}
try {
return Key.of(obj.toString());
} catch(final InvalidKeyException ex) {
throw new ObjectMappingException(ex);
}
}
@Override
public Object serialize(final Key item, final Predicate<Class<?>> typeSupported) {
return item.asString();
}
}
@@ -0,0 +1,81 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2020 KyoriPowered
*
* 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 net.draycia.carbon.common.config;
import com.google.common.reflect.TypeToken;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.configurate.ConfigurationNode;
import org.spongepowered.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.configurate.serialize.TypeSerializer;
import java.lang.reflect.Type;
public final class SoundSerializer implements TypeSerializer<Sound> {
static final Type TYPE = TypeToken.of(Sound.class).getType();
static final Type SOURCE_TYPE = TypeToken.of(Sound.Source.class).getType();
public static final SoundSerializer INSTANCE = new SoundSerializer();
static final String NAME = "name";
static final String SOURCE = "source";
static final String PITCH = "pitch";
static final String VOLUME = "volume";
private SoundSerializer() {
}
@Override
public @Nullable Sound deserialize(@NonNull final Type type, @NonNull final ConfigurationNode value) throws ObjectMappingException {
if (value.isEmpty()) {
return null;
}
final Key name = value.getNode(NAME).getValue(KeySerializer.INSTANCE.type());
final Sound.Source source = (Sound.Source) value.getNode(SOURCE).getValue(SOURCE_TYPE); // unsure about this
final float volume = value.getNode(VOLUME).getFloat(1.0f);
final float pitch = value.getNode(PITCH).getFloat(1.0f);
if (name == null || source == null) {
throw new ObjectMappingException("A name and source are required to deserialize a Sound");
}
return Sound.of(name, source, volume, pitch);
}
@Override
public void serialize(@NonNull final Type type, @Nullable final Sound obj, @NonNull final ConfigurationNode value) throws ObjectMappingException {
if (obj == null) {
value.setValue(null);
return;
}
value.getNode(NAME).setValue(KeySerializer.INSTANCE.type(), obj.name());
value.getNode(SOURCE).setValue(SOURCE_TYPE, obj.source());
value.getNode(VOLUME).setValue(obj.volume());
value.getNode(PITCH).setValue(obj.pitch());
}
}