Class MagstarConfig<T extends ConfigHandler>
- Direct Known Subclasses:
DatabaseReader,GUIReader,MessageTemplate
A MagstarConfig represents a YAML configuration file associated with a specific
JavaPlugin. It provides mechanisms for:
- Loading and reloading configurations from file.
- Saving configurations asynchronously with thread-safety guarantees.
- Handling special annotated configs:
DefaultConfig: identifies the default plugin config (e.g.config.yml).MessageConfig: identifies message/localization configs.
- Convenience methods for fetching message strings or prefixes with color codes translated.
Usage Example
public class MyConfig extends MagstarConfig<ConfigHandlers> {
// This constructor method must be no-arg constructor.
MyConfig() {
super(Main.getInstance().getConfigHandler());
// Custom initial logics...
}
@Override
@DefaultConfig
public File initFile() {
return new File(getPlugin().getDataFolder(), "config.yml");
}
@Override
public JavaPlugin initPlugin() {
return MyPlugin.getInstance();
}
}
MyConfig cfg = new MyConfig();
String prefix = cfg.getPrefix(); // reads prefix from config.yml
Thread-safety: Config saves are protected with a Lock
and always executed asynchronously via BukkitRunnable.
- Since:
- 1.0
- Author:
- Magstar
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal org.bukkit.configuration.file.FileConfigurationGets (and if necessary, loads) the configuration from file.final StringgetMessage(String s) Gets a formatted message string from this configuration.final StringGets the configured prefix from this file.final SmartStringGets a formatted message string from this configuration.abstract FileinitFile()Initialize and return theFileobject representing the configuration file.abstract org.bukkit.plugin.java.JavaPluginInitialize and return theJavaPlugininstance that owns this configuration.final voidreload()Reloads the configuration from disk.final voidsaveConfig(org.bukkit.configuration.file.FileConfiguration config) Saves the given configuration to disk asynchronously.
-
Field Details
-
plugin
protected final org.bukkit.plugin.java.JavaPlugin plugin -
config
protected org.bukkit.configuration.file.FileConfiguration config
-
-
Constructor Details
-
MagstarConfig
-
-
Method Details
-
initFile
-
initPlugin
public abstract org.bukkit.plugin.java.JavaPlugin initPlugin()Initialize and return theJavaPlugininstance that owns this configuration.Subclasses must implement this method.
- Returns:
- the plugin instance
-
getConfig
public final org.bukkit.configuration.file.FileConfiguration getConfig()Gets (and if necessary, loads) the configuration from file.If the file does not exist, it will be created depending on whether it is marked as
DefaultConfigorMessageConfig.- Returns:
- the loaded
FileConfiguration
-
reload
public final void reload()Reloads the configuration from disk.Useful when external changes are made to the YAML file while the server is running.
-
saveConfig
public final void saveConfig(org.bukkit.configuration.file.FileConfiguration config) Saves the given configuration to disk asynchronously.The save operation is thread-safe and protected by a
Lock.- Parameters:
config- the configuration to save
-
getMessage
Gets a formatted message string from this configuration.Only available if the class is annotated with
MessageConfig.All ampersand color codes
&are replaced with§.- Parameters:
s- the path of the message in the config- Returns:
- the formatted message string
- Throws:
IllegalStateException- if the config class is not annotated asMessageConfig
-
getSmartMessage
Gets a formatted message string from this configuration.Only available if the class is annotated with
MessageConfig.SmartStringformatted color tags supported.- Parameters:
s- the path of the message in the config- Returns:
- the formatted SmartString message.
- Throws:
IllegalStateException- if the config class is not annotated asMessageConfig
-
getPrefix
Gets the configured prefix from this file.Only available if the class is annotated with
DefaultConfig.All ampersand color codes
&are replaced with§.- Returns:
- the formatted prefix string
- Throws:
IllegalStateException- if the config class is not annotated asDefaultConfig
-