|
| 1 | +package com.ownwn.ownwnaddons.features; |
| 2 | + |
| 3 | +import com.ownwn.ownwnaddons.OwnwnAddons; |
| 4 | +import com.ownwn.ownwnaddons.utils.ColourUtils; |
| 5 | +import com.ownwn.ownwnaddons.utils.FetchOnServerJoin; |
| 6 | +import com.ownwn.ownwnaddons.utils.NewConfig; |
| 7 | +import com.ownwn.ownwnaddons.utils.Utils; |
| 8 | +import net.minecraft.client.Minecraft; |
| 9 | +import net.minecraftforge.client.event.ClientChatReceivedEvent; |
| 10 | +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; |
| 11 | + |
| 12 | +import java.util.regex.Matcher; |
| 13 | +import java.util.regex.Pattern; |
| 14 | +public class CustomNames { |
| 15 | + |
| 16 | + public static String username = "-Placeholder"; |
| 17 | + // usernames cannot have dashes so there's no chance this will accidentally trigger. |
| 18 | + |
| 19 | + public static String[] hypixelRanks = { |
| 20 | + "", // leave empty for default rank |
| 21 | + "[VIP] ", |
| 22 | + "[VIP+] ", |
| 23 | + "[MVP] ", |
| 24 | + "[MVP+] ", |
| 25 | + "[MVP++] " |
| 26 | + |
| 27 | + }; |
| 28 | + |
| 29 | + |
| 30 | + public static Pattern VIP_PATTERN; |
| 31 | + public static Pattern VIP_PLUS_PATTERN; |
| 32 | + public static Pattern MVP_PATTERN; |
| 33 | + public static Pattern MVP_PLUS_PATTERN; |
| 34 | + public static Pattern MVP_DOUBLEPLUS_PATTERN; |
| 35 | + |
| 36 | + public static String replacePlayerNameAndRank(String text) { |
| 37 | + if (!NewConfig.CUSTOM_NAME_TOGGLE || NewConfig.CUSTOM_NAME_EDITOR.equals("")) { |
| 38 | + return text; |
| 39 | + } |
| 40 | + if (Minecraft.getMinecraft().thePlayer == null) { |
| 41 | + return text; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + if (!text.contains(username)) { |
| 46 | + return text; |
| 47 | + } |
| 48 | + |
| 49 | + if (NewConfig.CUSTOM_RANK_TOGGLE && !NewConfig.CUSTOM_RANK_EDITOR.equals("") && NewConfig.PLAYER_HYPIXEL_RANK != 0) { |
| 50 | + if (Utils.stripFormatting(text).contains(hypixelRanks[NewConfig.PLAYER_HYPIXEL_RANK] + username)) { |
| 51 | + Pattern chosenPattern; |
| 52 | + |
| 53 | + // kid named java 8 switch statement: |
| 54 | + switch (NewConfig.PLAYER_HYPIXEL_RANK) { |
| 55 | + case 1: |
| 56 | + chosenPattern = VIP_PATTERN; |
| 57 | + break; |
| 58 | + case 2: |
| 59 | + chosenPattern = VIP_PLUS_PATTERN; |
| 60 | + break; |
| 61 | + case 3: |
| 62 | + chosenPattern = MVP_PATTERN; |
| 63 | + break; |
| 64 | + case 4: |
| 65 | + chosenPattern = MVP_PLUS_PATTERN; |
| 66 | + break; |
| 67 | + case 5: |
| 68 | + chosenPattern = MVP_DOUBLEPLUS_PATTERN; |
| 69 | + break; |
| 70 | + default: |
| 71 | + throw new IllegalArgumentException(OwnwnAddons.PREFIX + "&cInvalid rank: " + NewConfig.PLAYER_HYPIXEL_RANK); |
| 72 | + } |
| 73 | + |
| 74 | + Matcher matcher = chosenPattern.matcher(text); |
| 75 | + if (matcher.find()) { |
| 76 | + |
| 77 | + String newRank = NewConfig.CUSTOM_RANK_EDITOR.replace("&&", "§"); |
| 78 | + text = text.replace(matcher.group(0), newRank + "§r " + username + "§r"); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + text = text.replace(username, NewConfig.CUSTOM_NAME_EDITOR.replace("&&", "§") + "§r"); |
| 84 | + return text; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + public static String replaceOtherNames(String text) { |
| 89 | + if (Minecraft.getMinecraft().thePlayer == null) { |
| 90 | + return text; |
| 91 | + } |
| 92 | + if (!NewConfig.SHARED_RAINBOW_NAMES) { |
| 93 | + return text; |
| 94 | + } |
| 95 | + if (FetchOnServerJoin.nameList == null || FetchOnServerJoin.nameList.size() == 0) { |
| 96 | + return text; |
| 97 | + } |
| 98 | + |
| 99 | + for (String name : FetchOnServerJoin.nameList) { |
| 100 | + |
| 101 | + if (name.equals(username)) { |
| 102 | + continue; |
| 103 | + } |
| 104 | + |
| 105 | + String newName = "§" + ColourUtils.chooseColour() + name + "§r"; |
| 106 | + text = text.replace(name, newName); |
| 107 | + } |
| 108 | + text = text.replace("§zOwnwn§rAddons", "§zOwnwnAddons§r"); |
| 109 | + // ^ stop "Ownwn" being coloured when typing the mod name |
| 110 | + return text; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + public static String replaceChromaMessages(String text) { |
| 115 | + |
| 116 | + if (Minecraft.getMinecraft().thePlayer == null) { |
| 117 | + return text; |
| 118 | + } |
| 119 | + if (NewConfig.CHROMA_TEXT_REPLACE.equals("")) { |
| 120 | + return text; |
| 121 | + } |
| 122 | + String newColour = "§"; |
| 123 | + |
| 124 | + newColour += ColourUtils.chooseColour(); |
| 125 | + |
| 126 | + for (String replacementText: NewConfig.CHROMA_TEXT_REPLACE.replace("&&", "§").split(", ")) { |
| 127 | + if (replacementText.startsWith("§l") || replacementText.startsWith("§l", 2)) { |
| 128 | + newColour = newColour.replace("§l", "") + "§l"; |
| 129 | + } else { |
| 130 | + newColour = newColour.replace("§l", ""); |
| 131 | + } |
| 132 | + text = text.replace(replacementText, newColour + Utils.stripFormatting(replacementText) + "§r"); |
| 133 | + // it *technically* works |
| 134 | + } |
| 135 | + |
| 136 | + return text; |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + @SubscribeEvent |
| 141 | + public void onChat(ClientChatReceivedEvent event) { |
| 142 | + /* listen for a chat message to ensure the player is not null so we can grab their name. |
| 143 | + this could be done with mc.get$ession().getUsername() but getting the session looks sus |
| 144 | + and probably doesn't support account switching (e.g. essential) */ |
| 145 | + |
| 146 | + if (!username.equals("-Placeholder")) { // the username has already been set |
| 147 | + return; |
| 148 | + } |
| 149 | + username = Minecraft.getMinecraft().thePlayer.getName(); |
| 150 | + |
| 151 | + // compile patterns when the game is open, so that we can include the players' username. |
| 152 | + VIP_PATTERN = Pattern.compile("(§a\\[VIP]) " + username); |
| 153 | + VIP_PLUS_PATTERN = Pattern.compile("(§a\\[VIP§6\\+§a]) " + username); |
| 154 | + |
| 155 | + MVP_PATTERN = Pattern.compile("(§b\\[MVP]) " + username); |
| 156 | + MVP_PLUS_PATTERN = Pattern.compile("(§b\\[MVP§.\\+§b]|§b\\[MVP(?:§r)+(?:§.)+\\+(?:§r)+(?:§b)+]) " + username); |
| 157 | + MVP_DOUBLEPLUS_PATTERN = Pattern.compile("(§6\\[MVP(?:§.)*\\+\\+(?:§.)*]) " + username); |
| 158 | + // ^ the MVP+ regex is completely cooked, for some reason my name gets load of extra section signs?? |
| 159 | + // e.g. §r§8[§r§5161§r§8] §r§b[MVP§r§4+§r§b] Ownwn§r§f: hi§r |
| 160 | + // have updated the MVP++ pattern just in case this occurs with MVP++ too. |
| 161 | + } |
| 162 | +} |
0 commit comments