Search in sources :

Example 1 with UnknownFlag

use of com.sk89q.worldguard.protection.flags.registry.UnknownFlag 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 2 with UnknownFlag

use of com.sk89q.worldguard.protection.flags.registry.UnknownFlag in project WorldGuard by EngineHub.

the class RegionPrintoutBuilder method appendFlagsList.

/**
 * Append just the list of flags (without "Flags:"), including colors.
 *
 * @param useColors true to use colors
 */
public void appendFlagsList(boolean useColors) {
    boolean hasFlags = false;
    for (Flag<?> flag : WorldGuard.getInstance().getFlagRegistry()) {
        Object val = region.getFlag(flag);
        // No value
        if (val == null) {
            continue;
        }
        if (hasFlags) {
            builder.append(TextComponent.of(", "));
        }
        RegionGroupFlag groupFlag = flag.getRegionGroupFlag();
        Object group = null;
        if (groupFlag != null) {
            group = region.getFlag(groupFlag);
        }
        String flagString;
        if (group == null) {
            flagString = flag.getName() + ": ";
        } else {
            flagString = flag.getName() + " -g " + group + ": ";
        }
        TextColor flagColor = TextColor.WHITE;
        if (useColors) {
            // passthrough is ok on global
            if (FlagHelperBox.DANGER_ZONE.contains(flag) && !(region.getId().equals(ProtectedRegion.GLOBAL_REGION) && flag == Flags.PASSTHROUGH)) {
                flagColor = TextColor.DARK_RED;
            } else if (Flags.INBUILT_FLAGS.contains(flag.getName())) {
                flagColor = TextColor.GOLD;
            } else if (flag instanceof UnknownFlag) {
                flagColor = TextColor.GRAY;
            } else {
                flagColor = TextColor.LIGHT_PURPLE;
            }
        }
        TextComponent flagText = TextComponent.of(flagString, flagColor).append(TextComponent.of(String.valueOf(val), useColors ? TextColor.YELLOW : TextColor.WHITE));
        if (perms != null && perms.maySetFlag(region, flag)) {
            flagText = flagText.hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to set flag"))).clickEvent(ClickEvent.of(ClickEvent.Action.SUGGEST_COMMAND, "/rg flag -w \"" + world + "\" " + region.getId() + " " + flag.getName() + " "));
        }
        builder.append(flagText);
        hasFlags = true;
    }
    if (!hasFlags) {
        TextComponent noFlags = TextComponent.of("(none)", useColors ? TextColor.RED : TextColor.WHITE);
        builder.append(noFlags);
    }
    if (perms != null && perms.maySetFlag(region)) {
        builder.append(TextComponent.space()).append(TextComponent.of("[Flags]", useColors ? TextColor.GREEN : TextColor.GRAY).hoverEvent(HoverEvent.of(HoverEvent.Action.SHOW_TEXT, TextComponent.of("Click to set a flag"))).clickEvent(ClickEvent.of(ClickEvent.Action.RUN_COMMAND, "/rg flags -w \"" + world + "\" " + region.getId())));
    }
}
Also used : TextComponent(com.sk89q.worldedit.util.formatting.text.TextComponent) RegionGroupFlag(com.sk89q.worldguard.protection.flags.RegionGroupFlag) TextColor(com.sk89q.worldedit.util.formatting.text.format.TextColor) UnknownFlag(com.sk89q.worldguard.protection.flags.registry.UnknownFlag)

Aggregations

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