Skip to content

How to work with Enums

Fulminazzo edited this page Oct 2, 2023 · 1 revision

Welcome to BearCommands Wiki!
In this section we will look into working with the Custom Enums provided by the plugin. These classes are thought to help you easily create and manage your project features like configurations and more. There are three enums of interest:

Enums
BearConfigOption
BearLoggingMessage
BearPermission

BearConfigOption

The BearConfigOption class is an extension of ClassEnum that allows to easily work with your config.yml file. All you need to do is create a custom class that extends BearConfigOption and pass it an instance of your plugin.
Let's say that your plugin main class is called TestPlugin and contains a static method getPlugin() which returns an instance of itself. Then, an example of use would be:

package it.fulminazzo.testplugin.Enums;

import it.angrybear.Enums.BearConfigOption;
import it.fulminazzo.testplugin.TestPlugin;

public class TestConfig extends BearConfigOption {
    public static TestConfig NUMERIC_VALUE = new TestConfig("numeric-value");
    public static TestConfig LITERAL_VALUE = new TestConfig("literal-value");
    public static TestConfig ITEMSTACK = new TestConfig("itemstack-value");
    
    public TestConfig(String path) {
        super(TestPlugin.getPlugin(), path);
    }
}

And just like that you now have access to your config.yml values at the paths numeric-value, literal-value and itemstack-value. If you want to access them, all you have to do is get the static variable and call the appropriate "get" method:

/* ... */
    int numericValue = TestConfig.NUMERIC_VALUE.getInt(); 
    String literalValue = TestConfig.LITERAL_VALUE.getString(); 
    ItemStack itemstackValue = TestConfig.ITEMSTACK_VALUE.getItemStack(); 
/* ... */

NOTE: the getItemStack() and getMaterial() methods are only available on Bukkit platforms, while getColorCodes() is not available on Velocity. Also, getItemStack() will only work if the section at the given path matches certain criteria. Check Savable Objects for more.

BearLoggingMessage

BearLoggingMessage is a simple way to display log messages on console when required (info, warnings or errors). Its usage is similar to BearConfigOption but the plugin instance is not required. Also, it offers a getMessage() method to which you can pass many strings to be replaced. For example:

package it.fulminazzo.testplugin.Enums;

import it.angrybear.Enums.BearLoggingMessage;

public class TestLoggingMessage extends BearLoggingMessage {
    public static final TestLoggingMessage STARTUP_MESSAGE = new TestLoggingMessage("Plugin %plugin% successfully activated!");
    
    public TestLoggingMessage(String message) {
        super(message);
    }
}

Then in another class:

package it.fulminazzo.testplugin;

import it.angrybear.Bukkit.SimpleBearPlugin;
import it.fulminazzo.testplugin.Enums.TestLoggingMessage;

public class TestPlugin extends SimpleBearPlugin {

    @Override
    public void onEnable() {
        super.onEnable();
        //logInfo(TestLoggingMessage.STARTUP_MESSAGE.getMessage("%plugin%", getName()));
        logInfo(TestLoggingMessage.STARTUP_MESSAGE, "%plugin%", getName());
    }
}

NOTE: the IBearPlugin interface offers to all its subclasses the methods logInfo(), logWarning() and logError() that support as parameters both a String or a BearLoggingMessage with its replacements. Basically, the two logInfo codes are equivalent.

BearPermission

Finally we have BearPermission, a simple Enum class thought to just hold all the plugin permissions. This might seem useless, however, not only the BearPermissions are required by commands, but it is always recommended to keep all permissions in one place to later modify them. An example of use is that the BearPermission automatically uses the plugin name as prefix for all permissions, meaning that, if it changes, the permissions will with it.

package it.fulminazzo.testplugin.Enums;

import it.angrybear.Enums.BearPermission;
import it.fulminazzo.testplugin.TestPlugin;

public class TestPermission extends BearPermission {
    public static final TestPermission PERMISSION = new TestPermission("test");
    
    public TestPermission(String permission) {
        super(permission);
    }

    @Override
    public String getPermission() {
        // Overridden method from BearPermission. An instance of the plugin main class is required.
        return getPermission(TestPlugin.getPlugin());
    }
}