Skip to content

Commit

Permalink
book items have a default color of black for text
Browse files Browse the repository at this point in the history
and everything else is still white
  • Loading branch information
mcmonkey4eva committed Dec 22, 2020
1 parent 31ced8a commit 41448a7
Show file tree
Hide file tree
Showing 21 changed files with 74 additions and 59 deletions.
Expand Up @@ -32,6 +32,7 @@
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.NPCSelector;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ChatMessageType;
import org.bukkit.*;
import org.bukkit.advancement.Advancement;
Expand Down Expand Up @@ -3394,7 +3395,7 @@ else if (split.length > 1) {

if (mechanism.matches("action_bar")) {
Deprecations.playerActionBarMech.warn(mechanism.context);
getPlayerEntity().spigot().sendMessage(ChatMessageType.ACTION_BAR, FormattedTextHelper.parse(mechanism.getValue().asString()));
getPlayerEntity().spigot().sendMessage(ChatMessageType.ACTION_BAR, FormattedTextHelper.parse(mechanism.getValue().asString(), ChatColor.WHITE));
}

// <--[mechanism]
Expand Down
Expand Up @@ -378,7 +378,7 @@ public static void registerTags() {
// Inverts <@link tag ElementTag.from_raw_json>.
// -->
PropertyParser.<BukkitElementProperties>registerTag("to_raw_json", (attribute, object) -> {
return new ElementTag(ComponentSerializer.toString(FormattedTextHelper.parse(object.asString())));
return new ElementTag(ComponentSerializer.toString(FormattedTextHelper.parse(object.asString(), ChatColor.WHITE)));
});

// <--[tag]
Expand All @@ -390,7 +390,7 @@ public static void registerTags() {
// Inverts <@link tag ElementTag.to_raw_json>.
// -->
PropertyParser.<BukkitElementProperties>registerTag("from_raw_json", (attribute, object) -> {
return new ElementTag(FormattedTextHelper.stringify(ComponentSerializer.parse(object.asString())));
return new ElementTag(FormattedTextHelper.stringify(ComponentSerializer.parse(object.asString()), ChatColor.WHITE));
});

// <--[tag]
Expand Down
Expand Up @@ -12,6 +12,7 @@
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
import com.denizenscript.denizencore.utilities.Deprecations;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.Material;
Expand Down Expand Up @@ -98,7 +99,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute.startsWith("book_pages")) {
ListTag output = new ListTag();
for (BaseComponent[] page : getBookInfo().spigot().getPages()) {
output.add(FormattedTextHelper.stringify(page));
output.add(FormattedTextHelper.stringify(page, ChatColor.BLACK));
}
return output.getObjectAttribute(attribute.fulfill(1));
}
Expand Down Expand Up @@ -135,7 +136,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
.getObjectAttribute(attribute.fulfill(1));
}
if ((attribute.startsWith("page") || attribute.startsWith("get_page")) && attribute.hasContext(1)) {
return new ElementTag(FormattedTextHelper.stringify(bookInfo.spigot().getPage(attribute.getIntContext(1))))
return new ElementTag(FormattedTextHelper.stringify(bookInfo.spigot().getPage(attribute.getIntContext(1)), ChatColor.BLACK))
.getObjectAttribute(attribute.fulfill(1));
}
if ((attribute.startsWith("raw_page") || attribute.startsWith("get_raw_page")) && attribute.hasContext(1)) {
Expand All @@ -146,7 +147,7 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute.startsWith("pages")) {
ListTag output = new ListTag();
for (BaseComponent[] page : bookInfo.spigot().getPages()) {
output.add(FormattedTextHelper.stringify(page));
output.add(FormattedTextHelper.stringify(page, ChatColor.BLACK));
}
return output.getObjectAttribute(attribute.fulfill(1));
}
Expand Down Expand Up @@ -186,7 +187,7 @@ public MapTag getBookMap() {
List<BaseComponent[]> pages = bookInfo.spigot().getPages();
ListTag pageList = new ListTag(pages.size());
for (BaseComponent[] page : pages) {
pageList.addObject(new ElementTag(FormattedTextHelper.stringify(page)));
pageList.addObject(new ElementTag(FormattedTextHelper.stringify(page, ChatColor.BLACK)));
}
outMap.putObject("pages", pageList);
}
Expand All @@ -204,7 +205,7 @@ public String getOutputString() {
output.append("pages|");
if (bookInfo.hasPages()) {
for (BaseComponent[] page : bookInfo.spigot().getPages()) {
output.append(EscapeTagBase.escape(FormattedTextHelper.stringify(page))).append("|");
output.append(EscapeTagBase.escape(FormattedTextHelper.stringify(page, ChatColor.BLACK))).append("|");
}
}
return output.substring(0, output.length() - 1);
Expand Down Expand Up @@ -245,7 +246,7 @@ public void adjust(Mechanism mechanism) {
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(str)));
newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(str), ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
item.setItemMeta(meta);
Expand Down Expand Up @@ -324,7 +325,7 @@ public void adjust(Mechanism mechanism) {
ListTag pageList = ListTag.getListFor(pages, mechanism.context);
ArrayList<BaseComponent[]> newPages = new ArrayList<>(pageList.size());
for (int i = 0; i < pageList.size(); i++) {
newPages.add(FormattedTextHelper.parse(pageList.get(i)));
newPages.add(FormattedTextHelper.parse(pageList.get(i), ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
}
Expand Down Expand Up @@ -363,7 +364,7 @@ public void adjust(Mechanism mechanism) {
else if (data.get(0).equalsIgnoreCase("pages")) {
ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
for (int i = 1; i < data.size(); i++) {
newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(data.get(i))));
newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(data.get(i)), ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.tags.TagManager;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ChatMessageType;

import java.util.Arrays;
Expand Down Expand Up @@ -138,7 +139,7 @@ public void execute(ScriptEntry scriptEntry) {
context.player = player;
personalText = TagManager.tag(personalText, context);
}
player.getPlayerEntity().spigot().sendMessage(ChatMessageType.ACTION_BAR, FormattedTextHelper.parse(format != null ? format.getFormattedText(personalText, scriptEntry) : personalText));
player.getPlayerEntity().spigot().sendMessage(ChatMessageType.ACTION_BAR, FormattedTextHelper.parse(format != null ? format.getFormattedText(personalText, scriptEntry) : personalText, ChatColor.WHITE));
}
else {
Debug.echoError("Sent actionbar to non-existent player!?");
Expand Down
Expand Up @@ -16,6 +16,7 @@
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.tags.TagManager;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -158,7 +159,7 @@ public void execute(ScriptEntry scriptEntry) {
context.player = player;
personalText = TagManager.tag(personalText, context);
}
BaseComponent[] component = FormattedTextHelper.parse(format != null ? format.getFormattedText(personalText, scriptEntry) : personalText);
BaseComponent[] component = FormattedTextHelper.parse(format != null ? format.getFormattedText(personalText, scriptEntry) : personalText, ChatColor.WHITE);
if (fromId == null) {
player.getPlayerEntity().spigot().sendMessage(component);
}
Expand Down
Expand Up @@ -12,6 +12,7 @@
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

Expand Down Expand Up @@ -136,20 +137,20 @@ public void execute(ScriptEntry scriptEntry) {

// Use Bukkit to broadcast the message to everybody in the server.
if (type == AnnounceType.ALL) {
Denizen.getInstance().getServer().spigot().broadcast(FormattedTextHelper.parse(message));
Denizen.getInstance().getServer().spigot().broadcast(FormattedTextHelper.parse(message, ChatColor.WHITE));
}
else if (type == AnnounceType.TO_OPS) {
for (Player player : Bukkit.getOnlinePlayers()) {
if (player.isOp()) {
player.spigot().sendMessage(FormattedTextHelper.parse(message));
player.spigot().sendMessage(FormattedTextHelper.parse(message, ChatColor.WHITE));
}
}
}
else if (type == AnnounceType.TO_FLAGGED) {
for (Player player : Bukkit.getOnlinePlayers()) {
PlayerTag plTag = new PlayerTag(player);
if (plTag.getFlagTracker().hasFlag(flag.asString())) {
player.spigot().sendMessage(FormattedTextHelper.parse(message));
player.spigot().sendMessage(FormattedTextHelper.parse(message, ChatColor.WHITE));
}
}
}
Expand Down
Expand Up @@ -8,6 +8,7 @@
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagManager;
import com.denizenscript.denizencore.utilities.YamlConfiguration;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.meta.BookMeta;

Expand Down Expand Up @@ -85,7 +86,7 @@ public ItemTag writeBookTo(ItemTag book, TagContext context) {
List<String> pages = getStringList("text");
for (String page : pages) {
page = TagManager.tag(page, context);
bookInfo.spigot().addPage(FormattedTextHelper.parse(page));
bookInfo.spigot().addPage(FormattedTextHelper.parse(page, ChatColor.BLACK));
}
}
book.setItemMeta(bookInfo);
Expand Down
Expand Up @@ -42,10 +42,10 @@ public static String unescape(String input) {
return input.replace("&sc", ";").replace("&lb", "[").replace("&rb", "]").replace("&ss", String.valueOf(ChatColor.COLOR_CHAR)).replace("&amp", "&");
}

public static String stringify(BaseComponent[] components) {
public static String stringify(BaseComponent[] components, ChatColor baseColor) {
StringBuilder builder = new StringBuilder(128 * components.length);
for (BaseComponent component : components) {
builder.append(stringify(component));
builder.append(stringify(component, baseColor));
}
while (builder.toString().endsWith(RESET)) {
builder.setLength(builder.length() - RESET.length());
Expand All @@ -69,7 +69,7 @@ public static String stringifyRGBSpigot(String hex) {
return outColor.toString();
}

public static String stringify(BaseComponent component) {
public static String stringify(BaseComponent component, ChatColor baseColor) {
StringBuilder builder = new StringBuilder(128);
ChatColor color = component.getColorRaw();
if (color != null) {
Expand Down Expand Up @@ -112,7 +112,7 @@ else if (component instanceof TranslatableComponent) {
List<BaseComponent> with = ((TranslatableComponent) component).getWith();
if (with != null) {
for (BaseComponent withComponent : with) {
builder.append(";").append(escape(stringify(withComponent)));
builder.append(";").append(escape(stringify(withComponent, baseColor)));
}
}
builder.append("]");
Expand All @@ -131,7 +131,7 @@ else if (component instanceof ScoreComponent) {
List<BaseComponent> after = component.getExtra();
if (after != null) {
for (BaseComponent afterComponent : after) {
builder.append(stringify(afterComponent));
builder.append(stringify(afterComponent, baseColor));
}
}
if (hasClick) {
Expand All @@ -145,17 +145,15 @@ else if (component instanceof ScoreComponent) {
}
builder.append(RESET);
String output = builder.toString();
while (output.contains(RESET + WHITE)) {
output = output.replace(RESET + WHITE, RESET);
while (output.contains(RESET + baseColor)) {
output = output.replace(RESET + baseColor, RESET);
}
while (output.contains(RESET + RESET)) {
output = output.replace(RESET + RESET, RESET);
}
return output;
}

public static final String WHITE = ChatColor.WHITE.toString();

public static final String RESET = ChatColor.RESET.toString();

public static TextComponent copyFormatToNewText(TextComponent last) {
Expand All @@ -169,11 +167,11 @@ public static TextComponent copyFormatToNewText(TextComponent last) {
return toRet;
}

public static BaseComponent[] parse(String str) {
return parse(str, true);
public static BaseComponent[] parse(String str, ChatColor baseColor) {
return parse(str, baseColor, true);
}

public static BaseComponent[] parse(String str, boolean cleanBase) {
public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cleanBase) {
str = CoreUtilities.clearNBSPs(str);
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
if (firstChar == -1) {
Expand All @@ -189,7 +187,7 @@ public static BaseComponent[] parse(String str, boolean cleanBase) {
base.setStrikethrough(false);
base.setUnderlined(false);
base.setObfuscated(false);
base.setColor(ChatColor.WHITE);
base.setColor(baseColor);
}
root.addExtra(base);
str = str.substring(firstChar);
Expand Down Expand Up @@ -234,7 +232,7 @@ else if (innardType.equals("translate")) {
TranslatableComponent component = new TranslatableComponent();
component.setTranslate(unescape(innardBase.get(1)));
for (String extra : innardParts) {
for (BaseComponent subComponent : parse(unescape(extra), false)) {
for (BaseComponent subComponent : parse(unescape(extra), baseColor, false)) {
component.addWith(subComponent);
}
}
Expand All @@ -251,7 +249,7 @@ else if (innardType.equals("click") && innardParts.size() == 1) {
}
TextComponent clickableText = new TextComponent();
clickableText.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(innardBase.get(1).toUpperCase()), unescape(innardParts.get(0))));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), false)) {
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
clickableText.addExtra(subComponent);
}
lastText.addExtra(clickableText);
Expand Down Expand Up @@ -290,11 +288,11 @@ else if (action == HoverEvent.Action.SHOW_ENTITY) {
hoverValue = new BaseComponent[] {new TextComponent(NMSHandler.getEntityHelper().getRawHoverText(entity.getBukkitEntity()))};
}
else {
hoverValue = parse(unescape(innardParts.get(0)), false);
hoverValue = parse(unescape(innardParts.get(0)), baseColor, false);
}
hoverableText.setHoverEvent(new HoverEvent(action, hoverValue));
}
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), false)) {
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
hoverableText.addExtra(subComponent);
}
lastText.addExtra(hoverableText);
Expand All @@ -311,7 +309,7 @@ else if (innardType.equals("insertion")) {
}
TextComponent insertableText = new TextComponent();
insertableText.setInsertion(unescape(innardBase.get(1)));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), false)) {
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
insertableText.addExtra(subComponent);
}
lastText.addExtra(insertableText);
Expand Down Expand Up @@ -396,7 +394,7 @@ else if (code == 'r' || code == 'R') {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.WHITE);
nextText.setColor(baseColor);
}
else if (code == 'x') {
if (i + 13 >= chars.length) {
Expand Down
Expand Up @@ -4,6 +4,7 @@
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
Expand Down Expand Up @@ -33,14 +34,14 @@ else if (action == HoverEvent.Action.SHOW_ENTITY) {
BaseComponent name = null;
if (entity.getBukkitEntity() != null && entity.getBukkitEntity().isCustomNameVisible()) {
name = new TextComponent();
for (BaseComponent component : FormattedTextHelper.parse(entity.getBukkitEntity().getCustomName())) {
for (BaseComponent component : FormattedTextHelper.parse(entity.getBukkitEntity().getCustomName(), ChatColor.WHITE)) {
name.addExtra(component);
}
}
content = new Entity(entity.getBukkitEntityType().getKey().toString(), entity.getUUID().toString(), name);
}
else {
content = new Text(FormattedTextHelper.parse(FormattedTextHelper.unescape(input)));
content = new Text(FormattedTextHelper.parse(FormattedTextHelper.unescape(input), ChatColor.WHITE));
}
hoverableText.setHoverEvent(new HoverEvent(action, content));
return false;
Expand Down

0 comments on commit 41448a7

Please sign in to comment.