From 307bf8b332a8d25021f7f27c92e4c96bb9dc8302 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Thu, 13 Jun 2019 15:28:06 -0700 Subject: [PATCH] SoundSettingSerializer --- .../gmail/nossr50/config/ConfigManager.java | 2 + .../serializers/SoundSettingSerializer.java | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/main/java/com/gmail/nossr50/config/hocon/serializers/SoundSettingSerializer.java diff --git a/src/main/java/com/gmail/nossr50/config/ConfigManager.java b/src/main/java/com/gmail/nossr50/config/ConfigManager.java index ee62c2370..3ec4c9a1e 100644 --- a/src/main/java/com/gmail/nossr50/config/ConfigManager.java +++ b/src/main/java/com/gmail/nossr50/config/ConfigManager.java @@ -44,6 +44,7 @@ import com.gmail.nossr50.config.hocon.skills.taming.ConfigTaming; import com.gmail.nossr50.config.hocon.skills.unarmed.ConfigUnarmed; import com.gmail.nossr50.config.hocon.skills.woodcutting.ConfigWoodcutting; import com.gmail.nossr50.config.hocon.sound.ConfigSound; +import com.gmail.nossr50.config.hocon.sound.SoundSetting; import com.gmail.nossr50.config.hocon.superabilities.ConfigSuperAbilities; import com.gmail.nossr50.config.hocon.worldblacklist.ConfigWorldBlacklist; import com.gmail.nossr50.config.treasure.ExcavationTreasureConfig; @@ -277,6 +278,7 @@ public final class ConfigManager { customSerializers.registerType(TypeToken.of(SkillRankProperty.class), new SkillRankPropertySerializer()); customSerializers.registerType(TypeToken.of(MaxBonusLevel.class), new MaxBonusLevelSerializer()); customSerializers.registerType(TypeToken.of(PlayerNotificationSettings.class), new PlayerNotificationSerializer()); + customSerializers.registerType(TypeToken.of(SoundSetting.class), new SoundSettingSerializer()); } /** diff --git a/src/main/java/com/gmail/nossr50/config/hocon/serializers/SoundSettingSerializer.java b/src/main/java/com/gmail/nossr50/config/hocon/serializers/SoundSettingSerializer.java new file mode 100644 index 000000000..edb8e92b4 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/hocon/serializers/SoundSettingSerializer.java @@ -0,0 +1,48 @@ +package com.gmail.nossr50.config.hocon.serializers; + +import com.gmail.nossr50.config.hocon.sound.SoundSetting; +import com.google.common.reflect.TypeToken; +import ninja.leaping.configurate.ConfigurationNode; +import ninja.leaping.configurate.ValueType; +import ninja.leaping.configurate.objectmapping.ObjectMappingException; +import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +public class SoundSettingSerializer implements TypeSerializer { + + private static final String VOLUME_NODE = "Volume"; + private static final String PITCH_NODE = "Pitch"; + public static final String ENABLED_NODE = "Enabled"; + + @Nullable + @Override + public SoundSetting deserialize(@NonNull TypeToken type, @NonNull ConfigurationNode value) throws ObjectMappingException { + float volume = 1.0f; + float pitch = 1.0f; + boolean enabled = true; + + if(value.getNode(ENABLED_NODE).getValueType() != ValueType.NULL) { + enabled = value.getNode(ENABLED_NODE).getValue(TypeToken.of(Boolean.class)); + } + + if(value.getNode(VOLUME_NODE).getValueType() != ValueType.NULL) { + volume = (float) value.getNode(VOLUME_NODE).getValue(TypeToken.of(Double.class)).doubleValue(); + } + + if(value.getNode(PITCH_NODE).getValueType() != ValueType.NULL) { + volume = (float) value.getNode(PITCH_NODE).getValue(TypeToken.of(Double.class)).doubleValue(); + } + + SoundSetting soundSetting = new SoundSetting(enabled, volume, pitch); + return soundSetting; + } + + @Override + public void serialize(@NonNull TypeToken type, @Nullable SoundSetting obj, @NonNull ConfigurationNode value) throws ObjectMappingException { + value.getNode(ENABLED_NODE).setValue(obj.isEnabled()); + value.getNode(VOLUME_NODE).setValue(obj.getVolume()); + value.getNode(PITCH_NODE).setValue(obj.getPitch()); + } + +}