Skip to content

Commit

Permalink
bungee config loader
Browse files Browse the repository at this point in the history
  • Loading branch information
michaljaz committed Jan 2, 2024
1 parent d9de399 commit a24283f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package app.shopmc.plugin.bungee;

import app.shopmc.plugin.resource.ResourceLoader;
import app.shopmc.plugin.resource.ResourceLoaderException;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;

import java.io.File;
import java.nio.file.Path;

public class BungeeResourceLoader extends ResourceLoader<Configuration> {

public BungeeResourceLoader(final Class<?> loadingClass, final File dataFolder) {
super(loadingClass, dataFolder.toPath());
}

@Override
protected Configuration loadResource(final Path resourcePath) throws ResourceLoaderException {
try {
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(resourcePath.toFile());
} catch (final Exception exception) {
throw new ResourceLoaderException(ResourceLoaderException.Reason.FILE_NOT_LOADED, exception);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import app.shopmc.plugin.config.Config;
import app.shopmc.plugin.config.EmptyConfigFieldException;
import app.shopmc.plugin.resource.ResourceLoader;
import app.shopmc.plugin.resource.ResourceLoaderException;
import app.shopmc.plugin.router.Socket;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.YamlConfiguration;
import org.java_websocket.handshake.ServerHandshake;

Expand All @@ -16,7 +19,6 @@

import static net.md_5.bungee.config.ConfigurationProvider.getProvider;

@SuppressWarnings("ResultOfMethodCallIgnored")
public class BungeeShopMCPlugin extends Plugin {
private Socket socket;
public static Config config;
Expand All @@ -26,24 +28,21 @@ public class BungeeShopMCPlugin extends Plugin {
@Override
public void onEnable() {
// init config file
File dataFolder = getDataFolder();
dataFolder.mkdirs();
File configFile = new File(dataFolder, "config.yml");
if (!configFile.exists()) {
try {
configFile.createNewFile();
} catch (IOException e) {
getLogger().log(Level.SEVERE, "Error creating config file", e);
final ResourceLoader<Configuration> resourceLoader = new BungeeResourceLoader(this.getClass(), this.getDataFolder());
try {
if (resourceLoader.saveDefault("config.yml")) {
this.getLogger().info("Default file config.yml has been saved, configure it and restart proxy");
return;
}
}

// check if config is correct
try {
config = new Config(new BungeeConfigLoader(getProvider(YamlConfiguration.class).load(configFile)));
} catch (EmptyConfigFieldException | IOException exception) {
getLogger().log(Level.SEVERE, exception.getMessage());
proxyServer.getPluginManager().unregisterCommands(this);
return;
try {
final Configuration cfgFile = resourceLoader.load("config.yml");
config = new Config(new BungeeConfigLoader(cfgFile));
} catch (final EmptyConfigFieldException exception) {
this.getLogger().severe(exception.getMessage());
}
} catch (final ResourceLoaderException exception) {
this.getLogger().severe("Reason: " + exception.getCause().getMessage());
}

socket = new Socket(config.key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package app.shopmc.plugin.resource;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

public abstract class ResourceLoader<T> {

private final Class<?> loadingClass;
private final Path dataDirectory;

protected ResourceLoader(final Class<?> loadingClass, final Path dataDirectory) {
this.loadingClass = loadingClass;
this.dataDirectory = dataDirectory;
}

protected abstract T loadResource(final Path resourcePath) throws ResourceLoaderException;

public boolean saveDefault(final String resourceName) throws ResourceLoaderException {
if (!Files.exists(this.dataDirectory)) {
try {
Files.createDirectories(this.dataDirectory);
} catch (final Exception exception) {
throw new ResourceLoaderException(ResourceLoaderException.Reason.DIRECTORY_NOT_CREATED, exception);
}
}

final Path resourcePath = this.dataDirectory.resolve(resourceName);
if (!Files.exists(resourcePath)) {
try (final InputStream in = this.loadingClass.getClassLoader().getResourceAsStream(resourceName)) {
Files.copy(Objects.requireNonNull(in), resourcePath);
return true;
} catch (final Exception exception) {
throw new ResourceLoaderException(ResourceLoaderException.Reason.DEFAULT_FILE_NOT_SAVED, exception);
}
}

return false;
}

public T load(final String resourceName) throws ResourceLoaderException {
return this.loadResource(this.dataDirectory.resolve(resourceName));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package app.shopmc.plugin.resource;

public class ResourceLoaderException extends Exception {

private final Reason reason;

public ResourceLoaderException(final Reason reason, final Throwable cause) {
super(cause);
this.reason = reason;
}

public Reason getReason() {
return this.reason;
}

public enum Reason {
DIRECTORY_NOT_CREATED("Nie udało się utworzyć folderu pluginu"),
DEFAULT_FILE_NOT_SAVED("Nie udało się zapisać domyślnego pliku %s"),
FILE_NOT_LOADED("Nie udało się otworzyć pliku %s");

private final String message;

Reason(final String message) {
this.message = message;
}

public String getMessage(final String fileName) {
return String.format(this.message, fileName);
}
}

}

0 comments on commit a24283f

Please sign in to comment.