From dd3345ff9a238b2749e0cc10d6a24eca5a40c022 Mon Sep 17 00:00:00 2001 From: themode Date: Tue, 6 Oct 2020 07:41:35 +0200 Subject: [PATCH] ColoredText comments + cleanup --- .../net/minestom/server/chat/ColoredText.java | 114 ++++++++++++------ .../net/minestom/server/chat/RichMessage.java | 2 + .../server/entity/damage/DamageType.java | 4 +- .../server/event/handler/EventHandler.java | 18 +-- 4 files changed, 91 insertions(+), 47 deletions(-) diff --git a/src/main/java/net/minestom/server/chat/ColoredText.java b/src/main/java/net/minestom/server/chat/ColoredText.java index 7523efe9a..132c70649 100644 --- a/src/main/java/net/minestom/server/chat/ColoredText.java +++ b/src/main/java/net/minestom/server/chat/ColoredText.java @@ -23,66 +23,73 @@ public class ColoredText extends JsonMessage { refreshUpdate(); } + /** + * Create a {@link ColoredText} + * + * @param color the text color + * @param message the text message + * @return the created {@link ColoredText} + */ public static ColoredText of(ChatColor color, String message) { return new ColoredText(color + message); } + /** + * Create a {@link ColoredText} + * + * @param message the text message + * @return the created {@link ColoredText} + */ public static ColoredText of(String message) { return of(ChatColor.WHITE, message); } - public static ColoredText ofFormat(String message) { - return new ColoredText(message); - } - + /** + * Create a {@link ColoredText} with a legacy text + * + * @param message the text message + * @param colorChar the char used before the color code + * @return the created {@link ColoredText} + */ public static ColoredText ofLegacy(String message, char colorChar) { String legacy = toLegacy(message, colorChar); - return ofFormat(legacy); + return of(legacy); } + /** + * Append the text + * + * @param color the text color + * @param message the text message + * @return this {@link ColoredText} + */ public ColoredText append(ChatColor color, String message) { this.message += color + message; refreshUpdate(); return this; } + /** + * Append the text + * + * @param message the text message + * @return this {@link ColoredText} + */ public ColoredText append(String message) { return append(ChatColor.NO_COLOR, message); } - public ColoredText appendFormat(String message) { - this.message += message; - refreshUpdate(); - return this; - } - - private static String toLegacy(String message, char colorChar) { - StringBuilder result = new StringBuilder(); - - for (int i = 0; i < message.length(); i++) { - final char c = message.charAt(i); - if (c == colorChar) { - final char nextChar = message.charAt(i + 1); - final ChatColor color = ChatColor.fromLegacyColorCodes(nextChar); - if (color != ChatColor.NO_COLOR) { - final String replacement = color.toString(); - result.append(replacement); - i++; // Increment to ignore the color code - } else { - result.append(c); - } - } else { - result.append(c); - } - } - - return result.toString(); - } - + /** + * Add legacy text + * + * @param message the legacy text + * @param colorChar the char used before the color code + * @return this {@link ColoredText} + */ public ColoredText appendLegacy(String message, char colorChar) { - String legacy = toLegacy(message, colorChar); - return appendFormat(legacy); + final String legacy = toLegacy(message, colorChar); + return of(legacy); } /** @@ -287,6 +294,41 @@ public class ColoredText extends JsonMessage { return value ? "true" : "false"; } + /** + * Convert a legacy text to our format which can be used by {@link #of(String)} etc... + *

+ * eg: "&fHey" -> "{#white}Hey" + * + * @param message the legacy text + * @param colorChar the char used before the color code + * @return the converted legacy text + */ + private static String toLegacy(String message, char colorChar) { + StringBuilder result = new StringBuilder(); + + for (int i = 0; i < message.length(); i++) { + final char c = message.charAt(i); + if (c == colorChar) { + final char nextChar = message.charAt(i + 1); + final ChatColor color = ChatColor.fromLegacyColorCodes(nextChar); + if (color != ChatColor.NO_COLOR) { + final String replacement = color.toString(); + result.append(replacement); + i++; // Increment to ignore the color code + } else { + result.append(c); + } + } else { + result.append(c); + } + } + + return result.toString(); + } + + /** + * Represents an element which can change based on the client which receive the text + */ private enum MessageType { RAW, KEYBIND, TRANSLATABLE } diff --git a/src/main/java/net/minestom/server/chat/RichMessage.java b/src/main/java/net/minestom/server/chat/RichMessage.java index 82e2d0337..58ee31435 100644 --- a/src/main/java/net/minestom/server/chat/RichMessage.java +++ b/src/main/java/net/minestom/server/chat/RichMessage.java @@ -7,6 +7,8 @@ import net.minestom.server.utils.validate.Check; import java.util.ArrayList; import java.util.List; +// TODO format retention + /** * Represent multiple {@link ColoredText} batched together with the possibility to add * click and hover events diff --git a/src/main/java/net/minestom/server/entity/damage/DamageType.java b/src/main/java/net/minestom/server/entity/damage/DamageType.java index 4c976db0f..f767473e6 100644 --- a/src/main/java/net/minestom/server/entity/damage/DamageType.java +++ b/src/main/java/net/minestom/server/entity/damage/DamageType.java @@ -38,7 +38,7 @@ public class DamageType implements DataContainer { } public RichMessage buildChatMessage(Player killed) { - RichMessage richMessage = RichMessage.of(ColoredText.ofFormat("{@death." + identifier + "," + killed.getUsername() + "}")); + RichMessage richMessage = RichMessage.of(ColoredText.of("{@death." + identifier + "," + killed.getUsername() + "}")); return richMessage; } @@ -51,7 +51,7 @@ public class DamageType implements DataContainer { } public ColoredText buildDeathScreenMessage(Player killed) { - return ColoredText.ofFormat("{@death." + identifier + "}"); + return ColoredText.of("{@death." + identifier + "}"); } /** diff --git a/src/main/java/net/minestom/server/event/handler/EventHandler.java b/src/main/java/net/minestom/server/event/handler/EventHandler.java index 88ce0f2bd..bda00b767 100644 --- a/src/main/java/net/minestom/server/event/handler/EventHandler.java +++ b/src/main/java/net/minestom/server/event/handler/EventHandler.java @@ -65,14 +65,14 @@ public interface EventHandler { /** * Get a {@link Stream} containing all the {@link EventCallback}, no matter to which {@link Event} they are linked * - * @return a {@link Stream} containing all the added callbacks + * @return a {@link Stream} containing all the callbacks */ default Stream getEventCallbacks() { return getEventCallbacksMap().values().stream().flatMap(Collection::stream); } /** - * Call the specified {@link Event} + * Call the specified {@link Event} with all the assigned {@link EventCallback} * * @param eventClass the event class * @param event the event object @@ -87,20 +87,20 @@ public interface EventHandler { } /** - * Call a {@link CancellableEvent} and execute {@code runnable} if the event is not cancelled + * Call a {@link CancellableEvent} and execute {@code successCallback} if the event is not cancelled *

* Does call {@link #callEvent(Class, Event)} internally * - * @param eventClass the event class - * @param event the event object - * @param runnable the callback called when the event is not cancelled - * @param the event type + * @param eventClass the event class + * @param event the event object + * @param successCallback the callback called when the event is not cancelled + * @param the event type * @see #callEvent(Class, Event) */ - default void callCancellableEvent(Class eventClass, E event, Runnable runnable) { + default void callCancellableEvent(Class eventClass, E event, Runnable successCallback) { callEvent(eventClass, event); if (!event.isCancelled()) { - runnable.run(); + successCallback.run(); } }