Skip to content

Commit

Permalink
smarter color/font layering handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed May 2, 2021
1 parent cb7fb8b commit d8e31d6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -592,27 +592,28 @@ public static void registerTags() {
}
}
else if (colorName.length() == 7 && colorName.startsWith("#")) {
return new ElementTag(ChatColor.COLOR_CHAR + "[color=" + colorName + "]" + object.asString() + ChatColor.COLOR_CHAR + "[reset=f]");
return new ElementTag(ChatColor.COLOR_CHAR + "[color=" + colorName + "]" + object.asString() + ChatColor.COLOR_CHAR + "[reset=color]");
}
else if (colorName.startsWith("co@")) {
ColorTag color = ColorTag.valueOf(colorName, attribute.context);
StringBuilder hex = new StringBuilder(Integer.toHexString(color.getColor().asRGB()));
while (hex.length() < 6) {
hex.insert(0, "0");
}
return new ElementTag(ChatColor.COLOR_CHAR + "[color=#" + hex + "]" + object.asString() + ChatColor.COLOR_CHAR + "[reset=f]");
return new ElementTag(ChatColor.COLOR_CHAR + "[color=#" + hex + "]" + object.asString() + ChatColor.COLOR_CHAR + "[reset=color]");
}
if (colorOut == null) {
try {
ChatColor color = ChatColor.of(colorName.toUpperCase());
colorOut = color.toString();
String colorStr = color.toString().replace(String.valueOf(ChatColor.COLOR_CHAR), "").replace("x", "#");
colorOut = ChatColor.COLOR_CHAR + "[color=" + colorStr + "]";
}
catch (IllegalArgumentException ex) {
attribute.echoError("Color '" + colorName + "' doesn't exist (for ElementTag.color[...]).");
return null;
}
}
return new ElementTag(colorOut + object.asString() + ChatColor.COLOR_CHAR + "[reset=" + colorOut.substring(1) + "]");
return new ElementTag(colorOut + object.asString() + ChatColor.COLOR_CHAR + "[reset=color]");
});

// <--[tag]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.event.entity.EntityDamageEvent;

import java.util.ArrayList;
Expand Down Expand Up @@ -92,6 +93,7 @@ else if (!scriptEntry.hasObject("cause")
}
else if (!scriptEntry.hasObject("source_once")
&& arg.matches("source_once")) {
Deprecations.hurtSourceOne.warn(scriptEntry);
scriptEntry.addObject("source_once", new ElementTag(true));
}
else {
Expand Down Expand Up @@ -124,13 +126,8 @@ public void execute(ScriptEntry scriptEntry) {
EntityTag source = scriptEntry.getObjectTag("source");
ElementTag amountElement = scriptEntry.getElement("amount");
ElementTag cause = scriptEntry.getElement("cause");
ElementTag source_once = scriptEntry.getElement("source_once");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), amountElement.debug()
+ ArgumentHelper.debugList("entities", entities)
+ (source_once == null ? "" : source_once.debug())
+ (cause == null ? "" : cause.debug())
+ (source == null ? "" : source.debug()));
Debug.report(scriptEntry, getName(), amountElement, ArgumentHelper.debugList("entities", entities), cause, source);
}
double amount = amountElement.asDouble();
for (EntityTag entity : entities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,20 @@ public static BaseComponent[] parse(String str, ChatColor baseColor) {
return parse(str, baseColor, true);
}

public static int findEndIndexFor(String base, String type, int startAt) {
public static int findEndIndexFor(String base, String startSymbol, String endSymbol, int startAt) {
int layers = 1;
while (true) {
int next = base.indexOf(ChatColor.COLOR_CHAR, startAt);
if (next == -1) {
return -1;
}
if (next + type.length() + 2 >= base.length()) {
if (next + endSymbol.length() >= base.length()) {
return -1;
}
if (base.startsWith("[" + type + "=", next + 1)) {
if (base.startsWith(startSymbol, next + 1)) {
layers++;
}
else if (base.startsWith("[/" + type + "]", next + 1)){
else if (base.startsWith(endSymbol, next + 1)){
layers--;
if (layers == 0) {
return next;
Expand All @@ -227,6 +227,10 @@ else if (base.startsWith("[/" + type + "]", next + 1)){
}
}

public static int findEndIndexFor(String base, String type, int startAt) {
return findEndIndexFor(base, "[" + type + "=", "[/" + type + "]", startAt);
}

public static AsciiMatcher allowedCharCodes = new AsciiMatcher("0123456789abcdefABCDEFklmnorxKLMNORX[");

public static AsciiMatcher hexMatcher = new AsciiMatcher("0123456789abcdefABCDEF");
Expand Down Expand Up @@ -404,7 +408,7 @@ public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cle
char[] chars = str.toCharArray();
int started = 0;
TextComponent nextText = new TextComponent();
TextComponent lastText = new TextComponent();
TextComponent lastText;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ChatColor.COLOR_CHAR && i + 1 < chars.length) {
char code = chars[i + 1];
Expand All @@ -424,7 +428,6 @@ public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cle
if (innardBase.size() == 2) {
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
TextComponent doublelasttext = lastText;
lastText = nextText;
nextText = copyFormatToNewText(lastText);
nextText.setText("");
Expand Down Expand Up @@ -540,23 +543,54 @@ else if (subCode == 'o' || subCode == 'O') {
}
}
else if (innardBase.get(1).equals("font")) {
nextText.setFont(doublelasttext.getFont());
nextText.setFont(base.getFont());
}
else {
nextText.setColor(doublelasttext.getColor());
nextText.setColor(base.getColor());
}
}
else if (innardType.equals("color")) {
String colorChar = innardBase.get(1);
ChatColor color = null;
if (colorChar.length() == 1) {
nextText.setColor(ChatColor.getByChar(colorChar.charAt(0)));
color = ChatColor.getByChar(colorChar.charAt(0));
}
else if (colorChar.length() == 7) {
nextText.setColor(ChatColor.of(colorChar));
color = ChatColor.of(colorChar);
}
else if (Debug.verbose) {
Debug.echoError("Text parse issue: cannot interpret color '" + innardBase.get(1) + "'.");
}
if (color != null) {
int endIndex = findEndIndexFor(str, "[color=", "[reset=color]", i + 1);
if (endIndex == -1) {
nextText.setColor(color);
}
else {
TextComponent colorText = new TextComponent();
colorText.setColor(color);
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), color, false)) {
colorText.addExtra(subComponent);
}
lastText.addExtra(colorText);
endBracket = endIndex + "&[reset=color".length();
}
}
}
else if (innardType.equals("font")) {
nextText.setFont(innardBase.get(1));
int endIndex = findEndIndexFor(str, "[font=", "[reset=font]", i + 1);
if (endIndex == -1) {
nextText.setFont(innardBase.get(1));
}
else {
TextComponent fontText = new TextComponent();
fontText.setFont(innardBase.get(1));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
fontText.addExtra(subComponent);
}
lastText.addExtra(fontText);
endBracket = endIndex + "&[reset=font".length();
}
}
else {
if (Debug.verbose) {
Expand Down

0 comments on commit d8e31d6

Please sign in to comment.