Java Code Examples for com.griefcraft.model.Protection#getPermissions()

The following examples show how to use com.griefcraft.model.Protection#getPermissions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InfoModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() != Result.DEFAULT) {
        return;
    }

    if (!event.hasAction("info")) {
        return;
    }

    LWC lwc = event.getLWC();
    Protection protection = event.getProtection();
    Player player = event.getPlayer();
    event.setResult(Result.CANCEL);

    String type = lwc.getPlugin().getMessageParser().parseMessage(protection.typeToString().toLowerCase());

    lwc.sendLocale(player, "lwc.info", "owner", protection.getFormattedOwnerPlayerName(), "type", type);

    if (event.canAdmin()) {
        if (protection.getType() == Protection.Type.PRIVATE || protection.getType() == Protection.Type.DONATION) {
            lwc.sendLocale(player, "lwc.acl", "size", protection.getPermissions().size());
            int index = 0;
            for (Permission permission : protection.getPermissions()) {
                if (index >= 9) {
                    break;
                }

                player.sendMessage(permission.toString());
                index++;
            }

            if (index == 0) {
                lwc.sendLocale(player, "lwc.acl.empty");
            } else if (index >= 9) {
                lwc.sendLocale(player, "lwc.acl.limitreached");
            }

            player.sendMessage("");
        }
    }

    if (lwc.isAdmin(player)) {
        lwc.sendLocale(player, "protection.interact.info.raw", "raw", protection.toString());
    }

    lwc.removeModes(player);
}
 
Example 2
Source File: OwnersModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() != Result.DEFAULT) {
        return;
    }

    if (!event.hasAction("owners")) {
        return;
    }

    LWC lwc = event.getLWC();
    Protection protection = event.getProtection();
    LWCPlayer player = lwc.wrapPlayer(event.getPlayer());
    event.setResult(Result.CANCEL);

    Action action = player.getAction("owners");
    int accessPage = Integer.parseInt(action.getData());

    // Calculate range
    int start = (accessPage - 1) * RESULTS_PER_PAGE;
    int max = start + RESULTS_PER_PAGE;

    List<Permission> permissions = protection.getPermissions();
    int numRights = permissions.size();

    // May have only been 2 rows left, or something. Get the real max
    int realMax = start + permissions.size();

    lwc.sendLocale(player, "lwc.owners.results", "start", start, "max", realMax, "total", numRights);

    for (int index = 0; index < max; index++) {
        if ((start + index) >= numRights) {
            break;
        }

        Permission permission = permissions.get(start + index);
        player.sendMessage(permission.toString());
    }

    lwc.removeModes(player);
}
 
Example 3
Source File: WorldGuard.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onAccessRequest(LWCAccessEvent event) {
    if (event.getAccess() != Permission.Access.NONE) {
        // Player already has access.
        return;
    }

    // WorldGuard must be running and LWC must be configured to interface
    // with it.
    if (worldGuardPlugin == null) {
        return;
    }
    if (!configuration.getBoolean("worldguard.enabled", false)) {
        return;
    }
    if (!configuration
            .getBoolean("worldguard.allowRegionPermissions", true)) {
        return;
    }

    Protection protection = event.getProtection();
    LocalPlayer wgPlayer = worldGuardPlugin.wrapPlayer(event.getPlayer());
    for (Permission permission : protection.getPermissions()) {
        if (permission.getType() != Permission.Type.REGION) {
            continue;
        }
        String regionName = permission.getName();
        if (regionName.equalsIgnoreCase("#this")) {
            // Handle the special value which tells us to not actually look
            // up a region but
            // check just the player's WG build permissions on the block. It
            // may be in multiple
            // regions or none; we don't care here. That's WorldGuard's
            // domain.
            if (!canBuild(event.getPlayer(), protection.getBlock())) {
                continue;
            }
        } else if (regionName.startsWith("#")) {
            // Silently disallow looking up regions by index, a newer WG
            // feature.
            // Iterating through potentially thousands of regions each time
            // we check a block's
            // ACL is not a good idea. It would be cleaner to use
            // regionManager.getRegionExact()
            // below, but that would break compatibility with older versions
            // of WG.
            continue;
        } else {
            // Region name specified, go look it up
            World world;
            int c = regionName.indexOf(':');
            if (c < 0) {
                // No world specified in ACL. Use the block's world.
                world = protection.getBlock().getWorld();
            } else {
                // World specified. Partition the string and look up the
                // world.
                String worldName = regionName.substring(c + 1);
                world = event.getLWC().getPlugin().getServer()
                        .getWorld(worldName);
                regionName = regionName.substring(0, c);
            }
            if (world == null) {
                continue;
            }
            RegionManager regionManager = com.sk89q.worldguard.WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(world));
            if (regionManager == null) {
                continue;
            }
            ProtectedRegion region = regionManager.getRegion(regionName);
            if (region == null) {
                continue;
            }
            // Check the region (and its parents) to see if the player is a
            // member (or an owner).
            if (!region.isMember(wgPlayer)) {
                continue;
            }
        }
        // We match the region, so bump up our access level. Whether we get
        // PLAYER access or
        // ADMIN access depends solely on the LWC permission entry. (I.e.,
        // WG owner does not
        // imply LWC admin.)
        if (permission.getAccess().ordinal() > event.getAccess().ordinal()) {
            event.setAccess(permission.getAccess());
            if (event.getAccess().ordinal() >= Permission.Access.ADMIN
                    .ordinal()) {
                return;
            }
            // else we just have PLAYER access. Keep looking; maybe we match
            // another region
            // that grants us ADMIN.
        }
    }
}
 
Example 4
Source File: Towny.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onAccessRequest(LWCAccessEvent event) {
    Player player = event.getPlayer();
    Protection protection = event.getProtection();

    if (event.getAccess() != Permission.Access.NONE) {
        return;
    }

    if (protection.getType() != Protection.Type.PRIVATE) {
        return;
    }

    if (towny == null) {
        return;
    }

    for (Permission permission : protection.getPermissions()) {
        if (permission.getType() != Permission.Type.TOWN) {
            continue;
        }

        // Does the town exist?
        try {
            Town town = WorldCoord.parseWorldCoord(event.getProtection().getBlock()).getTownBlock().getTown();

            if (town == null) {
                return;
            }

            // check if the player is a resident of said town
            if (!town.hasResident(player.getName())) {
                // Uh-oh!
                event.setAccess(Permission.Access.NONE);
            } else if (town.getMayor().getName().equalsIgnoreCase(player.getName())) {
                event.setAccess(Permission.Access.ADMIN);
            } else {
                // They're in the town :-)
                event.setAccess(Permission.Access.PLAYER);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
 
Example 5
Source File: TownyModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public void onAccessRequest(LWCAccessEvent event) {
    Player player = event.getPlayer();
    Protection protection = event.getProtection();

    if (event.getAccess() != Permission.Access.NONE) {
        return;
    }

    if (protection.getType() != Protection.Type.PRIVATE) {
        return;
    }

    if (towny == null) {
        return;
    }

    for (Permission permission : protection.getPermissions()) {
        if (permission.getType() != Permission.Type.TOWN) {
            continue;
        }

        String townName = permission.getName();

        // Does the town exist?
        try {
            Town town = towny.getTownyUniverse().getTown(townName);

            if (town == null) {
                return;
            }

            // check if the player is a resident of said town
            if (!town.hasResident(player.getName())) {
                // Uh-oh!
                event.setAccess(Permission.Access.NONE);
            } else {
                // They're in the town :-)
                event.setAccess(Permission.Access.PLAYER);
            }

            // If they're the major, let them admin the protection
            if (town.getMayor().getName().equalsIgnoreCase(player.getName())) {
                event.setAccess(Permission.Access.ADMIN);
            }
        } catch (Exception e) {

        }
    }
}
 
Example 6
Source File: LWC.java    From Modern-LWC with MIT License 4 votes vote down vote up
/**
 * Check if a player has the ability to access a protection
 *
 * @param player
 * @param protection
 * @return
 */
@SuppressWarnings("deprecation")
public boolean canAccessProtection(Player player, Protection protection) {
    if (protection == null || player == null) {
        return true;
    }

    if (isAdmin(player)) {
        return true;
    }

    if (isMod(player)) {
        Player protectionOwner = protection.getBukkitOwner();

        if (protectionOwner == null) {
            return true;
        }

        if (!isAdmin(protectionOwner)) {
            return true;
        }
    }

    // Their access level
    Permission.Access access = Permission.Access.NONE;

    switch (protection.getType()) {
        case PUBLIC:
        case DONATION:
            return true;

        case PASSWORD:
            if (wrapPlayer(player).getAccessibleProtections().contains(protection)) {
                return true;
            }

            break;

        case PRIVATE:
            if (protection.isOwner(player)) {
                return true;
            }

            if (protection.getAccess(player.getUniqueId().toString(), Permission.Type.PLAYER)
                    .ordinal() >= Permission.Access.PLAYER.ordinal()) {
                return true;
            }

            if (protection.getAccess(player.getName(), Permission.Type.PLAYER).ordinal() >= Permission.Access.PLAYER
                    .ordinal()) {
                return true;
            }

            // Check for item keys
            for (Permission permission : protection.getPermissions()) {
                if (permission.getType() != Permission.Type.ITEM) {
                    continue;
                }

                // Get the item they need to have
                int item = Integer.parseInt(permission.getName());

                // Are they wielding it?
                BlockCache blockCache = BlockCache.getInstance();
                if (blockCache.getBlockId(player.getItemInHand().getType()) == item) {
                    return true;
                }
            }

            for (String groupName : permissions.getGroups(player)) {
                if (protection.getAccess(groupName, Permission.Type.GROUP).ordinal() >= Permission.Access.PLAYER
                        .ordinal()) {
                    return true;
                }
            }

            break;
        default:
            break;
    }

    // call the canAccessProtection hook
    LWCAccessEvent event = new LWCAccessEvent(player, protection, access);
    moduleLoader.dispatchEvent(event);

    return event.getAccess() == Permission.Access.PLAYER || event.getAccess() == Permission.Access.ADMIN;
}