diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java index 4f3fcaf..c99b69a 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java @@ -6,7 +6,9 @@ import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** @@ -17,6 +19,7 @@ public final class FormatBuilder { private static @Nullable StringFormatter stringFormatter = null; private static final IllegalStateException NOT_SETUP_EXCEPTION = new IllegalStateException("String formatter has not been set!"); + private static final String LIST_DELIMITER = ","; private @NotNull String toFormat; /** @@ -41,7 +44,7 @@ public final class FormatBuilder { * @param translatableMessage
The translatable message to format
* @throws IllegalStateExceptionIf the string formatter has not been set
*/ - public FormatBuilder(@NotNull TranslatableMessage translatableMessage) { + public FormatBuilder(@NotNull TranslatableMessage translatableMessage) throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -98,10 +101,12 @@ public final class FormatBuilder { * @param placeholderThe placeholder to replace
* @param replacementThe replacement
* @returnThis format builder
+ * @throws IllegalStateExceptionIf the string formatter has not been set, and the placeholder or replacement is + * a translatable message
*/ @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull String replacement) { - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement); + publicThe translatable message to replace
- * @param replacementThe replacement translatable message
- * @returnThis format builder
- * @throws IllegalStateExceptionIf the string formatter has not been set
- */ - @NotNull - public FormatBuilder replace(@NotNull TranslatableMessage placeholder, - @NotNull TranslatableMessage replacement) { - if (FormatBuilder.stringFormatter == null) { - throw NOT_SETUP_EXCEPTION; - } - return replace(FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), - FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe placeholder to replace
- * @param replacementThe replacement translatable message
- * @returnThis format builder
- * @throws IllegalStateExceptionIf the string formatter has not been set
- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull TranslatableMessage replacement) { - if (FormatBuilder.stringFormatter == null) { - throw NOT_SETUP_EXCEPTION; - } - return replace(placeholder, FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe translatable message placeholder to replace
- * @param replacementThe replacement string
- * @returnThis format builder
- * @throws IllegalStateExceptionIf the string formatter has not been set
- */ - @NotNull - public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull String replacement) { - if (FormatBuilder.stringFormatter == null) { - throw NOT_SETUP_EXCEPTION; - } - return replace(FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), replacement); - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe placeholder to replace
- * @param stringBuilderThe replacement string builder
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull StringBuilder stringBuilder) { - return replace(placeholder, stringBuilder.toString()); - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe placeholder to replace
- * @param replacementThe replacement
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull FormatBuilder replacement) { - return replace(placeholder, replacement.toString()); - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe placeholder to replace
- * @param replacementThe replacement
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull String replacement) { - return replace(placeholder.toString(), replacement); - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe placeholder to replace
- * @param replacementThe replacement
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull FormatBuilder replacement) { - return replace(placeholder.toString(), replacement.toString()); - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe placeholder to replace
- * @param replacementThe replacement
- * @returnThis format builder
- * @throws IllegalStateExceptionIf the string formatter has not been set
- */ - @NotNull - public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull FormatBuilder replacement) { - return replace(placeholder, replacement.toString()); - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe placeholder to replace
- * @param replacementThe replacement
- * @returnThis format builder
- * @throws IllegalStateExceptionIf the string formatter has not been set
- */ - @NotNull - public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull TranslatableMessage replacement) { - return replace(placeholder.toString(), replacement); - } - - /** - * Replaces a placeholder for the current string - * - * @param placeholderThe translatable message placeholder to replace
- * @param replacementThe object to get the string value of
- * @returnThis format builder
- * @throws IllegalStateExceptionIf the string formatter has not been set
- */ - @NotNull - public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull Object replacement) { - return replace(placeholder, String.valueOf(replacement)); - } - - /** - * Replaces a placeholder for the current string - * - * @param placeholderThe placeholder to replace
- * @param replacementThe object to get the string value of
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull Object replacement) { - return replace(placeholder, String.valueOf(replacement)); - } - - /** - * Replaces a placeholder for the current string - * - *This method turns the given array into a string, and replaces the placeholder with that string.
- * - * @param placeholderThe placeholder to replace
- * @param replacementThe list of strings to replace with
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull ListThis method turns the given array into a string, and replaces the placeholder with that string.
- * - * @param placeholderThe placeholder to replace
- * @param replacementThe list of strings to replace with
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull String[] replacement) { - return replace(placeholder, Arrays.toString(replacement)); - } - /** * Converts color codes in the current string * @@ -304,7 +132,7 @@ public final class FormatBuilder { * @throws IllegalStateExceptionIf the string formatter has not been set
*/ @NotNull - public FormatBuilder color() { + public FormatBuilder color() throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -331,57 +159,21 @@ public final class FormatBuilder { * * @param inputThe input to append
* @returnThis format builder
+ * @throws IllegalStateExceptionIf the string formatter has not been set, and the input is a translatable message
*/ @NotNull - public FormatBuilder append(@NotNull String input) { - this.toFormat += input; + publicThe translatable message to append
- * @returnThis format builder
- * @throws IllegalStateExceptionIf the string formatter has not been set
- */ - @NotNull - public FormatBuilder append(@NotNull TranslatableMessage translatableMessage) { - if (FormatBuilder.stringFormatter == null) { - throw NOT_SETUP_EXCEPTION; - } - return append(FormatBuilder.stringFormatter.getUnFormattedMessage(translatableMessage)); - } - - /** - * Appends the given string builder's output to this format builder - * - * @param stringBuilderThe string builder to append
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder append(@NotNull StringBuilder stringBuilder) { - return append(stringBuilder.toString()); - } - - /** - * Appends the given format builder's output to this format builder - * - * @param formatBuilderThe format builder to append
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder append(@NotNull FormatBuilder formatBuilder) { - return append(formatBuilder.toString()); - } - /** * Displays the result to the specified command sender as a success message * * @param commandSenderThe recipient
* @throws IllegalStateExceptionIf the string formatter has not been set
*/ - public void success(@NotNull CommandSender commandSender) { + public void success(@NotNull CommandSender commandSender) throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -394,7 +186,7 @@ public final class FormatBuilder { * @param commandSenderThe recipient
* @throws IllegalStateExceptionIf the string formatter has not been set
*/ - public void error(@NotNull CommandSender commandSender) { + public void error(@NotNull CommandSender commandSender) throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -407,7 +199,7 @@ public final class FormatBuilder { * @param commandSenderThe recipient
* @throws IllegalStateExceptionIf the string formatter has not been set
*/ - public void neutral(@NotNull CommandSender commandSender) { + public void neutral(@NotNull CommandSender commandSender) throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -420,4 +212,41 @@ public final class FormatBuilder { return this.toFormat; } + /** + * Converts the given input to a string + * + * @param inputThe input to convert
+ * @returnThe corresponding string
+ * @throws IllegalStateExceptionIf the string formatter has not been set, and the input is a translatable message.
+ */ + @NotNull + private