Skip to content

Commit

Permalink
Parse message when added to ChatHub rather than at each frame
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinrobot committed Aug 12, 2023
1 parent e1410df commit f3266c7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public void onInitializeClient() {
final ServiceLoader<IEmoteProvider> serviceLoader = ServiceLoader.load(IEmoteProvider.class);
final List<IEmoteProvider> providers = ListHelper.sort(serviceLoader);

int codePoint = 100;

for (final IEmoteProvider provider : providers) {
final String providerName = provider.getClass().getName();

Expand All @@ -43,7 +41,7 @@ public void onInitializeClient() {
provider.registerEmotes(config, emotes::add);

for (final Emote emote : emotes) {
EMOTES_MANAGER.addEmote(codePoint++, emote);
EMOTES_MANAGER.addEmote(emote);
}

MinecraftEmoteMod.LOGGER.info("Registered " + emotes.size() + " emotes from provider " + providerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.ChatHud;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Text;
import net.vinrobot.mcemote.client.MinecraftEmoteModClient;
import net.vinrobot.mcemote.client.text.EmoteParser;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(ChatHud.class)
@Environment(EnvType.CLIENT)
public class ChatHudMixin {
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/DrawContext;drawTextWithShadow(Lnet/minecraft/client/font/TextRenderer;Lnet/minecraft/text/OrderedText;III)I"))
private int drawTextWithShadow(DrawContext instance, TextRenderer textRenderer, OrderedText orderedText, int x, int y, int color) {
orderedText = EmoteParser.wrapOrderedText(orderedText, MinecraftEmoteModClient.EMOTES_MANAGER);
return instance.drawTextWithShadow(textRenderer, orderedText, x, y, color);
@ModifyVariable(at = @At("HEAD"), method = "addMessage(Lnet/minecraft/text/Text;Lnet/minecraft/network/message/MessageSignatureData;ILnet/minecraft/client/gui/hud/MessageIndicator;Z)V", ordinal = 0)
private Text addMessage(Text message) {
return EmoteParser.wrapText(message, MinecraftEmoteModClient.EMOTES_MANAGER);
}
}
14 changes: 14 additions & 0 deletions src/client/java/net/vinrobot/mcemote/client/text/EmoteParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.text.CharacterVisitor;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -37,6 +38,19 @@ public static OrderedText wrapOrderedText(OrderedText orderedText, EmotesManager
};
}

public static Text wrapText(Text text, EmotesManager emotesManager) {
final OrderedText orderedText = wrapOrderedText(text.asOrderedText(), emotesManager);

final MutableText mutableText = Text.empty();
orderedText.accept((index, style, codePoint) -> {
final String character = String.valueOf(Character.toChars(codePoint));
mutableText.append(Text.literal(character).setStyle(style));
return true;
});

return mutableText;
}

@Override
public boolean accept(int index, Style style, int codePoint) {
if (Character.isWhitespace(codePoint)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public class EmotesManager {
private final Map<String, EmotePair> emoteByNames = new HashMap<>();
private final Map<Integer, EmotePair> emoteByCodePoints = new HashMap<>();

public EmotePair addEmote(int codePoint, Emote emote) {
private int nextCodePoint = 1;

public EmotePair addEmote(Emote emote) {
final int codePoint = this.nextCodePoint++;
final String name = emote.getName();
final EmotePair emotePair = new EmotePair(codePoint, emote);

Expand All @@ -39,6 +42,13 @@ public Optional<EmotePair> removeEmote(int codePoint) {
.map(this.emoteByNames::remove);
}

public void clearEmotes() {
// Don't reset nextCodePoint, so that new emotes will have
// unique code points during the entire life of this object.
this.emoteByNames.clear();
this.emoteByCodePoints.clear();
}

public Optional<EmotePair> getByName(String name) {
return Optional.ofNullable(this.emoteByNames.get(name));
}
Expand Down

0 comments on commit f3266c7

Please sign in to comment.