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