Skip to content

Commit

Permalink
RGB Colors
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 26, 2020
1 parent bb3d3cb commit b423fe4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 15 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
Expand All @@ -272,7 +272,7 @@ public static void registerTags() {
// Returns the ChatColors used last in an element.
// -->
PropertyParser.<BukkitElementProperties>registerTag("last_color", (attribute, object) -> {
return new ElementTag(ChatColor.getLastColors(object.asString()));
return new ElementTag(org.bukkit.ChatColor.getLastColors(object.asString()));
});

// <--[tag]
Expand Down Expand Up @@ -595,7 +595,7 @@ public static void registerTags() {
// @group text manipulation
// @description
// Makes the input text colored by the input color. Equivalent to "<COLOR><ELEMENT_HERE><COLOR.end_format>"
// 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).
Expand All @@ -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]
Expand Down
@@ -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;
Expand Down Expand Up @@ -596,6 +597,54 @@ public void run(ReplaceableTagEvent event) {
}
}, "&score");

// <--[tag]
// @attribute <&color[<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
Expand Down
Expand Up @@ -133,6 +133,7 @@ public static BaseComponent[] parse(String str) {
List<BaseComponent> 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];
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b423fe4

Please sign in to comment.