Search in sources :

Example 21 with CommandPermissionsException

use of com.sk89q.minecraft.util.commands.CommandPermissionsException in project WorldGuard by EngineHub.

the class RegionCommands method define.

/**
 * Defines a new region.
 *
 * @param args the arguments
 * @param sender the sender
 * @throws CommandException any error
 */
@Command(aliases = { "define", "def", "d", "create" }, usage = "[-w <world>] <id> [<owner1> [<owner2> [<owners...>]]]", flags = "ngw:", desc = "Defines a region", min = 1)
public void define(CommandContext args, Actor sender) throws CommandException {
    warnAboutSaveFailures(sender);
    // Check permissions
    if (!getPermissionModel(sender).mayDefine()) {
        throw new CommandPermissionsException();
    }
    String id = checkRegionId(args.getString(0), false);
    World world = checkWorld(args, sender, 'w');
    RegionManager manager = checkRegionManager(world);
    checkRegionDoesNotExist(manager, id, true);
    ProtectedRegion region;
    if (args.hasFlag('g')) {
        region = new GlobalProtectedRegion(id);
    } else {
        region = checkRegionFromSelection(sender, id);
    }
    RegionAdder task = new RegionAdder(manager, region);
    task.addOwnersFromCommand(args, 2);
    final String description = String.format("Adding region '%s'", region.getId());
    AsyncCommandBuilder.wrap(task, sender).registerWithSupervisor(worldGuard.getSupervisor(), description).onSuccess((Component) null, t -> {
        sender.print(String.format("A new region has been made named '%s'.", region.getId()));
        warnAboutDimensions(sender, region);
        informNewUser(sender, manager, region);
        checkSpawnOverlap(sender, world, region);
    }).onFailure(String.format("Failed to add the region '%s'", region.getId()), worldGuard.getExceptionConverter()).buildAndExec(worldGuard.getExecutorService());
}
Also used : RegionAdder(com.sk89q.worldguard.commands.task.RegionAdder) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) 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)

Example 22 with CommandPermissionsException

use of com.sk89q.minecraft.util.commands.CommandPermissionsException in project WorldGuard by EngineHub.

the class RegionCommands method remove.

/**
 * Remove a region.
 *
 * @param args the arguments
 * @param sender the sender
 * @throws CommandException any error
 */
@Command(aliases = { "remove", "delete", "del", "rem" }, usage = "<id>", flags = "fuw:", desc = "Remove a region", min = 1, max = 1)
public void remove(CommandContext args, Actor sender) throws CommandException {
    warnAboutSaveFailures(sender);
    // Get the world
    World world = checkWorld(args, sender, 'w');
    boolean removeChildren = args.hasFlag('f');
    boolean unsetParent = args.hasFlag('u');
    // Lookup the existing region
    RegionManager manager = checkRegionManager(world);
    ProtectedRegion existing = checkExistingRegion(manager, args.getString(0), true);
    // Check permissions
    if (!getPermissionModel(sender).mayDelete(existing)) {
        throw new CommandPermissionsException();
    }
    RegionRemover task = new RegionRemover(manager, existing);
    if (removeChildren && unsetParent) {
        throw new CommandException("You cannot use both -u (unset parent) and -f (remove children) together.");
    } else if (removeChildren) {
        task.setRemovalStrategy(RemovalStrategy.REMOVE_CHILDREN);
    } else if (unsetParent) {
        task.setRemovalStrategy(RemovalStrategy.UNSET_PARENT_IN_CHILDREN);
    }
    final String description = String.format("Removing region '%s' in '%s'", existing.getId(), world.getName());
    AsyncCommandBuilder.wrap(task, sender).registerWithSupervisor(WorldGuard.getInstance().getSupervisor(), description).sendMessageAfterDelay("Please wait... removing region.").onSuccess((Component) null, removed -> sender.print(TextComponent.of("Successfully removed " + removed.stream().map(ProtectedRegion::getId).collect(Collectors.joining(", ")) + ".", TextColor.LIGHT_PURPLE))).onFailure("Failed to remove region", WorldGuard.getInstance().getExceptionConverter()).buildAndExec(WorldGuard.getInstance().getExecutorService());
}
Also used : RegionRemover(com.sk89q.worldguard.commands.task.RegionRemover) 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) CommandException(com.sk89q.minecraft.util.commands.CommandException) World(com.sk89q.worldedit.world.World) Command(com.sk89q.minecraft.util.commands.Command)

Example 23 with CommandPermissionsException

use of com.sk89q.minecraft.util.commands.CommandPermissionsException 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

CommandPermissionsException (com.sk89q.minecraft.util.commands.CommandPermissionsException)23 Command (com.sk89q.minecraft.util.commands.Command)22 World (com.sk89q.worldedit.world.World)18 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)18 CommandException (com.sk89q.minecraft.util.commands.CommandException)16 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)16 GlobalProtectedRegion (com.sk89q.worldguard.protection.regions.GlobalProtectedRegion)12 LocalPlayer (com.sk89q.worldguard.LocalPlayer)9 RegionPermissionModel (com.sk89q.worldguard.internal.permission.RegionPermissionModel)4 RegionContainer (com.sk89q.worldguard.protection.regions.RegionContainer)4 DomainInputResolver (com.sk89q.worldguard.protection.util.DomainInputResolver)4 RegionAdder (com.sk89q.worldguard.commands.task.RegionAdder)3 MigrationException (com.sk89q.worldguard.protection.managers.migration.MigrationException)3 RegionDriver (com.sk89q.worldguard.protection.managers.storage.RegionDriver)3 LoggerToChatHandler (com.sk89q.worldguard.util.logging.LoggerToChatHandler)3 Logger (java.util.logging.Logger)3 ConfigurationManager (com.sk89q.worldguard.config.ConfigurationManager)2 DefaultDomain (com.sk89q.worldguard.domains.DefaultDomain)2 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)2 CircularInheritanceException (com.sk89q.worldguard.protection.regions.ProtectedRegion.CircularInheritanceException)2