-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Hutch79/rewrite/everything
Rewrite/everything
- Loading branch information
Showing
29 changed files
with
824 additions
and
285 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
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
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
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
2 changes: 1 addition & 1 deletion
2
...n/java/ch/hutch79/command/CommandTab.java → ...tch79/application/command/CommandTab.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
54 changes: 54 additions & 0 deletions
54
src/main/java/ch/hutch79/application/configManager/ConfigManager.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 ch.hutch79.application.configManager; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
|
||
public class ConfigManager { | ||
|
||
private String _pluginPath; | ||
private ObjectMapper writeMapper; | ||
private ObjectMapper readMapper; | ||
private HashMap<Class<?>, Object> configCache = new HashMap<>(); | ||
public ConfigManager(@NotNull File pluginPath) { | ||
_pluginPath = pluginPath.toString(); | ||
|
||
writeMapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER)); | ||
writeMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); | ||
writeMapper.findAndRegisterModules(); | ||
|
||
readMapper = new ObjectMapper(new YAMLFactory()); | ||
readMapper.findAndRegisterModules(); | ||
} | ||
|
||
|
||
public void writeConfig (Object configClass, String localPath) throws FileNotFoundException { | ||
try { | ||
writeMapper.writeValue(new File(_pluginPath + File.separator + localPath), configClass); | ||
} catch (IOException e) { | ||
throw new FileNotFoundException(); | ||
} | ||
} | ||
|
||
public <T> ConfigManager loadConfig(Class<?> configClass, String localPath) throws FileNotFoundException { | ||
T config; | ||
try { | ||
config = (T) readMapper.readValue(new File(_pluginPath + File.separator + localPath), configClass); | ||
} catch (IOException e) { | ||
throw new FileNotFoundException(); | ||
} | ||
configCache.put(configClass, config); | ||
return this; | ||
} | ||
|
||
public <T> T getConfig(Class<?> configClass) { | ||
return (T) configCache.get(configClass); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/ch/hutch79/application/configManager/ConfigMigrator.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,33 @@ | ||
package ch.hutch79.application.configManager; | ||
|
||
import ch.hutch79.application.configManager.Migrations.MigrationV1; | ||
import ch.hutch79.domain.configs.v1.Config; | ||
import ch.hutch79.application.messages.ConsoleMessanger; | ||
import com.google.inject.Inject; | ||
import com.google.inject.Injector; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
|
||
|
||
public class ConfigMigrator { | ||
|
||
@Inject | ||
public ConfigMigrator(Injector injector, ConfigManager configManager) throws IOException { | ||
ConsoleMessanger messanger = new ConsoleMessanger(); | ||
try { | ||
configManager.loadConfig(Config.class, "config.yml"); | ||
} catch (Exception e1) { | ||
try { | ||
var migrationV1 = injector.getInstance(MigrationV1.class); | ||
Config configNew = migrationV1.configMigration(Paths.get("plugins" + File.separator + "F-Command")); | ||
configManager.writeConfig(configNew, "config.yml"); | ||
} catch (Exception e2) { | ||
messanger.message("§cThe Config Migration failed. Therefore the Plugin will be disabled."); | ||
throw e2; | ||
} | ||
} | ||
configManager.loadConfig(Config.class, "config.yml"); | ||
} | ||
} |
Oops, something went wrong.