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();
|
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) {
|
public static ColoredText of(ChatColor color, String message) {
|
||||||
return new ColoredText(color + 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) {
|
public static ColoredText of(String message) {
|
||||||
return of(ChatColor.WHITE, 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) {
|
public static ColoredText ofLegacy(String message, char colorChar) {
|
||||||
String legacy = toLegacy(message, 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) {
|
public ColoredText append(ChatColor color, String message) {
|
||||||
this.message += color + message;
|
this.message += color + message;
|
||||||
refreshUpdate();
|
refreshUpdate();
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Append the text
|
||||||
|
*
|
||||||
|
* @param message the text message
|
||||||
|
* @return this {@link ColoredText}
|
||||||
|
*/
|
||||||
public ColoredText append(String message) {
|
public ColoredText append(String message) {
|
||||||
return append(ChatColor.NO_COLOR, message);
|
return append(ChatColor.NO_COLOR, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColoredText appendFormat(String message) {
|
/**
|
||||||
this.message += message;
|
* Add legacy text
|
||||||
refreshUpdate();
|
*
|
||||||
return this;
|
* @param message the legacy text
|
||||||
}
|
* @param colorChar the char used before the color code
|
||||||
|
* @return this {@link ColoredText}
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ColoredText appendLegacy(String message, char colorChar) {
|
public ColoredText appendLegacy(String message, char colorChar) {
|
||||||
String legacy = toLegacy(message, colorChar);
|
final String legacy = toLegacy(message, colorChar);
|
||||||
return appendFormat(legacy);
|
return of(legacy);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -287,6 +294,41 @@ public class ColoredText extends JsonMessage {
|
|||||||
return value ? "true" : "false";
|
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 {
|
private enum MessageType {
|
||||||
RAW, KEYBIND, TRANSLATABLE
|
RAW, KEYBIND, TRANSLATABLE
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ import net.minestom.server.utils.validate.Check;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
// TODO format retention
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represent multiple {@link ColoredText} batched together with the possibility to add
|
* Represent multiple {@link ColoredText} batched together with the possibility to add
|
||||||
* click and hover events
|
* click and hover events
|
||||||
|
@ -38,7 +38,7 @@ public class DamageType implements DataContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public RichMessage buildChatMessage(Player killed) {
|
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;
|
return richMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ public class DamageType implements DataContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ColoredText buildDeathScreenMessage(Player killed) {
|
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
|
* 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() {
|
default Stream<EventCallback> getEventCallbacks() {
|
||||||
return getEventCallbacksMap().values().stream().flatMap(Collection::stream);
|
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 eventClass the event class
|
||||||
* @param event the event object
|
* @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>
|
* <p>
|
||||||
* Does call {@link #callEvent(Class, Event)} internally
|
* Does call {@link #callEvent(Class, Event)} internally
|
||||||
*
|
*
|
||||||
* @param eventClass the event class
|
* @param eventClass the event class
|
||||||
* @param event the event object
|
* @param event the event object
|
||||||
* @param runnable the callback called when the event is not cancelled
|
* @param successCallback the callback called when the event is not cancelled
|
||||||
* @param <E> the event type
|
* @param <E> the event type
|
||||||
* @see #callEvent(Class, Event)
|
* @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);
|
callEvent(eventClass, event);
|
||||||
if (!event.isCancelled()) {
|
if (!event.isCancelled()) {
|
||||||
runnable.run();
|
successCallback.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user