Search in sources :

Example 1 with CircularInheritanceException

use of com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException in project WorldGuard by EngineHub.

the class HashMapIndex method removeAndReplaceParents.

private Set<ProtectedRegion> removeAndReplaceParents(String id, RemovalStrategy strategy, @Nullable ProtectedRegion replacement, boolean rebuildIndex) {
    checkNotNull(id);
    checkNotNull(strategy);
    Set<ProtectedRegion> removedSet = new HashSet<>();
    synchronized (lock) {
        ProtectedRegion removed = regions.remove(normalize(id));
        if (removed != null) {
            removedSet.add(removed);
            while (true) {
                int lastSize = removedSet.size();
                Iterator<ProtectedRegion> it = regions.values().iterator();
                // Handle children
                while (it.hasNext()) {
                    ProtectedRegion current = it.next();
                    ProtectedRegion parent = current.getParent();
                    if (parent != null && removedSet.contains(parent)) {
                        switch(strategy) {
                            case REMOVE_CHILDREN:
                                removedSet.add(current);
                                it.remove();
                                break;
                            case UNSET_PARENT_IN_CHILDREN:
                                try {
                                    current.setParent(replacement);
                                } catch (CircularInheritanceException e) {
                                    WorldGuard.logger.log(Level.WARNING, "Failed to replace parent '" + parent.getId() + "' of child '" + current.getId() + "' with replacement '" + replacement.getId() + "'", e);
                                    current.clearParent();
                                }
                        }
                    }
                }
                if (strategy == RemovalStrategy.UNSET_PARENT_IN_CHILDREN || removedSet.size() == lastSize) {
                    break;
                }
            }
        }
        this.removed.addAll(removedSet);
        if (rebuildIndex) {
            rebuildIndex();
        }
    }
    return removedSet;
}
Also used : CircularInheritanceException(com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) HashSet(java.util.HashSet)

Example 2 with CircularInheritanceException

use of com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException in project WorldGuard by EngineHub.

the class RegionDatabaseUtils method relinkParents.

/**
 * Re-link parent regions on each provided region using the two
 * provided maps.
 *
 * @param regions the map of regions from which parent regions are found
 * @param parentSets a mapping of region to parent name
 */
public static void relinkParents(Map<String, ProtectedRegion> regions, Map<ProtectedRegion, String> parentSets) {
    checkNotNull(regions);
    checkNotNull(parentSets);
    for (Map.Entry<ProtectedRegion, String> entry : parentSets.entrySet()) {
        ProtectedRegion target = entry.getKey();
        ProtectedRegion parent = regions.get(entry.getValue());
        if (parent != null) {
            try {
                target.setParent(parent);
            } catch (CircularInheritanceException e) {
                log.warning("Circular inheritance detected! Can't set the parent of '" + target + "' to parent '" + parent.getId() + "'");
            }
        } else {
            log.warning("Unknown region parent: " + entry.getValue());
        }
    }
}
Also used : CircularInheritanceException(com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) Map(java.util.Map)

Example 3 with CircularInheritanceException

use of com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException in project WorldGuard by EngineHub.

the class HashMapIndexRemovalTest method setUpDeeplyNestedRegions.

private void setUpDeeplyNestedRegions() {
    ProtectedRegion parent = null;
    for (int i = 0; i < NEST_DEPTH; i++) {
        ProtectedRegion newRegion = new ProtectedCuboidRegion(NESTED_ID_PREFIX + i, BlockVector3.ZERO, // bounds don't matter for this test
        BlockVector3.ZERO);
        if (parent != null) {
            try {
                newRegion.setParent(parent);
            } catch (CircularInheritanceException ignored) {
            }
        }
        parent = newRegion;
    }
    manager.addRegion(parent);
}
Also used : CircularInheritanceException(com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion)

Example 4 with CircularInheritanceException

use of com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException in project WorldGuard by EngineHub.

the class RegionCommands method claim.

/**
 * Claiming command for users.
 *
 * <p>This command is a joke and it needs to be rewritten. It was contributed
 * code :(</p>
 *
 * @param args the arguments
 * @param sender the sender
 * @throws CommandException any error
 */
@Command(aliases = { "claim" }, usage = "<id>", desc = "Claim a region", min = 1, max = 1)
public void claim(CommandContext args, Actor sender) throws CommandException {
    warnAboutSaveFailures(sender);
    LocalPlayer player = worldGuard.checkPlayer(sender);
    RegionPermissionModel permModel = getPermissionModel(player);
    // Check permissions
    if (!permModel.mayClaim()) {
        throw new CommandPermissionsException();
    }
    String id = checkRegionId(args.getString(0), false);
    RegionManager manager = checkRegionManager(player.getWorld());
    checkRegionDoesNotExist(manager, id, false);
    ProtectedRegion region = checkRegionFromSelection(player, id);
    WorldConfiguration wcfg = WorldGuard.getInstance().getPlatform().getGlobalStateManager().get(player.getWorld());
    // Check whether the player has created too many regions
    if (!permModel.mayClaimRegionsUnbounded()) {
        int maxRegionCount = wcfg.getMaxRegionCount(player);
        if (maxRegionCount >= 0 && manager.getRegionCountOfPlayer(player) >= maxRegionCount) {
            throw new CommandException("You own too many regions, delete one first to claim a new one.");
        }
    }
    ProtectedRegion existing = manager.getRegion(id);
    // Check for an existing region
    if (existing != null) {
        if (!existing.getOwners().contains(player)) {
            throw new CommandException("This region already exists and you don't own it.");
        }
    }
    // We have to check whether this region violates the space of any other region
    ApplicableRegionSet regions = manager.getApplicableRegions(region);
    // Check if this region overlaps any other region
    if (regions.size() > 0) {
        if (!regions.isOwnerOfAll(player)) {
            throw new CommandException("This region overlaps with someone else's region.");
        }
    } else {
        if (wcfg.claimOnlyInsideExistingRegions) {
            throw new CommandException("You may only claim regions inside " + "existing regions that you or your group own.");
        }
    }
    if (wcfg.maxClaimVolume >= Integer.MAX_VALUE) {
        throw new CommandException("The maximum claim volume get in the configuration is higher than is supported. " + "Currently, it must be " + Integer.MAX_VALUE + " or smaller. Please contact a server administrator.");
    }
    // Check claim volume
    if (!permModel.mayClaimRegionsUnbounded()) {
        if (region instanceof ProtectedPolygonalRegion) {
            throw new CommandException("Polygons are currently not supported for /rg claim.");
        }
        if (region.volume() > wcfg.maxClaimVolume) {
            player.printError("This region is too large to claim.");
            player.printError("Max. volume: " + wcfg.maxClaimVolume + ", your volume: " + region.volume());
            return;
        }
    }
    // Inherit from a template region
    if (!Strings.isNullOrEmpty(wcfg.setParentOnClaim)) {
        ProtectedRegion templateRegion = manager.getRegion(wcfg.setParentOnClaim);
        if (templateRegion != null) {
            try {
                region.setParent(templateRegion);
            } catch (CircularInheritanceException e) {
                throw new CommandException(e.getMessage());
            }
        }
    }
    RegionAdder task = new RegionAdder(manager, region);
    task.setLocatorPolicy(UserLocatorPolicy.UUID_ONLY);
    task.setOwnersInput(new String[] { player.getName() });
    final String description = String.format("Claiming region '%s'", id);
    AsyncCommandBuilder.wrap(task, sender).registerWithSupervisor(WorldGuard.getInstance().getSupervisor(), description).sendMessageAfterDelay("(Please wait... " + description + ")").onSuccess(TextComponent.of(String.format("A new region has been claimed named '%s'.", id)), null).onFailure("Failed to claim region", WorldGuard.getInstance().getExceptionConverter()).buildAndExec(WorldGuard.getInstance().getExecutorService());
}
Also used : CommandPermissionsException(com.sk89q.minecraft.util.commands.CommandPermissionsException) LocalPlayer(com.sk89q.worldguard.LocalPlayer) CommandException(com.sk89q.minecraft.util.commands.CommandException) CircularInheritanceException(com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException) RegionAdder(com.sk89q.worldguard.commands.task.RegionAdder) WorldConfiguration(com.sk89q.worldguard.config.WorldConfiguration) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) RegionPermissionModel(com.sk89q.worldguard.internal.permission.RegionPermissionModel) ApplicableRegionSet(com.sk89q.worldguard.protection.ApplicableRegionSet) Command(com.sk89q.minecraft.util.commands.Command)

Example 5 with CircularInheritanceException

use of com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException in project WorldGuard by EngineHub.

the class RegionCommands method setParent.

/**
 * Set the parent of a region.
 *
 * @param args the arguments
 * @param sender the sender
 * @throws CommandException any error
 */
@Command(aliases = { "setparent", "parent", "par" }, usage = "<id> [parent-id]", flags = "w:", desc = "Set the parent of a region", min = 1, max = 2)
public void setParent(CommandContext args, Actor sender) throws CommandException {
    warnAboutSaveFailures(sender);
    // Get the world
    World world = checkWorld(args, sender, 'w');
    ProtectedRegion parent;
    ProtectedRegion child;
    // Lookup the existing region
    RegionManager manager = checkRegionManager(world);
    // Get parent and child
    child = checkExistingRegion(manager, args.getString(0), false);
    if (args.argsLength() == 2) {
        parent = checkExistingRegion(manager, args.getString(1), false);
    } else {
        parent = null;
    }
    // Check permissions
    if (!getPermissionModel(sender).maySetParent(child, parent)) {
        throw new CommandPermissionsException();
    }
    try {
        child.setParent(parent);
    } catch (CircularInheritanceException e) {
        // Tell the user what's wrong
        RegionPrintoutBuilder printout = new RegionPrintoutBuilder(world.getName(), parent, null, sender);
        assert parent != null;
        printout.append(ErrorFormat.wrap("Uh oh! Setting '", parent.getId(), "' to be the parent of '", child.getId(), "' would cause circular inheritance.")).newline();
        printout.append(SubtleFormat.wrap("(Current inheritance on '", parent.getId(), "':")).newline();
        printout.appendParentTree(true);
        printout.append(SubtleFormat.wrap(")"));
        printout.send(sender);
        return;
    }
    // Tell the user the current inheritance
    RegionPrintoutBuilder printout = new RegionPrintoutBuilder(world.getName(), child, null, sender);
    printout.append(TextComponent.of("Inheritance set for region '" + child.getId() + "'.", TextColor.LIGHT_PURPLE));
    if (parent != null) {
        printout.newline();
        printout.append(SubtleFormat.wrap("(Current inheritance:")).newline();
        printout.appendParentTree(true);
        printout.append(SubtleFormat.wrap(")"));
    } else {
        printout.append(LabelFormat.wrap(" Region is now orphaned."));
    }
    printout.send(sender);
}
Also used : CircularInheritanceException(com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException) CommandPermissionsException(com.sk89q.minecraft.util.commands.CommandPermissionsException) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) World(com.sk89q.worldedit.world.World) Command(com.sk89q.minecraft.util.commands.Command)

Aggregations

ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)5 CircularInheritanceException (com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException)5 Command (com.sk89q.minecraft.util.commands.Command)2 CommandPermissionsException (com.sk89q.minecraft.util.commands.CommandPermissionsException)2 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)2 GlobalProtectedRegion (com.sk89q.worldguard.protection.regions.GlobalProtectedRegion)2 CommandException (com.sk89q.minecraft.util.commands.CommandException)1 World (com.sk89q.worldedit.world.World)1 LocalPlayer (com.sk89q.worldguard.LocalPlayer)1 RegionAdder (com.sk89q.worldguard.commands.task.RegionAdder)1 WorldConfiguration (com.sk89q.worldguard.config.WorldConfiguration)1 RegionPermissionModel (com.sk89q.worldguard.internal.permission.RegionPermissionModel)1 ApplicableRegionSet (com.sk89q.worldguard.protection.ApplicableRegionSet)1 ProtectedCuboidRegion (com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion)1 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1