Skip to content

Commit cb073ad

Browse files
committed
Show nicknames when hovering names in chat
1 parent a2ab83d commit cb073ad

File tree

14 files changed

+109
-25
lines changed

14 files changed

+109
-25
lines changed

gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ yarn_mappings=1.21.4+build.8
99
loader_version=0.16.10
1010

1111
# Mod Properties
12-
mod_version=1.1.0
12+
mod_version=1.1.1
1313
maven_group=com.scnamelink
1414
archives_base_name=sc-name-link
1515

1616
# Dependencies
17-
fabric_version=0.114.2+1.21.4
17+
fabric_version=0.115.1+1.21.4
1818
modmenu_version=11.0.2
1919
clothconfig_version=16.0.141

src/client/java/golden/scnamelink/DisplayMapping.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
public class DisplayMapping {
66
final String mc_name;
77
final UUID mc_uuid;
8-
final String discord_nick;
9-
final String colour;
8+
public final String discord_nick;
9+
public final String colour;
1010

1111
public DisplayMapping(String mc_name, UUID mc_uuid, String discord_nick,
1212
String colour) {

src/client/java/golden/scnamelink/SpooncraftNameLinkClient.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ public class SpooncraftNameLinkClient implements ClientModInitializer {
3131
// List of mappings for replacement and optional color changes
3232
private static List<DisplayMapping> mappings = new ArrayList<>();
3333

34+
/**
35+
* Retrieves a mapping matching either the UUID or the name of the Minecraft player.
36+
*
37+
* @param uuid The {@code UUID} of the Minecraft player
38+
* @param name The in-game name of the Minecraft player
39+
* @return The {@code DisplayMapping} object if found, otherwise null
40+
*/
41+
public static DisplayMapping getMapping(UUID uuid, String name) {
42+
// Iterate over the mappings to find the correct match based on UUID or Minecraft name
43+
for (DisplayMapping mapping : mappings) {
44+
if (Objects.equals(mapping.mc_uuid, uuid) || Objects.equals(mapping.mc_name, name)) {
45+
return mapping;
46+
}
47+
}
48+
return null;
49+
}
50+
3451
/**
3552
* Applies the mapping to a given message. It optionally replaces the Minecraft name with
3653
* the Discord nickname and applies the colour styling.
@@ -43,7 +60,7 @@ public class SpooncraftNameLinkClient implements ClientModInitializer {
4360
* @return A new MutableText object with the mapping applied (replacements and color changes)
4461
*/
4562
static MutableText applyMapping(Text message, DisplayMapping mapping,
46-
boolean replaceName, boolean replaceColour) {
63+
boolean replaceName, boolean replaceColour) {
4764
if (message == null || message.getString().isEmpty() || mapping == null) {
4865
return Text.empty();
4966
}
@@ -71,7 +88,7 @@ static MutableText applyMapping(Text message, DisplayMapping mapping,
7188
outputMessage.append(newText);
7289

7390
return Optional.empty(); // Continue visiting
74-
}, Style.EMPTY);
91+
}, Style.EMPTY);
7592

7693
return outputMessage;
7794
}
@@ -91,12 +108,9 @@ static MutableText applyMapping(Text message, DisplayMapping mapping,
91108
*/
92109
public static Text getStyledName(Text displayName, UUID uuid, String name, boolean replaceName,
93110
boolean replaceColour) {
94-
// Iterate over the mappings to find the correct match based on UUID or Minecraft name
95-
for (DisplayMapping mapping : mappings) {
96-
// If the UUID matches or the name matches, apply the mapping and return it
97-
if (Objects.equals(mapping.mc_uuid, uuid) || Objects.equals(mapping.mc_name, name)) {
98-
return applyMapping(displayName, mapping, replaceName, replaceColour);
99-
}
111+
DisplayMapping mapping = getMapping(uuid, name);
112+
if (mapping != null) {
113+
return applyMapping(displayName, mapping, replaceName, replaceColour);
100114
}
101115
return displayName;
102116
}
@@ -113,7 +127,7 @@ public static Text getStyledName(Text displayName, UUID uuid, String name, boole
113127
*/
114128
public static Text getStyledName(Text displayName, String name, boolean replaceName,
115129
boolean replaceColour) {
116-
return getStyledName(displayName, UUID.fromString("00000000-0000-0000-0000-000000000000"), name, replaceName, replaceColour);
130+
return getStyledName(displayName, UUID.randomUUID(), name, replaceName, replaceColour);
117131
}
118132

119133
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package golden.scnamelink.mixin.client;
2+
3+
import golden.scnamelink.DisplayMapping;
4+
import golden.scnamelink.SpooncraftNameLinkClient;
5+
import net.minecraft.entity.EntityType;
6+
import net.minecraft.text.HoverEvent;
7+
import net.minecraft.text.Text;
8+
import org.jetbrains.annotations.Nullable;
9+
import org.spongepowered.asm.mixin.Final;
10+
import org.spongepowered.asm.mixin.Mixin;
11+
import org.spongepowered.asm.mixin.Shadow;
12+
import org.spongepowered.asm.mixin.injection.At;
13+
import org.spongepowered.asm.mixin.injection.Inject;
14+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Objects;
19+
import java.util.Optional;
20+
import java.util.UUID;
21+
22+
@Mixin (HoverEvent.EntityContent.class)
23+
public abstract class HoverEventMixin {
24+
@Final
25+
@Shadow
26+
public UUID uuid;
27+
@Final
28+
@Shadow
29+
public Optional<Text> name;
30+
@Final
31+
@Shadow
32+
public EntityType<?> entityType;
33+
@Shadow
34+
@Nullable
35+
private List<Text> tooltip;
36+
37+
@Inject (method = "asTooltip", at = @At ("HEAD"))
38+
public @Nullable List<Text> asTooltip(CallbackInfoReturnable<List<Text>> cir) {
39+
if (this.tooltip == null) {
40+
this.tooltip = new ArrayList<>();
41+
Optional<Text> var10000 = this.name;
42+
List<Text> var10001 = this.tooltip;
43+
Objects.requireNonNull(var10001);
44+
var10000.ifPresent(var10001::add);
45+
this.tooltip.add(Text.translatable("gui.entity_tooltip.type",
46+
this.entityType.getName()));
47+
if (this.entityType == EntityType.PLAYER && this.name.isPresent()) {
48+
DisplayMapping mapping = SpooncraftNameLinkClient.getMapping(this.uuid,
49+
this.name.get().getString());
50+
if (mapping != null) {
51+
this.tooltip.add(Text.translatable("gui.scnamelink.hover_nickname",
52+
mapping.discord_nick));
53+
}
54+
}
55+
this.tooltip.add(Text.literal(this.uuid.toString()));
56+
}
57+
58+
return this.tooltip;
59+
}
60+
}

src/client/resources/sc-name-link.client.mixins.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
"ChatHudMixin",
77
"ClientPlayNetworkHandlerMixin",
88
"PlayerEntityRendererMixin",
9-
"PlayerListEntryMixin"
9+
"PlayerListEntryMixin",
10+
"HoverEventMixin"
1011
],
1112
"injectors": {
1213
"defaultRequire": 1
13-
}
14+
}
1415
}

src/main/resources/assets/sc-name-link/lang/en_au.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"text.scnamelink.status.success": "Loaded mappings from the server.",
1616
"text.autoconfig.scnamelink.option.enableMod.@Tooltip": "Game should be restarted after enabling.",
1717
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[0]": "Leave blank if unknown.",
18-
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Game should be restarted after changing."
18+
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Game should be restarted after changing.",
19+
"gui.scnamelink.hover_nickname": "Nickname: %s"
1920
}

src/main/resources/assets/sc-name-link/lang/en_ca.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"text.scnamelink.status.success": "Loaded mappings from the server.",
1616
"text.autoconfig.scnamelink.option.enableMod.@Tooltip": "Game should be restarted after enabling.",
1717
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[0]": "Leave blank if unknown.",
18-
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Game should be restarted after changing."
18+
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Game should be restarted after changing.",
19+
"gui.scnamelink.hover_nickname": "Nickname: %s"
1920
}

src/main/resources/assets/sc-name-link/lang/en_gb.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"text.scnamelink.status.success": "Loaded mappings from the server.",
1616
"text.autoconfig.scnamelink.option.enableMod.@Tooltip": "Game should be restarted after enabling.",
1717
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[0]": "Leave blank if unknown.",
18-
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Game should be restarted after changing."
18+
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Game should be restarted after changing.",
19+
"gui.scnamelink.hover_nickname": "Nickname: %s"
1920
}

src/main/resources/assets/sc-name-link/lang/en_nz.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"text.scnamelink.status.success": "Loaded mappings from the server.",
1616
"text.autoconfig.scnamelink.option.enableMod.@Tooltip": "Game should be restarted after enabling.",
1717
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[0]": "Leave blank if unknown.",
18-
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Game should be restarted after changing."
18+
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Game should be restarted after changing.",
19+
"gui.scnamelink.hover_nickname": "Nickname: %s"
1920
}

src/main/resources/assets/sc-name-link/lang/en_pt.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"text.scnamelink.status.success": "Mappings loaded from the server’s hoard.",
1616
"text.autoconfig.scnamelink.option.enableMod.@Tooltip": "Restart the game after hoistin’ this option.",
1717
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[0]": "Leave this map blank if ye don’t know the way.",
18-
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Restart the voyage game after chartin’ a new map."
18+
"text.autoconfig.scnamelink.option.apiLink.@Tooltip[1]": "Restart the voyage game after chartin’ a new map.",
19+
"gui.scnamelink.hover_nickname": "Nickname: %s"
1920
}

0 commit comments

Comments
 (0)