Skip to content

Commit bed8e18

Browse files
committed
Optimize the code by deduplicating some code
1 parent 913029a commit bed8e18

File tree

8 files changed

+57
-16
lines changed

8 files changed

+57
-16
lines changed

common/src/main/java/io/github/communityradargg/forgemod/util/CommonHandler.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,37 @@ public void handleKeyInputEvent() {
259259
}
260260
}
261261

262+
/**
263+
* Formats a prefix by replacing {@literal &} with {@literal §} and adding a space to the end.
264+
*
265+
* @param rawText The raw input text.
266+
* @return Returns the formatted prefix.
267+
*/
268+
public String formatPrefix(final @NotNull String rawText) {
269+
return rawText.replace("&", "§") + " ";
270+
}
271+
272+
/**
273+
* Formats the text, wraps it into the version specific text component and gets the unformatted text from it for the possibility of comparison of texts.
274+
*
275+
* @param rawText The raw input text.
276+
* @return Returns the unformatted text
277+
*/
278+
public String unformatPrefixForCompare(final @NotNull String rawText) {
279+
return versionBridge.wrapAndUnformatText(formatPrefix(rawText));
280+
}
281+
282+
/**
283+
* Checks if the given prefix candidate is matching with any of the old prefixes.
284+
*
285+
* @param prefixCandidate The possible prefix candidate.
286+
* @param oldPrefixes The old prefixes.
287+
* @return Returns {@code true} if there is a match, else {@code false}.
288+
*/
289+
public boolean isPrefixMatching(final @NotNull String prefixCandidate, final @NotNull Set<@NotNull String> oldPrefixes) {
290+
return oldPrefixes.stream().anyMatch(old -> unformatPrefixForCompare(old).equals(prefixCandidate));
291+
}
292+
262293
/**
263294
* Gets the list manager.
264295
*

common/src/main/java/io/github/communityradargg/forgemod/util/VersionBridge.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ public interface VersionBridge {
1919
void updatePrefixes(final @NotNull CommonHandler commonHandler, final @NotNull Set<String> oldPrefixes);
2020

2121
boolean isPlayerListKeyPressed();
22+
23+
@NotNull String wrapAndUnformatText(final @NotNull String text);
2224
}

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void init(FMLInitializationEvent event) {
4949

5050
registerEvents();
5151
registerCommands();
52+
5253
LOGGER.info("Successfully loaded the mod '{}'", CommonHandler.MOD_ID);
5354
}
5455

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/util/Utils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ public static void updatePlayerByUuid(final @NotNull CommonHandler commonHandler
8484
* @param oldPrefixes The old prefixes that need to be removed before adding the new one.
8585
*/
8686
public static void updatePlayerNameTag(final @NotNull CommonHandler commonHandler, final @NotNull EntityPlayer player, final @NotNull Set<String> oldPrefixes) {
87-
player.getPrefixes().removeIf(prefix -> oldPrefixes.stream().anyMatch(oldPrefix -> new TextComponentString(oldPrefix.replace("&", "§") + " ").getUnformattedText().equals(prefix.getUnformattedText())));
87+
player.getPrefixes().removeIf(prefix -> commonHandler.isPrefixMatching(prefix.getUnformattedText(), oldPrefixes));
8888
final String addonPrefix = commonHandler.getListManager()
89-
.getPrefix(player.getGameProfile().getId())
90-
.replace("&", "§");
89+
.getPrefix(player.getGameProfile().getId());
9190

9291
if (!addonPrefix.isEmpty()) {
93-
player.addPrefix(new TextComponentString(addonPrefix + " "));
92+
player.addPrefix(new TextComponentString(commonHandler.formatPrefix(addonPrefix)));
9493
}
9594
}
9695

@@ -123,17 +122,16 @@ private static void updatePlayerPrefix(final @NotNull CommonHandler commonHandle
123122
final ITextComponent displayName = player.getDisplayName();
124123
ITextComponent newDisplayName = displayName;
125124
for (final String prefix : oldPrefixes) {
126-
if (!displayName.getUnformattedText().startsWith(new TextComponentString(prefix.replace("&", "§") + " ").getUnformattedText())) {
125+
if (!displayName.getUnformattedText().startsWith(commonHandler.unformatPrefixForCompare(prefix))) {
127126
continue;
128127
}
129128
newDisplayName = displayName.getSiblings().get(displayName.getSiblings().size() - 1);
130129
}
131130

132131
final String addonPrefix = commonHandler.getListManager()
133-
.getPrefix(player.getGameProfile().getId())
134-
.replace("&", "§");
132+
.getPrefix(player.getGameProfile().getId());
135133
if (!addonPrefix.isEmpty()) {
136-
newDisplayName = new TextComponentString(addonPrefix + " ").appendSibling(newDisplayName);
134+
newDisplayName = new TextComponentString(commonHandler.formatPrefix(addonPrefix)).appendSibling(newDisplayName);
137135
}
138136
player.setDisplayName(newDisplayName);
139137
}

versions/1.12.2/src/main/java/io/github/communityradargg/forgemod/util/VersionBridgeImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ public void updatePrefixes(final @NotNull CommonHandler commonHandler, final @No
7272
public boolean isPlayerListKeyPressed() {
7373
return Minecraft.getMinecraft().gameSettings.keyBindPlayerList.isPressed();
7474
}
75+
76+
@Override
77+
public @NotNull String wrapAndUnformatText(final @NotNull String text) {
78+
return new TextComponentString(text).getUnformattedText();
79+
}
7580
}

versions/1.8.9/src/main/java/io/github/communityradargg/forgemod/CommunityRadarMod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public void init(final FMLInitializationEvent event) {
4949

5050
registerEvents();
5151
registerCommands();
52+
5253
LOGGER.info("Successfully loaded the mod '{}'", CommonHandler.MOD_ID);
5354
}
5455

versions/1.8.9/src/main/java/io/github/communityradargg/forgemod/util/Utils.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,12 @@ public static void updatePlayerByUuid(final @NotNull CommonHandler commonHandler
7979
* @param oldPrefixes The old prefixes that need to be removed before adding the new one.
8080
*/
8181
public static void updatePlayerNameTag(final @NotNull CommonHandler commonHandler, final @NotNull EntityPlayer player, final @NotNull Set<String> oldPrefixes) {
82-
player.getPrefixes().removeIf(prefix -> oldPrefixes.stream().anyMatch(oldPrefix -> new ChatComponentText(oldPrefix.replace("&", "§") + " ").getUnformattedText().equals(prefix.getUnformattedText())));
82+
player.getPrefixes().removeIf(prefix -> commonHandler.isPrefixMatching(prefix.getUnformattedText(), oldPrefixes));
8383
final String addonPrefix = commonHandler.getListManager()
84-
.getPrefix(player.getGameProfile().getId())
85-
.replace("&", "§");
84+
.getPrefix(player.getGameProfile().getId());
8685

8786
if (!addonPrefix.isEmpty()) {
88-
player.addPrefix(new ChatComponentText(addonPrefix + " "));
87+
player.addPrefix(new ChatComponentText(commonHandler.formatPrefix(addonPrefix)));
8988
}
9089
}
9190

@@ -115,17 +114,16 @@ private static void updatePlayerPrefix(final @NotNull CommonHandler commonHandle
115114
final IChatComponent displayName = player.getDisplayName();
116115
IChatComponent newDisplayName = displayName;
117116
for (final String prefix : oldPrefixes) {
118-
if (!displayName.getUnformattedText().startsWith(new ChatComponentText(prefix.replace("&", "§") + " ").getUnformattedText())) {
117+
if (!displayName.getUnformattedText().startsWith(commonHandler.unformatPrefixForCompare(prefix))) {
119118
continue;
120119
}
121120
newDisplayName = displayName.getSiblings().get(displayName.getSiblings().size() - 1);
122121
}
123122

124123
final String addonPrefix = commonHandler.getListManager()
125-
.getPrefix(player.getGameProfile().getId())
126-
.replace("&", "§");
124+
.getPrefix(player.getGameProfile().getId());
127125
if (!addonPrefix.isEmpty()) {
128-
newDisplayName = new ChatComponentText(addonPrefix + " ").appendSibling(newDisplayName);
126+
newDisplayName = new ChatComponentText(commonHandler.formatPrefix(addonPrefix)).appendSibling(newDisplayName);
129127
}
130128
player.setDisplayName(newDisplayName);
131129
}

versions/1.8.9/src/main/java/io/github/communityradargg/forgemod/util/VersionBridgeImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,9 @@ public void updatePrefixes(final @NotNull CommonHandler commonHandler, final @No
7272
public boolean isPlayerListKeyPressed() {
7373
return Minecraft.getMinecraft().gameSettings.keyBindPlayerList.isPressed();
7474
}
75+
76+
@Override
77+
public @NotNull String wrapAndUnformatText(final @NotNull String text) {
78+
return new ChatComponentText(text).getUnformattedText();
79+
}
7580
}

0 commit comments

Comments
 (0)