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