The AbstractConfig class defines the foundation for managing
configuration files in Bukkit/Spigot-based plugins.
Its goal is to provide an abstract layer that unifies: - Loading and
saving YAML files. - Internal cache for fast value access. - Strong
typing system using enum as keys. - Automatic processing of formatted
messages.
This architecture eliminates repetitive code across different configurations and allows the creation of robust, safe, and consistent configurations throughout the plugin.
- The storage location for data, typically in YAML format.
- Examples:
config.yml,messages.yml.
- Each configuration key is represented by an
enumimplementing theConfigKeyinterface. - Each key defines:
- Path in the YAML (
path). - Value type (
ValueType). - Default value (
defaultValue).
- Path in the YAML (
- Loaded values are stored in a
Map<Enum<?>, Object>. - Avoids repeated file access.
- Allows fast retrieval with utility methods (
getString,getInt, etc.).
Standardizes access to configuration keys:
public interface ConfigKey {
String getPath(); // YAML path
ValueType getType(); // Value type
Object getDefaultValue(); // Default value
}Defines supported configuration value types:
STRINGSTRING_LISTINTINT_LISTBOOLEANDOUBLE
Each type is mapped to its corresponding Java class.
public enum MyKeys implements ConfigKey {
ENABLE_FEATURE("my-section.enable-feature", ValueType.BOOLEAN, true),
MAX_PLAYERS("my-section.max-players", ValueType.INT, 10),
WELCOME_MESSAGE("my-section.welcome", ValueType.STRING, "&aWelcome!");
private final String path;
private final ValueType type;
private final Object defaultValue;
MyKeys(String path, ValueType type, Object defaultValue) {
this.path = path;
this.type = type;
this.defaultValue = defaultValue;
}
@Override
public String getPath() { return path; }
@Override
public ValueType getType() { return type; }
@Override
public Object getDefaultValue() { return defaultValue; }
}public class MyConfig extends AbstractConfig {
public MyConfig(File file) {
super(file);
loadValues(MyKeys.class, "my-section");
}
@Override
protected Object getDefaultValue(Enum<?> key) {
return ((MyKeys) key).getDefaultValue();
}
@Override
protected ConfigKey getConfigKey(Enum<?> key) {
return (MyKeys) key;
}
}my-section:
enable-feature: true
max-players: 15
welcome: "&bWelcome to the server!"MyConfig config = new MyConfig(new File(plugin.getDataFolder(), "config.yml"));
boolean enabled = config.getBoolean(MyConfig.MyKeys.ENABLE_FEATURE);
int maxPlayers = config.getInt(MyConfig.MyKeys.MAX_PLAYERS);
String welcomeMessage = config.getString(MyConfig.MyKeys.WELCOME_MESSAGE);
config.setValue(MyConfig.MyKeys.MAX_PLAYERS, 20); // Update and save to fileAll values of type STRING or STRING_LIST go through the
processMessage method, which uses GradientMessage for formatted
message processing.
This allows gradient-colored messages to be declared directly in YAML.
- The plugin instantiates the concrete class extending
AbstractConfig. - The
loadValuesmethod loads enum-defined keys into the cache. - The developer accesses values using typed methods (
getString,getInt, etc.). - Invalid or missing YAML values are automatically replaced with the default value defined in the enum.
- Updates are performed via
setValue, which updates both cache and file.
- Centralized configuration logic.
- Elimination of code duplication.
- Strongly typed consistency in value access.
- Native support for gradient messages.
- Easy maintenance and expansion.