Search in sources :

Example 16 with Component

use of com.sk89q.worldedit.util.formatting.text.Component in project FastAsyncWorldEdit by IntellectualSites.

the class GeneralCommands method limit.

@Command(name = "/limit", desc = "Modify block change limit")
@CommandPermissions("worldedit.limit")
public void limit(Actor actor, LocalSession session, @Arg(desc = "The limit to set", def = "") Integer limit) {
    LocalConfiguration config = worldEdit.getConfiguration();
    boolean mayDisable = actor.hasPermission("worldedit.limit.unrestricted");
    limit = limit == null ? config.defaultChangeLimit : Math.max(-1, limit);
    if (!mayDisable && config.maxChangeLimit > -1) {
        if (limit > config.maxChangeLimit) {
            actor.print(Caption.of("worldedit.limit.too-high", TextComponent.of(config.maxChangeLimit)));
            return;
        }
    }
    session.setBlockChangeLimit(limit);
    Component component = TextComponent.empty().append(Caption.of("worldedit.limit.set", TextComponent.of(limit)));
    if (limit != config.defaultChangeLimit) {
        component.append(TextComponent.space()).append(Caption.of("worldedit.limit.return-to-default"));
    }
    actor.print(component);
}
Also used : Component(com.sk89q.worldedit.util.formatting.text.Component) TranslatableComponent(com.sk89q.worldedit.util.formatting.text.TranslatableComponent) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) LocalConfiguration(com.sk89q.worldedit.LocalConfiguration) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 17 with Component

use of com.sk89q.worldedit.util.formatting.text.Component in project WorldGuard by EngineHub.

the class FlagHelperBox method appendValueText.

private <V> void appendValueText(TextComponent.Builder builder, Flag<V> flag, String display, @Nullable Component hover) {
    V defVal = flag.getDefault();
    V currVal = region.getFlag(flag);
    boolean inherited = false;
    if (currVal == null) {
        currVal = getInheritedValue(region, flag);
        if (currVal != null) {
            inherited = true;
        }
    }
    boolean isExplicitSet = currVal != null && !inherited;
    boolean maySet = !(flag instanceof UnknownFlag) && perms.maySetFlag(region, flag);
    TextColor col = isExplicitSet ? TextColor.WHITE : inherited ? TextColor.GRAY : TextColor.DARK_GRAY;
    Set<TextDecoration> styles = currVal == defVal ? ImmutableSet.of(TextDecoration.UNDERLINED) : Collections.emptySet();
    Component displayComponent = TextComponent.empty().append(TextComponent.of(display, col, styles));
    List<Component> hoverTexts = new ArrayList<>();
    if (maySet) {
        if (isExplicitSet) {
            hoverTexts.add(TextComponent.of("Click to change", TextColor.GOLD));
        } else {
            hoverTexts.add(TextComponent.of("Click to set", TextColor.GOLD));
        }
    }
    Component valType = getToolTipHint(defVal, currVal, inherited);
    if (valType != null) {
        hoverTexts.add(valType);
    }
    if (!hoverTexts.isEmpty()) {
        TextComponent.Builder hoverBuilder = TextComponent.builder("");
        for (Iterator<Component> hovIt = hoverTexts.iterator(); hovIt.hasNext(); ) {
            hoverBuilder.append(hovIt.next());
            if (hovIt.hasNext()) {
                hoverBuilder.append(TextComponent.newline());
            }
        }
        if (hover != null) {
            hoverBuilder.append(TextComponent.newline());
            hoverBuilder.append(hover);
        }
        displayComponent = displayComponent.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, hoverBuilder.build()));
    }
    if (maySet) {
        builder.append(displayComponent.clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, makeCommand(flag, ""))));
    } else {
        builder.append(displayComponent);
    }
    builder.append(TextComponent.space());
}
Also used : TextDecoration(com.sk89q.worldedit.util.formatting.text.format.TextDecoration) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) ArrayList(java.util.ArrayList) UnknownFlag(com.sk89q.worldguard.protection.flags.registry.UnknownFlag) TextColor(com.sk89q.worldedit.util.formatting.text.format.TextColor) Component(com.sk89q.worldedit.util.formatting.text.Component) TranslatableComponent(com.sk89q.worldedit.util.formatting.text.TranslatableComponent) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent)

Example 18 with Component

use of com.sk89q.worldedit.util.formatting.text.Component in project WorldGuard by EngineHub.

the class RegionCommands method flag.

/**
 * Set a flag.
 *
 * @param args the arguments
 * @param sender the sender
 * @throws CommandException any error
 */
@Command(aliases = { "flag", "f" }, usage = "<id> <flag> [-w world] [-g group] [value]", flags = "g:w:eh:", desc = "Set flags", min = 2)
public void flag(CommandContext args, Actor sender) throws CommandException {
    warnAboutSaveFailures(sender);
    // Get the world
    World world = checkWorld(args, sender, 'w');
    String flagName = args.getString(1);
    String value = args.argsLength() >= 3 ? args.getJoinedStrings(2) : null;
    RegionGroup groupValue = null;
    FlagRegistry flagRegistry = WorldGuard.getInstance().getFlagRegistry();
    RegionPermissionModel permModel = getPermissionModel(sender);
    if (args.hasFlag('e')) {
        if (value != null) {
            throw new CommandException("You cannot use -e(mpty) with a flag value.");
        }
        value = "";
    }
    // Lookup the existing region
    RegionManager manager = checkRegionManager(world);
    ProtectedRegion existing = checkExistingRegion(manager, args.getString(0), true);
    // Check permissions
    if (!permModel.maySetFlag(existing)) {
        throw new CommandPermissionsException();
    }
    String regionId = existing.getId();
    Flag<?> foundFlag = Flags.fuzzyMatchFlag(flagRegistry, flagName);
    // can use, and do nothing afterwards
    if (foundFlag == null) {
        AsyncCommandBuilder.wrap(new FlagListBuilder(flagRegistry, permModel, existing, world, regionId, sender, flagName), sender).registerWithSupervisor(WorldGuard.getInstance().getSupervisor(), "Flag list for invalid flag command.").onSuccess((Component) null, sender::print).onFailure((Component) null, WorldGuard.getInstance().getExceptionConverter()).buildAndExec(WorldGuard.getInstance().getExecutorService());
        return;
    } else if (value != null) {
        if (foundFlag == Flags.BUILD || foundFlag == Flags.BLOCK_BREAK || foundFlag == Flags.BLOCK_PLACE) {
            sender.print(buildFlagWarning);
            if (!sender.isPlayer()) {
                sender.printRaw("https://worldguard.enginehub.org/en/latest/regions/flags/#protection-related");
            }
        } else if (foundFlag == Flags.PASSTHROUGH) {
            sender.print(passthroughFlagWarning);
            if (!sender.isPlayer()) {
                sender.printRaw("https://worldguard.enginehub.org/en/latest/regions/flags/#overrides");
            }
        }
    }
    // but not here -- in the model
    if (!permModel.maySetFlag(existing, foundFlag, value)) {
        throw new CommandPermissionsException();
    }
    // -g for group flag
    if (args.hasFlag('g')) {
        String group = args.getFlag('g');
        RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
        if (groupFlag == null) {
            throw new CommandException("Region flag '" + foundFlag.getName() + "' does not have a group flag!");
        }
        // the [value] part throws an error.
        try {
            groupValue = groupFlag.parseInput(FlagContext.create().setSender(sender).setInput(group).setObject("region", existing).build());
        } catch (InvalidFlagFormat e) {
            throw new CommandException(e.getMessage());
        }
    }
    // Set the flag value if a value was set
    if (value != null) {
        // Set the flag if [value] was given even if [-g group] was given as well
        try {
            value = setFlag(existing, foundFlag, sender, value).toString();
        } catch (InvalidFlagFormat e) {
            throw new CommandException(e.getMessage());
        }
        if (!args.hasFlag('h')) {
            sender.print("Region flag " + foundFlag.getName() + " set on '" + regionId + "' to '" + value + "'.");
        }
    // No value? Clear the flag, if -g isn't specified
    } else if (!args.hasFlag('g')) {
        // Clear the flag only if neither [value] nor [-g group] was given
        existing.setFlag(foundFlag, null);
        // Also clear the associated group flag if one exists
        RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
        if (groupFlag != null) {
            existing.setFlag(groupFlag, null);
        }
        if (!args.hasFlag('h')) {
            sender.print("Region flag " + foundFlag.getName() + " removed from '" + regionId + "'. (Any -g(roups) were also removed.)");
        }
    }
    // Now set the group
    if (groupValue != null) {
        RegionGroupFlag groupFlag = foundFlag.getRegionGroupFlag();
        // If group set to the default, then clear the group flag
        if (groupValue == groupFlag.getDefault()) {
            existing.setFlag(groupFlag, null);
            sender.print("Region group flag for '" + foundFlag.getName() + "' reset to default.");
        } else {
            existing.setFlag(groupFlag, groupValue);
            sender.print("Region group flag for '" + foundFlag.getName() + "' set.");
        }
    }
    // Print region information
    if (args.hasFlag('h')) {
        int page = args.getFlagInteger('h');
        sendFlagHelper(sender, world, existing, permModel, page);
    } else {
        RegionPrintoutBuilder printout = new RegionPrintoutBuilder(world.getName(), existing, null, sender);
        printout.append(SubtleFormat.wrap("(Current flags: "));
        printout.appendFlagsList(false);
        printout.append(SubtleFormat.wrap(")"));
        printout.send(sender);
        checkSpawnOverlap(sender, world, existing);
    }
}
Also used : CommandPermissionsException(com.sk89q.minecraft.util.commands.CommandPermissionsException) FlagRegistry(com.sk89q.worldguard.protection.flags.registry.FlagRegistry) CommandException(com.sk89q.minecraft.util.commands.CommandException) RegionGroupFlag(com.sk89q.worldguard.protection.flags.RegionGroupFlag) World(com.sk89q.worldedit.world.World) RegionGroup(com.sk89q.worldguard.protection.flags.RegionGroup) InvalidFlagFormat(com.sk89q.worldguard.protection.flags.InvalidFlagFormat) ProtectedRegion(com.sk89q.worldguard.protection.regions.ProtectedRegion) GlobalProtectedRegion(com.sk89q.worldguard.protection.regions.GlobalProtectedRegion) RegionManager(com.sk89q.worldguard.protection.managers.RegionManager) RegionPermissionModel(com.sk89q.worldguard.internal.permission.RegionPermissionModel) Component(com.sk89q.worldedit.util.formatting.text.Component) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) Command(com.sk89q.minecraft.util.commands.Command)

Aggregations

Component (com.sk89q.worldedit.util.formatting.text.Component)18 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)18 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)8 Command (org.enginehub.piston.annotation.Command)8 TranslatableComponent (com.sk89q.worldedit.util.formatting.text.TranslatableComponent)7 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)6 ArrayList (java.util.ArrayList)6 URIClipboardHolder (com.fastasyncworldedit.core.extent.clipboard.URIClipboardHolder)3 LocalConfiguration (com.sk89q.worldedit.LocalConfiguration)3 Vector3 (com.sk89q.worldedit.math.Vector3)3 Region (com.sk89q.worldedit.regions.Region)3 List (java.util.List)3 EditSession (com.sk89q.worldedit.EditSession)2 Logging (com.sk89q.worldedit.command.util.Logging)2 Location (com.sk89q.worldedit.util.Location)2 PaginationBox (com.sk89q.worldedit.util.formatting.component.PaginationBox)2 TextComponentProducer (com.sk89q.worldedit.util.formatting.component.TextComponentProducer)2 TextColor (com.sk89q.worldedit.util.formatting.text.format.TextColor)2 World (com.sk89q.worldedit.world.World)2 File (java.io.File)2