diff --git a/BauSystem_Main/build.gradle b/BauSystem_Main/build.gradle index 1052e30a..704046b0 100644 --- a/BauSystem_Main/build.gradle +++ b/BauSystem_Main/build.gradle @@ -43,9 +43,6 @@ sourceSets { } dependencies { - // implementation 'yoyosource:YAPION:0.25.3' - implementation files("${projectDir}/../libs/YAPION-SNAPSHOT.jar") - compileOnly 'org.projectlombok:lombok:1.18.22' testCompileOnly 'org.projectlombok:lombok:1.18.22' annotationProcessor 'org.projectlombok:lombok:1.18.22' diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 1d1e6362..daa9fb7a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -97,7 +97,7 @@ public class BauSystem extends JavaPlugin implements Listener { LinkageUtils.unlink(); WorldData.write(); - Config.getInstance().saveAll(); + Config.saveAll(); TinyProtocol.instance.close(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/config/ConfigField.java b/BauSystem_Main/src/de/steamwar/bausystem/config/ConfigField.java new file mode 100644 index 00000000..6e8fe349 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/config/ConfigField.java @@ -0,0 +1,29 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.config; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface ConfigField {} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/config/YamlConfig.java b/BauSystem_Main/src/de/steamwar/bausystem/config/YamlConfig.java new file mode 100644 index 00000000..534c6780 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/config/YamlConfig.java @@ -0,0 +1,76 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.config; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.configuration.serialization.ConfigurationSerialization; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; + +public class YamlConfig implements ConfigurationSerializable { + + public static void register(Class configType) { + ConfigurationSerialization.registerClass(configType); + } + + public YamlConfig(ConfigurationSection configuration) { + this(configuration.getValues(false)); + } + + public YamlConfig(Map map) { + Arrays.stream(getClass().getDeclaredFields()) + .filter(field -> field.isAnnotationPresent(ConfigField.class)) + .forEach(field -> { + try { + field.setAccessible(true); + field.set(this, map.computeIfAbsent(field.getName(), name -> { + try { + return getClass().getDeclaredField(name.toUpperCase() + "_DEFAULT").get(null); + } catch (IllegalAccessException | NoSuchFieldException e) { + throw new IllegalStateException(getClass().getName() + "." + field.getName() + " has no default field", e); + } + })); + } catch (IllegalAccessException e) { + throw new IllegalStateException(getClass().getName() + "." + field.getName() + " is inaccessible for deserializer", e); + } + }); + } + + @Override + public Map serialize() { + return Arrays.stream(getClass().getDeclaredFields()) + .filter(field -> field.isAnnotationPresent(ConfigField.class)) + .collect(Collectors.toMap( + Field::getName, + field -> { + try { + field.setAccessible(true); + return field.get(this); + } catch (IllegalAccessException e) { + throw new IllegalStateException(getClass().getName() + "." + field.getName() + " is inaccessible for serializer", e); + } + } + )); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java index 2f929687..f5fff3a9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/Config.java @@ -19,129 +19,69 @@ package de.steamwar.bausystem.configplayer; -import de.steamwar.bausystem.configplayer.serializer.ConfigurationSerializableSerializer; +import de.steamwar.bausystem.config.ConfigField; +import de.steamwar.bausystem.config.YamlConfig; import de.steamwar.linkage.Linked; import de.steamwar.sql.UserConfig; import lombok.Getter; -import org.bukkit.Bukkit; +import lombok.Setter; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import yapion.hierarchy.output.StringOutput; -import yapion.hierarchy.types.YAPIONObject; -import yapion.parser.YAPIONParser; -import yapion.serializing.SerializeManager; +import java.io.StringReader; import java.util.HashMap; import java.util.Map; import java.util.UUID; -import java.util.logging.Level; -@Linked -public class Config implements Listener { +public class Config extends YamlConfig { - static { - SerializeManager.add(new ConfigurationSerializableSerializer()); + private static final Map playerConfigurations = new HashMap<>(); + + public static Config get(Player player) { + return playerConfigurations.computeIfAbsent(player.getUniqueId(), uuid -> new Config(uuid, YamlConfiguration.loadConfiguration(new StringReader(UserConfig.getConfig(uuid, "bausystem"))))); } - @Getter - private static Config instance; - - { - instance = this; - } - - private final Map playerConfigurations = new HashMap<>(); - - private static final Map CONFIG_CONVERTER_MAP = new HashMap<>(); - - public static void addConfigConverter(ConfigConverter configConverter) { - CONFIG_CONVERTER_MAP.putIfAbsent(configConverter.version(), configConverter); - } - - @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) { - get(event.getPlayer()); - } - - @EventHandler - public void onPlayerQuit(PlayerQuitEvent event) { - save(event.getPlayer()); - playerConfigurations.remove(event.getPlayer().getUniqueId()); - } - - /** - * Get a PlayerConfig, optionally loads it from the DataBase and migrates it if necessary. - * - * @param player the player from whom to get the config. - * @return the config object - */ - public YAPIONObject get(Player player) { - UUID uuid = player.getUniqueId(); - if (!playerConfigurations.containsKey(uuid)) { - String s = UserConfig.getConfig(uuid, "bausystem"); - YAPIONObject yapionObject; - if (s == null) { - yapionObject = ConfigCreator.createDefaultConfig(); - } else { - yapionObject = YAPIONParser.parse(s); - } - yapionObject = update(yapionObject); - playerConfigurations.put(uuid, yapionObject); - return yapionObject; - } - return playerConfigurations.get(uuid); - } - - public void saveAll() { - playerConfigurations.forEach((uuid, yapionObject) -> { - String string = yapionObject.toYAPION(new StringOutput()).getResult(); - UserConfig.updatePlayerConfig(uuid, "bausystem", string); - }); + public static void saveAll() { + playerConfigurations.forEach((uuid, config) -> config.save()); playerConfigurations.clear(); } - /** - * Save a PlayerConfig, this does not remove the key value mapping from the map. - * - * @param player the player to save the config. - */ - public void save(Player player) { - UUID uuid = player.getUniqueId(); - if (playerConfigurations.containsKey(uuid)) { - YAPIONObject yapionObject = playerConfigurations.get(uuid); - String string = yapionObject.toYAPION(new StringOutput()).getResult(); - UserConfig.updatePlayerConfig(uuid, "bausystem", string); - } + private final UUID uuid; + + @Getter + @Setter + @ConfigField + private boolean nightvision; + private static final boolean NIGHTVISION_DEFAULT = false; + + private Config(UUID uuid, ConfigurationSection configuration) { + super(configuration); + this.uuid = uuid; } - private YAPIONObject update(YAPIONObject yapionObject) { - int version = yapionObject.getPlainValue("@version"); - while (version < ConfigCreator.currentVersion) { - ConfigConverter configConverter = CONFIG_CONVERTER_MAP.getOrDefault(version, null); - if (configConverter == null) { - Bukkit.getLogger().log(Level.SEVERE, "No updater found for version " + version); - return ConfigCreator.createDefaultConfig(); - } - try { - configConverter.update(yapionObject); - } catch (Exception e) { - Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e); - return ConfigCreator.createDefaultConfig(); - } - int newVersion = yapionObject.getPlainValue("@version"); - if (version == newVersion) { - Bukkit.getLogger().log(Level.SEVERE, "Version Tag was the same after conversion"); - return ConfigCreator.createDefaultConfig(); - } - if (newVersion < version) { - Bukkit.getLogger().log(Level.SEVERE, "Version Tag was earlier after conversion"); - return ConfigCreator.createDefaultConfig(); - } - version = newVersion; + public void save() { + YamlConfiguration config = new YamlConfiguration(); + for(Map.Entry entry : serialize().entrySet()) + config.set(entry.getKey(), entry.getValue()); + UserConfig.updatePlayerConfig(uuid, "bausystem", config.saveToString()); + } + + @Linked + public static class ConfigListener implements Listener { + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + get(event.getPlayer()); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + get(event.getPlayer()).save(); + playerConfigurations.remove(event.getPlayer().getUniqueId()); } - return yapionObject; } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/ConfigConverter.java b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/ConfigConverter.java deleted file mode 100644 index bf9f1fdf..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/ConfigConverter.java +++ /dev/null @@ -1,37 +0,0 @@ -package de.steamwar.bausystem.configplayer; - -import yapion.hierarchy.types.YAPIONObject; - -/** - * A new {@link ConfigConverter} should be written when you remove anything - * from the Config or modify any mayor part. When you move anything from - * any key to any other key you should write a new {@link ConfigConverter}. - * For adding any new key you should be able to get the default without - * having it in the Config. Anything you need to change you should also - * change the {@link ConfigCreator} accordingly, to produce the new Config. - */ -public interface ConfigConverter { - - /** - * This describes the version this Converter can convert from. The version - * it should convert to is the version 1 above this number. But this is not - * a necessity. In the config Object as parameter given in {@link #update(YAPIONObject)} - * you should update the @version variable in the root object to the - * new version this converter produced. - * - * @return the version number - */ - int version(); - - /** - * This method should update everything needed to go from a lower config - * version to a higher. It should update the @version variable - * accordingly. Anything else is up the implementation. If anything goes wrong - * do not silently exit this method, throw an Exception. The updater Code will - * deal with it. Never leave the inputted object in a corrupted state. - * - * @param config the config object to update - */ - void update(YAPIONObject config); - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/serializer/ConfigurationSerializableSerializer.java b/BauSystem_Main/src/de/steamwar/bausystem/configplayer/serializer/ConfigurationSerializableSerializer.java deleted file mode 100644 index eecd1ef1..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/configplayer/serializer/ConfigurationSerializableSerializer.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2021 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.bausystem.configplayer.serializer; - -import org.bukkit.configuration.serialization.ConfigurationSerializable; -import org.bukkit.configuration.serialization.ConfigurationSerialization; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.meta.ItemMeta; -import yapion.hierarchy.api.groups.YAPIONAnyType; -import yapion.hierarchy.types.YAPIONObject; -import yapion.serializing.api.SerializerObject; -import yapion.serializing.data.DeserializeData; -import yapion.serializing.data.SerializeData; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - -import static yapion.utils.IdentifierUtils.TYPE_IDENTIFIER; - -public class ConfigurationSerializableSerializer extends SerializerObject { - - @Override - public Class type() { - return ConfigurationSerializable.class; - } - - @Override - public boolean isInterface() { - return true; - } - - @Override - public YAPIONObject serialize(SerializeData serializeData) { - YAPIONObject yapionObject = new YAPIONObject(); - yapionObject.add(TYPE_IDENTIFIER, serializeData.object.getClass().getTypeName()); - if (serializeData.object instanceof ItemStack) { - yapionObject.add(TYPE_IDENTIFIER, ItemStack.class.getTypeName()); - } - if (serializeData.object instanceof ItemMeta) { - yapionObject.add(TYPE_IDENTIFIER, ItemMeta.class.getTypeName()); - } - Map serializeDataMap = serializeData.object.serialize(); - serializeDataMap.forEach((s, o) -> { - YAPIONAnyType yapionAnyType = serializeData.serialize(o); - if (yapionAnyType instanceof YAPIONObject) { - YAPIONObject object = (YAPIONObject) yapionAnyType; - if (object.containsKey(TYPE_IDENTIFIER) && object.getPlainValue(TYPE_IDENTIFIER).equals("com.google.common.collect.RegularImmutableList")) { - object.put(TYPE_IDENTIFIER, ArrayList.class.getTypeName()); - } - } - yapionObject.add(s, yapionAnyType); - }); - return yapionObject; - } - - @Override - public ConfigurationSerializable deserialize(DeserializeData deserializeData) { - Map deserializeDataMap = new HashMap<>(); - deserializeData.object.forEach((s, yapionAnyType) -> { - if (s.equals(TYPE_IDENTIFIER)) { - if (yapionAnyType.toString().equals("(org.bukkit.inventory.meta.ItemMeta)")) { - deserializeDataMap.put("==", "ItemMeta"); - } else { - deserializeDataMap.put("==", deserializeData.deserialize(yapionAnyType)); - } - return; - } - deserializeDataMap.put(s, deserializeData.deserialize(yapionAnyType)); - }); - return ConfigurationSerialization.deserializeObject(deserializeDataMap); - } -} \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/NightVisionCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/NightVisionCommand.java index 0308846d..db7b1ee0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/util/NightVisionCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/NightVisionCommand.java @@ -30,7 +30,6 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; -import yapion.hierarchy.types.YAPIONObject; @Linked public class NightVisionCommand extends SWCommand implements Listener { @@ -41,15 +40,15 @@ public class NightVisionCommand extends SWCommand implements Listener { @Register(description = "NIGHT_VISION_HELP") public void genericCommand(Player p) { - YAPIONObject yapionObject = Config.getInstance().get(p); - boolean value = !yapionObject.getBooleanOrDefault("nightvision", false); - yapionObject.put("nightvision", value); - setNightVision(p, value); + Config config = Config.get(p); + config.setNightvision(!config.isNightvision()); + config.save(); + setNightVision(p, config.isNightvision()); } @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { - setNightVision(event.getPlayer(), Config.getInstance().get(event.getPlayer()).getBooleanOrDefault("nightvision", false)); + setNightVision(event.getPlayer(), Config.get(event.getPlayer()).isNightvision()); } private void setNightVision(Player p, boolean value) { diff --git a/build.gradle b/build.gradle index 653fbc20..af89ef46 100644 --- a/build.gradle +++ b/build.gradle @@ -81,10 +81,6 @@ allprojects { repositories { mavenCentral() - maven { - url = uri("https://raw.githubusercontent.com/yoyosource/YAPION/master/releases") - } - maven { url = uri("https://repo.codemc.io/repository/maven-snapshots/") } diff --git a/libs/YAPION-SNAPSHOT.jar b/libs/YAPION-SNAPSHOT.jar deleted file mode 100644 index 5568663c..00000000 Binary files a/libs/YAPION-SNAPSHOT.jar and /dev/null differ