Java Code Examples for org.spongepowered.api.text.Text#Builder

The following examples show how to use org.spongepowered.api.text.Text#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConfigEditTab.java    From ChatUI with MIT License 6 votes vote down vote up
@Override
public void draw(PlayerContext ctx, LineFactory lineFactory) {
    ConfigEditTab tab = ConfigEditTab.this;
    Text.Builder builder = Text.builder();
    Object[] path = tab.control.getPath();
    for (int i = 0; i < path.length; i++) {
        Text.Builder part = Text.builder(path[i].toString());
        final int distance = path.length - i - 1;
        part.color(TextColors.BLUE).onClick(clickAction(() -> {
            int dist = distance;
            ConfigurationNode newNode = tab.control.getNode();
            while (dist-- > 0) {
                newNode = newNode.getParent();
            }
            tab.control.setNode(newNode);
        }, tab));
        builder.append(part.build());
        if (i < path.length - 1) {
            builder.append(Text.of("->"));
        }
    }
    lineFactory.addAll(ctx.utils().splitLines(builder.build(), ctx.width), ctx);
}
 
Example 2
Source File: CommandClaimInfo.java    From GriefPrevention with MIT License 6 votes vote down vote up
public static Text getClickableInfoText(CommandSource src, Claim claim, String title, Text infoText) {
    Text onClickText = Text.of("Click here to toggle value.");
    boolean hasPermission = true;
    if (src instanceof Player) {
        Text denyReason = ((GPClaim) claim).allowEdit((Player) src);
        if (denyReason != null) {
            onClickText = denyReason;
            hasPermission = false;
        }
    }

    Text.Builder textBuilder = Text.builder()
            .append(infoText)
            .onHover(TextActions.showText(Text.of(onClickText)));
    if (hasPermission) {
        textBuilder.onClick(TextActions.executeCallback(createClaimInfoConsumer(src, claim, title)));
    }
    return textBuilder.build();
}
 
Example 3
Source File: CommandHelper.java    From GriefPrevention with MIT License 6 votes vote down vote up
public static Text getClickableText(CommandSource src, Subject subject, String subjectName, Set<Context> contexts, GPClaim claim, String flagPermission, Tristate flagValue, String source, FlagType type) {
    Text onClickText = Text.of("Click here to toggle flag value.");
    boolean hasPermission = true;
    if (type == FlagType.INHERIT) {
        onClickText = Text.of("This flag is inherited from parent claim ", claim.getName().orElse(claim.getFriendlyNameType()), " and ", TextStyles.UNDERLINE, "cannot", TextStyles.RESET, " be changed.");
        hasPermission = false;
    } else if (src instanceof Player) {
        Text denyReason = claim.allowEdit((Player) src);
        if (denyReason != null) {
            onClickText = denyReason;
            hasPermission = false;
        }
    }

    Text.Builder textBuilder = Text.builder()
    .append(Text.of(flagValue.toString().toLowerCase()))
    .onHover(TextActions.showText(Text.of(onClickText, "\n", getFlagTypeHoverText(type))));
    if (hasPermission) {
        textBuilder.onClick(TextActions.executeCallback(createFlagConsumer(src, subject, subjectName, contexts, claim, flagPermission, flagValue, source)));
    }
    return textBuilder.build();
}
 
Example 4
Source File: Window.java    From ChatUI with MIT License 6 votes vote down vote up
private int addTabElement(LineFactory lineFactory, TextBuffer buffer, Text.Builder builder, int currentLineWidth, int maxWidth,
        PlayerContext ctx) {
    if (currentLineWidth + buffer.getWidth() > maxWidth) {
        // Overspilled - finish this line and move to another one
        builder.append(ctx.utils().repeatAndTerminate('═', this.tabListLines == 1 ? '╗' : '╣', maxWidth - currentLineWidth));
        lineFactory.appendNewLine(builder.build(), ctx);
        char startChar = '╠';
        builder.removeAll();
        currentLineWidth = ctx.utils().getWidth(startChar, false);
        builder.append(TextUtils.charCache(startChar));
        this.tabListLines++;
    }
    currentLineWidth += buffer.getWidth();
    builder.append(buffer.getContents());
    return currentLineWidth;
}
 
Example 5
Source File: ConfigEditTab.java    From ChatUI with MIT License 6 votes vote down vote up
@Override
public void draw(PlayerContext ctx, LineFactory lineFactory) {
    Text.Builder builder = Text.builder();
    ConfigEditTab tab = ConfigEditTab.this;
    if (tab.control.getActiveEntry() == null) {
        builder.append(Text.of(clickAction(tab.scroll::scrollUp, tab),
                tab.scroll.canScrollUp() ? TextColors.WHITE : TextColors.DARK_GRAY, "[Scroll Up] "));
        builder.append(Text.of(clickAction(tab.scroll::scrollDown, tab),
                tab.scroll.canScrollDown() ? TextColors.WHITE : TextColors.DARK_GRAY, "[Scroll Down] "));
        if (tab.control.options.canAdd && !tab.control.inDeleteMode()) {
            builder.append(Text.of(clickAction(() -> {
                tab.nodeBuilder = NodeBuilder.forNode(tab.control.getNode(), tab);
            }, tab), TextColors.GREEN, "[Add]"));
        }
    } else {
        builder.append(Text.of(clickAction(tab.control::closeActiveEntry, tab), TextColors.RED, "[Close]"));
    }
    if (tab.control.options.canDelete) {
        builder.append(Text.of(clickAction(tab.control::setDeleteModeOrDeleteNode, tab), TextColors.RED, " [Delete]"));
    }
    lineFactory.appendNewLine(builder.build(), ctx);
}
 
Example 6
Source File: Format.java    From Prism with MIT License 6 votes vote down vote up
/**
 * Return content formatted with location information.
 * Optionally a click action can be added to teleport
 * the message recipients to the provided location.
 * @param x X Coordinate
 * @param y Y Coordinate
 * @param z Z Coordinate
 * @param world World
 * @param clickAction Click Action
 * @return Text Formatted content.
 */
public static Text location(int x, int y, int z, World world, boolean clickAction) {
    Text.Builder textBuilder = Text.builder();
    textBuilder.append(Text.of("(x:", x, " y:", y, " z:", z));
    if (world != null) {
        textBuilder.append(Text.of(" world:", world.getName()));

        if (clickAction) {
            textBuilder.onClick(TextActions.executeCallback(commandSource -> {
                if (!(commandSource instanceof Player)) {
                    return;
                }

                ((Player) commandSource).setLocation(world.getLocation(x, y, z));
            }));
        }
    }

    return textBuilder.append(Text.of(")")).build();
}
 
Example 7
Source File: DefaultTableRenderer.java    From ChatUI with MIT License 5 votes vote down vote up
@Override
public Text createBorder(TableModel model, int rowIndex, int[] colMaxWidths, PlayerContext ctx) {
    char left = '├';
    char right = '┤';
    char join = '┼';
    if (rowIndex == -1) {
        left = '┌';
        right = '┐';
        join = '┬';
    } else if (rowIndex == model.getRowCount() - 1) {
        left = '└';
        right = '┘';
        join = '┴';
    }
    Text.Builder lineBuilder = Text.builder();
    for (int i = 0; i < colMaxWidths.length; i++) {
        char edge = i == 0 ? left : join;
        int edgeWidth = ctx.utils().getWidth(edge, false);
        int width = colMaxWidths[i] + edgeWidth;
        if (i < colMaxWidths.length - 1) {
            ctx.utils().startAndRepeat(lineBuilder, edge, '─', width);
        } else {
            width += edgeWidth;
            ctx.utils().startRepeatTerminate(lineBuilder, edge, '─', right, width);
        }
    }
    return lineBuilder.build();
}
 
Example 8
Source File: Window.java    From ChatUI with MIT License 5 votes vote down vote up
private Text createTabButton(int tabIndex) {
    Text.Builder button = getTabs().get(tabIndex).getTitle().toBuilder();
    button.color(TextColors.GREEN);
    if (tabIndex == getActiveIndex()) {
        button.style(TextStyles.BOLD);
    } else {
        button.onClick(ChatUILib.command("settab " + tabIndex));
    }
    return button.build();
}
 
Example 9
Source File: CommandHelper.java    From GriefPrevention with MIT License 5 votes vote down vote up
public static Text getClickableText(CommandSource src, Subject subject, String subjectName, Set<Context> contexts, String flagPermission, Tristate flagValue, FlagType type) {
    String onClickText = "Click here to toggle " + type.name().toLowerCase() + " value.";
    Text.Builder textBuilder = Text.builder()
            .append(Text.of(flagValue.toString().toLowerCase()))
            .onHover(TextActions.showText(Text.of(onClickText, "\n", getFlagTypeHoverText(type))))
            .onClick(TextActions.executeCallback(createFlagConsumer(src, subject, subjectName, contexts, flagPermission, flagValue, type)));
    return textBuilder.build();
}
 
Example 10
Source File: FirstParsingWrapper.java    From UltimateCore with MIT License 5 votes vote down vote up
public Text getUsageKey(CommandSource src) {
    final Text.Builder ret = Text.builder();
    for (Iterator<UCommandElement> it = this.elements.iterator(); it.hasNext(); ) {
        ret.append(it.next().getUsageKey(src));
        if (it.hasNext()) {
            ret.append(CommandMessageFormatting.PIPE_TEXT);
        }
    }
    return ret.build();
}
 
Example 11
Source File: LineDrawingContext.java    From ChatUI with MIT License 5 votes vote down vote up
public Text toText() {
    if (this.characters.isEmpty()) {
        return Text.EMPTY;
    }
    StringBuilder string = new StringBuilder();
    Text.Builder rootBuilder = Text.builder();
    PixelMetadata prevData = null;
    for (int i = 0; i <= this.currentMax; i++) {
        PixelMetadata data = this.metadata.get(i);
        char c = this.characters.get(i);
        if (data == null) {
            data = this.emptyData;
            c = this.emptyChar;
        }
        if (prevData == null) {
            prevData = data;
        }
        if (!prevData.equals(data)) {
            rootBuilder.append(prevData.toText(string.toString()));
            string = new StringBuilder();
            prevData = data;
        }
        string.append(c);
    }
    if (string.length() > 0 && prevData != null) {
        rootBuilder.append(prevData.toText(string.toString()));
    }
    return rootBuilder.build();
}
 
Example 12
Source File: TextUtil.java    From UltimateCore with MIT License 5 votes vote down vote up
public static Text replaceColors(Text text, CommandSource p, String permissionPrefix) {
    Text.Builder builder = Text.builder();
    for (Text child : getAllChildren(text)) {
        Text fnl = merge(replaceColors(child.toPlain(), p, permissionPrefix), child);
        builder.append(fnl);
    }
    return builder.toText();
}
 
Example 13
Source File: ConfigEntry.java    From ChatUI with MIT License 4 votes vote down vote up
@Override
public Text.Builder toText(Object value) {
    return super.toText(value).color((Boolean) value ? TextColors.GREEN : TextColors.RED);
}
 
Example 14
Source File: GriefPreventionPlugin.java    From GriefPrevention with MIT License 4 votes vote down vote up
public static void addEventLogEntry(Event event, Location<World> location, String sourceId, String targetId, Subject permissionSubject, String permission, String trust, Tristate result) {
    final String eventName = event.getClass().getSimpleName().replace('$', '.').replace(".Impl", "");
    final String eventLocation = location == null ? "none" : location.getBlockPosition().toString();
    for (GPDebugData debugEntry : GriefPreventionPlugin.instance.getDebugUserMap().values()) {
        final CommandSource debugSource = debugEntry.getSource();
        final User debugUser = debugEntry.getTarget();
        if (debugUser != null) {
            if (permissionSubject == null) {
                continue;
            }
            // Check event source user
            if (!permissionSubject.getIdentifier().equals(debugUser.getUniqueId().toString())) {
                continue;
            }
        }

        String messageUser = permissionSubject.getIdentifier();
        if (permissionSubject instanceof User) {
            messageUser = ((User) permissionSubject).getName();
        }
        // record
        if (debugEntry.isRecording()) {
            String messageFlag = permission;
            permission = permission.replace("griefprevention.flag.", "");
            messageFlag = GPPermissionHandler.getFlagFromPermission(permission).toString();
            final String messageSource = sourceId == null ? "none" : sourceId;
            String messageTarget = targetId == null ? "none" : targetId;
            if (messageTarget.endsWith(".0")) {
                messageTarget = messageTarget.substring(0, messageTarget.length() - 2);
            }
            if (trust == null) {
                trust = "none";
            }

            debugEntry.addRecord(messageFlag, trust, messageSource, messageTarget, eventLocation, messageUser, result);
            continue;
        }

        final Text textEvent = Text.of(GP_TEXT, TextColors.GRAY, "Event: ", TextColors.GREEN, eventName, "\n");
        final Text textCause = Text.of(GP_TEXT, TextColors.GRAY, "Cause: ", TextColors.LIGHT_PURPLE, sourceId, "\n");
        final Text textLocation = Text.of(GP_TEXT, TextColors.GRAY, "Location: ", TextColors.WHITE, eventLocation == null ? "NONE" : eventLocation);
        final Text textUser = Text.of(TextColors.GRAY, "User: ", TextColors.GOLD, messageUser, "\n");
        final Text textLocationAndUser = Text.of(textLocation, " ", textUser);
        Text textContext = null;
        Text textPermission = null;
        if (targetId != null) {
            textContext = Text.of(GP_TEXT, TextColors.GRAY, "Target: ", TextColors.YELLOW, GPPermissionHandler.getPermissionIdentifier(targetId), "\n");
        }
        if (permission != null) {
            textPermission = Text.of(GP_TEXT, TextColors.GRAY, "Permission: ", TextColors.RED, permission, "\n");
        }
        Text.Builder textBuilder = Text.builder().append(textEvent);
        if (textContext != null) {
            textBuilder.append(textContext);
        } else {
            textBuilder.append(textCause);
        }
        if (textPermission != null) {
            textBuilder.append(textPermission);
        }
        textBuilder.append(textLocationAndUser);
        debugSource.sendMessage(textBuilder.build());
    }
}
 
Example 15
Source File: ConfigEntry.java    From ChatUI with MIT License 4 votes vote down vote up
@Override
public Text.Builder toText(Object value) {
    return Text.builder("NULL");
}
 
Example 16
Source File: TextUtils.java    From ChatUI with MIT License 4 votes vote down vote up
public Text repeatAndTerminate(char repChar, char termChar, int length) {
    Text.Builder builder = Text.builder();
    repeatAndTerminate(builder, repChar, termChar, length);
    return builder.build();
}
 
Example 17
Source File: SpongeChatManager.java    From FastAsyncWorldedit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Text.Builder builder() {
    return Text.builder();
}
 
Example 18
Source File: ConfigEntry.java    From ChatUI with MIT License 4 votes vote down vote up
public Text.Builder toText(Object value) {
    return Text.builder(checkType(value).toString());
}
 
Example 19
Source File: TextUtils.java    From ChatUI with MIT License 4 votes vote down vote up
public Text startRepeatTerminate(char startChar, char repChar, char termChar, int length) {
    Text.Builder builder = Text.builder();
    startRepeatTerminate(builder, startChar, repChar, termChar, length);
    return builder.build();
}
 
Example 20
Source File: TextSplitter.java    From ChatUI with MIT License 4 votes vote down vote up
public Text.Builder applyToBuilder(Text.Builder builder) {
    return builder.format(this.format).onClick(this.onClick).onHover(this.onHover).onShiftClick(this.onShiftClick);
}