Skip to content

Commit

Permalink
7.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dags- committed May 1, 2018
1 parent 82f02ea commit 1e0c814
Show file tree
Hide file tree
Showing 9 changed files with 593 additions and 496 deletions.
10 changes: 6 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import org.apache.tools.ant.filters.ReplaceTokens

apply plugin: 'java'

group 'me.dags'
version '0.1'
def spongeAPI = '5.1.0'
version '1.0'
def spongeAPI = '7.1.0'
def spongeChannel = 'SNAPSHOT'

repositories {
Expand All @@ -13,8 +15,8 @@ repositories {
}

dependencies {
apply plugin: 'java'
compileOnly "org.spongepowered:spongeapi:${spongeAPI}-$spongeChannel"
compile "org.spongepowered:spongeapi:${spongeAPI}-$spongeChannel"
runtime "org.spongepowered:spongeapi:${spongeAPI}-$spongeChannel"
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Thu Mar 16 09:16:52 GMT 2017
#Tue Oct 03 08:21:06 BST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
143 changes: 2 additions & 141 deletions src/main/java/me/dags/textmu/MUBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import org.spongepowered.api.text.LiteralText;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.TextRepresentable;

import java.lang.reflect.Array;
import java.util.Iterator;
import java.util.Map;

/**
* @author dags <dags@dags.me>
Expand All @@ -15,24 +10,18 @@ class MUBuilder {

private final LiteralText.Builder builder = (LiteralText.Builder) LiteralText.builder();
private final StringBuilder stringBuilder = new StringBuilder(128);
private final StringBuilder argBuilder = new StringBuilder(16);
private final MarkupSpec markupSpec;
private final Map<?, ?> arguments;
private final boolean argsEnabled;

private boolean buildingArg = false;
private boolean escaped = false;
private boolean quoted = false;
private boolean empty = true;

MUBuilder(MarkupSpec markupSpec, Map<?, ?> arguments) {
this.arguments = arguments;
MUBuilder(MarkupSpec markupSpec) {
this.markupSpec = markupSpec;
this.argsEnabled = arguments != MUParser.EMPTY;
}

boolean isEmpty() {
return empty && stringBuilder.length() == 0 && argBuilder.length() == 0;
return empty && stringBuilder.length() == 0;
}

boolean isRaw() {
Expand All @@ -59,17 +48,6 @@ MUBuilder append(char c) {
if (escaped || quoted) {
stringBuilder.append(c);
escaped = false;
} else if (buildingArg) {
if (c == '}') {
String argument = argBuilder.toString();
argBuilder.setLength(0);
buildingArg = false;
appendArg(argument);
} else {
argBuilder.append(c);
}
} else if (argsEnabled && c == '{') {
buildingArg = true;
} else {
stringBuilder.append(c);
escaped = false;
Expand All @@ -79,11 +57,6 @@ MUBuilder append(char c) {
}

MUBuilder append(Text other) {
if (buildingArg) {
stringBuilder.append(argBuilder);
argBuilder.setLength(0);
buildingArg = false;
}
appendString();
builder.append(other);
empty = false;
Expand All @@ -103,118 +76,6 @@ private MUBuilder appendString() {
return this;
}

private void appendArg(String name) {
int splitPoint = name.indexOf(":");
int joinPoint = name.indexOf(":", 1 + splitPoint);

if (splitPoint < 0) {
Object value = arguments.get(name);
appendObject(value);
} else if (splitPoint == 0) {
String templateKey = name.substring(splitPoint + 1);
appendTemplate(templateKey);
} else if (joinPoint > 0) {
String valueKey = name.substring(0, splitPoint);
String templateKey = name.substring(splitPoint + 1, joinPoint);
String separator = name.substring(joinPoint + 1);
appendTemplate(templateKey, valueKey, separator);
} else {
String valueKey = name.substring(0, splitPoint);
String templateKey = name.substring(splitPoint + 1);
appendTemplate(templateKey, valueKey, "");
}
}

private void appendTemplate(String templateKey) {
Object template = arguments.get(templateKey);
if (template != null) {
if (MarkupTemplate.class.isInstance(template)) {
Text text = MarkupTemplate.class.cast(template).renderTemplate(arguments);
append(text);
} else if (String.class.isInstance(template)) {
Text text = markupSpec.template((String) template).renderTemplate(arguments);
append(text);
}
}
}

private void appendTemplate(String templateKey, String valueKey, String separator) {
Object templ = arguments.get(templateKey);
Object value = arguments.get(valueKey);

if (value != null && templ != null) {
MarkupTemplate template = null;
if (MarkupTemplate.class.isInstance(templ)) {
template = MarkupTemplate.class.cast(templ);
} else if (String.class.isInstance(templ)) {
template = markupSpec.template((String) templ);
}

if (template != null) {
if (Map.class.isInstance(value)) {
appendMap(template, value, separator);
} else if (Iterable.class.isInstance(value)) {
appendIterable(template, value, separator);
} else if (value.getClass().isArray()) {
appendArray(template, value, separator);
} else {
Text text = template.applier().withUnchecked(arguments).with(value).render();
append(text);
}
}
}
}

private void appendObject(Object value) {
if (value != null) {
if (MarkupTemplate.class.isInstance(value)) {
Text text = MarkupTemplate.class.cast(value).renderTemplate(arguments);
append(text);
} else if (TextRepresentable.class.isInstance(value)) {
TextRepresentable text = TextRepresentable.class.cast(value);
append(text.toText());
} else {
stringBuilder.append(value);
}
}
}

private void appendIterable(MarkupTemplate template, Object value, String separator) {
MarkupTemplate.Applier applier = template.applier().withUnchecked(arguments);
Iterator iterator = Iterable.class.cast(value).iterator();
while (iterator.hasNext()) {
Object child = iterator.next();
Text one = applier.with(child).render();
Text text = !separator.isEmpty() && iterator.hasNext() ? Text.of(one, separator) : one;
append(text);
}
}

private void appendArray(MarkupTemplate template, Object value, String separator) {
MarkupTemplate.Applier applier = template.applier().withUnchecked(arguments);
int length = Array.getLength(value);
for (int i = 0; i < length; i++) {
Object child = Array.get(value, i);
if (child != null) {
Text one = applier.with(child).render();
Text text = !separator.isEmpty() && i + 1 < length ? Text.of(one, separator) : one;
append(text);
}
}
}

private void appendMap(MarkupTemplate template, Object value, String separator) {
MarkupTemplate.Applier applier = template.applier().withUnchecked(arguments);
Map<?, ?> map = Map.class.cast(value);
Iterator<? extends Map.Entry<?, ?>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<?, ?> entry = iterator.next();
Text one = applier.with(entry).render();
Text text = !separator.isEmpty() && iterator.hasNext() ? Text.of(one, separator) : one;
append(text);
}
}

String string() {
return stringBuilder.toString();
}
Expand Down
70 changes: 39 additions & 31 deletions src/main/java/me/dags/textmu/MUParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class MUParam {

abstract void apply(Text.Builder builder);

private static final String URL = "((ht|f)tp(s?):\\/\\/|www\\.)(([\\w\\-]+\\.){1,}?([\\w\\-.~]+\\/?)*[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)";
private static final String URL = "^((ht|f)tp(s?)://|www\\.)?([\\da-z-]+)(\\.([\\da-z-]+))*((\\.[a-z]{2,6})+|:[0-9]+)(/[\\p{Alnum}.,%_=?&#\\-+()\\[\\]\\*$~@!:/{};']*)*?$";
private static final Pattern URL_PATTERN = Pattern.compile(URL, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
static final MUParam EMPTY = new MUParam() {
@Override
Expand All @@ -42,44 +42,52 @@ void apply(Text.Builder builder) {}
static Map<String, TextColor> colors() {
if (MUParam.colors.isEmpty()) {
Map<String, TextColor> colors = new HashMap<>();
Sponge.getRegistry().getAllOf(TextColor.class).stream()
.filter(color -> color != TextColors.RESET && color != TextColors.NONE)
try {
Sponge.getRegistry().getAllOf(TextColor.class).stream()
.filter(color -> color != TextColors.RESET && color != TextColors.NONE)
.forEach(color -> colors.put(color.getId().toLowerCase(), color));

colors.put("a", TextColors.GREEN);
colors.put("b", TextColors.AQUA);
colors.put("c", TextColors.AQUA);
colors.put("d", TextColors.RED);
colors.put("e", TextColors.LIGHT_PURPLE);
colors.put("f", TextColors.YELLOW);
colors.put("0", TextColors.BLACK);
colors.put("1", TextColors.DARK_BLUE);
colors.put("2", TextColors.DARK_GREEN);
colors.put("3", TextColors.DARK_AQUA);
colors.put("4", TextColors.DARK_RED);
colors.put("5", TextColors.DARK_PURPLE);
colors.put("6", TextColors.GOLD);
colors.put("7", TextColors.GRAY);
colors.put("8", TextColors.DARK_GRAY);
colors.put("9", TextColors.BLUE);
MUParam.colors = colors;
} catch (IllegalStateException e) {
//e.printStackTrace();
} finally {
colors.put("a", TextColors.GREEN);
colors.put("b", TextColors.AQUA);
colors.put("c", TextColors.AQUA);
colors.put("d", TextColors.RED);
colors.put("e", TextColors.LIGHT_PURPLE);
colors.put("f", TextColors.YELLOW);
colors.put("0", TextColors.BLACK);
colors.put("1", TextColors.DARK_BLUE);
colors.put("2", TextColors.DARK_GREEN);
colors.put("3", TextColors.DARK_AQUA);
colors.put("4", TextColors.DARK_RED);
colors.put("5", TextColors.DARK_PURPLE);
colors.put("6", TextColors.GOLD);
colors.put("7", TextColors.GRAY);
colors.put("8", TextColors.DARK_GRAY);
colors.put("9", TextColors.BLUE);
MUParam.colors = colors;
}
}
return MUParam.colors;
}

static Map<String, TextStyle.Base> styles() {
if (MUParam.styles.isEmpty()) {
Map<String, TextStyle.Base> styles = new HashMap<>();
Sponge.getRegistry().getAllOf(TextStyle.Base.class).stream()
.filter(style -> style != TextStyles.NONE && style != TextStyles.RESET)
.forEach(style -> styles.put(style.getId().toLowerCase(), style));

styles.put("k", TextStyles.OBFUSCATED);
styles.put("l", TextStyles.BOLD);
styles.put("m", TextStyles.STRIKETHROUGH);
styles.put("n", TextStyles.UNDERLINE);
styles.put("o", TextStyles.ITALIC);
MUParam.styles = styles;
try {
Sponge.getRegistry().getAllOf(TextStyle.Base.class).stream()
.filter(style -> style != TextStyles.NONE && style != TextStyles.RESET)
.forEach(style -> styles.put(style.getId().toLowerCase(), style));
} catch (IllegalStateException e) {
//e.printStackTrace();
} finally {
styles.put("k", TextStyles.OBFUSCATED);
styles.put("l", TextStyles.BOLD);
styles.put("m", TextStyles.STRIKETHROUGH);
styles.put("n", TextStyles.UNDERLINE);
styles.put("o", TextStyles.ITALIC);
MUParam.styles = styles;
}
}
return MUParam.styles;
}
Expand Down
Loading

0 comments on commit 1e0c814

Please sign in to comment.