Add TextUtils
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package eu.mikroskeem.utils.text;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class MinecraftText {
|
||||
/**
|
||||
* Validate Minecraft username
|
||||
*
|
||||
* @param name Name to validate
|
||||
* @return Whether name is valid or not
|
||||
*/
|
||||
public static boolean validateUsername(@NotNull String name){
|
||||
if(
|
||||
(name.length() > 16 || name.length() < 3) || /* Check length */
|
||||
!name.matches("^[A-Za-z0-9_]+$") /* Check characters */
|
||||
){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate alternate color codes to Minecraft color codes.
|
||||
*
|
||||
* Note: shittier version of {@link org.bukkit.ChatColor}
|
||||
* @param seq Sequence to replace
|
||||
* @param text Text to do replace on
|
||||
* @return Text with replaced color codes
|
||||
*/
|
||||
@NotNull public static String translateAltColors(char seq, @NotNull String text){
|
||||
return text.replace(seq, '§');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package eu.mikroskeem.utils.text;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TextUtils {
|
||||
/**
|
||||
* Check if string contains whitespace
|
||||
* @param str String to check
|
||||
* @return Whether string contained whitespace or not
|
||||
*/
|
||||
public static boolean hasWhitespace(@NotNull String str){
|
||||
return !str.matches("\\S+");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package eu.mikroskeem.utils.test.text;
|
||||
|
||||
import eu.mikroskeem.utils.text.MinecraftText;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestMinecraftUtils {
|
||||
@Test public void testValidUsername(){
|
||||
Assert.assertTrue(MinecraftText.validateUsername("mikroskeem"));
|
||||
Assert.assertTrue(MinecraftText.validateUsername("abcd"));
|
||||
Assert.assertTrue(MinecraftText.validateUsername("abc_"));
|
||||
}
|
||||
|
||||
@Test public void testInvalidUsername(){
|
||||
Assert.assertFalse(MinecraftText.validateUsername("mikros--"));
|
||||
Assert.assertFalse(MinecraftText.validateUsername("mikr_os--"));
|
||||
Assert.assertFalse(MinecraftText.validateUsername("aa"));
|
||||
Assert.assertFalse(MinecraftText.validateUsername(" "));
|
||||
Assert.assertFalse(MinecraftText.validateUsername(" "));
|
||||
}
|
||||
|
||||
@Test public void testColorReplacing(){
|
||||
Assert.assertEquals("§c§lHello", MinecraftText.translateAltColors('&', "&c&lHello"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package eu.mikroskeem.utils.test.text;
|
||||
|
||||
import eu.mikroskeem.utils.text.TextUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TestTextUtils {
|
||||
@Test public void testSpaceWhitespace(){
|
||||
String text = "wut lel";
|
||||
Assert.assertTrue(TextUtils.hasWhitespace(text));
|
||||
}
|
||||
|
||||
@Test public void testTabWhitespace(){
|
||||
String text = "wut" + '\t' + "lel";
|
||||
Assert.assertTrue(TextUtils.hasWhitespace(text));
|
||||
}
|
||||
|
||||
@Test public void testNoWhitespace(){
|
||||
String text = "wut";
|
||||
Assert.assertFalse(TextUtils.hasWhitespace(text));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user