Last active
March 3, 2018 14:04
-
-
Save HaedHutner/500648082d1a544da4a5b016501b49c1 to your computer and use it in GitHub Desktop.
[Configurate/Sponge] An abstract utility class for creating quick and simple HOCON configuration classes using an object mapper with a Sponge GameInitializationEvent Listener as an example.
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
public final class ExampleConfig extends PluginConfig { | |
@Setting( value = "defaultConfig", comment = "Whether or not this is the default config. If this is set to true, the plugin will not start.") | |
public boolean DEFAULT = true; | |
@Setting( value = "database" ) | |
public DatabaseConfig DATABASE = new DatabaseConfig(); | |
@ConfigSerializable | |
public static class DatabaseConfig { | |
@Setting(value = "host", comment = "The host ip address of MongoDB.") | |
public String HOST = "localhost"; | |
@Setting(value = "port", comment = "The port on which MongoDB is running.") | |
public int PORT = 27017; | |
@Setting(value = "name", comment = "The name of the database this plugin will use.") | |
public String NAME = "core_Database"; | |
@Setting(value = "userDb", comment = "The name of the user database which this plugin will reference for authentication.") | |
public String USER_DB = "user_Database"; | |
@Setting(value = "username", comment = "The username used for authentication.") | |
public String USERNAME = "username"; | |
@Setting(value = "password", comment = "The password used for authentication.") | |
public String PASSWORD = "password"; | |
} | |
public ExampleConfig( String directory ) throws IOException { | |
super( directory, "config.conf" ); | |
} | |
} |
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
ExampleConfig config; | |
@Listener | |
public void onInit (GameInitializationEvent event) { | |
try { | |
config = new ExampleConfig(); | |
config.init(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return; | |
} | |
if ( config.DEFAULT ) { | |
this.getLogger().error( "Configuration set to default. Plugin will not start. Modify 'defaultConfig' to false once non-default configuration values have been inserted." ); | |
return; | |
} | |
} |
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
/** | |
* An abstract utility class for creating quick and simple configuration classes using an object mapper. | |
*/ | |
public abstract class PluginConfig { | |
private boolean newFile = false; | |
protected ObjectMapper<PluginConfig>.BoundInstance configMapper; | |
protected ConfigurationLoader<CommentedConfigurationNode> loader; | |
/** | |
* This constructor will load all serializable fields ( the ones marked with {@link Setting} and {@link ConfigSerializable}, then | |
* attempt to create a HOCON file in the given directory with the given name and a {@link HoconConfigurationLoader} from that file. | |
* @param directory The directory where the config file will be saved. | |
* @param filename The name of the config file. | |
* @throws IOException when either the file or the directory could not be created. | |
*/ | |
protected PluginConfig( String directory, String filename ) throws IOException { | |
try { | |
this.configMapper = ObjectMapper.forObject(this); | |
} catch (ObjectMappingException e) { | |
e.printStackTrace(); | |
} | |
File configFile = new File( directory + "/" + filename ); | |
if ( !configFile.exists() ) { | |
if ( configFile.getParentFile().exists() || configFile.getParentFile().mkdirs() ) { | |
if (configFile.createNewFile()) { | |
newFile = true; | |
} else throw new IOException("Failed to create " + filename); | |
} else throw new IOException("Failed to create config directory " + directory); | |
} | |
this.loader = HoconConfigurationLoader.builder().setPath( configFile.toPath() ).build(); | |
} | |
/** | |
* Save the contents of the object mapper to the config file. This will override config values already-present in the file. | |
*/ | |
public void save() { | |
try { | |
SimpleConfigurationNode out = SimpleConfigurationNode.root(); | |
this.configMapper.serialize(out); | |
this.loader.save(out); | |
} catch (ObjectMappingException | IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Populate the object mapper with the contents of the config file. This will override any default values. | |
*/ | |
public void load() { | |
try { | |
this.configMapper.populate( this.loader.load() ); | |
} catch (ObjectMappingException | IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Initialize the config. | |
* If the config file had already existed, this will load values from the config file, overriding the defaults. | |
* If it did not, this will save to the file with the default values provided. | |
*/ | |
public void init() { | |
if ( newFile ) this.save(); | |
else this.load(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment