001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.util;
020
021import net.kyori.adventure.text.Component;
022import net.kyori.adventure.text.ComponentLike;
023import net.kyori.adventure.text.TextComponent;
024
025import java.util.Collection;
026
027/**
028 * A utility class for modifying components.
029 *
030 * @since 7.0.0
031 */
032public class ComponentHelper {
033
034    /**
035     * Joins multiple {@link Component}s into one final {@link ComponentLike}
036     *
037     * @param components The components to join
038     * @param delimiter  The delimiter to use between the components
039     * @return The joined components
040     * @since 7.0.0
041     */
042    public static ComponentLike join(Collection<? extends ComponentLike> components, Component delimiter) {
043        return join(components.toArray(ComponentLike[]::new), delimiter);
044    }
045
046    /**
047     * Joins multiple {@link ComponentLike}s into one final {@link ComponentLike}
048     *
049     * @param components The components to join
050     * @param delimiter  The delimiter to use between the components
051     * @return The joined components
052     * @since 7.0.0
053     */
054    public static Component join(ComponentLike[] components, Component delimiter) {
055        TextComponent.Builder builder = Component.text();
056        for (int i = 0, j = components.length; i < j; i++) {
057            if (i > 0) {
058                builder.append(delimiter);
059            }
060            builder.append(components[i]);
061        }
062        return builder.build();
063    }
064
065}