Skip to content

Identity API

Draylar edited this page Oct 10, 2021 · 2 revisions

Identity provides several events for other mods to hook into.


IdentitySwapCallback

Called when a player attempts to switch Identities. If any listener returns ActionResult.FAIL, the switch is canceled.

IdentitySwapCallback.EVENT.register((player, to) -> {
    // 'to' will be null when the user is attempting to switch back into a player.
    // Return FAIL to deny the switch.
    if(to == null) {
        return ActionResult.FAIL;
    }
            
    // Return SUCCESS to force the swap.
    if(to instanceof CowEntity) {
        return ActionResult.SUCCESS;
    }
            
    // Default to PASS to allow the next listener to run.
    return ActionResult.PASS;
});

UnlockIdentityCallback

Called when a player unlocks a new Identity. If any listener returns ActionResult.FAIL, the player is not granted the Identity.

UnlockIdentityCallback.EVENT.register((player, id) -> {
    // Return FAIL to deny the unlock.
    if(id.toString().equals("minecraft:wither")) {
        return ActionResult.FAIL;
    }
            
    // Return SUCCESS to force the unlock and prevent other listeners from running.
    if(id.getNamespace().equals("mymod")) {
        player.sendMessage(new LiteralText("You just unlocked an Identity from my mod!"), false);
        return ActionResult.SUCCESS;
    }

    // Default to PASS to allow the next listener to run.
    return ActionResult.PASS;
});
Clone this wiki locally