Skip to content

Commit

Permalink
perf: Multiple StyledText optimizations [skip ci] (#2444)
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofbolyai committed May 5, 2024
1 parent 14de02f commit 91e84e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
44 changes: 25 additions & 19 deletions common/src/main/java/com/wynntils/core/text/PartStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@
package com.wynntils.core.text;

import com.wynntils.utils.colors.CustomColor;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.network.chat.TextColor;

public final class PartStyle {
private static final String STYLE_PREFIX = "§";
private static final Int2ObjectMap<ChatFormatting> INTEGER_TO_CHATFORMATTING_MAP = Arrays.stream(
ChatFormatting.values())
.filter(ChatFormatting::isColor)
.collect(
() -> new Int2ObjectOpenHashMap<>(ChatFormatting.values().length),
(map, cf) -> map.put(cf.getColor(), cf),
Int2ObjectMap::putAll);

private final StyledTextPart owner;

Expand Down Expand Up @@ -124,13 +133,10 @@ public String asString(PartStyle previousStyle, StyleType type) {
if (!skipFormatting) {
// 1. Color
if (color != CustomColor.NONE) {
Optional<ChatFormatting> chatFormatting = Arrays.stream(ChatFormatting.values())
.filter(ChatFormatting::isColor)
.filter(c -> c.getColor() == color.asInt())
.findFirst();
ChatFormatting chatFormatting = INTEGER_TO_CHATFORMATTING_MAP.get(color.asInt());

if (chatFormatting.isPresent()) {
styleString.append(STYLE_PREFIX).append(chatFormatting.get().getChar());
if (chatFormatting != null) {
styleString.append(STYLE_PREFIX).append(chatFormatting.getChar());
} else {
styleString.append(STYLE_PREFIX).append(color.toHexString());
}
Expand Down Expand Up @@ -178,18 +184,10 @@ public String asString(PartStyle previousStyle, StyleType type) {
}

public Style getStyle() {
Style reconstructedStyle = Style.EMPTY
.withObfuscated(obfuscated)
.withBold(bold)
.withStrikethrough(strikethrough)
.withUnderlined(underlined)
.withItalic(italic)
.withClickEvent(clickEvent)
.withHoverEvent(hoverEvent);

if (color != CustomColor.NONE) {
reconstructedStyle = reconstructedStyle.withColor(color.asInt());
}
// Optimization: Use raw Style constructor, instead of the builder.
TextColor textColor = color == CustomColor.NONE ? null : TextColor.fromRgb(color.asInt());
Style reconstructedStyle = new Style(
textColor, bold, italic, underlined, strikethrough, obfuscated, clickEvent, hoverEvent, null, null);

return reconstructedStyle;
}
Expand Down Expand Up @@ -225,6 +223,14 @@ public boolean isItalic() {
return italic;
}

public ClickEvent getClickEvent() {
return clickEvent;
}

public HoverEvent getHoverEvent() {
return hoverEvent;
}

public PartStyle withBold(boolean bold) {
return new PartStyle(owner, color, obfuscated, bold, strikethrough, underlined, italic, clickEvent, hoverEvent);
}
Expand Down
13 changes: 7 additions & 6 deletions common/src/main/java/com/wynntils/core/text/StyledText.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ public final class StyledText implements Iterable<StyledTextPart> {

/**
* Note: All callers of this constructor should ensure that the event lists are collected from the parts.
* Additionally, they should ensure that the events are distinct.
*/
private StyledText(List<StyledTextPart> parts, List<ClickEvent> clickEvents, List<HoverEvent> hoverEvents) {
this.parts = parts.stream()
.filter(styledTextPart -> !styledTextPart.isEmpty())
.map(styledTextPart -> new StyledTextPart(styledTextPart, this))
.collect(Collectors.toList());
this.clickEvents = clickEvents.stream().distinct().collect(Collectors.toList());
this.hoverEvents = hoverEvents.stream().distinct().collect(Collectors.toList());
this.clickEvents = Collections.unmodifiableList(clickEvents);
this.hoverEvents = Collections.unmodifiableList(hoverEvents);
}

public static StyledText fromComponent(Component component) {
Expand Down Expand Up @@ -124,13 +125,13 @@ public static StyledText fromParts(List<StyledTextPart> parts) {
List<HoverEvent> hoverEvents = new ArrayList<>();

for (StyledTextPart part : parts) {
ClickEvent clickEvent = part.getPartStyle().getStyle().getClickEvent();
if (clickEvent != null) {
ClickEvent clickEvent = part.getPartStyle().getClickEvent();
if (clickEvent != null && !clickEvents.contains(clickEvent)) {
clickEvents.add(clickEvent);
}

HoverEvent hoverEvent = part.getPartStyle().getStyle().getHoverEvent();
if (hoverEvent != null) {
HoverEvent hoverEvent = part.getPartStyle().getHoverEvent();
if (hoverEvent != null && !hoverEvents.contains(hoverEvent)) {
hoverEvents.add(hoverEvent);
}
}
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/wynntils.accessWidener
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ accessible method net/minecraft/client/renderer/RenderType create (Ljava/lang/St
accessible method net/minecraft/client/renderer/blockentity/BeaconRenderer renderPart (Lcom/mojang/blaze3d/vertex/PoseStack;Lcom/mojang/blaze3d/vertex/VertexConsumer;FFFFIIFFFFFFFFFFFF)V
accessible method net/minecraft/client/renderer/entity/EntityRenderer renderNameTag (Lnet/minecraft/world/entity/Entity;Lnet/minecraft/network/chat/Component;Lcom/mojang/blaze3d/vertex/PoseStack;Lnet/minecraft/client/renderer/MultiBufferSource;I)V
accessible method net/minecraft/client/resources/DownloadedPackSource checkHash (Ljava/lang/String;Ljava/io/File;)Z
accessible method net/minecraft/network/chat/Style <init> (Lnet/minecraft/network/chat/TextColor;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Ljava/lang/Boolean;Lnet/minecraft/network/chat/ClickEvent;Lnet/minecraft/network/chat/HoverEvent;Ljava/lang/String;Lnet/minecraft/resources/ResourceLocation;)V
accessible method net/minecraft/server/packs/FilePackResources <init> (Ljava/lang/String;Lnet/minecraft/server/packs/FilePackResources$SharedZipFileAccess;ZLjava/lang/String;)V
accessible method net/minecraft/server/packs/FilePackResources$SharedZipFileAccess <init> (Ljava/io/File;)V
accessible method net/minecraft/server/packs/repository/Pack <init> (Ljava/lang/String;ZLnet/minecraft/server/packs/repository/Pack$ResourcesSupplier;Lnet/minecraft/network/chat/Component;Lnet/minecraft/server/packs/repository/Pack$Info;Lnet/minecraft/server/packs/repository/Pack$Position;ZLnet/minecraft/server/packs/repository/PackSource;)V
Expand Down

0 comments on commit 91e84e2

Please sign in to comment.