11package dzwdz .chat_heads ;
22
33import com .mojang .blaze3d .platform .NativeImage ;
4+ import com .mojang .blaze3d .systems .RenderSystem ;
45import dzwdz .chat_heads .config .ChatHeadsConfig ;
56import dzwdz .chat_heads .config .ChatHeadsConfigDefaults ;
67import dzwdz .chat_heads .config .RenderPosition ;
8+ import dzwdz .chat_heads .mixin .AbstractClientPlayerInvoker ;
79import dzwdz .chat_heads .mixininterface .HeadRenderable ;
810import dzwdz .chat_heads .mixininterface .Ownable ;
911import net .minecraft .client .GuiMessage ;
1012import net .minecraft .client .Minecraft ;
1113import net .minecraft .client .gui .GuiGraphics ;
14+ import net .minecraft .client .multiplayer .ClientLevel ;
1215import net .minecraft .client .multiplayer .ClientPacketListener ;
1316import net .minecraft .client .multiplayer .PlayerInfo ;
1417import net .minecraft .network .chat .*;
1922import org .jetbrains .annotations .Nullable ;
2023
2124import java .util .*;
25+ import java .util .stream .Collectors ;
2226
2327import static dzwdz .chat_heads .config .SenderDetection .HEURISTIC_ONLY ;
2428import 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}
0 commit comments