Search in sources :

Example 1 with TextDecoration

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

the class FlagHelperBox method appendValueChoices.

private <V> void appendValueChoices(TextComponent.Builder builder, Flag<V> flag, Iterator<V> choices, @Nullable String suggestChoice) {
    V defVal = flag.getDefault();
    V currVal = region.getFlag(flag);
    boolean inherited = false;
    if (currVal == null) {
        currVal = getInheritedValue(region, flag);
        if (currVal != null) {
            inherited = true;
        }
    }
    while (choices.hasNext()) {
        V choice = choices.next();
        boolean isExplicitSet = currVal == choice && !inherited;
        boolean maySet = perms.maySetFlag(region, flag, isExplicitSet ? null : String.valueOf(choice));
        TextColor col = isExplicitSet ? TextColor.WHITE : inherited && currVal == choice ? TextColor.GRAY : TextColor.DARK_GRAY;
        Set<TextDecoration> styles = choice == defVal ? ImmutableSet.of(TextDecoration.UNDERLINED) : Collections.emptySet();
        Component choiceComponent = TextComponent.empty().append(TextComponent.of(capitalize(String.valueOf(choice)), col, styles));
        List<Component> hoverTexts = new ArrayList<>();
        if (maySet) {
            if (isExplicitSet) {
                hoverTexts.add(TextComponent.of("Click to unset", TextColor.GOLD));
            } else if (DANGER_ZONE.contains(flag) && !(ProtectedRegion.GLOBAL_REGION.equals(region.getId()) && flag == Flags.PASSTHROUGH)) {
                hoverTexts.add(TextComponent.of("Setting this flag may have unintended consequences.", TextColor.RED).append(TextComponent.newline()).append(TextComponent.of("Please read the documentation and set this flag manually if you really intend to.").append(TextComponent.newline()).append(TextComponent.of("(Hint: You do not need to set this to protect the region!)"))));
            } else {
                hoverTexts.add(TextComponent.of("Click to set", TextColor.GOLD));
            }
        }
        Component valType = getToolTipHint(defVal, choice, 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());
                }
            }
            choiceComponent = choiceComponent.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, hoverBuilder.build()));
        }
        if (maySet && (isExplicitSet || !DANGER_ZONE.contains(flag))) {
            builder.append(choiceComponent.clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, makeCommand(flag, isExplicitSet ? "" : choice))));
        } else {
            builder.append(choiceComponent);
        }
        builder.append(TextComponent.space());
    }
    if (suggestChoice != null && perms.maySetFlag(region, flag)) {
        builder.append(TextComponent.of(suggestChoice, TextColor.DARK_GRAY).hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to set custom value", TextColor.GOLD))).clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, makeCommand(flag, ""))));
    }
}
Also used : TextDecoration(com.sk89q.worldedit.util.formatting.text.format.TextDecoration) TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) ArrayList(java.util.ArrayList) 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 2 with TextDecoration

use of com.sk89q.worldedit.util.formatting.text.format.TextDecoration 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)

Aggregations

Component (com.sk89q.worldedit.util.formatting.text.Component)2 TextComponent (com.sk89q.worldedit.util.formatting.text.TextComponent)2 TranslatableComponent (com.sk89q.worldedit.util.formatting.text.TranslatableComponent)2 TextColor (com.sk89q.worldedit.util.formatting.text.format.TextColor)2 TextDecoration (com.sk89q.worldedit.util.formatting.text.format.TextDecoration)2 ArrayList (java.util.ArrayList)2 UnknownFlag (com.sk89q.worldguard.protection.flags.registry.UnknownFlag)1