Abilities

This page shows a way you could further build onto the framework and customize abilities.

Ability attributes

What I like to do for customizing abilities is adding an abstract class to handle ability attributes and then extending all my abilities off of my own class. This would be an example of my own AbilityAttributes class:

import com.jewishbanana.uiframework.items.Ability;

public abstract class AbilityAttributes extends Ability {
	
	protected double damage;
	protected double range;
	protected double knockbackMultiplier;
	
	public double getDamage() {
		return damage;
	}
	public void setDamage(double damage) {
		this.damage = damage;
	}
	public double getRange() {
		return range;
	}
	public void setRange(double range) {
		this.range = range;
	}
	public double getKnockbackMultiplier() {
		return knockbackMultiplier;
	}
	public void setKnockbackMultiplier(double knockbackMultiplier) {
		this.knockbackMultiplier = knockbackMultiplier;
	}
}

This gives me the ability to extend my ability classes to this and modify some extra attributes that can be configurable in the config. For example:

public class RocketJump extends AbilityAtrributes {
    
    //we are overriding the interacted method since we will bind our ability to a PlayerInteractEvent
    public boolean interacted(PlayerInteractEvent event, GenericItem base) {
        Player player = event.getPlayer();
        if (use(player)) {
            //notice how here I utilize our ability attributes range, damage, and knockbackMultiplier
            player.setVelocity(player.getVelocity().add(new Vector(0, range, 0)));
            for (Entity e : player.getNearbyEntities(3, 3, 3)) {
                e.setVelocity(e.getVelocity().add(Utils.getVectorTowards(player.getLocation(), e.getLocation()).multiply(knockbackMultiplier)));
                if (e instanceof LivingEntity)
                    ((LivingEntity) e).damage(damage);
            }
            return true;
        }
        return false;
    }
}

Now you can easily fetch and cast your abilities from the AbilityType class to configure your custom attributes like this:

//register our ability somewhere in our plugin first
AbilityType.registerAbility("rocket_jump", RocketJump.class);

//you can fetch registered abilities like this and we can cast it to AbilityAttributes without checking because we know it is since we are the ones that registered it
AbilityAttributes ability = (AbilityAttributes) AbilityType.getAbilityType("rocket_jump").createNewInstance();
//examples for how we can create configurable ability attributes using the config
ability.setCooldownTicks((int) (plugin.getConfig().getDouble("ability.rocket_jump.cooldown") * 20.0));
ability.setDamage(plugin.getConfig().getDouble("ability.rocket_jump.damage"));
ability.setRange(plugin.getConfig().getDouble("ability.rocket_jump.range"));
ability.setKnockbackMultiplier(plugin.getConfig().getDouble("ability.rocket_jump.knockback"));

//adding ability and binding it to right-click for our custom item "jetpack" (not created in this guide)
ItemType.getItemType("jetpack").addAbility(Ability.Action.RIGHT_CLICK, ability);

Last updated