SoundSettingSerializer

This commit is contained in:
nossr50 2019-06-13 15:28:06 -07:00
parent 86a4b74eb3
commit 307bf8b332
2 changed files with 50 additions and 0 deletions

View File

@ -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());
}
/**

View File

@ -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<SoundSetting> {
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());
}
}