mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-18 22:21:22 +01:00
ColoredText comments + cleanup
This commit is contained in:
parent
a757f4b97b
commit
dd3345ff9a
@ -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...
|
||||
* <p>
|
||||
* 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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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 + "}");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<EventCallback> 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
|
||||
* <p>
|
||||
* 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 <E> 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 <E> the event type
|
||||
* @see #callEvent(Class, Event)
|
||||
*/
|
||||
default <E extends CancellableEvent> void callCancellableEvent(Class<E> eventClass, E event, Runnable runnable) {
|
||||
default <E extends CancellableEvent> void callCancellableEvent(Class<E> eventClass, E event, Runnable successCallback) {
|
||||
callEvent(eventClass, event);
|
||||
if (!event.isCancelled()) {
|
||||
runnable.run();
|
||||
successCallback.run();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user