mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +00:00
Fix serializers
This commit is contained in:
1 parent
935e1bbf0e
commit
3c196e59fb
4 files changed
+164
-5
No files matched your search
@@ -118,7 +118,7 @@ public class ModerationSettings {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@ConfigSerializable
|
@ConfigSerializable
|
||||||
public final class Filters {
|
public static class Filters {
|
||||||
|
|
||||||
@Setting
|
@Setting
|
||||||
private boolean enabled = true;
|
private boolean enabled = true;
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ import net.draycia.carbon.api.config.StorageType;
|
|||||||
import net.draycia.carbon.api.messaging.MessageService;
|
import net.draycia.carbon.api.messaging.MessageService;
|
||||||
import net.draycia.carbon.api.users.ChatUser;
|
import net.draycia.carbon.api.users.ChatUser;
|
||||||
import net.draycia.carbon.common.adventure.FormatType;
|
import net.draycia.carbon.common.adventure.FormatType;
|
||||||
|
import net.draycia.carbon.common.config.KeySerializer;
|
||||||
|
import net.draycia.carbon.common.config.SoundSerializer;
|
||||||
import net.draycia.carbon.common.messaging.EmptyMessageService;
|
import net.draycia.carbon.common.messaging.EmptyMessageService;
|
||||||
import net.draycia.carbon.api.config.SQLCredentials;
|
import net.draycia.carbon.api.config.SQLCredentials;
|
||||||
import net.draycia.carbon.common.messaging.RedisMessageService;
|
import net.draycia.carbon.common.messaging.RedisMessageService;
|
||||||
@@ -50,7 +52,9 @@ import net.draycia.carbon.storage.BukkitChatUser;
|
|||||||
import net.draycia.carbon.util.CarbonPlaceholders;
|
import net.draycia.carbon.util.CarbonPlaceholders;
|
||||||
import net.draycia.carbon.util.FunctionalityConstants;
|
import net.draycia.carbon.util.FunctionalityConstants;
|
||||||
import net.draycia.carbon.util.Metrics;
|
import net.draycia.carbon.util.Metrics;
|
||||||
|
import net.kyori.adventure.key.Key;
|
||||||
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||||
|
import net.kyori.adventure.sound.Sound;
|
||||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
import net.milkbowl.vault.economy.Economy;
|
import net.milkbowl.vault.economy.Economy;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@@ -61,6 +65,7 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.spongepowered.configurate.BasicConfigurationNode;
|
import org.spongepowered.configurate.BasicConfigurationNode;
|
||||||
|
import org.spongepowered.configurate.ConfigurationOptions;
|
||||||
import org.spongepowered.configurate.objectmapping.ObjectMappingException;
|
import org.spongepowered.configurate.objectmapping.ObjectMappingException;
|
||||||
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
import org.spongepowered.configurate.yaml.YamlConfigurationLoader;
|
||||||
|
|
||||||
@@ -229,7 +234,11 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
|||||||
private void loadCarbonSettings() {
|
private void loadCarbonSettings() {
|
||||||
final File settingsFile = new File(this.getDataFolder(), "config.yml");
|
final File settingsFile = new File(this.getDataFolder(), "config.yml");
|
||||||
final YamlConfigurationLoader settingsLoader = YamlConfigurationLoader.builder()
|
final YamlConfigurationLoader settingsLoader = YamlConfigurationLoader.builder()
|
||||||
.setDefaultOptions(opts -> opts.withShouldCopyDefaults(true)).setFile(settingsFile).build();
|
.setDefaultOptions(opts -> {
|
||||||
|
return opts.withShouldCopyDefaults(true).withSerializers(builder -> {
|
||||||
|
builder.register(Key.class, KeySerializer.INSTANCE).register(Sound.class, SoundSerializer.INSTANCE);
|
||||||
|
});
|
||||||
|
}).setFile(settingsFile).build();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final BasicConfigurationNode settingsNode = settingsLoader.load();
|
final BasicConfigurationNode settingsNode = settingsLoader.load();
|
||||||
@@ -243,7 +252,11 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
|||||||
private void loadLanguage() {
|
private void loadLanguage() {
|
||||||
final File languageFile = new File(this.getDataFolder(), "language.yml");
|
final File languageFile = new File(this.getDataFolder(), "language.yml");
|
||||||
final YamlConfigurationLoader languageLoader = YamlConfigurationLoader.builder()
|
final YamlConfigurationLoader languageLoader = YamlConfigurationLoader.builder()
|
||||||
.setDefaultOptions(opts -> opts.withShouldCopyDefaults(true)).setFile(languageFile).build();
|
.setDefaultOptions(opts -> {
|
||||||
|
return opts.withShouldCopyDefaults(true).withSerializers(builder -> {
|
||||||
|
builder.register(Key.class, KeySerializer.INSTANCE).register(Sound.class, SoundSerializer.INSTANCE);
|
||||||
|
});
|
||||||
|
}).setFile(languageFile).build();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final BasicConfigurationNode languageNode = languageLoader.load();
|
final BasicConfigurationNode languageNode = languageLoader.load();
|
||||||
@@ -257,7 +270,11 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
|||||||
private void loadModerationSettings() {
|
private void loadModerationSettings() {
|
||||||
final File moderationFile = new File(this.getDataFolder(), "moderation.yml");
|
final File moderationFile = new File(this.getDataFolder(), "moderation.yml");
|
||||||
final YamlConfigurationLoader moderationLoader = YamlConfigurationLoader.builder()
|
final YamlConfigurationLoader moderationLoader = YamlConfigurationLoader.builder()
|
||||||
.setDefaultOptions(opts -> opts.withShouldCopyDefaults(true)).setFile(moderationFile).build();
|
.setDefaultOptions(opts -> {
|
||||||
|
return opts.withShouldCopyDefaults(true).withSerializers(builder -> {
|
||||||
|
builder.register(Key.class, KeySerializer.INSTANCE).register(Sound.class, SoundSerializer.INSTANCE);
|
||||||
|
});
|
||||||
|
}).setFile(moderationFile).build();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final BasicConfigurationNode moderationNode = moderationLoader.load();
|
final BasicConfigurationNode moderationNode = moderationLoader.load();
|
||||||
@@ -277,7 +294,11 @@ public final class CarbonChatBukkit extends JavaPlugin implements CarbonChat {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final YamlConfigurationLoader commandSettingsLoader = YamlConfigurationLoader.builder()
|
final YamlConfigurationLoader commandSettingsLoader = YamlConfigurationLoader.builder()
|
||||||
.setDefaultOptions(opts -> opts.withShouldCopyDefaults(true)).setFile(carbonCommands).build();
|
.setDefaultOptions(opts -> {
|
||||||
|
return opts.withShouldCopyDefaults(true).withSerializers(builder -> {
|
||||||
|
builder.register(Key.class, KeySerializer.INSTANCE).register(Sound.class, SoundSerializer.INSTANCE);
|
||||||
|
});
|
||||||
|
}).setFile(carbonCommands).build();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final BasicConfigurationNode commandSettingsNode = commandSettingsLoader.load();
|
final BasicConfigurationNode commandSettingsNode = commandSettingsLoader.load();
|
||||||
|
|||||||
@@ -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());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user