Skip to content

Commit

Permalink
Version increment, DumpWriter ExtensionPoint and first migration
Browse files Browse the repository at this point in the history
  • Loading branch information
CubBossa committed Apr 4, 2024
1 parent 285d4e0 commit e48371e
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 108 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ java {
}

group = "de.cubbossa"
version = "4.6.0"
version = "5.0.0"

subprojects {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,8 @@ enum ApplicationState {
Task repeatingTask(Runnable runnable, long delay, long interval);

void cancelTask(Task task);

default Object[] getMigrations() {
return new Object[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import de.cubbossa.pathfinder.events.BukkitEventDispatcher;
import de.cubbossa.pathfinder.listener.BukkitEffects;
import de.cubbossa.pathfinder.listener.PlayerListener;
import de.cubbossa.pathfinder.migration.V5_0_0__Config;
import de.cubbossa.pathfinder.node.NodeSelectionProviderImpl;
import de.cubbossa.pathfinder.node.selection.BukkitNodeSelectionParser;
import de.cubbossa.pathfinder.util.BukkitMainThreadExecutor;
Expand Down Expand Up @@ -174,4 +175,11 @@ public Logger getLogger() {
public String getVersion() {
return javaPlugin.getDescription().getVersion();
}

@Override
public Object[] getMigrations() {
return new Object[] {
new V5_0_0__Config()
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -339,28 +339,26 @@ public <N extends Node> Argument<NodeType<N>> nodeTypeArgument(
public CommandArgument<NodeSelection, CustomArgument<NodeSelection, String>> nodeSelectionArgument(
String nodeName) {
return (CommandArgument<NodeSelection, CustomArgument<NodeSelection, String>>) CommandArgument.arg(
new CustomArgument<>(new TextArgument(nodeName), info -> {
if (info.sender() instanceof Player player) {
try {
return NodeSelection.ofSender(info.input().substring(1, info.input().length() - 1), player);
} catch (ParseCancellationException e) {
throw CustomArgument.CustomArgumentException.fromString(e.getMessage());
}
}
return new NodeSelectionImpl();
})).includeSuggestions((suggestionInfo, suggestionsBuilder) -> {
if (!(suggestionInfo.sender() instanceof Player player)) {
return suggestionsBuilder.buildFuture();
}
int offset = suggestionInfo.currentInput().length() - suggestionInfo.currentArg().length();

return NodeSelectionProviderImpl.getNodeSelectionSuggestions(suggestionInfo)
// add quotations to suggestions
.thenApply(s -> CommandUtils.wrapWithQuotation(suggestionInfo.currentArg(), s,
suggestionInfo.currentArg(), offset))
// shift suggestions toward actual command argument offset
.thenApply(s -> CommandUtils.offsetSuggestions(suggestionInfo.currentArg(), s, offset));
});
new CustomArgument<>(new TextArgument(nodeName), info -> {
if (info.sender() instanceof Player player) {
try {
return NodeSelection.ofSender(info.input().substring(1, info.input().length() - 1), player);
} catch (ParseCancellationException e) {
throw CustomArgument.CustomArgumentException.fromString(e.getMessage());
}
}
return new NodeSelectionImpl();
}))
.includeSuggestions((suggestionInfo, suggestionsBuilder) -> {
int offset = suggestionInfo.currentInput().length() - suggestionInfo.currentArg().length();

return NodeSelectionProviderImpl.getNodeSelectionSuggestions(suggestionInfo)
// add quotations to suggestions
.thenApply(s -> CommandUtils.wrapWithQuotation(suggestionInfo.currentArg(), s,
suggestionInfo.currentArg(), offset))
// shift suggestions toward actual command argument offset
.thenApply(s -> CommandUtils.offsetSuggestions(suggestionInfo.currentArg(), s, offset));
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.cubbossa.pathfinder.migration;

import de.cubbossa.pathapi.PathFinder;
import de.cubbossa.pathapi.PathFinderProvider;
import java.io.File;
import org.bukkit.configuration.file.YamlConfiguration;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;

public class V5_0_0__Config extends BaseJavaMigration {

@Override
public void migrate(Context context) throws Exception {

PathFinder pathFinder = PathFinderProvider.get();

File config = new File(pathFinder.getDataFolder(), "config.yml");
if (!config.exists()) {
return;
}
YamlConfiguration yml = YamlConfiguration.loadConfiguration(config);
yml.set("version", null);
yml.save(config);
}
}
2 changes: 1 addition & 1 deletion pathfinder-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {
implementation("io.r2dbc:r2dbc-spi:1.0.0.RELEASE")
implementation("com.zaxxer:HikariCP:5.0.1")
implementation("de.cubbossa:disposables-api:1.0-SNAPSHOT")
implementation("org.flywaydb:flyway-core:8.0.0")
api("org.flywaydb:flyway-core:8.0.0")

// Particles
api("de.cubbossa:splinelib:1.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import de.cubbossa.translations.MessageBundle;
import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Locale;
import java.util.Objects;
import java.util.UUID;
Expand All @@ -43,6 +44,7 @@
import lombok.SneakyThrows;
import net.kyori.adventure.platform.AudienceProvider;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.flywaydb.core.api.migration.JavaMigration;
import org.jetbrains.annotations.NotNull;

@Getter
Expand Down Expand Up @@ -166,7 +168,10 @@ public void onEnable() {
new File(getDataFolder(), "data/").mkdirs();
StorageImplementation impl = getStorageImplementation();

new Migrator(getDataFolder(), getLogger()).migrate();
new Migrator(getDataFolder(), Arrays.stream(getMigrations())
.filter(object -> object instanceof JavaMigration)
.map(object -> (JavaMigration) object)
.toArray(JavaMigration[]::new)).migrate();

((StorageAdapterImpl) storage).setImplementation(impl);
storage.setEventDispatcher(eventDispatcher);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package de.cubbossa.pathfinder;

import de.cubbossa.disposables.Disposable;
import de.cubbossa.pathapi.storage.DatabaseType;
import de.cubbossa.pathfinder.util.Version;
import de.exlll.configlib.NameFormatters;
import de.exlll.configlib.Serializer;
import de.exlll.configlib.YamlConfigurationProperties;
Expand All @@ -11,94 +9,62 @@
import java.io.File;
import java.util.Locale;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@AllArgsConstructor
@RequiredArgsConstructor
public class ConfigFileLoader implements Disposable {

private final File dataFolder;
private Version configRegenerationVersion = new Version("4.0.0");
private static final YamlConfigurationProperties properties = YamlConfigurationProperties.newBuilder()
.setNameFormatter(NameFormatters.LOWER_KEBAB_CASE)
.addSerializer(Locale.class, new Serializer<Locale, String>() {
@Override
public String serialize(Locale element) {
return element.toLanguageTag();
}

@Getter
private boolean versionChange;
@Getter
private DatabaseType oldDatabaseType;
@Override
public Locale deserialize(String element) {
return Locale.forLanguageTag(element.replace("_", "-"));
}
})
.addSerializer(Color.class, new Serializer<Color, String>() {
@Override
public String serialize(Color element) {
return Integer.toHexString(element.getRGB() & 0xffffff);
}

public PathFinderConfigImpl loadConfig() {
PathFinderConfigImpl configuration;
@Override
public Color deserialize(String element) {
return new Color(Integer.parseInt(element, 16));
}
})
.createParentDirectories(true)
.header("""
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
# #
# _____ _ _ ______ _ _ #
# | __ \\ | | | | | ____(_) | | #
# | |__) |_ _| |_| |__ | |__ _ _ __ __| | ___ _ __ #
# | ___/ _` | __| '_ \\| __| | | '_ \\ / _` |/ _ \\ '__| #
# | | | (_| | |_| | | | | | | | | | (_| | __/ | #
# |_| \\__,_|\\__|_| |_|_| |_|_| |_|\\__,_|\\___|_| #
# Configuration #
# #
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
""")
.build();

File configFile = new File(dataFolder, "config.yml");
YamlConfigurationProperties properties = YamlConfigurationProperties.newBuilder()
.setNameFormatter(NameFormatters.LOWER_KEBAB_CASE)
.addSerializer(Locale.class, new Serializer<Locale, String>() {
@Override
public String serialize(Locale element) {
return element.toLanguageTag();
}
private final File dataFolder;

@Override
public Locale deserialize(String element) {
return Locale.forLanguageTag(element.replace("_", "-"));
}
})
.addSerializer(Color.class, new Serializer<Color, String>() {
@Override
public String serialize(Color element) {
return Integer.toHexString(element.getRGB() & 0xffffff);
}

@Override
public Color deserialize(String element) {
return new Color(Integer.parseInt(element, 16));
}
})
.createParentDirectories(true)
.header("""
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
# #
# _____ _ _ ______ _ _ #
# | __ \\ | | | | | ____(_) | | #
# | |__) |_ _| |_| |__ | |__ _ _ __ __| | ___ _ __ #
# | ___/ _` | __| '_ \\| __| | | '_ \\ / _` |/ _ \\ '__| #
# | | | (_| | |_| | | | | | | | | | (_| | __/ | #
# |_| \\__,_|\\__|_| |_|_| |_|_| |_|\\__,_|\\___|_| #
# Configuration #
# #
#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#=#
""")
.build();
public PathFinderConfigImpl loadConfig() {
PathFinderConfigImpl configuration;

File configFile = new File(dataFolder, "config.yml");
if (!configFile.exists()) {
configuration = new PathFinderConfigImpl();
YamlConfigurations.save(configFile.toPath(), PathFinderConfigImpl.class, configuration, properties);
return configuration;
}
configuration = YamlConfigurations.load(configFile.toPath(), PathFinderConfigImpl.class, properties);

if (new Version(configuration.version).compareTo(configRegenerationVersion) < 0) {
this.versionChange = true;


saveFileAsOld(configFile, "config", ".yml");
saveFileAsOld(new File(dataFolder, "effects.nbo"), "effects", ".nbo");
oldDatabaseType = configuration.database.type;

configuration = loadConfig();
}
return configuration;
}

private void saveFileAsOld(File file, String base, String suffix) {
int test = 1;
String b = base + "_old";
File f = new File(file.getParentFile(), b + suffix);
while (f.exists()) {
b = String.format("%s_old_%02d%s", base, test++, suffix);
f = new File(file.getParentFile(), b);
}
file.renameTo(f);
return YamlConfigurations.load(configFile.toPath(), PathFinderConfigImpl.class, properties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.cubbossa.pathfinder.dump;

public interface DumpWriterDataProvider {

String getDumpKey();

Object getDumpData();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import com.google.gson.stream.JsonWriter;
import de.cubbossa.pathapi.dump.DumpWriter;
import de.cubbossa.pathapi.dump.DumpWriterProvider;

import java.awt.*;
import de.cubbossa.pathfinder.util.ExtensionPoint;
import java.awt.Color;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -18,6 +18,8 @@

public class DumpWriterImpl implements DumpWriter {

public static final ExtensionPoint<DumpWriterDataProvider> EXTENSION_POINT = new ExtensionPoint<>(DumpWriterDataProvider.class);

private final LinkedHashMap<String, Supplier<Object>> dumpMap;
private final Gson gson;

Expand All @@ -29,6 +31,8 @@ public DumpWriterImpl(boolean prettyPrinting) {
dumpMap = new LinkedHashMap<>();
DumpWriterProvider.set(this);

EXTENSION_POINT.getExtensions().forEach(e -> this.addProperty(e.getDumpKey(), e::getDumpData));

GsonBuilder builder = new GsonBuilder()
.registerTypeAdapter(File.class, new TypeAdapter<File>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.logging.Logger;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.migration.JavaMigration;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;

Expand All @@ -15,7 +15,7 @@ public class Migrator implements Disposable {
private final File file;
private final Flyway flyway;

public Migrator(File pluginDirectory, Logger logger) {
public Migrator(File pluginDirectory, JavaMigration... migrations) {

this.file = new File(pluginDirectory, ".flyway.sqlite");
if (!file.exists()) {
Expand All @@ -38,7 +38,9 @@ public Migrator(File pluginDirectory, Logger logger) {

flyway = Flyway.configure()
.dataSource(dataSource)
//.callbacks((String[]) null /* Callbacks here ... */)
.baselineVersion("5.0.0")
.javaMigrations(migrations)
.locations("classpath:db/migration")
.load();
}

Expand Down
2 changes: 1 addition & 1 deletion pathfinder-test-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "de.cubbossa"
version = "4.6.0"
version = "5.0.0"

repositories {
mavenCentral()
Expand Down

0 comments on commit e48371e

Please sign in to comment.