Skip to content
This repository was archived by the owner on Mar 6, 2021. It is now read-only.

Tutorial: Using AConfig

Aurium_ @ElytraForce edited this page Feb 7, 2021 · 3 revisions

Foreword

Configs are annoying in spigot. Everything is stringly typed and annoying to use, and you have to provide a YAML config file. That's absolutely awful. AConfig is a solution for this.

Furthermore, AConfig isn't version specific. Deploy it into bungee or spigot, and it will still work.

Disclaimer: AConfig currently only works with primitive types. I'll add collection support later when i'm not playing Synthetik

another disclaimer at the bottom

How to get started

Configs are easy with AUtils.

Make a config class extending AConfig, and optionally mark it with annotation @Singleton.

Then implement the method filePosition, which will be the file position of the generated config relative to your plugin's data folder. In the future, this will support File and better filepaths than stringly typed bullshit but laziness

@Singleton
public class ConfigTest extends AConfig {
   @Override
    public String filePosition() {
        return "cheeseman.yml";
    }
}

On loading, this config will generate into the file cheeseman.yml, and will be empty. Let's fix that!

Defining your config

Now, the fun part. Setting up config fields.

Let's say I want a few configurable options.

  • A String called pluginPrefix that stores the prefix of the plugin
  • An Integer called countdownTimer that is the length of the config
  • A boolean called isLockdown that stores if the plugin is on lockdown mode or not

With spigot's default system, you'd need to make a config.yml and provide it, and do all sorts of annoying loading/saving/resetting.


isLockdown: true

Stuff:
  pluginPrefix: `coolPlugin`
  countdownTimer: 15

In AConfig, you simply define fields into your command class and then annotate them with the annotation @AField. These fields must be public.

@Singleton
public class ConfigTest extends AConfig {
   @Override
    public String filePosition() {
        return "cheeseman.yml";
    }

#fields

   @AField(location="Stuff.pluginPrefix")
   public String pluginPrefix = "coolPlugin";

   @AField(location="Stuff.countdownTimer")
   public Integer countdownTimer = 15;

   @AField(location = "isLockdown")
   public boolean isLockdown = true;

}

The field location in AField determines where in the config the value is located. If it is not present, the location will be determined based on the field name. (public boolean bad = true; with no location defined will be the value of "bad" in the config.)

These fields must be public.

The values you define the fields as are the defaults: If they are not present when the file is loaded / created, they will be written to the config.

This means even when updating your plugin and adding new fields, the configs can stay the same: New values will be autogenerated based on the field definitions while old values will simply be loaded from the file position.

You can also add comments to this text with the annotation parameter of comment="some comment here".

Once you have laid out your config, the location of fields, and the default values of fields, it's time to load and interact with your config.

Loading your config.

Use GUICE to inject your config to a class if your config is defined as a singleton. Then, call the methods AConfig#create and AConfig#load. Create will create the file and Load will load defaults if not present, current config data if present.

@Inject private ClassExtendingAConfig config

public void onEnable() {
  config.create().load();
}

After calling these methods, your config is safe to interact with.

Interacting with the config

Interacting with the config in ACONFIG is super easy! No more config.get or any of that bullshit.

Interacting with data by simply retrieving it is the most simple to do: Get an instance of your config and retrieve the field as if it were static. config.field and tada! An accurate value of what is currently stored in the config.

Setting data is a bit more difficult, but still a walk in the park, with no stupid io shit crap fuck.

Edit your field config.field = "ShitAss"

And then when you are done editing / writing to config values, run a save.

config.save();

public class Example {
   private TestConfig config;

   //simple constructor
   public Example(TestConfig config) {
      this.config = config;
   }

   public void interactAndGetDataFromConfigMethod() {
       if (config.isLockdown) {
               //do shit here
       }

        System.out.println(config.pluginPrefix)
   }

   public void interactAndSaveDataToConfig(boolean newValue) {
       config.isLockdown = newValue
       config.save();
   }
}

To reload your config, simply call config.load() again. This will erase any in-memory changes to the config that were not saved via the save method.

another fucking disclaimer

Disclaimer 2:

Yes, this breaks encapsulation rules. No, i do not care. I wrote the config system a while back and do not feel like rewriting it, and it works fine for anyone wanting simple config fun sexy time. If you give enough shits, use a config system that handles encapsulation via method like DazzleConf

-Aurium

Sidebar

Clone this wiki locally