diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java index 6680199..4f3fcaf 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java @@ -1,9 +1,12 @@ package net.knarcraft.knarlib.formatting; +import net.knarcraft.knarlib.property.ColorConversion; +import net.knarcraft.knarlib.util.ColorHelper; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; import java.util.List; /** @@ -76,88 +79,6 @@ public final class FormatBuilder { return this; } - /** - * Replaces placeholders for the current string - * - * @param placeholder
The placeholder to replace
- * @param replacementThe replacement
- * @returnThis format builder
- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull String replacement) { - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement); - return this; - } - - /** - * Replaces placeholders for the current string - * - * @param placeholderThe 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; - } - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, - FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), - FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); - return this; - } - - /** - * 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; - } - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, - FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); - return this; - } - - /** - * 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; - } - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, - FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), replacement); - return this; - } - - /** - * 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) { - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, stringBuilder.toString()); - return this; - } - /** * Replaces placeholders for the current string * @@ -171,6 +92,19 @@ public final class FormatBuilder { return this; } + /** + * 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 String replacement) { + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement); + return this; + } + /** * Replaces placeholders for the current string * @@ -183,6 +117,68 @@ public final class FormatBuilder { return this; } + /** + * Replaces placeholders for the current string + * + * @param placeholderThe 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 * @@ -192,8 +188,7 @@ public final class FormatBuilder { */ @NotNull public FormatBuilder replace(@NotNull String placeholder, @NotNull FormatBuilder replacement) { - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement.toString()); - return this; + return replace(placeholder, replacement.toString()); } /** @@ -205,8 +200,7 @@ public final class FormatBuilder { */ @NotNull public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull String replacement) { - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), replacement); - return this; + return replace(placeholder.toString(), replacement); } /** @@ -218,8 +212,7 @@ public final class FormatBuilder { */ @NotNull public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull FormatBuilder replacement) { - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), replacement.toString()); - return this; + return replace(placeholder.toString(), replacement.toString()); } /** @@ -232,12 +225,7 @@ public final class FormatBuilder { */ @NotNull public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull FormatBuilder replacement) { - if (FormatBuilder.stringFormatter == null) { - throw NOT_SETUP_EXCEPTION; - } - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, - FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), replacement.toString()); - return this; + return replace(placeholder, replacement.toString()); } /** @@ -250,12 +238,60 @@ public final class FormatBuilder { */ @NotNull public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull TranslatableMessage replacement) { - if (FormatBuilder.stringFormatter == null) { - throw NOT_SETUP_EXCEPTION; - } - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), - FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); - return this; + 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)); } /** @@ -276,6 +312,20 @@ public final class FormatBuilder { return this; } + /** + * Converts color codes in the current string + * + *This is performed as part of the success, error and neutral methods, so it's only necessary when displaying + * the message through other means.
+ * + * @param colorConversionThe type of color conversion to use
+ * @returnThis format builder
+ */ + public FormatBuilder color(@NotNull ColorConversion colorConversion) { + this.toFormat = ColorHelper.translateColorCodes(this.toFormat, colorConversion); + return this; + } + /** * Appends the given string to this format builder * @@ -300,8 +350,7 @@ public final class FormatBuilder { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } - this.toFormat += FormatBuilder.stringFormatter.getUnFormattedMessage(translatableMessage); - return this; + return append(FormatBuilder.stringFormatter.getUnFormattedMessage(translatableMessage)); } /** @@ -312,8 +361,7 @@ public final class FormatBuilder { */ @NotNull public FormatBuilder append(@NotNull StringBuilder stringBuilder) { - this.toFormat += stringBuilder.toString(); - return this; + return append(stringBuilder.toString()); } /** @@ -324,8 +372,7 @@ public final class FormatBuilder { */ @NotNull public FormatBuilder append(@NotNull FormatBuilder formatBuilder) { - this.toFormat += formatBuilder.toString(); - return this; + return append(formatBuilder.toString()); } /**