From b423fe438338df643c206d5bb2759b2801c67585 Mon Sep 17 00:00:00 2001 From: Alex 'mcmonkey' Goodwin Date: Thu, 25 Jun 2020 22:22:36 -0700 Subject: [PATCH] RGB Colors --- .../bukkit/BukkitElementProperties.java | 37 +++++++++----- .../denizen/tags/core/TextTagBase.java | 49 +++++++++++++++++++ .../utilities/FormattedTextHelper.java | 16 ++++-- 3 files changed, 87 insertions(+), 15 deletions(-) diff --git a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementProperties.java b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementProperties.java index 1cf3a303e2..a543878eaf 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementProperties.java +++ b/plugin/src/main/java/com/denizenscript/denizen/objects/properties/bukkit/BukkitElementProperties.java @@ -17,7 +17,7 @@ import com.denizenscript.denizencore.objects.properties.Property; import com.denizenscript.denizencore.scripts.ScriptRegistry; import net.md_5.bungee.chat.ComponentSerializer; -import org.bukkit.ChatColor; +import net.md_5.bungee.api.ChatColor; import javax.xml.bind.DatatypeConverter; import java.nio.charset.StandardCharsets; @@ -259,7 +259,7 @@ public static void registerTags() { String colors = ""; for (String line : CoreUtilities.split(object.asString(), '\n')) { output.add(colors + line); - colors = ChatColor.getLastColors(colors + line); + colors = org.bukkit.ChatColor.getLastColors(colors + line); } return output; }); @@ -272,7 +272,7 @@ public static void registerTags() { // Returns the ChatColors used last in an element. // --> PropertyParser.registerTag("last_color", (attribute, object) -> { - return new ElementTag(ChatColor.getLastColors(object.asString())); + return new ElementTag(org.bukkit.ChatColor.getLastColors(object.asString())); }); // <--[tag] @@ -595,7 +595,7 @@ public static void registerTags() { // @group text manipulation // @description // Makes the input text colored by the input color. Equivalent to "" - // Color can be either a color name, or code... that is: ".color[gold]" and ".color[6]" are both valid. + // Color can be a color name, color code, hex, or ColorTag... that is: ".color[gold]", ".color[6]", ".color[#AABB00]", and ".color[co@128,64,0]" are all valid. // // Note that end_format is a magic Denizen tool, and unlike other format codes (like 'bold') does not appear in Spigot's API or the old Minecraft chat system. // As such, it only works when sent through certain Denizen commands (narrate, announce, etc) or mechanisms (like ItemTag.book). @@ -606,22 +606,35 @@ public static void registerTags() { return null; } String colorName = attribute.getContext(1); - ChatColor color = null; + String colorOut = null; if (colorName.length() == 1) { - color = ChatColor.getByChar(colorName.charAt(0)); + ChatColor color = ChatColor.getByChar(colorName.charAt(0)); + if (color != null) { + colorOut = color.toString(); + } + } + else if (colorName.length() == 7 && colorName.startsWith("#")) { + return new ElementTag(ChatColor.COLOR_CHAR + "[color=" + colorName + "]" + object.asString() + ChatColor.COLOR_CHAR + "[reset=f]"); + } + else if (colorName.startsWith("co@")) { + ColorTag color = ColorTag.valueOf(colorName, attribute.context); + String hex = Integer.toHexString(color.getColor().asRGB()); + while (hex.length() < 6) { + hex = "0" + hex; + } + return new ElementTag(ChatColor.COLOR_CHAR + "[color=#" + hex + "]" + object.asString() + ChatColor.COLOR_CHAR + "[reset=f]"); } - if (color == null) { + if (colorOut == null) { try { - color = ChatColor.valueOf(colorName.toUpperCase()); + ChatColor color = ChatColor.of(colorName.toUpperCase()); + colorOut = color.toString(); } catch (IllegalArgumentException ex) { - if (!attribute.hasAlternative()) { - Debug.echoError("Color '" + colorName + "' doesn't exist (for ElementTag.color[...])."); - } + attribute.echoError("Color '" + colorName + "' doesn't exist (for ElementTag.color[...])."); return null; } } - return new ElementTag(color + object.asString() + ChatColor.COLOR_CHAR + "[reset=" + color.getChar() + "]"); + return new ElementTag(colorOut + object.asString() + ChatColor.COLOR_CHAR + "[reset=" + colorOut.substring(1) + "]"); }); // <--[tag] diff --git a/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java b/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java index 20a2cce6e7..b2fa71fc24 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java +++ b/plugin/src/main/java/com/denizenscript/denizen/tags/core/TextTagBase.java @@ -1,5 +1,6 @@ package com.denizenscript.denizen.tags.core; +import com.denizenscript.denizen.objects.ColorTag; import com.denizenscript.denizen.utilities.FormattedTextHelper; import com.denizenscript.denizencore.objects.core.ElementTag; import com.denizenscript.denizencore.objects.core.ListTag; @@ -596,6 +597,54 @@ public void run(ReplaceableTagEvent event) { } }, "&score"); + // <--[tag] + // @attribute <&color[]> + // @returns ElementTag + // @description + // Returns a chat code that makes the following text be the specified color. + // Color can be a color name, color code, hex, or ColorTag... that is: "&color[gold]", "&color[6]", "&color[#AABB00]", and "&color[co@128,64,0]" are all valid. + // --> + TagManager.registerTagHandler(new TagRunnable.RootForm() { + @Override + public void run(ReplaceableTagEvent event) { + Attribute attribute = event.getAttributes(); + if (!attribute.hasContext(1)) { + return; + } + String colorName = attribute.getContext(1); + String colorOut = null; + if (colorName.length() == 1) { + ChatColor color = ChatColor.getByChar(colorName.charAt(0)); + if (color != null) { + colorOut = color.toString(); + } + } + else if (colorName.length() == 7 && colorName.startsWith("#")) { + event.setReplacedObject(new ElementTag(net.md_5.bungee.api.ChatColor.COLOR_CHAR + "[color=" + colorName + "]").getObjectAttribute(attribute.fulfill(1))); + return; + } + else if (colorName.startsWith("co@")) { + ColorTag color = ColorTag.valueOf(colorName, attribute.context); + String hex = Integer.toHexString(color.getColor().asRGB()); + while (hex.length() < 6) { + hex = "0" + hex; + } + event.setReplacedObject(new ElementTag(net.md_5.bungee.api.ChatColor.COLOR_CHAR + "[color=#" + hex + "]").getObjectAttribute(attribute.fulfill(1))); + } + if (colorOut == null) { + try { + ChatColor color = ChatColor.valueOf(colorName.toUpperCase()); + colorOut = color.toString(); + } + catch (IllegalArgumentException ex) { + attribute.echoError("Color '" + colorName + "' doesn't exist (for tag &color[...])."); + return; + } + } + event.setReplacedObject(new ElementTag(colorOut).getObjectAttribute(attribute.fulfill(1))); + } + }, "&color"); + // <--[tag] // @attribute <&0> // @returns ElementTag diff --git a/plugin/src/main/java/com/denizenscript/denizen/utilities/FormattedTextHelper.java b/plugin/src/main/java/com/denizenscript/denizen/utilities/FormattedTextHelper.java index 5779a59fd3..a80a9e83e0 100644 --- a/plugin/src/main/java/com/denizenscript/denizen/utilities/FormattedTextHelper.java +++ b/plugin/src/main/java/com/denizenscript/denizen/utilities/FormattedTextHelper.java @@ -133,6 +133,7 @@ public static BaseComponent[] parse(String str) { List outputList = new ArrayList<>(); int started = 0; TextComponent nextText = new TextComponent(); + TextComponent lastText = new TextComponent(); for (int i = 0; i < chars.length; i++) { if (chars[i] == ChatColor.COLOR_CHAR && i + 1 < chars.length) { char code = chars[i + 1]; @@ -149,7 +150,8 @@ public static BaseComponent[] parse(String str) { if (innardBase.size() == 2) { nextText.setText(nextText.getText() + str.substring(started, i)); outputList.add(nextText); - TextComponent lastText = nextText; + TextComponent doublelasttext = lastText; + lastText = nextText; nextText = copyFormatToNewText(lastText); nextText.setText(""); if (innardType.equals("score") && innardParts.size() == 2) { @@ -264,10 +266,18 @@ else if (subCode == 'n' || subCode == 'N') { else if (subCode == 'o' || subCode == 'O') { nextText.setItalic(false); } + else { + nextText.setColor(doublelasttext.getColor()); + } } else if (innardType.equals("color")) { String colorChar = innardBase.get(1); - nextText.setColor(ChatColor.getByChar(colorChar.charAt(0))); + if (colorChar.length() == 1) { + nextText.setColor(ChatColor.getByChar(colorChar.charAt(0))); + } + else if (colorChar.length() == 7) { + nextText.setColor(ChatColor.of(colorChar)); + } } } i = endBracket; @@ -321,7 +331,7 @@ else if (i + "https://a.".length() < chars.length && chars[i] == 'h' && chars[i String url = str.substring(i, nextSpace); nextText.setText(nextText.getText() + str.substring(started, i)); outputList.add(nextText); - TextComponent lastText = nextText; + lastText = nextText; nextText = new TextComponent(lastText); nextText.setText(""); TextComponent clickableText = new TextComponent(url);