Skip to content

Commit

Permalink
Push config management work so far (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
avaerian committed Sep 18, 2023
1 parent a6af587 commit ce7fb46
Show file tree
Hide file tree
Showing 20 changed files with 427 additions and 31 deletions.
36 changes: 36 additions & 0 deletions main/src/main/java/org/minerift/ether/config/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.minerift.ether.config;

import org.minerift.ether.Ether;
import org.minerift.ether.EtherPlugin;
import org.minerift.ether.config.exceptions.ConfigFileReadException;
import org.minerift.ether.config.types.ConfigType;

import java.io.File;
import java.util.logging.Level;

public abstract class Config<T extends Config<T>> {

public File getPluginDirectory() {
return EtherPlugin.getInstance().getDataFolder();
}

public void save() {
getType().getWriter().write((T) this);
}

public void reload() {
// Loads from file again
try {
T reload = getType().getReader().read(getType());
copyFrom(reload);
} catch (ConfigFileReadException ex) {
Ether.getLogger().log(Level.SEVERE, String.format("Failed to read %s", getType().getName()));
ex.printStackTrace();
}
}

// Copies data from a similar config over to this config
protected abstract void copyFrom(T other);

public abstract ConfigType<T> getType();
}
54 changes: 54 additions & 0 deletions main/src/main/java/org/minerift/ether/config/ConfigFileView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.minerift.ether.config;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.minerift.ether.util.Result;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

// Wrapper class for the YamlConfiguration Bukkit API
public class ConfigFileView {

private final ConfigurationSection head;

public static Result<ConfigFileView, IOException> from(File file) {

final Result<ConfigFileView, IOException> result = new Result<>();

// Attempt to load config file
final YamlConfiguration bukkitView = new YamlConfiguration();
try {
bukkitView.load(file);
result.ok(new ConfigFileView(bukkitView));
} catch (IOException ex) {
result.err(ex);
} catch (InvalidConfigurationException ex) {
result.err(new IOException(ex.getMessage()));
}

return result;
}

private ConfigFileView(ConfigurationSection head) {
this.head = head;
}

public Optional<Object> get(String path) {
return Optional.ofNullable(head.get(path, null));
}

public Optional<ConfigFileView> getSectionView(String path) {
final ConfigurationSection section = head.getConfigurationSection(path);
return Optional.ofNullable(section == null ? null : new ConfigFileView(section));
}

// TODO: create get() method with an adapter parameter

public ConfigurationSection getBukkitView() {
return head;
}

}
11 changes: 0 additions & 11 deletions main/src/main/java/org/minerift/ether/config/ConfigLoader.java

This file was deleted.

67 changes: 67 additions & 0 deletions main/src/main/java/org/minerift/ether/config/ConfigRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package org.minerift.ether.config;

import org.minerift.ether.config.exceptions.ConfigFileReadException;
import org.minerift.ether.config.types.ConfigType;

import java.io.IOException;
import java.util.*;

// TODO: ConfigRegistry should only handle registry operations (registering + loading/reading, unloading/writing, unregistering)
public class ConfigRegistry {

/**
* For reading configs:
* If a config file exists, attempt to read and log exceptions (if any)
* If a config file doesn't exist, return default config object
*
* For writing configs:
* write() should handle all data dumping to storage
* It will always "create a new file" (replacing old configs) to store the data.
*
*
*
* FOR REGISTERING:
* If a config file doesn't exist, load defined default (into memory)
* After loading, write to file for users to interact with
*
* If a config file does exist, attempt to load file.
*/

private final Map<ConfigType<?>, Config> configs;

public ConfigRegistry() {
this.configs = new HashMap<>();
}

// Register and load a config
// Returns the config read from the file
// If a config cannot be read, return the default config
public <T extends Config<T>> T register(ConfigType<T> type) {
T config;
try {
config = type.getReader().read(type);
} catch (ConfigFileReadException ex) {
// TODO: change this to throw the error and set the default config only if config doesn't exist
config = type.getDefaultConfig();
}
configs.putIfAbsent(type, config);
return config;
}

public <T extends Config<T>> T get(ConfigType<T> type) {
final T config = (T) configs.get(type);
if(config == null) {
throw new IllegalArgumentException(String.format("Config type %s was not found!", type.getName()));
}
return config;
}

public Collection<Config> getAll() {
return configs.values();
}

public Set<ConfigType<?>> getAllTypes() {
return configs.keySet();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.minerift.ether.config;
package org.minerift.ether.config.deprecated;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -7,6 +7,7 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ConfigurationFile {
@Deprecated
public @interface DeprecatedConfigurationFile {
String name();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.minerift.ether.config;
package org.minerift.ether.config.deprecated;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -7,6 +7,7 @@

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface YamlPath {
@Deprecated
public @interface DeprecatedYamlPath {
String path();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.minerift.ether.config.deprecated;

/**
* Represents the config.yml that is loaded into memory for ease-of-use
* @author Avaerian
*/
@Deprecated
@DeprecatedConfigurationFile(name = "config.yml")
public class MainConfig {

@DeprecatedYamlPath(path = "island.tile.size")
public final static int TILE_SIZE = 200; // default value for now

@DeprecatedYamlPath(path = "island.tile.height")
public final static int TILE_HEIGHT = 90;

// I plan on adding permissions to this and allowing for different tiers
@DeprecatedYamlPath(path = "island.tile.height")
public final static int TILE_ACCESSIBLE_AREA = 150; // default value for now; this is subject to change

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.minerift.ether.config.exceptions;

public class ConfigException extends Exception {
public ConfigException() {
super();
}

public ConfigException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.minerift.ether.config.exceptions;

import java.io.IOException;

/**
* An exception for any config reading error that occurs when parsing
* @author Avaerian
*/
public class ConfigFileReadException extends IOException {
public ConfigFileReadException() {}

public ConfigFileReadException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.minerift.ether.config.exceptions;

import java.io.IOException;

public class ConfigFileWriteException extends IOException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.minerift.ether.config.exceptions;

import java.io.IOException;

// TODO: reconsider
public class ConfigNotFoundException extends IOException {
public ConfigNotFoundException() {
super();
}

public ConfigNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.minerift.ether.config.readers;

import org.minerift.ether.config.Config;
import org.minerift.ether.config.exceptions.ConfigFileReadException;
import org.minerift.ether.config.types.ConfigType;

// Reads a file into a Config object
public interface IConfigReader<T extends Config<T>> {

// Reads a config as an object
// Throws an IOException if there are any issues with file access/IO
// Throws a ConfigFileReadException if the config fails to read/parse
T read(ConfigType<T> type) throws ConfigFileReadException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.minerift.ether.config.readers;

import org.minerift.ether.config.ConfigFileView;
import org.minerift.ether.config.exceptions.ConfigFileReadException;
import org.minerift.ether.config.types.ConfigType;
import org.minerift.ether.config.types.MainConfig;
import org.minerift.ether.util.Result;

public class MainConfigReader implements IConfigReader<MainConfig> {

@Override
public MainConfig read(ConfigType<MainConfig> type) throws ConfigFileReadException {

final Result<MainConfig, ConfigFileReadException> result = new Result<>();

// TODO
ConfigFileView.from(type.getFile()).handle((view) -> {

// Read islands as an example
view.getSectionView("user.islands").ifPresent((section) -> {
//section.get("");
});

},
// Delegate ConfigFileView error to result
(ex) -> result.err((ConfigFileReadException) ex));

if(result.isErr()) {
throw result.getErr();
}

return result.getOk();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.minerift.ether.config.readers;

import org.minerift.ether.config.exceptions.ConfigFileReadException;
import org.minerift.ether.config.types.ConfigType;
import org.minerift.ether.config.types.SchematicConfig;

import java.io.IOException;

public class SchematicConfigReader implements IConfigReader<SchematicConfig> {
@Override
public SchematicConfig read(ConfigType<SchematicConfig> type) throws ConfigFileReadException {
return null;
}
}
Loading

0 comments on commit ce7fb46

Please sign in to comment.