Skip to content

Commit

Permalink
Added unParse
Browse files Browse the repository at this point in the history
  • Loading branch information
divios committed Jan 18, 2022
1 parent d30d3e0 commit a4d76dd
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/github/divios/jtext/JTextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.github.divios.jtext.parsers.HexColorParser;
import io.github.divios.jtext.parsers.PlaceholderApiParser;
import io.github.divios.jtext.parsers.legacyColorsParser;
import io.github.divios.jtext.parsers.legacyColorParser;
import io.github.divios.jtext.parsers.miniTextParser;
import io.github.divios.jtext.wrappers.Template;
import org.bukkit.ChatColor;
Expand All @@ -18,7 +18,7 @@ public class JTextBuilder {

private static final Map<String, Template> defaultValues;

private static final legacyColorsParser legacyParser = new legacyColorsParser();
private static final legacyColorParser legacyParser = new legacyColorParser();
private static final HexColorParser hexParser = new HexColorParser();
private static final PlaceholderApiParser papiParser = new PlaceholderApiParser();
private static final miniTextParser adventureParser = new miniTextParser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ public String parse(String s) {
}

public String unparse(String s) {
char[] array = s.toCharArray();
for (int i = 0; i < array.length - 1; i++) {
if (array[i] == ChatColor.COLOR_CHAR && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(array[i + 1]) != -1) {
array[i] = '&';
array[i + 1] = Character.toLowerCase(array[i + 1]);
}
}
return new String(array);
return s;
}

public @Nullable Template parseAsTemplate(final String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.divios.jtext.parsers;

import net.md_5.bungee.api.ChatColor;

public class legacyColorParser {

public String parse(String s) {
return ChatColor.translateAlternateColorCodes('&', s);
}

public String unparse(String s) {
char[] array = s.toCharArray();
for (int i = 0; i < array.length - 1; i++) {
if (array[i] == ChatColor.COLOR_CHAR && "0123456789AaBbCcDdEeFfKkLlMmNnOoRr".indexOf(array[i + 1]) != -1) {
array[i] = '&';
array[i + 1] = Character.toLowerCase(array[i + 1]);
}
}
return new String(array);
}

}

This file was deleted.

4 changes: 2 additions & 2 deletions src/test/java/parsers/HexColorParserTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package parsers;

import io.github.divios.jtext.parsers.HexColorParser;
import io.github.divios.jtext.parsers.legacyColorsParser;
import io.github.divios.jtext.parsers.legacyColorParser;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -11,7 +11,7 @@ public class HexColorParserTest {
public void testHex() {
String hex = "#fb1239 This is an example #fc1245";
String parsed = new HexColorParser().parse(hex);
String expected = new legacyColorsParser().parse("&x&f&b&1&2&3&9 This is an example &x&f&c&1&2&4&5");
String expected = new legacyColorParser().parse("&x&f&b&1&2&3&9 This is an example &x&f&c&1&2&4&5");

Assert.assertEquals(expected, parsed);
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/parsers/legacyColorParserText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package parsers;

import io.github.divios.jtext.parsers.legacyColorParser;
import org.bukkit.ChatColor;
import org.junit.Assert;
import org.junit.Test;

public class legacyColorParserText {

@Test
public void testParse() {
String toParse = "&7Hello";
String parsed = new legacyColorParser().parse(toParse);
String expected = ChatColor.COLOR_CHAR + "7Hello";

Assert.assertEquals(expected, parsed);
}

@Test
public void testUnParse() {
String toParse = ChatColor.COLOR_CHAR + "7Hello";
String parsed = new legacyColorParser().unparse(toParse);
String expected = "&7Hello";

Assert.assertEquals(expected, parsed);
}

}

0 comments on commit a4d76dd

Please sign in to comment.