Ladysnake / PlayerAbilityLib

It's your new pal!
GNU Lesser General Public License v3.0
14 stars 8 forks source link
fabricmc fabricmc-mod hacktoberfest library

PlayerAbilityLib

Curseforge

A lightweight serverside library to provide compatibility between mods that make use of player abilities.

Conflicts arise often when several mods update the same field in PlayerAbilities. This library exists so that all modifications can be made from a single place, keeping track of what mod enabled what ability.

Looking for the Elytra Flight equivalent? Try FallFlyingLib or CaelusApi.

credits to InsomniaPrincess Chloe Dawn for some of the API design

Adding PAL to your project

You can add the library by inserting the following in your build.gradle :

repositories {
    maven {
        name = 'Ladysnake Mods'
        url = 'https://maven.ladysnake.org/releases'
        content {
            includeGroup 'io.github.ladysnake'
            includeGroupByRegex 'org\\.ladysnake.*'
        }
    }
}

dependencies {
    modImplementation "io.github.ladysnake:PlayerAbilityLib:${pal_version}"
    include "io.github.ladysnake:PlayerAbilityLib:${pal_version}"
}

You can then add the library version to your gradle.propertiesfile:

# PlayerAbilityLib
pal_version = 1.x.y

You can find the current version of PAL in the releases tab of the repository on Github.

Using PAL

You can find a couple examples in the Test Mod.

Note that PAL interfaces can only be accessed serverside, as no synchronization is done on the ability sources.
Read accesses can still be done directly on the PlayerAbilities instance, both serverside and clientside.

If you want to store more complex data, or to synchronize it between server and client, you should take a look at Cardinal Components API.

Item that toggles an ability :

public static final AbilitySource FLIGHT_CHARM = Pal.getAbilitySource("mymod", "flight_charm");  // works like an identifier

@Override
public ActionResult use(World world, PlayerEntity user, Hand hand) {
    if (!world.isClient) {
        if (FLIGHT_CHARM.grants(user, VanillaAbilities.ALLOW_FLYING)) { // check whether the source is granting the ability
            FLIGHT_CHARM.revokeFrom(user, VanillaAbilities.ALLOW_FLYING); // if it is, revoke it
       } else {
            FLIGHT_CHARM.grantTo(user, VanillaAbilities.ALLOW_FLYING);  // otherwise, grant it
        }
    }
    return ActionResult.SUCCESS;
}

Potion that grants an ability :

public static final AbilitySource FLIGHT_POTION = Pal.getAbilitySource("mymod", "flight_potion");

@Override
public void onApplied(LivingEntity effected, AbstractEntityAttributeContainer attributes, int amplifier) {
    if (effected instanceof PlayerEntity) {
        Pal.grantAbility((PlayerEntity) effected, VanillaAbilities.ALLOW_FLYING, FLIGHT_POTION);
        // equivalent to: FLIGHT_POTION.grantTo((PlayerEntity) effected, VanillaAbilities.ALLOW_FLYING);
    }
}

public void onRemoved(LivingEntity effected) {
    if (effected instanceof PlayerEntity) {
        Pal.revokeAbility((PlayerEntity) effected, VanillaAbilities.ALLOW_FLYING, FLIGHT_POTION);
        // equivalent to: FLIGHT_POTION.revokeFrom((PlayerEntity) effected, VanillaAbilities.ALLOW_FLYING);
    }
}

Note that the onRemoved method needs a mixin to work, as the vanilla StatusEffect#onRemoved method does not get a LivingEntity parameter. You can find an example of such a mixin here