Skip to content

Creating Custom Attributes

Redned edited this page Aug 17, 2024 · 2 revisions

For creating custom attributes, you will need to extend the RaceAttributeEffect class. You might notice that there is an interface called PwingRaceAttribute, but it's more of a wrapper that stores attribute data value.

Creating Custom Attributes

Here is an example using the ManaAttribute ability:

package net.pwing.races.race.attribute.attributes;

import net.pwing.races.PwingRaces;
import net.pwing.races.api.race.attribute.RaceAttributeEffect;
import net.pwing.races.hooks.MagicSpellsHook;

import org.bukkit.entity.Player;

public class ManaAttribute extends RaceAttributeEffect {

    private PwingRaces plugin;

    public ManaAttribute(PwingRaces plugin, String name) {
        super(name);

        this.plugin = plugin;
    }

    @Override
    public void onAttributeApply(Player player, double amount) {
        plugin.getMagicSpellsHook().setMaxMana(player, (int) amount);
    }

    @Override
    public void onAttributeLose(Player player) {
        MagicSpellsHook msHook = plugin.getMagicSpellsHook();
        msHook.setMaxMana(player, msHook.getDefaultMaxMana());
    }
}

Some of the methods in this class aren't publicly available, but the point is demonstrated. onAttributeApply is when the attribute is applied to the player, and onAttributeLose. Attributes are generally applied when the player joins or teleports, and are taken away when they leave (to prevent bugs), swap to a world where races is disabled or change races entirely.

To register your attribute, here is how you would do that:

RaceAttributeManager attributeManager = PwingRacesAPI.getAttributeManager();
attributeManager.getAttributeEffects().put("max-mana", new ManaAttribute(plugin, "max-mana"));

And now to utilise it, here is an example in the config:

attributes:
    max-mana: 250

Clone this wiki locally