From 7c00af18febf6c0b833c7633b4fb60a9a1bb93af Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Sat, 16 Oct 2021 15:50:41 -0400 Subject: Code Clean Up (#2) * intellij code clean up * optimize imports * format * intellij suggestions * fix empty catch issues --- .../miscfeatures/DamageCommas.java | 80 +++++++++++----------- 1 file changed, 40 insertions(+), 40 deletions(-) (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java index aafcfb4d..5f3a5afa 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java @@ -9,7 +9,6 @@ import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; import java.text.NumberFormat; -import java.util.HashMap; import java.util.WeakHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -21,18 +20,18 @@ public class DamageCommas { private static final EnumChatFormatting[] coloursHypixel = {EnumChatFormatting.WHITE, EnumChatFormatting.YELLOW, EnumChatFormatting.GOLD, EnumChatFormatting.RED, EnumChatFormatting.RED, EnumChatFormatting.WHITE}; private static final char STAR = '\u2727'; - private static final Pattern PATTERN_CRIT = Pattern.compile("\u00a7f"+STAR+"((?:\u00a7.\\d)+)\u00a7."+STAR+"(.*)"); + private static final Pattern PATTERN_CRIT = Pattern.compile("\u00a7f" + STAR + "((?:\u00a7.\\d)+)\u00a7." + STAR + "(.*)"); private static final Pattern PATTERN_NO_CRIT = Pattern.compile("\u00a77(\\d+)(.*)"); public static IChatComponent replaceName(EntityLivingBase entity) { - if(!entity.hasCustomName()) return entity.getDisplayName(); + if (!entity.hasCustomName()) return entity.getDisplayName(); IChatComponent name = entity.getDisplayName(); - if(NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 0) return name; + if (NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 0) return name; - if(replacementMap.containsKey(entity)) { + if (replacementMap.containsKey(entity)) { ChatComponentText component = replacementMap.get(entity); - if(component == null) return name; + if (component == null) return name; return component; } @@ -44,17 +43,17 @@ public class DamageCommas { String suffix; Matcher matcherCrit = PATTERN_CRIT.matcher(formatted); - if(matcherCrit.matches()) { + if (matcherCrit.matches()) { crit = true; numbers = StringUtils.cleanColour(matcherCrit.group(1)); - prefix = "\u00a7f"+STAR; - suffix = "\u00a7f"+STAR+matcherCrit.group(2); + prefix = "\u00a7f" + STAR; + suffix = "\u00a7f" + STAR + matcherCrit.group(2); } else { Matcher matcherNoCrit = PATTERN_NO_CRIT.matcher(formatted); - if(matcherNoCrit.matches()) { + if (matcherNoCrit.matches()) { numbers = matcherNoCrit.group(1); prefix = "\u00A77"; - suffix = "\u00A7r"+matcherNoCrit.group(2); + suffix = "\u00A7r" + matcherNoCrit.group(2); } else { replacementMap.put(entity, null); return name; @@ -66,22 +65,22 @@ public class DamageCommas { try { int number = Integer.parseInt(numbers); - if(number > 999 && NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 2) { + if (number > 999 && NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 2) { newFormatted.append(Utils.shortNumberFormat(number, 0)); } else { newFormatted.append(NumberFormat.getIntegerInstance().format(number)); } - } catch(NumberFormatException e) { + } catch (NumberFormatException e) { replacementMap.put(entity, null); return name; } - if(crit) { + if (crit) { StringBuilder newFormattedCrit = new StringBuilder(); int colourIndex = 0; - for(char c : newFormatted.toString().toCharArray()) { - if(c == ',') { + for (char c : newFormatted.toString().toCharArray()) { + if (c == ',') { newFormattedCrit.append(EnumChatFormatting.GRAY); } else { newFormattedCrit.append(coloursHypixel[colourIndex++ % coloursHypixel.length]); @@ -92,23 +91,23 @@ public class DamageCommas { newFormatted = newFormattedCrit; } - ChatComponentText finalComponent = new ChatComponentText(prefix+newFormatted.toString()+suffix); + ChatComponentText finalComponent = new ChatComponentText(prefix + newFormatted + suffix); replacementMap.put(entity, finalComponent); return finalComponent; - /*if(formatted.startsWith("\u00A7f\u2727")) System.out.println(formatted); + /*if (formatted.startsWith("\u00A7f\u2727")) System.out.println(formatted); - if(formatted.length() >= 7 && (formatted.startsWith("\u00A7f\u2727") || formatted.startsWith("\u00A7f\u2694")) && + if (formatted.length() >= 7 && (formatted.startsWith("\u00A7f\u2727") || formatted.startsWith("\u00A7f\u2694")) && (formatted.endsWith("\u2727\u00a7r") || formatted.endsWith("\u2694\u00a7r"))) { - if(NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 2) { - String numbers = Utils.cleanColour(formatted.substring(3, formatted.length()-3)).trim().replaceAll("[^0-9]", ""); + if (NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 2) { + String numbers = Utils.cleanColour(formatted.substring(3, formatted.length() - 3)).trim().replaceAll("[^0-9]", ""); try { int damage = Integer.parseInt(numbers); String damageString; - if(damage > 999) { + if (damage > 999) { damageString = Utils.shortNumberFormat(damage, 0); } else { damageString = NumberFormat.getIntegerInstance().format(damage); @@ -116,10 +115,10 @@ public class DamageCommas { StringBuilder colouredString = new StringBuilder(); int colourIndex = 0; - for(int i=0; i= '0' && c <= '9') { + if (c >= '0' && c <= '9') { colouredString.insert(0, c); colouredString.insert(0, colours[colourIndex++ % colours.length]); } else { @@ -127,10 +126,11 @@ public class DamageCommas { } } - ChatComponentText ret = new ChatComponentText("\u00A7f\u2727"+colouredString+"\u00a7r\u2727\u00a7r"); + ChatComponentText ret = new ChatComponentText("\u00A7f\u2727" + colouredString + "\u00a7r\u2727\u00a7r"); replacementMap.put(hashCode, ret); return ret; - } catch(NumberFormatException ignored) {} + } catch (NumberFormatException ignored) { + } } StringBuilder builder = new StringBuilder(); @@ -138,42 +138,42 @@ public class DamageCommas { boolean colLast = false; boolean colLastLast; int numCount = 0; - for(int i=formatted.length()-4; i>=3; i--) { + for (int i = formatted.length() - 4; i >= 3; i--) { char c = formatted.charAt(i); colLastLast = colLast; - if(c == '\u00a7') { - if(numLast) numCount--; + if (c == '\u00a7') { + if (numLast) numCount--; numLast = false; colLast = true; - } else if((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) { + } else if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) { numLast = true; colLast = false; numCount++; } else { - if(colLast) { + if (colLast) { replacementMap.put(hashCode, null); return name; } numLast = false; } - if(colLastLast && numLast && numCount > 1 && (numCount-1) % 3 == 0) builder.append(','); + if (colLastLast && numLast && numCount > 1 && (numCount - 1) % 3 == 0) builder.append(','); builder.append(c); } - ChatComponentText ret = new ChatComponentText("\u00A7f\u2727"+builder.reverse().toString()+"\u2727\u00a7r"); + ChatComponentText ret = new ChatComponentText("\u00A7f\u2727" + builder.reverse().toString() + "\u2727\u00a7r"); replacementMap.put(hashCode, ret); return ret; } - if(formatted.length() >= 5 && formatted.startsWith(EnumChatFormatting.GRAY.toString()) && + if (formatted.length() >= 5 && formatted.startsWith(EnumChatFormatting.GRAY.toString()) && formatted.endsWith(EnumChatFormatting.RESET.toString())) { - String damageS = formatted.substring(2, formatted.length()-2); + String damageS = formatted.substring(2, formatted.length() - 2); - for(int i=0; i '9') { + if (c < '0' || c > '9') { replacementMap.put(hashCode, null); return name; } @@ -183,16 +183,16 @@ public class DamageCommas { int damage = Integer.parseInt(damageS); String damageFormatted; - if(NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 2 && damage > 999) { + if (NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 2 && damage > 999) { damageFormatted = Utils.shortNumberFormat(damage, 0); } else { damageFormatted = NumberFormat.getIntegerInstance().format(damage); } - ChatComponentText ret = new ChatComponentText(EnumChatFormatting.GRAY+damageFormatted+EnumChatFormatting.RESET); + ChatComponentText ret = new ChatComponentText(EnumChatFormatting.GRAY + damageFormatted + EnumChatFormatting.RESET); replacementMap.put(hashCode, ret); return ret; - } catch(Exception e) { + } catch (Exception e) { replacementMap.put(hashCode, null); return name; } -- cgit From ea3ec354ba3bb5b4ac64b8032816b8e4c407f099 Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Tue, 28 Dec 2021 00:49:28 -0500 Subject: more code clean up (#38) --- .../github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java index 5f3a5afa..ba12235d 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java @@ -14,7 +14,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class DamageCommas { - private static final WeakHashMap replacementMap = new WeakHashMap<>(); private static final EnumChatFormatting[] coloursHypixel = {EnumChatFormatting.WHITE, EnumChatFormatting.YELLOW, EnumChatFormatting.GOLD, EnumChatFormatting.RED, EnumChatFormatting.RED, EnumChatFormatting.WHITE}; @@ -129,8 +128,7 @@ public class DamageCommas { ChatComponentText ret = new ChatComponentText("\u00A7f\u2727" + colouredString + "\u00a7r\u2727\u00a7r"); replacementMap.put(hashCode, ret); return ret; - } catch (NumberFormatException ignored) { - } + } catch (NumberFormatException ignored) {} } StringBuilder builder = new StringBuilder(); @@ -200,5 +198,4 @@ public class DamageCommas { replacementMap.put(hashCode, null); return name;*/ } - } -- cgit From b09f774d422263ce15b97d6d0804beddf856176d Mon Sep 17 00:00:00 2001 From: IRONM00N <64110067+IRONM00N@users.noreply.github.com> Date: Sun, 27 Feb 2022 11:53:57 -0500 Subject: feat: improve formating :) --- .../miscfeatures/DamageCommas.java | 170 +++++++++++---------- 1 file changed, 89 insertions(+), 81 deletions(-) (limited to 'src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java index ba12235d..9c93b9c5 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/DamageCommas.java @@ -14,86 +14,94 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; public class DamageCommas { - private static final WeakHashMap replacementMap = new WeakHashMap<>(); - - private static final EnumChatFormatting[] coloursHypixel = {EnumChatFormatting.WHITE, EnumChatFormatting.YELLOW, EnumChatFormatting.GOLD, EnumChatFormatting.RED, EnumChatFormatting.RED, EnumChatFormatting.WHITE}; - - private static final char STAR = '\u2727'; - private static final Pattern PATTERN_CRIT = Pattern.compile("\u00a7f" + STAR + "((?:\u00a7.\\d)+)\u00a7." + STAR + "(.*)"); - private static final Pattern PATTERN_NO_CRIT = Pattern.compile("\u00a77(\\d+)(.*)"); - - public static IChatComponent replaceName(EntityLivingBase entity) { - if (!entity.hasCustomName()) return entity.getDisplayName(); - - IChatComponent name = entity.getDisplayName(); - if (NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 0) return name; - - if (replacementMap.containsKey(entity)) { - ChatComponentText component = replacementMap.get(entity); - if (component == null) return name; - return component; - } - - String formatted = name.getFormattedText(); - - boolean crit = false; - String numbers; - String prefix; - String suffix; - - Matcher matcherCrit = PATTERN_CRIT.matcher(formatted); - if (matcherCrit.matches()) { - crit = true; - numbers = StringUtils.cleanColour(matcherCrit.group(1)); - prefix = "\u00a7f" + STAR; - suffix = "\u00a7f" + STAR + matcherCrit.group(2); - } else { - Matcher matcherNoCrit = PATTERN_NO_CRIT.matcher(formatted); - if (matcherNoCrit.matches()) { - numbers = matcherNoCrit.group(1); - prefix = "\u00A77"; - suffix = "\u00A7r" + matcherNoCrit.group(2); - } else { - replacementMap.put(entity, null); - return name; - } - } - - StringBuilder newFormatted = new StringBuilder(); - - try { - int number = Integer.parseInt(numbers); - - if (number > 999 && NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 2) { - newFormatted.append(Utils.shortNumberFormat(number, 0)); - } else { - newFormatted.append(NumberFormat.getIntegerInstance().format(number)); - } - } catch (NumberFormatException e) { - replacementMap.put(entity, null); - return name; - } - - if (crit) { - StringBuilder newFormattedCrit = new StringBuilder(); - - int colourIndex = 0; - for (char c : newFormatted.toString().toCharArray()) { - if (c == ',') { - newFormattedCrit.append(EnumChatFormatting.GRAY); - } else { - newFormattedCrit.append(coloursHypixel[colourIndex++ % coloursHypixel.length]); - } - newFormattedCrit.append(c); - } - - newFormatted = newFormattedCrit; - } - - ChatComponentText finalComponent = new ChatComponentText(prefix + newFormatted + suffix); - - replacementMap.put(entity, finalComponent); - return finalComponent; + private static final WeakHashMap replacementMap = new WeakHashMap<>(); + + private static final EnumChatFormatting[] coloursHypixel = { + EnumChatFormatting.WHITE, + EnumChatFormatting.YELLOW, + EnumChatFormatting.GOLD, + EnumChatFormatting.RED, + EnumChatFormatting.RED, + EnumChatFormatting.WHITE + }; + + private static final char STAR = '\u2727'; + private static final Pattern PATTERN_CRIT = Pattern.compile( + "\u00a7f" + STAR + "((?:\u00a7.\\d)+)\u00a7." + STAR + "(.*)"); + private static final Pattern PATTERN_NO_CRIT = Pattern.compile("\u00a77(\\d+)(.*)"); + + public static IChatComponent replaceName(EntityLivingBase entity) { + if (!entity.hasCustomName()) return entity.getDisplayName(); + + IChatComponent name = entity.getDisplayName(); + if (NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 0) return name; + + if (replacementMap.containsKey(entity)) { + ChatComponentText component = replacementMap.get(entity); + if (component == null) return name; + return component; + } + + String formatted = name.getFormattedText(); + + boolean crit = false; + String numbers; + String prefix; + String suffix; + + Matcher matcherCrit = PATTERN_CRIT.matcher(formatted); + if (matcherCrit.matches()) { + crit = true; + numbers = StringUtils.cleanColour(matcherCrit.group(1)); + prefix = "\u00a7f" + STAR; + suffix = "\u00a7f" + STAR + matcherCrit.group(2); + } else { + Matcher matcherNoCrit = PATTERN_NO_CRIT.matcher(formatted); + if (matcherNoCrit.matches()) { + numbers = matcherNoCrit.group(1); + prefix = "\u00A77"; + suffix = "\u00A7r" + matcherNoCrit.group(2); + } else { + replacementMap.put(entity, null); + return name; + } + } + + StringBuilder newFormatted = new StringBuilder(); + + try { + int number = Integer.parseInt(numbers); + + if (number > 999 && NotEnoughUpdates.INSTANCE.config.misc.damageIndicatorStyle == 2) { + newFormatted.append(Utils.shortNumberFormat(number, 0)); + } else { + newFormatted.append(NumberFormat.getIntegerInstance().format(number)); + } + } catch (NumberFormatException e) { + replacementMap.put(entity, null); + return name; + } + + if (crit) { + StringBuilder newFormattedCrit = new StringBuilder(); + + int colourIndex = 0; + for (char c : newFormatted.toString().toCharArray()) { + if (c == ',') { + newFormattedCrit.append(EnumChatFormatting.GRAY); + } else { + newFormattedCrit.append(coloursHypixel[colourIndex++ % coloursHypixel.length]); + } + newFormattedCrit.append(c); + } + + newFormatted = newFormattedCrit; + } + + ChatComponentText finalComponent = new ChatComponentText(prefix + newFormatted + suffix); + + replacementMap.put(entity, finalComponent); + return finalComponent; /*if (formatted.startsWith("\u00A7f\u2727")) System.out.println(formatted); @@ -197,5 +205,5 @@ public class DamageCommas { } replacementMap.put(hashCode, null); return name;*/ - } + } } -- cgit