Skip to content

Commit ac68b92

Browse files
committed
Use a HashMap instead of a List.
1 parent 1ef0a01 commit ac68b92

7 files changed

Lines changed: 41 additions & 65 deletions

File tree

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ org.gradle.parallel=true
66
# check these on https://fabricmc.net/develop
77
minecraft_version=1.21.8
88
yarn_mappings=1.21.8+build.1
9-
loader_version=0.16.14
9+
loader_version=0.17.2
1010
loom_version=1.11-SNAPSHOT
1111

1212
# Mod Properties
13-
mod_version=1.4.0-a4
13+
mod_version=1.4.0-a-hashmappings0
1414
maven_group=golden.scnicknamer
1515
archives_base_name=scnicknamer
1616

1717
# Dependencies
18-
fabric_version=0.130.0+1.21.8
18+
fabric_version=0.133.4+1.21.8
1919
modmenu_version=15.0.0-beta.3
2020
clothconfig_version=19.0.147

src/client/java/golden/scnicknamer/NameLinkAPI.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
import java.nio.file.Files;
1515
import java.nio.file.Paths;
1616
import java.util.ArrayList;
17+
import java.util.HashMap;
1718
import java.util.List;
19+
import java.util.UUID;
1820

1921
/**
2022
* NameLinkAPI is a utility class responsible for fetching and caching mappings between
@@ -52,15 +54,15 @@ public class NameLinkAPI {
5254
* @return A list of {@code DisplayMapping} objects, either from the API or the cached file,
5355
* or an empty list in case of failure.
5456
*/
55-
public static List<DisplayMapping> getMappings(String source) {
57+
public static HashMap<UUID, DisplayMapping> getMappings(String source) {
5658
status = "Working";
5759

5860
try {
5961
// Load JSON from URL
6062
final String jsonData = loadJsonFromUrl(source);
6163
LOGGER.info("Load data from url");
6264
// Convert String JSON into Java objects
63-
final List<DisplayMapping> displayMappings = loadJsonToObjects(jsonData);
65+
final HashMap<UUID, DisplayMapping> displayMappings = loadJsonToObjects(jsonData);
6466
LOGGER.info("Converted sting to Object");
6567
// Save to file as a backup
6668
saveJsonToFile(jsonData);
@@ -73,14 +75,14 @@ public static List<DisplayMapping> getMappings(String source) {
7375
// If an exception occurs
7476
try {
7577
// Try loading it from the cached file
76-
List<DisplayMapping> displayMappings = loadJsonFromFile();
78+
HashMap<UUID, DisplayMapping> displayMappings = loadJsonFromFile();
7779
status = "Fallback";
7880
LOGGER.warn("Could not reach the server. Using cached fallback.");
7981
return displayMappings;
8082
} catch (RuntimeException | IOException ex) {
8183
status = "Failure";
8284
LOGGER.warn("Could not reach the server or find a fallback.");
83-
return new ArrayList<>(0);
85+
return new HashMap<>(); // Return an empty list on failure
8486
}
8587
}
8688
}
@@ -167,9 +169,9 @@ private static void saveJsonToFile(String jsonData) throws IOException {
167169
* @return A list of {@code DisplayMapping} objects parsed from the JSON string.
168170
* @throws IOException If an error occurs while parsing the JSON data.
169171
*/
170-
private static List<DisplayMapping> loadJsonToObjects(String jsonData) throws IOException {
172+
private static HashMap<UUID, DisplayMapping> loadJsonToObjects(String jsonData) throws IOException {
171173
Gson gson = new Gson();
172-
return gson.fromJson(jsonData, getDisplayMappingListType());
174+
return gson.fromJson(jsonData, getDisplayMappingMapType());
173175
}
174176

175177
/**
@@ -181,10 +183,10 @@ private static List<DisplayMapping> loadJsonToObjects(String jsonData) throws IO
181183
* @return A list of {@code DisplayMapping} objects loaded from the cache file.
182184
* @throws IOException If an error occurs while reading the cache file.
183185
*/
184-
private static List<DisplayMapping> loadJsonFromFile() throws IOException {
186+
private static HashMap<UUID, DisplayMapping> loadJsonFromFile() throws IOException {
185187
Gson gson = new Gson();
186188
try (Reader reader = Files.newBufferedReader(Paths.get(NameLinkAPI.CACHE_PATH))) {
187-
return gson.fromJson(reader, getDisplayMappingListType());
189+
return gson.fromJson(reader, getDisplayMappingMapType());
188190
}
189191
}
190192

@@ -195,8 +197,8 @@ private static List<DisplayMapping> loadJsonFromFile() throws IOException {
195197
*
196198
* @return The Type representing {@code List<DisplayMapping>}.
197199
*/
198-
private static Type getDisplayMappingListType() {
199-
return new TypeToken<List<DisplayMapping>>() {}.getType();
200+
private static Type getDisplayMappingMapType() {
201+
return new TypeToken<HashMap<UUID, DisplayMapping>>() {}.getType();
200202
}
201203

202204
/**

src/client/java/golden/scnicknamer/SCNicknamerClient.java

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
import org.slf4j.Logger;
1414
import org.slf4j.LoggerFactory;
1515

16-
import java.util.*;
16+
import java.util.Map;
17+
import java.util.Optional;
18+
import java.util.UUID;
1719

1820
/**
1921
* Client-side mod initializer for the Minecraft mod "Spooncraft Name Link".
@@ -29,7 +31,7 @@ public class SCNicknamerClient implements ClientModInitializer {
2931
static SCNicknamerConfig config;
3032

3133
// List of mappings for replacement and optional color changes
32-
private static List<DisplayMapping> mappings = new ArrayList<>();
34+
private static Map<UUID, DisplayMapping> mappings = Map.of();
3335

3436
/**
3537
* Retrieves a mapping matching either the UUID or the name of the Minecraft player.
@@ -38,14 +40,8 @@ public class SCNicknamerClient implements ClientModInitializer {
3840
* @param name The in-game name of the Minecraft player
3941
* @return The {@code DisplayMapping} object if found, otherwise null
4042
*/
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;
43+
public static DisplayMapping getMapping(UUID uuid) {
44+
return mappings.get(uuid);
4945
}
5046

5147
/**
@@ -94,40 +90,22 @@ static MutableText applyMapping(Text message, DisplayMapping mapping,
9490
}
9591

9692
/**
97-
* Retrieves and applies the correct name mapping (if any) for a given Minecraft username or
98-
* UUID.
99-
* Checks the mappings list to see if the provided displayName or uuid has a corresponding
100-
* mapping, and if found, applies it by optionally altering the name and color.
93+
* Retrieves and applies the correct name mapping (if any) for a given UUID.
94+
* Checks the mappings list to see if the provided uuid has a corresponding mapping,
95+
* and if found, applies it by optionally altering the name and color.
10196
*
10297
* @param displayName The original in-game name to be displayed
10398
* @param uuid The UUID of the Minecraft player
104-
* @param name The in-game name as a Text object
105-
* @param replaceName Whether to replace the name with the name defined in the mapping
106-
* @param replaceColour Whether to replace the colour with the colour defined in the mapping
107-
* @return A Text object containing the potentially modified name with appropriate styling
108-
*/
109-
public static Text getStyledName(Text displayName, UUID uuid, String name, boolean replaceName,
110-
boolean replaceColour) {
111-
DisplayMapping mapping = getMapping(uuid, name);
112-
if (mapping != null) {
113-
return applyMapping(displayName, mapping, replaceName, replaceColour);
114-
}
115-
return displayName;
116-
}
117-
118-
/**
119-
* Retrieves and applies the correct name mapping (if any) for a given Minecraft username.
120-
*
121-
* @param displayName The original in-game name to be displayed
122-
* @param name The in-game name as a Text object
12399
* @param replaceName Whether to replace the name with the name defined in the mapping
124100
* @param replaceColour Whether to replace the colour with the colour defined in the mapping
125101
* @return A Text object containing the potentially modified name with appropriate styling
126-
* @see #getStyledName(Text, UUID, String, boolean, boolean)
127102
*/
128-
public static Text getStyledName(Text displayName, String name, boolean replaceName,
103+
public static Text getStyledName(Text displayName, UUID uuid, boolean replaceName,
129104
boolean replaceColour) {
130-
return getStyledName(displayName, UUID.randomUUID(), name, replaceName, replaceColour);
105+
DisplayMapping mapping = getMapping(uuid);
106+
return (mapping != null)
107+
? applyMapping(displayName, mapping, replaceName, replaceColour)
108+
: displayName;
131109
}
132110

133111
/**
@@ -149,16 +127,10 @@ public static Text getStyledChat(Text message, boolean replaceName, boolean repl
149127
MutableText newText = Text.literal(text).setStyle(style);
150128

151129
HoverEvent event = style.getHoverEvent();
152-
if (event != null) {
153-
154-
if (event.getAction() == HoverEvent.Action.SHOW_ENTITY) {
155-
HoverEvent.EntityContent entity = ((HoverEvent.ShowEntity) event).entity();
156-
157-
newText = (MutableText) getStyledName(newText, entity.uuid,
158-
String.valueOf(entity.name),
159-
replaceName, replaceColour);
160-
newText.setStyle(newText.getStyle().withHoverEvent(event));
161-
}
130+
if (event != null && event.getAction() == HoverEvent.Action.SHOW_ENTITY) {
131+
HoverEvent.EntityContent entity = ((HoverEvent.ShowEntity) event).entity();
132+
newText = (MutableText) getStyledName(newText, entity.uuid, replaceName, replaceColour);
133+
newText.setStyle(newText.getStyle().withHoverEvent(event));
162134
}
163135

164136
outputMessage.append(newText);
@@ -177,7 +149,9 @@ public static Text getStyledChat(Text message, boolean replaceName, boolean repl
177149
* @return The number of mappings retrieved.
178150
*/
179151
public static int getMappings(String source) {
180-
String s = (source == null || source.isEmpty()) ? "https://gwaff.uqcloud.net/api/spooncraft" : source;
152+
String s = (source == null || source.isEmpty())
153+
? "https://gwaff.uqcloud.net/scnicknamer/test/hashmappings"
154+
: source;
181155
mappings = NameLinkAPI.getMappings(s);
182156
return mappings.size();
183157
}

src/client/java/golden/scnicknamer/client/HoverEventMixin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ public abstract class HoverEventMixin {
4141
this.tooltip.add(Text.translatable("gui.entity_tooltip.type",
4242
this.entityType.getName()));
4343
if (this.entityType == EntityType.PLAYER && this.name.isPresent()) {
44-
DisplayMapping mapping = SCNicknamerClient.getMapping(this.uuid,
45-
this.name.get().getString());
44+
DisplayMapping mapping = SCNicknamerClient.getMapping(this.uuid);
4645
if (mapping != null && mapping.discord_nick != null) {
4746
this.tooltip.add(Text.translatable("gui.scnicknamer.hover_nickname",
4847
mapping.discord_nick));

src/client/java/golden/scnicknamer/client/PlayerEntityRendererMixin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.spongepowered.asm.mixin.injection.ModifyArgs;
1414
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;
1515

16+
import java.util.UUID;
17+
1618
@Mixin (PlayerEntityRenderer.class)
1719
public abstract class PlayerEntityRendererMixin {
1820
@Unique
@@ -29,7 +31,7 @@ protected void renderLabelIfPresent(Args args) {
2931

3032
PlayerEntityRenderState player = args.get(0);
3133
Text display_name = args.get(1);
32-
Text label = SCNicknamerClient.getStyledName(display_name, player.name,
34+
Text label = SCNicknamerClient.getStyledName(display_name, UUID.randomUUID(), // TODO: Fix UUID
3335
CONFIG.replacenametag, CONFIG.colournametag);
3436
args.set(1, label);
3537
}

src/client/java/golden/scnicknamer/client/PlayerListEntryMixin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public void replaceDisplayName(CallbackInfoReturnable<Text> cir) {
4141
}
4242

4343
Text label = SCNicknamerClient.getStyledName(displayName, profile.getId(),
44-
profile.getName(),
4544
CONFIG.replacetablist, CONFIG.colourtablist);
4645
cir.setReturnValue(label);
4746
}

src/client/java/golden/scnicknamer/client/TrackedWaypointMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private void onGetConfig(CallbackInfoReturnable<Waypoint.Config> cir) {
3737
if (uuid == null) {
3838
return;
3939
}
40-
DisplayMapping mapping = SCNicknamerClient.getMapping(uuid, null);
40+
DisplayMapping mapping = SCNicknamerClient.getMapping(uuid);
4141
if (mapping == null || mapping.colour == null || mapping.colour.isEmpty()) {
4242
return;
4343
}

0 commit comments

Comments
 (0)