Configurable staff mod

This page is a tutorial that allows developers to learn how to create a configurable staff mod using StaffModLib.

Introduction

In this page, you'll learn how to create a configurable staff mod using StaffModLib. This kind of staff mod is very useful when you want to modify the items, their position or their data without modifying code and redeploying your plugin.

We'll build this tutorial on the previous one so if you have not already read it, we recommend you to do so.

Set a staff mod configurable

First of all, we'll implement the Configurable interface which will allow us to set our staff mod configurable. So, let's do this in the SimpleStaffMod class.

public class SimpleStaffMod extends SimpleBukkitStaffMod implements Configurable {

    // For lisibility reasons, we do not display all the previously
    // mentionned code but suppose that it is here.
    
    @Override
    public void configure(ConfigurationSection parent) {
        this.items.stream()
                .filter(item -> item instanceof Configurable)
                .map(item -> (Configurable) item)
                .forEach(configurable -> configurable.configure(parent));
    }
}

The configure(ConfigurationSection parent) method will iterate through all the registered items and configure the ones that implement the Configurable interface.

Now, at the end of the registerItems(Player player) method, add the following code which we'll use the default configuration file of the plugin to configure the staff mod. You can notice that in this tutorial, all the information about our staff mod are stored in the staffmod section.

@Override
public void registerItems(Player player) {
        
    // ...
        
    // Retrieving config to configure items.
    FileConfiguration config = this.plugin.getConfig();

    // Configuring the staff mod.
    this.configure(config.getConfigurationSection("staffmod"));
}

Set a staff mod item configurable

In this part, we'll go back to the FreezeItem class and we'll set it as configurable. So go ahead and implements the Configurable interface as in the SimpleStaffMod class and redefine the configure() method. We'll also create an instance variable which will store an ItemStack retrieved when configuring the item and we'll reimplement the getItem() method to access it.

public class FreezeItem extends BukkitStaffModItem implements Configurable {

    // For lisibility reasons, we do not display all the previously
    // mentionned code but suppose that it is here.
    
    private ItemStack item;
    
    // ...

    @Override
    public void configure(ConfigurationSection parent) {

    }
    
    @Override
    public ItemStack getItem() {
        // Don't forget to class to manipulate different instances of the item
        // without impacting the original one.
        return this.item.clone();
    }
    
    // ...
}

Now, let's write the configure(ConfigurationSection parent) method body. First, we'll retrieve into a variable the section in which the item is stored. Then, we'll initialize our item by retrieving it from this section and we'll also define its slot in the player inventory.

@Override
public void configure(ConfigurationSection parent) {

    // Retrieving the section of the item into a variable.
    ConfigurationSection section = parent.getConfigurationSection("freeze");

    this.item = section.getItemStack("item").clone();
    this.setSlot(section.getInt("slot"));
}

Now, your staff mod and its items can be configured from a configuration file.

Storing the staff mod in a configuration file

In this part, we'll see how to store our staff mod and its items in a configuration file. In this tutorial, we'll use the default configuration file of a plugin but you can also use your own if you want. So, in the main class of your plugin, add the following code :

// Loading default configuration.
super.saveDefaultConfig();

Now, create a file called config.yml in your resources directory. Open it and write the following code :

staffmod:
  # The name of the section in which the item is stored.
  # It can be anything you want.
  freeze:
    # The configuration of the ItemStack.
    # Refer to the Spigot doc for more information.
    item:
      ==: org.bukkit.inventory.ItemStack
      type: BLAZE_ROD
      amount: 1
      meta:
        ==: ItemMeta
        meta-type: UNSPECIFIC
        display-name: "§eFreeze"
        lore:
          - "§eClick to §afreeze §eor §cunfreeze §ea player."
    # The slot in which the item will be set in the
    # inventory of the player.
    slot: 0

It's done ! Now, do the same for all the items you want to be configurable and you'll have an amazing and easily configurable staff mod.

In the next tutorial, we'll see how to create a more advanced staff mod than this one by setting it as configurable and pageable.

Last updated