Search in sources :

Example 1 with PermissibleBase

use of cn.nukkit.permission.PermissibleBase in project LuckPerms by lucko.

the class PermissibleInjector method inject.

/**
 * Injects a {@link LPPermissible} into a {@link Player}.
 *
 * @param player the player to inject into
 * @param newPermissible the permissible to inject
 * @throws Exception propagates any exceptions which were thrown during injection
 */
public static void inject(Player player, LPPermissible newPermissible) throws Exception {
    // get the existing PermissibleBase held by the player
    PermissibleBase oldPermissible = (PermissibleBase) PLAYER_PERMISSIBLE_FIELD.get(player);
    // seems we have already injected into this player.
    if (oldPermissible instanceof LPPermissible) {
        throw new IllegalStateException("LPPermissible already injected into player " + player.toString());
    }
    // Move attachments over from the old permissible
    // noinspection unchecked
    Set<PermissionAttachment> attachments = (Set<PermissionAttachment>) PERMISSIBLE_BASE_ATTACHMENTS_FIELD.get(oldPermissible);
    newPermissible.convertAndAddAttachments(attachments);
    attachments.clear();
    oldPermissible.clearPermissions();
    // Setup the new permissible
    newPermissible.getActive().set(true);
    newPermissible.setOldPermissible(oldPermissible);
    // inject the new instance
    PLAYER_PERMISSIBLE_FIELD.set(player, newPermissible);
}
Also used : DummyPermissibleBase(me.lucko.luckperms.nukkit.model.dummy.DummyPermissibleBase) PermissibleBase(cn.nukkit.permission.PermissibleBase) Set(java.util.Set) PermissionAttachment(cn.nukkit.permission.PermissionAttachment)

Example 2 with PermissibleBase

use of cn.nukkit.permission.PermissibleBase in project LuckPerms by lucko.

the class PermissibleMonitoringInjector method injectConsole.

private void injectConsole() throws Exception {
    ConsoleCommandSender consoleSender = this.plugin.getBootstrap().getServer().getConsoleSender();
    // get the perm field
    Field permField = ConsoleCommandSender.class.getDeclaredField("perm");
    permField.setAccessible(true);
    // get the PermissibleBase instance
    PermissibleBase permBase = (PermissibleBase) permField.get(consoleSender);
    // create a monitored instance which delegates to the previous PermissibleBase
    MonitoredPermissibleBase newPermBase = wrap(permBase, "internal/console");
    // inject the monitored instance
    permField.set(consoleSender, newPermBase);
}
Also used : Field(java.lang.reflect.Field) PermissibleBase(cn.nukkit.permission.PermissibleBase) ConsoleCommandSender(cn.nukkit.command.ConsoleCommandSender)

Example 3 with PermissibleBase

use of cn.nukkit.permission.PermissibleBase in project LuckPerms by lucko.

the class PermissibleInjector method unInject.

/**
 * Uninjects a {@link LPPermissible} from a {@link Player}.
 *
 * @param player the player to uninject from
 * @param dummy if the replacement permissible should be a dummy.
 * @throws Exception propagates any exceptions which were thrown during uninjection
 */
public static void unInject(Player player, boolean dummy) throws Exception {
    // gets the players current permissible.
    PermissibleBase permissible = (PermissibleBase) PLAYER_PERMISSIBLE_FIELD.get(player);
    // only uninject if the permissible was a luckperms one.
    if (permissible instanceof LPPermissible) {
        LPPermissible lpPermissible = ((LPPermissible) permissible);
        // clear all permissions
        lpPermissible.clearPermissions();
        // set to inactive
        lpPermissible.getActive().set(false);
        // handle the replacement permissible.
        if (dummy) {
            // just inject a dummy class. this is used when we know the player is about to quit the server.
            PLAYER_PERMISSIBLE_FIELD.set(player, DummyPermissibleBase.INSTANCE);
        } else {
            PermissibleBase newPb = lpPermissible.getOldPermissible();
            if (newPb == null) {
                newPb = new PermissibleBase(player);
            }
            PLAYER_PERMISSIBLE_FIELD.set(player, newPb);
        }
    }
}
Also used : DummyPermissibleBase(me.lucko.luckperms.nukkit.model.dummy.DummyPermissibleBase) PermissibleBase(cn.nukkit.permission.PermissibleBase)

Aggregations

PermissibleBase (cn.nukkit.permission.PermissibleBase)3 DummyPermissibleBase (me.lucko.luckperms.nukkit.model.dummy.DummyPermissibleBase)2 ConsoleCommandSender (cn.nukkit.command.ConsoleCommandSender)1 PermissionAttachment (cn.nukkit.permission.PermissionAttachment)1 Field (java.lang.reflect.Field)1 Set (java.util.Set)1