Class MagstarConfig<T extends ConfigHandler>

java.lang.Object
top.magstar.framework.configs.MagstarConfig<T>
Direct Known Subclasses:
DatabaseReader, GUIReader, MessageTemplate

public abstract class MagstarConfig<T extends ConfigHandler> extends Object
Abstract base class for managing plugin configuration files in the Magstar framework.

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:
  • 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
    Modifier and Type
    Field
    Description
    protected org.bukkit.configuration.file.FileConfiguration
     
    protected final org.bukkit.plugin.java.JavaPlugin
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    MagstarConfig(T configHandler)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final org.bukkit.configuration.file.FileConfiguration
    Gets (and if necessary, loads) the configuration from file.
    final String
    Gets a formatted message string from this configuration.
    final String
    Gets the configured prefix from this file.
    Gets a formatted message string from this configuration.
    abstract File
    Initialize and return the File object representing the configuration file.
    abstract org.bukkit.plugin.java.JavaPlugin
    Initialize and return the JavaPlugin instance that owns this configuration.
    final void
    Reloads the configuration from disk.
    final void
    saveConfig(org.bukkit.configuration.file.FileConfiguration config)
    Saves the given configuration to disk asynchronously.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • plugin

      protected final org.bukkit.plugin.java.JavaPlugin plugin
    • config

      protected org.bukkit.configuration.file.FileConfiguration config
  • Constructor Details

    • MagstarConfig

      public MagstarConfig(T configHandler)
  • Method Details

    • initFile

      public abstract File initFile()
      Initialize and return the File object representing the configuration file.

      Subclasses must implement this method.

      Returns:
      the configuration file reference
    • initPlugin

      public abstract org.bukkit.plugin.java.JavaPlugin initPlugin()
      Initialize and return the JavaPlugin instance 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 DefaultConfig or MessageConfig.

      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

      public final String getMessage(String s)
      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 as MessageConfig
    • getSmartMessage

      public final SmartString getSmartMessage(String s)
      Gets a formatted message string from this configuration.

      Only available if the class is annotated with MessageConfig.

      SmartString formatted 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 as MessageConfig
    • getPrefix

      public final String 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 as DefaultConfig