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.google.common.base.Objects;
022import com.plotsquared.core.PlotSquared;
023import net.kyori.adventure.text.Component;
024import net.kyori.adventure.text.minimessage.MiniMessage;
025import net.kyori.adventure.text.minimessage.tag.Tag;
026import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
027import org.checkerframework.checker.nullness.qual.NonNull;
028import org.jetbrains.annotations.NotNull;
029
030import java.util.Arrays;
031import java.util.Locale;
032import java.util.regex.Pattern;
033
034/**
035 * Caption that is user modifiable
036 */
037public final class TranslatableCaption implements NamespacedCaption {
038
039    /**
040     * Default caption namespace
041     */
042    public static final String DEFAULT_NAMESPACE = "plotsquared";
043
044    private final String namespace;
045    private final String key;
046
047    private TranslatableCaption(final @NonNull String namespace, final @NonNull String key) {
048        this.namespace = namespace;
049        this.key = key;
050    }
051
052    /**
053     * Get a new {@link TranslatableCaption} instance
054     *
055     * @param rawKey Caption key in the format namespace:key. If no namespace is
056     *               included, {@link #DEFAULT_NAMESPACE} will be used.
057     * @return Caption instance
058     */
059    public static @NonNull TranslatableCaption of(final @NonNull String rawKey) {
060        final String namespace;
061        final String key;
062        if (rawKey.contains(":")) {
063            final String[] split = rawKey.split(Pattern.quote(":"));
064            namespace = split[0];
065            key = split[1];
066        } else {
067            namespace = DEFAULT_NAMESPACE;
068            key = rawKey;
069        }
070        return new TranslatableCaption(
071                namespace.toLowerCase(Locale.ENGLISH),
072                key.toLowerCase(Locale.ENGLISH)
073        );
074    }
075
076    /**
077     * Get a new {@link TranslatableCaption} instance
078     *
079     * @param namespace Caption namespace
080     * @param key       Caption key
081     * @return Caption instance
082     */
083    public static @NonNull TranslatableCaption of(
084            final @NonNull String namespace,
085            final @NonNull String key
086    ) {
087        return new TranslatableCaption(
088                namespace.toLowerCase(Locale.ENGLISH),
089                key.toLowerCase(Locale.ENGLISH)
090        );
091    }
092
093    @Override
094    public @NonNull String getComponent(final @NonNull LocaleHolder localeHolder) {
095        return PlotSquared.get().getCaptionMap(this.namespace).getMessage(this, localeHolder);
096    }
097
098    @Override
099    public @NonNull Component toComponent(@NonNull final LocaleHolder localeHolder) {
100        return this.toComponent(localeHolder, new TagResolver[0]);
101    }
102
103    @Override
104    public @NonNull Component toComponent(
105            @NonNull final LocaleHolder localeHolder,
106            final @NonNull TagResolver @NonNull ... tagResolvers
107    ) {
108        if (getKey().equals("core.prefix")) {
109            return MiniMessage.miniMessage().deserialize(getComponent(localeHolder));
110        }
111        TagResolver[] finalResolvers = Arrays.copyOf(tagResolvers, tagResolvers.length + 1);
112        finalResolvers[finalResolvers.length - 1] = TagResolver.resolver(
113                "prefix",
114                Tag.inserting(TranslatableCaption.of("core.prefix").toComponent(localeHolder))
115        );
116        return MiniMessage.miniMessage().deserialize(getComponent(localeHolder), finalResolvers);
117    }
118
119    @Override
120    public @NonNull String getKey() {
121        return this.key;
122    }
123
124    @Override
125    public @NonNull String getNamespace() {
126        return this.namespace;
127    }
128
129    @Override
130    public boolean equals(final Object o) {
131        if (this == o) {
132            return true;
133        }
134        if (o == null || this.getClass() != o.getClass()) {
135            return false;
136        }
137        final TranslatableCaption that = (TranslatableCaption) o;
138        return Objects.equal(this.getNamespace(), that.getNamespace()) && Objects
139                .equal(this.getKey(), that.getKey());
140    }
141
142    @Override
143    public int hashCode() {
144        return Objects.hashCode(this.getNamespace(), this.getKey());
145    }
146
147    @Override
148    public @NotNull String toString() {
149        return "TranslatableCaption(" + getNamespace() + ":" + getKey() + ")";
150    }
151
152}