From 8c9e6e4a9047f07afe9e478c3ef6808a31e2c30c Mon Sep 17 00:00:00 2001 From: RoseGoldIsntGay Date: Sat, 5 Feb 2022 22:27:18 +0200 Subject: Better Custom Name Mixins Added better mixins for custom names Fixed [if or and] mistake in hardstone nuker --- .../rosegoldaddons/mixins/MixinRenderString.java | 61 +++++++++------------- 1 file changed, 24 insertions(+), 37 deletions(-) (limited to 'src/main/java/rosegoldaddons/mixins/MixinRenderString.java') diff --git a/src/main/java/rosegoldaddons/mixins/MixinRenderString.java b/src/main/java/rosegoldaddons/mixins/MixinRenderString.java index d7a80b8..b010d84 100644 --- a/src/main/java/rosegoldaddons/mixins/MixinRenderString.java +++ b/src/main/java/rosegoldaddons/mixins/MixinRenderString.java @@ -1,48 +1,47 @@ package rosegoldaddons.mixins; import net.minecraft.client.gui.FontRenderer; -import net.minecraft.util.StringUtils; import org.apache.commons.codec.digest.DigestUtils; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyVariable; import rosegoldaddons.Main; +import rosegoldaddons.utils.ChatUtils; @Mixin(FontRenderer.class) public abstract class MixinRenderString { + private boolean replacedRank = false; + @ModifyVariable(method = "drawString(Ljava/lang/String;FFIZ)I", at = @At(value = "FIELD")) private String replaceName(String text) { if (Main.mc.theWorld == null || Main.mc.thePlayer == null) return text; - if(Main.pauseCustom) return text; + if (Main.pauseCustom) return text; if (Main.configFile.wydsi && text.contains("727")) { text = text.replace("727", "726"); } if (Main.init && Main.configFile.alchsleep != 88 || Main.configFile.skiblock != 263) { - String[] words = stripString(text).replace(":"," ").replace("'"," ").split(" "); - String[] formatteds = text.replace(":"," ").replace("'"," ").split(" "); + String[] words = removeFormatting(text).replace(":", " ").replace("'", " ").split(" "); + String[] formatteds = text.replace(":", " ").replace("'", " ").split(" "); for (String word : words) { - if(word.equals("")) continue; - if (Main.hashedCache.contains(word)) continue; - if(Main.rankCache.containsKey(word)) { + if (word.equals("") || Main.hashedCache.contains(word)) continue; + if (Main.rankCache.containsKey(word)) { String rank = getRank(text, word); - if(rank != null) { + if (rank != null) { text = text.replace(rank, Main.rankCache.get(word)); } } else { String hashed = DigestUtils.sha256Hex(word + word); - if (Main.ranks.containsKey(hashed)) { + if (Main.ranks.containsKey(hashed) && !Main.rankCache.containsKey(word)) { String rank = getRank(text, word); - if(rank != null) { + if (rank != null) { Main.rankCache.put(word, Main.ranks.get(hashed)); - System.out.println(word+":"+Main.ranks.get(hashed)); } } + if (!Main.names.containsKey(hashed) && !Main.ranks.containsKey(hashed) && !Main.hashedCache.contains(word)) { + Main.hashedCache.add(word); + } } - } - for (String word : words) { - if(word.equals("")) continue; - if (Main.hashedCache.contains(word)) continue; if (Main.nameCache.containsKey(word)) { String[] replaces = Main.nameCache.get(word).split(" "); for (String replace : replaces) { @@ -54,9 +53,10 @@ public abstract class MixinRenderString { text = text.replace(word, Main.nameCache.get(word) + color); } else { String hashed = DigestUtils.sha256Hex(word + word); - if (Main.names.containsKey(hashed)) { + if (Main.names.containsKey(hashed) && !Main.nameCache.containsKey(word)) { Main.nameCache.put(word, Main.names.get(hashed)); - } else { + } + if (!Main.names.containsKey(hashed) && !Main.ranks.containsKey(hashed) && !Main.hashedCache.contains(word)) { Main.hashedCache.add(word); } } @@ -66,12 +66,12 @@ public abstract class MixinRenderString { } private String getRank(String str, String sub) { - if(!str.contains("[") || !str.contains("]")) return null; - if(sub.contains("[") || sub.contains("]")) return null; - if(str.indexOf(sub)-sub.length() >= str.indexOf("]")) return null; - if(str.indexOf(sub) < str.indexOf("[")) return null; + if (!str.contains("[") || !str.contains("]")) return null; + if (sub.contains("[") || sub.contains("]")) return null; + if (str.indexOf(sub) - sub.length() >= str.indexOf("]")) return null; + if (str.indexOf(sub) < str.indexOf("[")) return null; - return str.substring(str.indexOf("["), str.indexOf("]")+1); + return str.substring(str.indexOf("["), str.indexOf("]") + 1); } private String getColorBeforeIndex(String str, int index) { @@ -86,20 +86,7 @@ public abstract class MixinRenderString { return lastColor; } - private String stripString(String s) { - char[] nonValidatedString; - try { - nonValidatedString = StringUtils.stripControlCodes(s).toCharArray(); - } catch (Exception e) { - return ""; - } - StringBuilder validated = new StringBuilder(); - for (char a : nonValidatedString) { - if ((int) a < 127 && (int) a > 20) { - validated.append(a); - } - } - - return validated.toString(); + private String removeFormatting(String input) { + return input.replaceAll("ยง[0-9a-fk-or]", ""); } } -- cgit