// The main class of your plugin, should extends from MinorityExtension (not from JavaPlugin as usual)
public class MinoritySamplePlugin extends MinorityExtension {
@Override
public void onEnable() {
super.getLogger().info("So much space! Need to see it all!");
}
}
As you may guess, MinorityExtension extends from JavaPlugin, so it has all the methods and other stuff that you're looking for. You can use it as main class in your plugin.yml and everything will work as entended.
Now we need some simple feature that we might want to configure later.
@Configurable(path = "exploding-cows", comment = "Ever dreamed of exploding cows? Now you have such a feature.")
public class CowExplosionFeature implements MinorityFeature, Listener {
@ConfigurationKey(name = "enable", type = Type.BOOLEAN, value = "true")
private boolean enabled;
@ConfigurationKey(name = "explosion-power", type = Type.INTEGER, value = "4", comment = "Explosion power levels: 4 is TNT, 6 is a charged creeper, 9999 is probably a nuke. Nuke-cowa.")
private int power;
@ConfigurationKey(name = "diffuse-material", type = Type.ENUM, value = "SLIME_BALL", comment = "If cow eat this material, it won't explode.")
private Material diffuser;
@ConfigurationKey(name = "death-message", value = "%s was cow-blasted")
private String deathMessage;
}
Looks impressive, doesn't it? It's only the beginning! The @Configurable annotation not only marks the class as configurable, but also accepts parameters. I won't describe them here, look for them in a separate article on annotations.
It is also worth noting that the @ConfigurationKey annotation takes the field type as a parameter. This is done for automatic initialization (more on that below), but if you don't plan to use it, then specifying the type is optional. The default type is Type.STRING.
Now we can add a constructor and some gameplay functionality.
@Configurable(path = "exploding-cows", comment = "Ever dreamed of exploding cows? Now you have such a feature.")
public class CowExplosionFeature implements MinorityFeature, Listener {
@ConfigurationKey(name = "enable", type = Type.BOOLEAN, value = "true")
private boolean enabled;
@ConfigurationKey(name = "explosion-power", type = Type.INTEGER, value = "4", comment = "Explosion power levels: 4 is TNT, 6 is a charged creeper, 9999 is probably a nuke. Nuke-cowa.")
private int power;
@ConfigurationKey(name = "diffuse-material", type = Type.ENUM, value = "SLIME_BALL", comment = "If cow eat this material, it won't explode.")
private Material diffuser;
@ConfigurationKey(name = "death-message", value = "%s was cow-blasted")
private String deathMessage;
// Constructor
public CowExplosionFeature(final MinorityExtension plugin) {
// Create a new config (or add these keys above to the existing one)
plugin.getConfigurationWizard().generate(this.getClass());
// You can init fields this way, but I've got something better
// this.enabled = plugin.getConfig().getBoolean("exploding-cows.enable");
// Automatic initialization! All fields with the @ConfigurationKey annotation will be automatically initialized!
this.init(this, this.getClass(), plugin);
if (enabled) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
plugin.getLogger().info("CowExplosionFeature is enabled.");
}
}
@EventHandler
private void onCowDeath(final EntityDeathEvent event) {
if (event.getEntity() instanceof Cow cow) {
cow.getWorld().createExplosion(cow.getLocation(), this.power);
}
}
/* Some other functionality.. */
}
If you want to use automatic initialization, you should not make @ConfigurationKey annotated fields final or static! Otherwise reflection will not work.
Result
config.yml
# This configuration file was automatically generated with Minority.
exploding-cows:
enable: 'true'
# Explosion power levels: 4 is TNT, 6 is a charged creeper, 9999 is probably a nuke. Nuke-cowa.
explosion-power: '4'
# If cow eat this material, it won't explode.
diffuse-material: SLIME_BALL
death-message: '%s was cow-blasted'
And that's it! Minority generated a config file and initialized the class fields. It is worth saying that Minority creates a file and adds configuration keys only if there are none. That is, if we shutdown the server, open config.yml and change any value we need, then the next time we start it, it will be used, and not the default one.
Also, if we add some new feature with its own settings, config.yml will be automatically updated! No more manual config updates!