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