Skip to content

Commit

Permalink
Merge pull request #49 from IllusionTheDev/2.0-new-databases
Browse files Browse the repository at this point in the history
2.0 new databases
  • Loading branch information
IllusionTheDev authored Oct 25, 2023
2 parents 1faadd3 + f8e4bdb commit 1341702
Show file tree
Hide file tree
Showing 83 changed files with 2,606 additions and 1,791 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.illusion.skyblockcore.bungee;

import java.io.File;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import lombok.Getter;
Expand All @@ -9,11 +10,10 @@
import me.illusion.skyblockcore.common.command.audience.SkyblockAudience;
import me.illusion.skyblockcore.common.command.manager.SkyblockCommandManager;
import me.illusion.skyblockcore.common.config.SkyblockMessagesFile;
import me.illusion.skyblockcore.common.config.impl.SkyblockCacheDatabasesFile;
import me.illusion.skyblockcore.common.config.impl.SkyblockDatabasesFile;
import me.illusion.skyblockcore.common.database.SkyblockDatabaseRegistry;
import me.illusion.skyblockcore.common.database.registry.SkyblockDatabaseRegistry;
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManager;
import me.illusion.skyblockcore.common.event.manager.SkyblockEventManagerImpl;
import me.illusion.skyblockcore.common.utilities.file.IOUtils;
import me.illusion.skyblockcore.proxy.SkyblockProxyPlatform;
import me.illusion.skyblockcore.proxy.command.PlaySkyblockCommand;
import me.illusion.skyblockcore.proxy.config.SkyblockMatchmakingFile;
Expand All @@ -30,8 +30,6 @@
@Getter
public class SkyblockBungeePlugin extends Plugin implements SkyblockProxyPlatform {

private SkyblockCacheDatabasesFile cacheDatabasesFile;
private SkyblockDatabasesFile databasesFile;
private SkyblockMatchmakingFile matchmakingFile;

private SkyblockDatabaseRegistry databaseRegistry;
Expand All @@ -51,8 +49,6 @@ public void onEnable() {

commandManager = new BungeeSkyblockCommandManager(this);

cacheDatabasesFile = new SkyblockCacheDatabasesFile(this);
databasesFile = new SkyblockDatabasesFile(this);
matchmakingFile = new SkyblockMatchmakingFile(this);
messagesFile = new SkyblockMessagesFile(this, "proxy-messages");

Expand Down Expand Up @@ -84,22 +80,25 @@ private void initMatchmaking() {
}

private void loadDatabase() {
databaseRegistry.tryEnableMultiple(cacheDatabasesFile, databasesFile).thenAccept(success -> {
if (Boolean.FALSE.equals(success)) { // The future returns a boxed boolean
getLogger().severe("Failed to enable databases. Disabling plugin.");
disableExceptionally();
File databasesFolder = new File(getDataFolder(), "databases");

IOUtils.traverseAndLoad(databasesFolder, file -> {
if (!file.getName().endsWith(".yml")) {
return;
}

initMatchmaking();
databaseRegistry.loadPossible(configurationProvider.loadConfiguration(file));
});

databaseRegistry.finishLoading().thenRun(() -> {
finishEnable();
initMatchmaking();
});
}

@Override
public void onDisable() {
databaseRegistry.getChosenCacheDatabase().flush().join();
databaseRegistry.getChosenDatabase().flush().join();
databaseRegistry.shutdown().join();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package me.illusion.skyblockcore.bungee.config;

import java.io.File;
import java.io.IOException;
import me.illusion.skyblockcore.bungee.SkyblockBungeePlugin;
import me.illusion.skyblockcore.bungee.utilities.config.BungeeConfigurationAdapter;
import me.illusion.skyblockcore.bungee.utilities.storage.BungeeYMLBase;
import me.illusion.skyblockcore.common.config.ConfigurationProvider;
import me.illusion.skyblockcore.common.config.ReadOnlyConfigurationSection;
import me.illusion.skyblockcore.common.config.section.ConfigurationSection;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.YamlConfiguration;

/**
* Bungee implementation of {@link ConfigurationProvider}.
Expand All @@ -24,8 +27,23 @@ public File getDataFolder() {
}

@Override
public ReadOnlyConfigurationSection loadConfiguration(File file) {
public ConfigurationSection loadConfiguration(File file) {
BungeeYMLBase base = new BungeeYMLBase(plugin, file, true);
return BungeeConfigurationAdapter.adapt("", base.getConfiguration());
return BungeeConfigurationAdapter.adapt(file, this, "", base.getConfiguration());
}

@Override
public void saveConfiguration(ConfigurationSection section, File file) {
Configuration configuration = new Configuration();

BungeeConfigurationAdapter.writeTo(section, configuration);

net.md_5.bungee.config.ConfigurationProvider provider = net.md_5.bungee.config.ConfigurationProvider.getProvider(YamlConfiguration.class);

try {
provider.save(configuration, file);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package me.illusion.skyblockcore.bungee.utilities.config;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import me.illusion.skyblockcore.common.config.ReadOnlyConfigurationSection;
import me.illusion.skyblockcore.common.config.ConfigurationProvider;
import me.illusion.skyblockcore.common.config.section.ConfigurationSection;
import me.illusion.skyblockcore.common.config.section.WritableConfigurationSection;
import net.md_5.bungee.config.Configuration;

public final class BungeeConfigurationAdapter {
Expand All @@ -12,13 +15,13 @@ private BungeeConfigurationAdapter() {
}

/**
* Adapt a {@link Configuration} to a {@link ReadOnlyConfigurationSection}, flattening the section
* Adapt a {@link Configuration} to a {@link ConfigurationSection}, flattening the section
*
* @param name The name of the section, this parameter only exists because Bungee's configuration system does not store the name of the section
* @param configuration The configuration to adapt
* @return The adapted section
*/
public static ReadOnlyConfigurationSection adapt(String name, Configuration configuration) {
public static ConfigurationSection adapt(File file, ConfigurationProvider provider, String name, Configuration configuration) {
if (configuration == null) {
return null;
}
Expand All @@ -30,11 +33,11 @@ public static ReadOnlyConfigurationSection adapt(String name, Configuration conf
Object value = entry.getValue();

if (value instanceof Configuration) {
map.put(key, adapt(key, (Configuration) value));
map.put(key, adapt(file, provider, key, (Configuration) value));
}
}

return new ReadOnlyConfigurationSection(name, map);
return new WritableConfigurationSection(name, map, file, provider);
}

/**
Expand All @@ -52,4 +55,18 @@ public static Map<String, Object> getValues(Configuration configuration) {

return map;
}

public static void writeTo(ConfigurationSection skyblockSection, Configuration configuration) {
for (String key : skyblockSection.getKeys()) {
Object value = skyblockSection.get(key);

if (value instanceof ConfigurationSection section) {
Configuration subSection = configuration.getSection(key);

writeTo(section, subSection);
} else {
configuration.set(key, value);
}
}
}
}
2 changes: 1 addition & 1 deletion SkyblockCore-Common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies {
implementation 'com.google.guava:guava:32.1.1-jre'
implementation 'redis.clients:jedis:5.0.1'

implementation 'org.mongodb:mongodb-driver-sync:4.10.2'
compileOnly 'org.mongodb:mongodb-driver-sync:4.10.2'
}

def targetJavaVersion = 17
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.illusion.skyblockcore.common.config;

import java.io.File;
import me.illusion.skyblockcore.common.config.section.ConfigurationSection;
import me.illusion.skyblockcore.common.platform.SkyblockPlatform;

/**
Expand All @@ -10,7 +11,7 @@ public abstract class AbstractConfiguration {

private final File file;
private final ConfigurationProvider provider;
protected ReadOnlyConfigurationSection configuration;
protected ConfigurationSection configuration;

protected AbstractConfiguration(SkyblockPlatform platform, String fileName) {
provider = platform.getConfigurationProvider();
Expand All @@ -24,7 +25,7 @@ protected AbstractConfiguration(SkyblockPlatform platform, String fileName) {
*
* @return The configuration section of this configuration.
*/
public ReadOnlyConfigurationSection getConfiguration() {
public ConfigurationSection getConfiguration() {
return configuration;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.illusion.skyblockcore.common.config;

import java.io.File;
import me.illusion.skyblockcore.common.config.section.ConfigurationSection;

/**
* Represents a configuration provider, which is used to load configuration files.
Expand All @@ -20,16 +21,24 @@ public interface ConfigurationProvider {
* @param file The file to load the configuration from.
* @return The configuration section of the file.
*/
ReadOnlyConfigurationSection loadConfiguration(File file);
ConfigurationSection loadConfiguration(File file);

/**
* Loads a configuration from a file name.
*
* @param fileName The name of the file to load the configuration from.
* @return The configuration section of the file.
*/
default ReadOnlyConfigurationSection loadConfiguration(String fileName) {
default ConfigurationSection loadConfiguration(String fileName) {
return loadConfiguration(new File(getDataFolder(), fileName));
}

/**
* Saves a configuration to a file.
*
* @param section The configuration section to save.
* @param file The file to save the configuration to.
*/
void saveConfiguration(ConfigurationSection section, File file);

}

This file was deleted.

Loading

0 comments on commit 1341702

Please sign in to comment.