SteamWar/BauSystem2.0
Archiviert
12
0

Initial config prototype
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Dieser Commit ist enthalten in:
Lixfel 2023-05-14 16:00:54 +02:00
Ursprung 072833f503
Commit d72d62c9ee
10 geänderte Dateien mit 153 neuen und 243 gelöschten Zeilen

Datei anzeigen

@ -43,9 +43,6 @@ sourceSets {
} }
dependencies { dependencies {
// implementation 'yoyosource:YAPION:0.25.3'
implementation files("${projectDir}/../libs/YAPION-SNAPSHOT.jar")
compileOnly 'org.projectlombok:lombok:1.18.22' compileOnly 'org.projectlombok:lombok:1.18.22'
testCompileOnly 'org.projectlombok:lombok:1.18.22' testCompileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22' annotationProcessor 'org.projectlombok:lombok:1.18.22'

Datei anzeigen

@ -97,7 +97,7 @@ public class BauSystem extends JavaPlugin implements Listener {
LinkageUtils.unlink(); LinkageUtils.unlink();
WorldData.write(); WorldData.write();
Config.getInstance().saveAll(); Config.saveAll();
TinyProtocol.instance.close(); TinyProtocol.instance.close();
} }

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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 {}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<? extends YamlConfig> configType) {
ConfigurationSerialization.registerClass(configType);
}
public YamlConfig(ConfigurationSection configuration) {
this(configuration.getValues(false));
}
public YamlConfig(Map<String, Object> 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<String, Object> 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);
}
}
));
}
}

Datei anzeigen

@ -19,48 +19,60 @@
package de.steamwar.bausystem.configplayer; 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.linkage.Linked;
import de.steamwar.sql.UserConfig; import de.steamwar.sql.UserConfig;
import lombok.Getter; 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.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent; 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.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
@Linked public class Config extends YamlConfig {
public class Config implements Listener {
static { private static final Map<UUID, Config> playerConfigurations = new HashMap<>();
SerializeManager.add(new ConfigurationSerializableSerializer());
public static Config get(Player player) {
return playerConfigurations.computeIfAbsent(player.getUniqueId(), uuid -> new Config(uuid, YamlConfiguration.loadConfiguration(new StringReader(UserConfig.getConfig(uuid, "bausystem")))));
} }
public static void saveAll() {
playerConfigurations.forEach((uuid, config) -> config.save());
playerConfigurations.clear();
}
private final UUID uuid;
@Getter @Getter
private static Config instance; @Setter
@ConfigField
private boolean nightvision;
private static final boolean NIGHTVISION_DEFAULT = false;
{ private Config(UUID uuid, ConfigurationSection configuration) {
instance = this; super(configuration);
this.uuid = uuid;
} }
private final Map<UUID, YAPIONObject> playerConfigurations = new HashMap<>(); public void save() {
YamlConfiguration config = new YamlConfiguration();
private static final Map<Integer, ConfigConverter> CONFIG_CONVERTER_MAP = new HashMap<>(); for(Map.Entry<String, Object> entry : serialize().entrySet())
config.set(entry.getKey(), entry.getValue());
public static void addConfigConverter(ConfigConverter configConverter) { UserConfig.updatePlayerConfig(uuid, "bausystem", config.saveToString());
CONFIG_CONVERTER_MAP.putIfAbsent(configConverter.version(), configConverter);
} }
@Linked
public static class ConfigListener implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
get(event.getPlayer()); get(event.getPlayer());
@ -68,80 +80,8 @@ public class Config implements Listener {
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
save(event.getPlayer()); get(event.getPlayer()).save();
playerConfigurations.remove(event.getPlayer().getUniqueId()); 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);
});
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 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;
}
return yapionObject;
} }
} }

Datei anzeigen

@ -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 <b>@version</b> 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 <b>@version</b> 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);
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<ConfigurationSerializable> {
@Override
public Class<ConfigurationSerializable> type() {
return ConfigurationSerializable.class;
}
@Override
public boolean isInterface() {
return true;
}
@Override
public YAPIONObject serialize(SerializeData<ConfigurationSerializable> 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<String, Object> 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<YAPIONObject> deserializeData) {
Map<String, Object> 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);
}
}

Datei anzeigen

@ -30,7 +30,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import yapion.hierarchy.types.YAPIONObject;
@Linked @Linked
public class NightVisionCommand extends SWCommand implements Listener { public class NightVisionCommand extends SWCommand implements Listener {
@ -41,15 +40,15 @@ public class NightVisionCommand extends SWCommand implements Listener {
@Register(description = "NIGHT_VISION_HELP") @Register(description = "NIGHT_VISION_HELP")
public void genericCommand(Player p) { public void genericCommand(Player p) {
YAPIONObject yapionObject = Config.getInstance().get(p); Config config = Config.get(p);
boolean value = !yapionObject.getBooleanOrDefault("nightvision", false); config.setNightvision(!config.isNightvision());
yapionObject.put("nightvision", value); config.save();
setNightVision(p, value); setNightVision(p, config.isNightvision());
} }
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { 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) { private void setNightVision(Player p, boolean value) {

Datei anzeigen

@ -81,10 +81,6 @@ allprojects {
repositories { repositories {
mavenCentral() mavenCentral()
maven {
url = uri("https://raw.githubusercontent.com/yoyosource/YAPION/master/releases")
}
maven { maven {
url = uri("https://repo.codemc.io/repository/maven-snapshots/") url = uri("https://repo.codemc.io/repository/maven-snapshots/")
} }

Binäre Datei nicht angezeigt.