Skip to content

Commit

Permalink
Refactor config
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreeam-qwq committed Jun 28, 2024
1 parent 7c0c487 commit e876931
Show file tree
Hide file tree
Showing 24 changed files with 431 additions and 512 deletions.
8 changes: 5 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ val adventureVersion = "4.17.0"

dependencies {
compileOnly("io.papermc.paper:paper-api:1.21-R0.1-SNAPSHOT")
implementation("com.github.thatsmusic99:ConfigurationMaster-API:v2.0.0-rc.2")
implementation("com.github.thatsmusic99:ConfigurationMaster-API:v2.0.0-rc.2") {
exclude(group = "org.yaml", module = "snakeyaml")
}
implementation("org.bstats:bstats-bukkit:3.0.2")
implementation("com.tcoded:FoliaLib:0.4.0")
implementation("com.github.cryptomorin:XSeries:11.2.0")
Expand Down Expand Up @@ -74,11 +76,11 @@ tasks {
archiveFileName = "${project.name}-${project.version}.${archiveExtension.get()}"
exclude("META-INF/**") // Dreeam - Avoid to include META-INF/maven in Jar
relocate("net.kyori", "cn.dreeam.surf.libs.kyori")
relocate("org.yaml.snakeyaml", "cn.dreeam.surf.libs.snakeyaml")
relocate("space.arim.dazzleconf", "cn.dreeam.surf.libs.dazzleconf")
relocate("io.github.thatsmusic99.configurationmaster", "cn.dreeam.surf.libs.configurationmaster")
relocate("org.bstats", "cn.dreeam.surf.libs.bstats")
relocate("com.tcoded.folialib", "cn.dreeam.surf.libs.folialib")
relocate("com.cryptomorin.xseries", "cn.dreeam.surf.libs.xseries")
relocate("de.tr7zw.nbtapi", "cn.dreeam.surf.libs.nbtapi")
}

processResources {
Expand Down
33 changes: 25 additions & 8 deletions src/main/java/cn/dreeam/surf/Surf.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.dreeam.surf;

import cn.dreeam.surf.command.CommandHandler;
import cn.dreeam.surf.config.Config;
import cn.dreeam.surf.config.ConfigManager;
import cn.dreeam.surf.modules.antiillegal.CheckIllegal;
import cn.dreeam.surf.modules.antiillegal.CheckRoseStackerItem;
Expand Down Expand Up @@ -31,6 +30,10 @@
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

Expand All @@ -39,8 +42,7 @@ public class Surf extends JavaPlugin {
private static Surf instance;
public static Logger LOGGER;

public ConfigManager<Config> configManager;
public static Config config;
private static ConfigManager configManager;
private final PluginManager pluginManager = getServer().getPluginManager();
private final CommandHandler commandHandler = new CommandHandler(this);

Expand All @@ -55,9 +57,9 @@ public void onEnable() {
LOGGER = LogManager.getLogger(instance.getName());
instance.adventure = BukkitAudiences.create(instance);

instance.loadConfig();
loadConfig();
commandHandler.registerCommands(); // register commands
instance.registerEvents(); // register event
registerEvents(); // register event
new Metrics(instance, 16810);

LOGGER.info("Surf {} enabled. By Dreeam.", instance.getDescription().getVersion());
Expand Down Expand Up @@ -110,10 +112,22 @@ public void registerEvents() {
}
}

public void createDirectory(File dir) throws IOException {
try {
Files.createDirectories(dir.toPath());
} catch (FileAlreadyExistsException e) { // Thrown if dir exists but is not a directory
if (dir.delete()) createDirectory(dir);
}
}

public void loadConfig() {
configManager = ConfigManager.create(instance.getDataFolder().toPath(), "config.yml", Config.class);
configManager.reloadConfig();
config = configManager.getConfigData();
try {
createDirectory(getDataFolder());
configManager = new ConfigManager();
configManager.saveConfig();
} catch (Exception e) {
LOGGER.error("Failed to load config file!", e);
}
}

public static Surf getInstance() {
Expand All @@ -122,6 +136,9 @@ public static Surf getInstance() {
public CommandHandler getCommandHandler() {
return commandHandler;
}
public static ConfigManager configManager() {
return configManager;
}

public @NotNull BukkitAudiences adventure() {
if (this.adventure == null) {
Expand Down
Loading

0 comments on commit e876931

Please sign in to comment.