use of org.spongepowered.api.text.format.TextStyle in project LanternServer by LanternPowered.
the class JsonTextBaseSerializer method serialize.
public static void serialize(JsonObject json, Text text, JsonSerializationContext context, List<Text> children) {
final TextColor color = text.getColor();
if (color != TextColors.NONE) {
json.addProperty(COLOR, color.getId());
}
final TextStyle style = text.getStyle();
style.isBold().ifPresent(v -> json.addProperty(BOLD, v));
style.isItalic().ifPresent(v -> json.addProperty(ITALIC, v));
style.hasUnderline().ifPresent(v -> json.addProperty(UNDERLINE, v));
style.hasStrikethrough().ifPresent(v -> json.addProperty(STRIKETHROUGH, v));
style.isObfuscated().ifPresent(v -> json.addProperty(OBFUSCATED, v));
if (!children.isEmpty()) {
json.add(CHILDREN, context.serialize(children.toArray(new Text[children.size()]), Text[].class));
}
text.getClickAction().ifPresent(clickAction -> {
final RawAction raw = LanternTextHelper.raw(clickAction);
final JsonObject jsonEvent = new JsonObject();
jsonEvent.addProperty(EVENT_ACTION, raw.getAction());
jsonEvent.addProperty(EVENT_VALUE, raw.getValueAsString());
json.add(CLICK_EVENT, jsonEvent);
});
text.getHoverAction().ifPresent(clickAction -> {
final RawAction raw = LanternTextHelper.raw(clickAction);
final JsonObject jsonEvent = new JsonObject();
jsonEvent.addProperty(EVENT_ACTION, raw.getAction());
jsonEvent.addProperty(EVENT_VALUE, raw.getValueAsString());
json.add(HOVER_EVENT, jsonEvent);
});
text.getShiftClickAction().ifPresent(shiftClickAction -> {
if (shiftClickAction instanceof ShiftClickAction.InsertText) {
json.addProperty(INSERTION, ((ShiftClickAction.InsertText) shiftClickAction).getResult());
}
});
}
use of org.spongepowered.api.text.format.TextStyle in project LanternServer by LanternPowered.
the class JsonTextBaseSerializer method deserialize.
public static void deserialize(JsonObject json, Text.Builder builder, JsonDeserializationContext context, @Nullable JsonArray children) throws JsonParseException {
JsonElement element;
if ((element = json.get(COLOR)) != null) {
Sponge.getRegistry().getType(TextColor.class, element.getAsString()).ifPresent(builder::color);
}
TextStyle style = builder.getStyle();
if ((element = json.get(BOLD)) != null) {
style = style.bold(element.getAsBoolean());
}
if ((element = json.get(ITALIC)) != null) {
style = style.italic(element.getAsBoolean());
}
if ((element = json.get(UNDERLINE)) != null) {
style = style.underline(element.getAsBoolean());
}
if ((element = json.get(STRIKETHROUGH)) != null) {
style = style.strikethrough(element.getAsBoolean());
}
if ((element = json.get(OBFUSCATED)) != null) {
style = style.obfuscated(element.getAsBoolean());
}
builder.style(style);
if (children != null) {
builder.append((Text[]) context.deserialize(children, Text[].class));
}
if ((element = json.get(CLICK_EVENT)) != null) {
final JsonObject jsonClickEvent = element.getAsJsonObject();
if (jsonClickEvent != null) {
final JsonPrimitive jsonEventAction = jsonClickEvent.getAsJsonPrimitive(EVENT_ACTION);
final JsonPrimitive jsonEventValue = jsonClickEvent.getAsJsonPrimitive(EVENT_VALUE);
if (jsonEventAction != null && jsonEventValue != null) {
final String action = jsonEventAction.getAsString();
final String value = jsonEventValue.getAsString();
final ClickAction<?> clickAction = LanternTextHelper.parseClickAction(action, value);
if (clickAction != null) {
builder.onClick(clickAction);
}
}
}
}
if ((element = json.get(HOVER_EVENT)) != null) {
final JsonObject jsonHoverEvent = element.getAsJsonObject();
if (jsonHoverEvent != null) {
final JsonPrimitive jsonEventAction = jsonHoverEvent.getAsJsonPrimitive(EVENT_ACTION);
final JsonPrimitive jsonEventValue = jsonHoverEvent.getAsJsonPrimitive(EVENT_VALUE);
if (jsonEventAction != null && jsonEventValue != null) {
final String action = jsonEventAction.getAsString();
final String value = jsonEventValue.getAsString();
final HoverAction<?> hoverAction = LanternTextHelper.parseHoverAction(action, value);
if (hoverAction != null) {
builder.onHover(hoverAction);
}
}
}
}
if ((element = json.get(INSERTION)) != null) {
builder.onShiftClick(TextActions.insertText(element.getAsString()));
}
}
use of org.spongepowered.api.text.format.TextStyle in project SpongeAPI by SpongePowered.
the class TextFormatConfigSerializer method deserialize.
@Override
public TextFormat deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException {
TextColor color = TextColors.NONE;
GameRegistry registry = Sponge.getRegistry();
String colorId = value.getNode(NODE_COLOR).getString();
if (colorId != null) {
color = registry.getType(TextColor.class, colorId).orElseThrow(() -> new ObjectMappingException("Color not found: " + colorId));
}
TextStyle style = TextStyles.NONE;
ConfigurationNode styleNode = value.getNode(NODE_STYLE);
for (TextStyle.Base component : registry.getAllOf(TextStyle.Base.class)) {
if (styleNode.getNode(component.getId().toLowerCase()).getBoolean()) {
style = style.and(component);
}
}
return TextFormat.NONE.color(color).style(style);
}
use of org.spongepowered.api.text.format.TextStyle in project SpongeAPI by SpongePowered.
the class TextFormatConfigSerializer method serialize.
@Override
public void serialize(TypeToken<?> type, TextFormat obj, ConfigurationNode value) throws ObjectMappingException {
value.getNode(NODE_COLOR).setValue(obj.getColor().getId());
ConfigurationNode styleNode = value.getNode(NODE_STYLE);
TextStyle composite = obj.getStyle();
Sponge.getRegistry().getAllOf(TextStyle.Base.class).forEach(style -> styleNode.getNode(style.getId().toLowerCase()).setValue(composite.contains(style)));
}
use of org.spongepowered.api.text.format.TextStyle in project Nucleus by NucleusPowered.
the class TextParsingUtils method addUrls.
public static Text addUrls(String message) {
if (message == null || message.isEmpty()) {
return Text.EMPTY;
}
Matcher m = urlParser.matcher(message);
if (!m.find()) {
return TextSerializers.FORMATTING_CODE.deserialize(message);
}
List<Text> texts = Lists.newArrayList();
String remaining = message;
TextParsingUtils.StyleTuple st = TextParsingUtils.EMPTY;
do {
// We found a URL. We split on the URL that we have.
String[] textArray = remaining.split(urlParser.pattern(), 2);
Text first = Text.builder().color(st.colour).style(st.style).append(TextSerializers.FORMATTING_CODE.deserialize(textArray[0])).build();
// Add this text to the list regardless.
texts.add(first);
// If we have more to do, shove it into the "remaining" variable.
if (textArray.length == 2) {
remaining = textArray[1];
} else {
remaining = null;
}
// Get the last colour & styles
String colourMatch = m.group("colour");
if (colourMatch != null && !colourMatch.isEmpty()) {
// If there is a reset, explicitly do it.
TextStyle reset = TextStyles.NONE;
if (m.group("reset") != null) {
reset = TextStyles.RESET;
}
first = Text.of(reset, TextSerializers.FORMATTING_CODE.deserialize(m.group("colour") + " "));
}
st = TextParsingUtils.getLastColourAndStyle(first, st);
// Build the URL
String whiteSpace = m.group("first");
String url = m.group("url");
texts.add(TextParsingUtils.getTextForUrl(url, url, whiteSpace, st, m.group("options")));
} while (remaining != null && m.find());
// Add the last bit.
if (remaining != null) {
texts.add(Text.builder().color(st.colour).style(st.style).append(TextSerializers.FORMATTING_CODE.deserialize(remaining)).build());
}
// Join it all together.
return Text.join(texts);
}
Aggregations