Created
January 19, 2019 09:42
-
-
Save Spotlightsrule/edcdf02747d6c628fdaf558557297546 to your computer and use it in GitHub Desktop.
Transform Bukkit formatting codes to lowercase to avoid console display problems. USAGE: BukkitFormatting.fixCodesToLowerCase(strIn);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class BukkitFormatting { | |
public static String fixCodesToLowerCase(String codeStr){ | |
//Create a StringBuilder to hold the input string | |
StringBuilder codeSB = new StringBuilder(codeStr); | |
//Set the pattern to use on the string (section sign + color code) | |
Pattern codePtn = Pattern.compile("\\u00A7[A-FK-OR]", Pattern.CASE_INSENSITIVE); | |
//Create the matcher to use on the input string | |
Matcher codeMatch = codePtn.matcher(codeSB); | |
//Loop through the matcher finds | |
while(codeMatch.find()){ | |
//Transform the finds to lower case, but leave everything else alone | |
String tmpStr = codeSB.substring(codeMatch.start(), codeMatch.end()).toLowerCase(); | |
//Replace the entries in the substring | |
codeSB = codeSB.replace(codeMatch.start(), codeMatch.end(), tmpStr); | |
} | |
//Return the fixed string | |
return codeSB.toString(); | |
} | |
public static void main(String[] args){ | |
//TESTS | |
String secSign = "\u00A7"; | |
String msgIn = (secSign + "AThis " + secSign + "BIs " + secSign + "CA " + secSign + "DTest " + secSign + "EOf " + secSign + "FUppercase " + secSign + "KSEECRET " + secSign + "LBukkit " + secSign + "MColor " + secSign + "NAnd " + secSign + "OFormat " + secSign + "RCodes"); | |
System.out.println(BukkitFormatting.fixCodesToLowerCase(msgIn)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment