-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Push config management work so far (#32)
- Loading branch information
Showing
20 changed files
with
427 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
main/src/main/java/org/minerift/ether/config/ConfigFileView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
main/src/main/java/org/minerift/ether/config/ConfigLoader.java
This file was deleted.
Oops, something went wrong.
67 changes: 67 additions & 0 deletions
67
main/src/main/java/org/minerift/ether/config/ConfigRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
16 changes: 0 additions & 16 deletions
16
main/src/main/java/org/minerift/ether/config/MainConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
main/src/main/java/org/minerift/ether/config/deprecated/MainConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
main/src/main/java/org/minerift/ether/config/exceptions/ConfigException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
main/src/main/java/org/minerift/ether/config/exceptions/ConfigFileReadException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
main/src/main/java/org/minerift/ether/config/exceptions/ConfigFileWriteException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
14 changes: 14 additions & 0 deletions
14
main/src/main/java/org/minerift/ether/config/exceptions/ConfigNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
main/src/main/java/org/minerift/ether/config/readers/IConfigReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
34 changes: 34 additions & 0 deletions
34
main/src/main/java/org/minerift/ether/config/readers/MainConfigReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
main/src/main/java/org/minerift/ether/config/readers/SchematicConfigReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.