Search in sources :

Example 1 with CommandException

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

the class RegionCommandsBase method setPlayerSelection.

/**
 * Set an actor's selection to a given region.
 *
 * @param actor the actor
 * @param region the region
 * @throws CommandException thrown on a command error
 */
protected static void setPlayerSelection(Actor actor, ProtectedRegion region, World world) throws CommandException {
    LocalSession session = WorldEdit.getInstance().getSessionManager().get(actor);
    RegionSelector selector = WorldEditRegionConverter.convertToSelector(region);
    if (selector != null) {
        selector.setWorld(world);
        session.setRegionSelector(world, selector);
        selector.explainRegionAdjust(actor, session);
        actor.print("Region selected as " + region.getType().getName());
    } else {
        throw new CommandException("Can't select that region! " + "The region type '" + region.getType().getName() + "' can't be selected.");
    }
}
Also used : CuboidRegionSelector(com.sk89q.worldedit.regions.selector.CuboidRegionSelector) Polygonal2DRegionSelector(com.sk89q.worldedit.regions.selector.Polygonal2DRegionSelector) RegionSelector(com.sk89q.worldedit.regions.RegionSelector) LocalSession(com.sk89q.worldedit.LocalSession) CommandException(com.sk89q.minecraft.util.commands.CommandException)

Example 2 with CommandException

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

the class RegionCommandsBase method checkRegionStandingIn.

/**
 * Get the region at the player's location, if possible.
 *
 * <p>If the player is standing in several regions, an error will be raised
 * and a list of regions will be provided.</p>
 *
 * <p>If the player is not standing in any regions, the global region will
 * returned if allowGlobal is true and it exists.</p>
 *
 * @param regionManager the region manager
 * @param player the player
 * @param allowGlobal whether to search for a global region if no others are found
 * @return a region
 * @throws CommandException thrown if no region was found
 */
protected static ProtectedRegion checkRegionStandingIn(RegionManager regionManager, LocalPlayer player, boolean allowGlobal, String rgCmd) throws CommandException {
    ApplicableRegionSet set = regionManager.getApplicableRegions(player.getLocation().toVector().toBlockPoint(), QueryOption.SORT);
    if (set.size() == 0) {
        if (allowGlobal) {
            ProtectedRegion global = checkExistingRegion(regionManager, "__global__", true);
            player.printDebug("You're not standing in any " + "regions. Using the global region for this world instead.");
            return global;
        }
        throw new CommandException("You're not standing in a region. " + "Specify an ID if you want to select a specific region.");
    } else if (set.size() > 1) {
        boolean first = true;
        final TextComponent.Builder builder = TextComponent.builder("");
        builder.append(TextComponent.of("Current regions: ", TextColor.GOLD));
        for (ProtectedRegion region : set) {
            if (!first) {
                builder.append(TextComponent.of(", "));
            }
            first = false;
            TextComponent regionComp = TextComponent.of(region.getId(), TextColor.AQUA);
            if (rgCmd != null && rgCmd.contains("%id%")) {
                regionComp = regionComp.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to pick this region"))).clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, rgCmd.replace("%id%", region.getId())));
            }
            builder.append(regionComp);
        }
        player.print(builder.build());
        throw new CommandException("You're standing in several regions (please pick one).");
    }
    return set.iterator().next();
}
Also used : TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) CommandException(com.sk89q.minecraft.util.commands.CommandException) ApplicableRegionSet(com.sk89q.worldguard.protection.ApplicableRegionSet)

Example 3 with CommandException

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

the class RegionCommandsBase method checkRegionFromSelection.

/**
 * Create a {@link ProtectedRegion} from the actor's selection.
 *
 * @param actor the actor
 * @param id the ID of the new region
 * @return a new region
 * @throws CommandException thrown on an error
 */
protected static ProtectedRegion checkRegionFromSelection(Actor actor, String id) throws CommandException {
    Region selection = checkSelection(actor);
    // Detect the type of region from WorldEdit
    if (selection instanceof Polygonal2DRegion) {
        Polygonal2DRegion polySel = (Polygonal2DRegion) selection;
        int minY = polySel.getMinimumPoint().getBlockY();
        int maxY = polySel.getMaximumPoint().getBlockY();
        return new ProtectedPolygonalRegion(id, polySel.getPoints(), minY, maxY);
    } else if (selection instanceof CuboidRegion) {
        BlockVector3 min = selection.getMinimumPoint();
        BlockVector3 max = selection.getMaximumPoint();
        return new ProtectedCuboidRegion(id, min, max);
    } else {
        throw new CommandException("Sorry, you can only use cuboids and polygons for WorldGuard regions.");
    }
}
Also used : Polygonal2DRegion(com.sk89q.worldedit.regions.Polygonal2DRegion) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) Polygonal2DRegion(com.sk89q.worldedit.regions.Polygonal2DRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Region(com.sk89q.worldedit.regions.Region) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) ProtectedPolygonalRegion(com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) ProtectedCuboidRegion(com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion) CommandException(com.sk89q.minecraft.util.commands.CommandException) BlockVector3(com.sk89q.worldedit.math.BlockVector3)

Example 4 with CommandException

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

the class BukkitDebugHandler method traceEntity.

/**
 * Find the first nearby entity in a ray trace.
 *
 * @param sender The sender
 * @param target The target
 * @param fromTarget Whether the trace should originate from the target
 * @return The entity found
 * @throws CommandException Throw on an incorrect parameter
 */
private Entity traceEntity(CommandSender sender, Player target, boolean fromTarget) throws CommandException {
    Player source = getSource(sender, target, fromTarget);
    BlockIterator it = new BlockIterator(source);
    int i = 0;
    while (it.hasNext() && i < MAX_TRACE_DISTANCE) {
        Block block = it.next();
        // A very in-accurate and slow search
        Entity[] entities = block.getChunk().getEntities();
        for (Entity entity : entities) {
            if (!entity.equals(target) && entity.getLocation().distanceSquared(block.getLocation()) < 10) {
                return entity;
            }
        }
        i++;
    }
    throw new CommandException("Not currently looking at an entity that is close enough.");
}
Also used : BlockIterator(org.bukkit.util.BlockIterator) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) LocalPlayer(com.sk89q.worldguard.LocalPlayer) Block(org.bukkit.block.Block) CommandException(com.sk89q.minecraft.util.commands.CommandException)

Example 5 with CommandException

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

the class MemberCommands method removeMember.

@Command(aliases = { "removemember", "remmember", "removemem", "remmem", "rm" }, usage = "<id> <owners...>", flags = "naw:", desc = "Remove an owner to a region", min = 1)
public void removeMember(CommandContext args, Actor sender) throws CommandException {
    warnAboutSaveFailures(sender);
    // Get the world
    World world = checkWorld(args, sender, 'w');
    String id = args.getString(0);
    RegionManager manager = checkRegionManager(world);
    ProtectedRegion region = checkExistingRegion(manager, id, true);
    // Check permissions
    if (!getPermissionModel(sender).mayRemoveMembers(region)) {
        throw new CommandPermissionsException();
    }
    Callable<DefaultDomain> callable;
    if (args.hasFlag('a')) {
        callable = region::getMembers;
    } else {
        if (args.argsLength() < 2) {
            throw new CommandException("List some names to remove, or use -a to remove all.");
        }
        // Resolve members asynchronously
        DomainInputResolver resolver = new DomainInputResolver(WorldGuard.getInstance().getProfileService(), args.getParsedPaddedSlice(1, 0));
        resolver.setLocatorPolicy(args.hasFlag('n') ? UserLocatorPolicy.NAME_ONLY : UserLocatorPolicy.UUID_AND_NAME);
        callable = resolver;
    }
    final String description = String.format("Removing members from the region '%s' on '%s'", region.getId(), world.getName());
    AsyncCommandBuilder.wrap(callable, sender).registerWithSupervisor(worldGuard.getSupervisor(), description).sendMessageAfterDelay("(Please wait... querying player names...)").onSuccess(String.format("Region '%s' updated with members removed.", region.getId()), region.getMembers()::removeAll).onFailure("Failed to remove members", worldGuard.getExceptionConverter()).buildAndExec(worldGuard.getExecutorService());
}
Also used : DomainInputResolver(com.sk89q.worldguard.protection.util.DomainInputResolver) CommandPermissionsException(com.sk89q.minecraft.util.commands.CommandPermissionsException) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) CommandException(com.sk89q.minecraft.util.commands.CommandException) World(com.sk89q.worldedit.world.World) DefaultDomain(com.sk89q.worldguard.domains.DefaultDomain) Command(com.sk89q.minecraft.util.commands.Command)

Aggregations

CommandException (com.sk89q.minecraft.util.commands.CommandException)26 Command (com.sk89q.minecraft.util.commands.Command)18 LocalPlayer (com.sk89q.worldguard.LocalPlayer)16 CommandPermissionsException (com.sk89q.minecraft.util.commands.CommandPermissionsException)15 World (com.sk89q.worldedit.world.World)14 ProtectedRegion (com.sk89q.worldguard.protection.regions.ProtectedRegion)12 RegionManager (com.sk89q.worldguard.protection.managers.RegionManager)11 GlobalProtectedRegion (com.sk89q.worldguard.protection.regions.GlobalProtectedRegion)10 LoggerToChatHandler (com.sk89q.worldguard.util.logging.LoggerToChatHandler)5 Logger (java.util.logging.Logger)5 ConfigurationManager (com.sk89q.worldguard.config.ConfigurationManager)4 RegionPermissionModel (com.sk89q.worldguard.internal.permission.RegionPermissionModel)4 CommandPermissions (com.sk89q.minecraft.util.commands.CommandPermissions)3 NestedCommand (com.sk89q.minecraft.util.commands.NestedCommand)3 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)3 ProtectedPolygonalRegion (com.sk89q.worldguard.protection.regions.ProtectedPolygonalRegion)3 RegionContainer (com.sk89q.worldguard.protection.regions.RegionContainer)3 WorldEdit (com.sk89q.worldedit.WorldEdit)2 Actor (com.sk89q.worldedit.extension.platform.Actor)2 Capability (com.sk89q.worldedit.extension.platform.Capability)2