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.configuration.caption;
020
021import com.plotsquared.core.plot.PlotArea;
022import com.plotsquared.core.util.PlayerManager;
023import net.kyori.adventure.text.minimessage.MiniMessage;
024import net.kyori.adventure.text.minimessage.Template;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027import java.util.UUID;
028
029/**
030 * Utility class that generates {@link net.kyori.adventure.text.minimessage.Template templates}
031 */
032public final class Templates {
033
034    private static final MiniMessage MINI_MESSAGE = MiniMessage.builder().build();
035
036    private Templates() {
037        throw new UnsupportedOperationException(
038                "This is a utility class and cannot be instantiated");
039    }
040
041    /**
042     * Create a {@link net.kyori.adventure.text.minimessage.Template} from a PlotSquared {@link Caption}
043     *
044     * @param localeHolder Locale holder
045     * @param key          Template key
046     * @param caption      Caption object
047     * @param replacements Replacements
048     * @return Generated template
049     */
050    public static @NonNull Template of(
051            final @NonNull LocaleHolder localeHolder,
052            final @NonNull String key, final @NonNull Caption caption,
053            final @NonNull Template... replacements
054    ) {
055        return Template.of(key, MINI_MESSAGE.parse(caption.getComponent(localeHolder), replacements));
056    }
057
058    /**
059     * Create a {@link Template} from a username (using UUID mappings)
060     *
061     * @param key  Template key
062     * @param uuid Player UUID
063     * @return Generated template
064     */
065    public static @NonNull Template of(final @NonNull String key, final @NonNull UUID uuid) {
066        final String username = PlayerManager.resolveName(uuid).getComponent(LocaleHolder.console());
067        return Template.of(key, username);
068    }
069
070    /**
071     * Create a {@link Template} from a string
072     *
073     * @param key   Template key
074     * @param value Template value
075     * @return Generated template
076     */
077    public static @NonNull Template of(final @NonNull String key, final @NonNull String value) {
078        return Template.of(key, value);
079    }
080
081    /**
082     * Create a {@link Template} from a plot area
083     *
084     * @param key  Template Key
085     * @param area Plot area
086     * @return Generated template
087     */
088    public static @NonNull Template of(final @NonNull String key, final @NonNull PlotArea area) {
089        return Template.of(key, area.toString());
090    }
091
092    /**
093     * Create a {@link Template} from a number
094     *
095     * @param key    Template key
096     * @param number Number
097     * @return Generated template
098     */
099    public static @NonNull Template of(final @NonNull String key, final @NonNull Number number) {
100        return Template.of(key, number.toString());
101    }
102
103}