package com.plotsquared.bukkit.chat; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import com.google.gson.stream.JsonWriter; import com.intellectualcrafters.configuration.serialization.ConfigurationSerializable; import com.intellectualcrafters.configuration.serialization.ConfigurationSerialization; import java.io.IOException; import java.util.HashMap; import java.util.Map; /** * Represents a textual component of a message part. * This can be used to not only represent string literals in a JSON message, * but also to represent localized strings and other text values. *
Different instances of this class can be created with static constructor methods.
*/ public abstract class TextualComponent implements Cloneable { static { ConfigurationSerialization.registerClass(TextualComponent.ArbitraryTextTypeComponent.class); ConfigurationSerialization.registerClass(TextualComponent.ComplexTextTypeComponent.class); } static TextualComponent deserialize(Map* If the specified translation key is not present on the client resource pack, the translation key will be displayed as a string literal to * the client. *
* @param translateKey The string key which maps to localized text. * @return The text component representing the specified localized text. */ public static TextualComponent localizedText(String translateKey) { return new ArbitraryTextTypeComponent("translate", translateKey); } private static void throwUnsupportedSnapshot() { throw new UnsupportedOperationException("This feature is only supported in snapshot releases."); } /** * Create a textual component representing a scoreboard value. * The client will see their own score for the specified objective as the text represented by this component. ** This method is currently guaranteed to throw an {@code UnsupportedOperationException} as it is only supported on snapshot clients. *
* @param scoreboardObjective The name of the objective for which to display the score. * @return The text component representing the specified scoreboard score (for the viewing player), or {@code null} if an error occurs during * JSON serialization. */ public static TextualComponent objectiveScore(String scoreboardObjective) { return objectiveScore("*", scoreboardObjective); } /** * Create a textual component representing a scoreboard value. * The client will see the score of the specified player for the specified objective as the text represented by this component. ** This method is currently guaranteed to throw an {@code UnsupportedOperationException} as it is only supported on snapshot clients. *
* @param playerName The name of the player whos score will be shown. If this string represents the single-character sequence "*", the viewing * player's score will be displayed. * Standard minecraft selectors (@a, @p, etc) are not supported. * @param scoreboardObjective The name of the objective for which to display the score. * @return The text component representing the specified scoreboard score for the specified player, or {@code null} if an error occurs during * JSON serialization. */ public static TextualComponent objectiveScore(String playerName, String scoreboardObjective) { throwUnsupportedSnapshot(); // Remove this line when the feature is released to non-snapshot versions, in addition to updating ALL THE // OVERLOADS documentation accordingly return new ComplexTextTypeComponent("score", ImmutableMap.* This method is currently guaranteed to throw an {@code UnsupportedOperationException} as it is only supported on snapshot clients. *
* @param selector The minecraft player or entity selector which will capture the entities whose string representations will be displayed in * the place of this text component. * @return The text component representing the name of the entities captured by the selector. */ public static TextualComponent selector(String selector) { throwUnsupportedSnapshot(); // Remove this line when the feature is released to non-snapshot versions, in addition to updating ALL THE // OVERLOADS documentation accordingly return new ArbitraryTextTypeComponent("selector", selector); } @Override public String toString() { return getReadableString(); } /** * @return The JSON key used to represent text components of this type. */ public abstract String getKey(); /** * @return A readable String */ public abstract String getReadableString(); /** * Clones a textual component instance. * The returned object should not reference this textual component instance, but should maintain the same key and value. */ @Override public abstract TextualComponent clone() throws CloneNotSupportedException; /** * Writes the text data represented by this textual component to the specified JSON writer object. * A new object within the writer is not started. * @param writer The object to which to write the JSON data. * @throws IOException If an error occurs while writing to the stream. */ public abstract void writeJson(JsonWriter writer) throws IOException; /** * Internal class used to represent all types of text components. * Exception validating done is on keys and values. */ private static final class ArbitraryTextTypeComponent extends TextualComponent implements ConfigurationSerializable { private String _key; private String _value; public ArbitraryTextTypeComponent(String key, String value) { setKey(key); setValue(value); } public static ArbitraryTextTypeComponent deserialize(Map