Skip to content

Commit 427d16c

Browse files
committed
more wip
1 parent e369c70 commit 427d16c

File tree

9 files changed

+121
-21
lines changed

9 files changed

+121
-21
lines changed

common/src/main/java/dzwdz/chat_heads/ChatHeads.java

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package dzwdz.chat_heads;
22

33
import com.mojang.blaze3d.platform.NativeImage;
4+
import com.mojang.blaze3d.systems.RenderSystem;
45
import dzwdz.chat_heads.config.ChatHeadsConfig;
56
import dzwdz.chat_heads.config.ChatHeadsConfigDefaults;
67
import dzwdz.chat_heads.config.RenderPosition;
8+
import dzwdz.chat_heads.mixin.AbstractClientPlayerInvoker;
79
import dzwdz.chat_heads.mixininterface.HeadRenderable;
810
import dzwdz.chat_heads.mixininterface.Ownable;
911
import net.minecraft.client.GuiMessage;
1012
import net.minecraft.client.Minecraft;
1113
import net.minecraft.client.gui.GuiGraphics;
14+
import net.minecraft.client.multiplayer.ClientLevel;
1215
import net.minecraft.client.multiplayer.ClientPacketListener;
1316
import net.minecraft.client.multiplayer.PlayerInfo;
1417
import net.minecraft.network.chat.*;
@@ -19,6 +22,7 @@
1922
import org.jetbrains.annotations.Nullable;
2023

2124
import java.util.*;
25+
import java.util.stream.Collectors;
2226

2327
import static dzwdz.chat_heads.config.SenderDetection.HEURISTIC_ONLY;
2428
import static dzwdz.chat_heads.config.SenderDetection.UUID_ONLY;
@@ -80,7 +84,9 @@ public class ChatHeads {
8084

8185
public static final Set<ResourceLocation> blendedHeadTextures = new HashSet<>();
8286

87+
// for rendering inside text:
8388
@NotNull public static HeadData renderHeadData = HeadData.EMPTY;
89+
public static float renderHeadOpacity;
8490
public static GuiGraphics guiGraphics = null;
8591
public static int charsRendered;
8692

@@ -105,23 +111,23 @@ public static void handleAddedMessage(Component message, @Nullable ChatType.Boun
105111

106112
HeadData headData = detectPlayer(message, bound, playerInfo);
107113

108-
// heuristic may have already got us the head position
109-
if (headData.hasHeadPosition()) {
114+
if (headData == HeadData.EMPTY || headData.hasHeadPosition()) { // heuristic may have already got us the head position
110115
ChatHeads.lastSenderData = headData;
111116
return;
112117
}
113118

114119
ClientPacketListener connection = Minecraft.getInstance().getConnection();
115120
if (connection != null) {
116-
ChatHeads.LOGGER.warn("scanning");
121+
ChatHeads.LOGGER.warn("scanning for {}", headData.playerInfo().getProfile().getName());
117122
// scan for names of this specific player
118123
PlayerInfoCache playerInfoCache = new PlayerInfoCache(connection);
119124
playerInfoCache.add(headData.playerInfo());
120125
HeadData foundHeadData = scanForPlayerName(message.getString(), playerInfoCache);
121-
ChatHeads.LOGGER.warn("got {}, {}", foundHeadData.playerInfo().getProfile().getName(), foundHeadData.codePointIndex());
126+
127+
ChatHeads.LOGGER.warn("got {}, {}", foundHeadData.playerInfo() == null ? null : foundHeadData.playerInfo().getProfile().getName(), foundHeadData.codePointIndex());
122128

123129
// possible weirdness
124-
if (!foundHeadData.hasHead()) {
130+
if (foundHeadData == HeadData.EMPTY) {
125131
ChatHeads.LOGGER.warn("couldn't find player inside chat message. profile name: {}", headData.playerInfo().getProfile().getName());
126132
} else if (!foundHeadData.playerInfo().equals(headData.playerInfo())) {
127133
ChatHeads.LOGGER.warn("found a different player than which was searched for. started with: {}, found: {}", headData.playerInfo().getProfile().getName(), foundHeadData.playerInfo().getProfile().getName());
@@ -189,7 +195,7 @@ public static int getChatOffset(@NotNull HeadData headData) {
189195
if (ChatHeads.CONFIG.renderPosition() != RenderPosition.BEFORE_LINE)
190196
return 0;
191197

192-
if (headData.hasHead() || (ChatHeads.CONFIG.offsetNonPlayerText() && !ChatHeads.serverDisabledChatHeads)) {
198+
if (headData.playerInfo() != null || (ChatHeads.CONFIG.offsetNonPlayerText() && !ChatHeads.serverDisabledChatHeads)) {
193199
return 8 + 2;
194200
} else {
195201
return 0;
@@ -300,6 +306,7 @@ private static HeadData scanForPlayerName(@NotNull String message, PlayerInfoCac
300306
return HeadData.EMPTY;
301307
}
302308

309+
@SuppressWarnings("ConstantValue")
303310
static class PlayerInfoCache {
304311
private final ClientPacketListener connection;
305312
private final Map<String, PlayerInfo> playerInfos = new HashMap<>();
@@ -339,6 +346,8 @@ public void collectAllNames() {
339346
addDisplayName(playerInfo);
340347
}
341348

349+
addEntityNames();
350+
342351
// add name aliases, copying player info from profile/display names
343352
addNameAliases();
344353
}
@@ -362,10 +371,44 @@ private void addDisplayName(PlayerInfo playerInfo) {
362371
}
363372
}
364373

374+
private void addEntityName(PlayerInfo playerInfo) {
375+
ClientLevel level = connection.getLevel();
376+
if (level == null)
377+
return;
378+
379+
for (var player : level.players()) {
380+
if (player.getUUID().equals(playerInfo.getProfile().getId())) {
381+
String name = player.getName().getString().replaceAll(FORMAT_REGEX, "");
382+
LOGGER.warn("entity {}", name);
383+
384+
playerInfos.putIfAbsent(name, playerInfo);
385+
}
386+
}
387+
}
388+
389+
private void addEntityNames() {
390+
ClientLevel level = connection.getLevel();
391+
if (level == null)
392+
return;
393+
394+
for (var player : level.players()) {
395+
String name = player.getName().getString().replaceAll(FORMAT_REGEX, "");
396+
PlayerInfo playerInfo = ((AbstractClientPlayerInvoker) player).chatheads$playerInfo();
397+
398+
playerInfos.putIfAbsent(name, playerInfo);
399+
}
400+
}
401+
365402
public void add(PlayerInfo playerInfo) {
366403
addProfileName(playerInfo);
367404
addDisplayName(playerInfo);
405+
addEntityName(playerInfo);
368406
addNameAliases();
407+
408+
LOGGER.warn("playerInfos = {}", playerInfos.entrySet()
409+
.stream()
410+
.collect(Collectors.toMap(Map.Entry::getKey,
411+
e -> e.getValue().getProfile().getName())));
369412
}
370413

371414
public Map<Integer, List<String>> createNamesByFirstCharacterMap() {
@@ -433,9 +476,14 @@ public static ResourceLocation getBlendedHeadLocation(ResourceLocation skinLocat
433476
return ResourceLocation.fromNamespaceAndPath(ChatHeads.MOD_ID, skinLocation.getPath());
434477
}
435478

436-
public static void renderChatHead(GuiGraphics guiGraphics, int x, int y, PlayerInfo owner) {
479+
public static void renderChatHead(GuiGraphics guiGraphics, int x, int y, PlayerInfo owner, float opacity) {
437480
ResourceLocation skinLocation = owner.getSkin().texture();
438481

482+
if (opacity != 1.0f) {
483+
RenderSystem.enableBlend();
484+
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, opacity);
485+
}
486+
439487
if (blendedHeadTextures.contains(skinLocation)) {
440488
// draw head in one draw call, fixing transparency issues of the "vanilla" path below
441489
guiGraphics.blit(getBlendedHeadLocation(skinLocation), x, y, 8, 8, 0, 0, 8, 8, 8, 8);
@@ -445,5 +493,10 @@ public static void renderChatHead(GuiGraphics guiGraphics, int x, int y, PlayerI
445493
// draw hat
446494
guiGraphics.blit(skinLocation, x, y, 8, 8, 40.0f, 8, 8, 8, 64, 64);
447495
}
496+
497+
if (opacity != 1.0f) {
498+
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
499+
RenderSystem.disableBlend();
500+
}
448501
}
449502
}

common/src/main/java/dzwdz/chat_heads/HeadData.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.minecraft.client.multiplayer.PlayerInfo;
44

5+
// playerInfo gives us the texture, codePointIndex the position inside the message
56
public record HeadData(PlayerInfo playerInfo, int codePointIndex) {
67
public static HeadData EMPTY = new HeadData(null, -1);
78

@@ -15,10 +16,6 @@ public static HeadData of(PlayerInfo playerInfo) {
1516
return new HeadData(playerInfo, -1);
1617
}
1718

18-
public boolean hasHead() {
19-
return playerInfo != null;
20-
}
21-
2219
public boolean hasHeadPosition() {
2320
return codePointIndex != -1;
2421
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dzwdz.chat_heads.mixin;
2+
3+
import net.minecraft.client.multiplayer.PlayerInfo;
4+
import net.minecraft.client.player.AbstractClientPlayer;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.gen.Invoker;
7+
8+
@Mixin(AbstractClientPlayer.class)
9+
public interface AbstractClientPlayerInvoker {
10+
@Invoker("getPlayerInfo")
11+
PlayerInfo chatheads$playerInfo();
12+
}

common/src/main/java/dzwdz/chat_heads/mixin/ChatComponentMixin.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.llamalad7.mixinextras.sugar.ref.LocalFloatRef;
66
import com.llamalad7.mixinextras.sugar.ref.LocalIntRef;
77
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
8-
import com.mojang.blaze3d.systems.RenderSystem;
98
import dzwdz.chat_heads.ChatHeads;
109
import dzwdz.chat_heads.HeadData;
1110
import dzwdz.chat_heads.config.RenderPosition;
@@ -73,17 +72,14 @@ public abstract class ChatComponentMixin {
7372
public void chatheads$renderChatHead(GuiGraphics guiGraphics, int tickCount, int mouseX, int mouseY, boolean focused, CallbackInfo ci,
7473
@Share("guiMessage") LocalRef<GuiMessage.Line> guiMessage, @Share("y") LocalIntRef yRef, @Share("opacity") LocalFloatRef opacityRef) {
7574
HeadData headData = ChatHeads.getHeadData(guiMessage.get());
76-
if (!headData.hasHead())
75+
if (headData.playerInfo() == null)
7776
return;
7877

7978
if (ChatHeads.CONFIG.renderPosition() == RenderPosition.BEFORE_LINE) {
80-
RenderSystem.enableBlend();
81-
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, opacityRef.get());
82-
ChatHeads.renderChatHead(guiGraphics, 0, yRef.get(), headData.playerInfo());
83-
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
84-
RenderSystem.disableBlend();
79+
ChatHeads.renderChatHead(guiGraphics, 0, yRef.get(), headData.playerInfo(), opacityRef.get());
8580
} else {
8681
ChatHeads.renderHeadData = headData;
82+
ChatHeads.renderHeadOpacity = opacityRef.get();
8783
}
8884
}
8985

common/src/main/java/dzwdz/chat_heads/mixin/CommandSuggestionSuggestionsListMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public abstract class CommandSuggestionSuggestionsListMixin {
9494
int x = rect.getX() - (8 + 2);
9595

9696
if (playerRef.get() != null) {
97-
ChatHeads.renderChatHead(graphicsRef.get(), x, y, playerRef.get());
97+
ChatHeads.renderChatHead(graphicsRef.get(), x, y, playerRef.get(), 1.0f);
9898
}
9999

100100
return y;

common/src/main/java/dzwdz/chat_heads/mixin/FontStringRenderOutputMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public abstract class FontStringRenderOutputMixin {
4343
poseStack.setIdentity();
4444
poseStack.mulPose(pose);
4545

46-
ChatHeads.renderChatHead(ChatHeads.guiGraphics, (int) x + 1, (int) y, ChatHeads.renderHeadData.playerInfo());
46+
ChatHeads.renderChatHead(ChatHeads.guiGraphics, (int) x + 1, (int) y, ChatHeads.renderHeadData.playerInfo(), ChatHeads.renderHeadOpacity);
4747

4848
poseStack.popPose();
4949
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package dzwdz.chat_heads.mixin;
2+
3+
import com.mojang.authlib.GameProfile;
4+
import org.spongepowered.asm.mixin.Final;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.Mutable;
7+
import org.spongepowered.asm.mixin.Shadow;
8+
import org.spongepowered.asm.mixin.injection.At;
9+
import org.spongepowered.asm.mixin.injection.Inject;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
11+
12+
import java.util.UUID;
13+
14+
@Mixin(GameProfile.class)
15+
public abstract class PlayerMixin2 {
16+
@Shadow @Final @Mutable
17+
private String name;
18+
19+
@Inject(method = "<init>", at = @At("RETURN"))
20+
private void illegalGameProfileName(UUID id, String name, CallbackInfo ci) {
21+
// this.name = "\uD800\uDF00\uD800\uDF01\uD800\uDF01\uD800\uDF00";
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dzwdz.chat_heads.mixin;
2+
3+
import net.minecraft.client.multiplayer.PlayerInfo;
4+
import net.minecraft.network.chat.Component;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
9+
10+
@Mixin(PlayerInfo.class)
11+
public abstract class PlayerMixin3 {
12+
@Inject(method = "getTabListDisplayName", at = @At("HEAD"), cancellable = true)
13+
public void f(CallbackInfoReturnable<Component> cir) {
14+
// cir.setReturnValue(Component.literal("\uD800\uDF00\uD800\uDF01\uD800\uDF01\uD800\uDF00"));
15+
}
16+
}

common/src/main/resources/chat_heads.mixins.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"package": "dzwdz.chat_heads.mixin",
55
"compatibilityLevel": "JAVA_${java_version}",
66
"client": [
7+
"AbstractClientPlayerInvoker",
78
"ChatComponentMixin",
89
"ChatComponentMixin2",
910
"ChatListenerMixin",
@@ -23,6 +24,8 @@
2324
"defaultRequire": 1
2425
},
2526
"mixins": [
26-
"PlayerMixin"
27+
"PlayerMixin",
28+
"PlayerMixin2",
29+
"PlayerMixin3"
2730
]
2831
}

0 commit comments

Comments
 (0)