use of net.md_5.bungee.api.chat.BaseComponent in project Denizen-For-Bukkit by DenizenScript.
the class ItemBook method adjust.
@Override
public void adjust(Mechanism mechanism) {
if (mechanism.matches("book_raw_pages")) {
Deprecations.bookItemRawTags.warn(mechanism.context);
BookMeta meta = (BookMeta) item.getItemMeta();
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
newPages.add(ComponentSerializer.parse(EscapeTagBase.unEscape(str)));
}
meta.spigot().setPages(newPages);
item.setItemMeta(meta);
}
// -->
if (mechanism.matches("book_pages")) {
BookMeta meta = (BookMeta) item.getItemMeta();
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
newPages.add(FormattedTextHelper.parse(str, ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
item.setItemMeta(meta);
}
// -->
if (mechanism.matches("book_author")) {
if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
} else {
BookMeta meta = (BookMeta) item.getItemMeta();
meta.setAuthor(mechanism.getValue().asString());
item.setItemMeta(meta);
}
}
// -->
if (mechanism.matches("book_title")) {
if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
} else {
BookMeta meta = (BookMeta) item.getItemMeta();
meta.setTitle(mechanism.getValue().asString());
item.setItemMeta(meta);
}
}
// -->
if (mechanism.matches("book")) {
BookMeta meta = (BookMeta) item.getItemMeta();
if (mechanism.getValue().asString().startsWith("map@")) {
MapTag mapData = mechanism.valueAsType(MapTag.class);
if (mapData == null) {
mechanism.echoError("Book input is an invalid map?");
return;
}
ObjectTag author = mapData.getObject("author");
ObjectTag title = mapData.getObject("title");
if (author != null && title != null) {
if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
} else {
meta.setAuthor(author.toString());
meta.setTitle(title.toString());
}
}
ObjectTag pages = mapData.getObject("pages");
if (pages != null) {
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), ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
}
item.setItemMeta(meta);
return;
}
ListTag data = mechanism.valueAsType(ListTag.class);
if (data.size() < 1) {
mechanism.echoError("Invalid book input!");
return;
}
if (data.size() < 2) {
// Nothing to do, but not necessarily invalid.
return;
}
if (data.size() > 4 && data.get(0).equalsIgnoreCase("author") && data.get(2).equalsIgnoreCase("title")) {
if (!item.getBukkitMaterial().equals(Material.WRITTEN_BOOK)) {
mechanism.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
} else {
meta.setAuthor(EscapeTagBase.unEscape(data.get(1)));
meta.setTitle(EscapeTagBase.unEscape(data.get(3)));
for (int i = 0; i < 4; i++) {
// No .removeRange?
data.removeObject(0);
}
}
}
if (data.get(0).equalsIgnoreCase("raw_pages")) {
ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
for (int i = 1; i < data.size(); i++) {
newPages.add(ComponentSerializer.parse(EscapeTagBase.unEscape(data.get(i))));
}
meta.spigot().setPages(newPages);
} 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)), ChatColor.BLACK));
}
meta.spigot().setPages(newPages);
} else {
mechanism.echoError("Invalid book input!");
}
item.setItemMeta(meta);
}
}
use of net.md_5.bungee.api.chat.BaseComponent in project Denizen-For-Bukkit by DenizenScript.
the class ItemBook method getObjectAttribute.
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// -->
if (attribute.startsWith("book_author") && item.getBukkitMaterial() == Material.WRITTEN_BOOK) {
return new ElementTag(getBookInfo().getAuthor(), true).getObjectAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("book_title") && item.getBukkitMaterial() == Material.WRITTEN_BOOK) {
return new ElementTag(getBookInfo().getTitle(), true).getObjectAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("book_pages")) {
ListTag output = new ListTag();
for (BaseComponent[] page : getBookInfo().spigot().getPages()) {
output.addObject(new ElementTag(FormattedTextHelper.stringify(page, ChatColor.BLACK), true));
}
return output.getObjectAttribute(attribute.fulfill(1));
}
// -->
if (attribute.startsWith("book_map")) {
return getBookMap().getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("book")) {
Deprecations.itemBookTags.warn(attribute.context);
BookMeta bookInfo = (BookMeta) item.getItemMeta();
attribute = attribute.fulfill(1);
if (item.getBukkitMaterial() == Material.WRITTEN_BOOK) {
if (attribute.startsWith("author")) {
return new ElementTag(bookInfo.getAuthor()).getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("title")) {
return new ElementTag(bookInfo.getTitle()).getObjectAttribute(attribute.fulfill(1));
}
}
if (attribute.startsWith("page_count")) {
return new ElementTag(bookInfo.getPageCount()).getObjectAttribute(attribute.fulfill(1));
}
if ((attribute.startsWith("page") || attribute.startsWith("get_page")) && attribute.hasParam()) {
return new ElementTag(FormattedTextHelper.stringify(bookInfo.spigot().getPage(attribute.getIntParam()), ChatColor.BLACK)).getObjectAttribute(attribute.fulfill(1));
}
if ((attribute.startsWith("raw_page") || attribute.startsWith("get_raw_page")) && attribute.hasParam()) {
Deprecations.bookItemRawTags.warn(attribute.context);
return new ElementTag(ComponentSerializer.toString(bookInfo.spigot().getPage(attribute.getIntParam()))).getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("pages")) {
ListTag output = new ListTag();
for (BaseComponent[] page : bookInfo.spigot().getPages()) {
output.add(FormattedTextHelper.stringify(page, ChatColor.BLACK));
}
return output.getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("raw_pages")) {
Deprecations.bookItemRawTags.warn(attribute.context);
ListTag output = new ListTag();
for (BaseComponent[] page : bookInfo.spigot().getPages()) {
output.add(ComponentSerializer.toString(page));
}
return output.getObjectAttribute(attribute.fulfill(1));
}
String output = getOutputString();
if (output == null) {
output = "null";
}
return new ElementTag(output).getObjectAttribute(attribute);
}
return null;
}
use of net.md_5.bungee.api.chat.BaseComponent in project Denizen-For-Bukkit by DenizenScript.
the class FormattedTextHelper method stringify.
public static String stringify(BaseComponent component) {
if (component == null) {
return null;
}
StringBuilder builder = new StringBuilder(128);
ChatColor color = component.getColorRaw();
if (color != null) {
builder.append(color);
}
if (boolNotNull(component.isBoldRaw())) {
builder.append(ChatColor.BOLD);
}
if (boolNotNull(component.isItalicRaw())) {
builder.append(ChatColor.ITALIC);
}
if (boolNotNull(component.isStrikethroughRaw())) {
builder.append(ChatColor.STRIKETHROUGH);
}
if (boolNotNull(component.isUnderlinedRaw())) {
builder.append(ChatColor.UNDERLINE);
}
if (boolNotNull(component.isObfuscatedRaw())) {
builder.append(ChatColor.MAGIC);
}
if (component.getFontRaw() != null) {
builder.append(ChatColor.COLOR_CHAR).append("[font=").append(component.getFontRaw()).append("]");
}
boolean hasInsertion = component.getInsertion() != null;
if (hasInsertion) {
builder.append(ChatColor.COLOR_CHAR).append("[insertion=").append(escape(component.getInsertion())).append("]");
}
boolean hasHover = component.getHoverEvent() != null;
if (hasHover) {
HoverEvent hover = component.getHoverEvent();
builder.append(ChatColor.COLOR_CHAR).append("[hover=").append(hover.getAction().name()).append(";").append(escape(NMSHandler.getInstance().stringForHover(hover))).append("]");
}
boolean hasClick = component.getClickEvent() != null;
if (hasClick) {
ClickEvent click = component.getClickEvent();
builder.append(ChatColor.COLOR_CHAR).append("[click=").append(click.getAction().name()).append(";").append(escape(click.getValue())).append("]");
}
if (component instanceof TextComponent) {
builder.append(((TextComponent) component).getText());
} else if (component instanceof TranslatableComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[translate=").append(escape(((TranslatableComponent) component).getTranslate()));
List<BaseComponent> with = ((TranslatableComponent) component).getWith();
if (with != null) {
for (BaseComponent withComponent : with) {
builder.append(";").append(escape(stringify(withComponent)));
}
}
builder.append("]");
} else if (component instanceof SelectorComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[selector=").append(escape(((SelectorComponent) component).getSelector())).append("]");
} else if (component instanceof KeybindComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[keybind=").append(escape(((KeybindComponent) component).getKeybind())).append("]");
} else if (component instanceof ScoreComponent) {
builder.append(ChatColor.COLOR_CHAR).append("[score=").append(escape(((ScoreComponent) component).getName())).append(";").append(escape(((ScoreComponent) component).getObjective())).append(";").append(escape(((ScoreComponent) component).getValue())).append("]");
}
List<BaseComponent> after = component.getExtra();
if (after != null) {
for (BaseComponent afterComponent : after) {
builder.append(stringify(afterComponent));
}
}
if (hasClick) {
builder.append(ChatColor.COLOR_CHAR + "[/click]");
}
if (hasHover) {
builder.append(ChatColor.COLOR_CHAR + "[/hover]");
}
if (hasInsertion) {
builder.append(ChatColor.COLOR_CHAR + "[/insertion]");
}
builder.append(RESET);
String output = builder.toString();
return cleanRedundantCodes(output);
}
use of net.md_5.bungee.api.chat.BaseComponent in project Denizen-For-Bukkit by DenizenScript.
the class FormattedTextHelper method parse.
public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cleanBase) {
if (str == null) {
return null;
}
str = CoreUtilities.clearNBSPs(str);
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
if (firstChar == -1) {
if (str.contains("://")) {
firstChar = 0;
} else {
TextComponent base = new TextComponent();
// This is for compat with how Spigot does parsing of plaintext.
base.addExtra(new TextComponent(str));
return new BaseComponent[] { base };
}
}
str = cleanRedundantCodes(str);
if (cleanBase && str.length() < 512 && !str.contains(ChatColor.COLOR_CHAR + "[") && !str.contains("://")) {
return parseSimpleColorsOnly(str);
}
TextComponent root = new TextComponent();
TextComponent base = new TextComponent();
if (cleanBase) {
base.setBold(false);
base.setItalic(false);
base.setStrikethrough(false);
base.setUnderlined(false);
base.setObfuscated(false);
base.setColor(baseColor);
if (firstChar > 0) {
root.addExtra(new TextComponent(str.substring(0, firstChar)));
}
} else {
base.setText(str.substring(0, firstChar));
}
root.addExtra(base);
str = str.substring(firstChar);
char[] chars = str.toCharArray();
int started = 0;
TextComponent nextText = 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];
if (!allowedCharCodes.isMatch(code)) {
continue;
}
if (code == '[') {
int endBracket = str.indexOf(']', i + 2);
if (endBracket == -1) {
continue;
}
String innards = str.substring(i + 2, endBracket);
List<String> innardParts = CoreUtilities.split(innards, ';');
List<String> innardBase = CoreUtilities.split(innardParts.get(0), '=', 2);
innardParts.remove(0);
String innardType = CoreUtilities.toLowerCase(innardBase.get(0));
if (innardBase.size() == 2) {
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = copyFormatToNewText(lastText);
nextText.setText("");
if (innardType.equals("score") && innardParts.size() == 2) {
ScoreComponent component = new ScoreComponent(unescape(innardBase.get(1)), unescape(innardParts.get(0)), unescape(innardParts.get(1)));
lastText.addExtra(component);
} else if (innardType.equals("keybind")) {
KeybindComponent component = new KeybindComponent();
component.setKeybind(unescape(innardBase.get(1)));
lastText.addExtra(component);
} else if (innardType.equals("selector")) {
SelectorComponent component = new SelectorComponent(unescape(innardBase.get(1)));
lastText.addExtra(component);
} 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), baseColor, false)) {
component.addWith(subComponent);
}
}
lastText.addExtra(component);
} else if (innardType.equals("click") && innardParts.size() == 1) {
int endIndex = findEndIndexFor(str, "click", i + 5);
if (endIndex == -1) {
continue;
}
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), baseColor, false)) {
clickableText.addExtra(subComponent);
}
lastText.addExtra(clickableText);
endBracket = endIndex + "&[/click".length();
} else if (innardType.equals("hover")) {
int endIndex = findEndIndexFor(str, "hover", i + 5);
if (endIndex == -1) {
continue;
}
TextComponent hoverableText = new TextComponent();
HoverEvent.Action action = HoverEvent.Action.valueOf(innardBase.get(1).toUpperCase());
if (HoverFormatHelper.processHoverInput(action, hoverableText, innardParts.get(0))) {
continue;
}
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
hoverableText.addExtra(subComponent);
}
lastText.addExtra(hoverableText);
endBracket = endIndex + "&[/hover".length();
} else if (innardType.equals("insertion")) {
int endIndex = str.indexOf(ChatColor.COLOR_CHAR + "[/insertion]", i);
int backupEndIndex = str.indexOf(ChatColor.COLOR_CHAR + "[insertion=", i + 5);
if (backupEndIndex > 0 && backupEndIndex < endIndex) {
endIndex = backupEndIndex;
}
if (endIndex == -1) {
continue;
}
TextComponent insertableText = new TextComponent();
insertableText.setInsertion(unescape(innardBase.get(1)));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
insertableText.addExtra(subComponent);
}
lastText.addExtra(insertableText);
endBracket = endIndex + "&[/insertion".length();
} else if (innardType.equals("reset")) {
if (innardBase.get(1).length() == 1) {
char subCode = innardBase.get(1).charAt(0);
if (subCode == 'k' || subCode == 'K') {
nextText.setObfuscated(false);
} else if (subCode == 'l' || subCode == 'L') {
nextText.setBold(false);
} else if (subCode == 'm' || subCode == 'M') {
nextText.setStrikethrough(false);
} else if (subCode == 'n' || subCode == 'N') {
nextText.setUnderlined(false);
} else if (subCode == 'o' || subCode == 'O') {
nextText.setItalic(false);
}
} else if (innardBase.get(1).equals("font")) {
nextText.setFont(base.getFont());
} else {
nextText.setColor(base.getColor());
}
} else if (innardType.equals("color")) {
String colorChar = innardBase.get(1);
ChatColor color = null;
if (colorChar.length() == 1) {
color = ChatColor.getByChar(colorChar.charAt(0));
} else if (colorChar.length() == 7) {
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")) {
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) {
Debug.echoError("Text parse issue: cannot interpret type '" + innardType + "' with " + innardParts.size() + " parts.");
}
}
}
i = endBracket;
started = endBracket + 1;
continue;
} else if (code == 'r' || code == 'R') {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(baseColor);
} else if (colorCodesOrReset.isMatch(code)) {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.getByChar(code));
} else if (code == 'x') {
if (i + 13 >= chars.length) {
continue;
}
StringBuilder color = new StringBuilder(12);
color.append("#");
for (int c = 1; c <= 6; c++) {
if (chars[i + c * 2] != ChatColor.COLOR_CHAR) {
color = null;
break;
}
char hexPart = chars[i + 1 + c * 2];
if (!hexMatcher.isMatch(hexPart)) {
color = null;
break;
}
color.append(hexPart);
}
if (color == null) {
continue;
}
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.of(color.toString()));
i += 13;
started = i + 1;
continue;
} else {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
if (code == 'k' || code == 'K') {
nextText.setObfuscated(true);
} else if (code == 'l' || code == 'L') {
nextText.setBold(true);
} else if (code == 'm' || code == 'M') {
nextText.setStrikethrough(true);
} else if (code == 'n' || code == 'N') {
nextText.setUnderlined(true);
} else if (code == 'o' || code == 'O') {
nextText.setItalic(true);
}
}
i++;
started = i + 1;
} else if (i + "https://a.".length() < chars.length && chars[i] == 'h' && chars[i + 1] == 't' && chars[i + 2] == 't' && chars[i + 3] == 'p') {
String subStr = str.substring(i, i + "https://a.".length());
if (subStr.startsWith("https://") || subStr.startsWith("http://")) {
int nextSpace = CoreUtilities.indexOfAny(str, i, ' ', '\t', '\n');
if (nextSpace == -1) {
nextSpace = str.length();
}
String url = str.substring(i, nextSpace);
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = new TextComponent(lastText);
nextText.setText("");
TextComponent clickableText = new TextComponent(url);
clickableText.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
lastText.addExtra(clickableText);
i = nextSpace;
started = nextSpace;
continue;
}
}
}
nextText.setText(nextText.getText() + str.substring(started));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
return new BaseComponent[] { cleanBase ? root : base };
}
use of net.md_5.bungee.api.chat.BaseComponent in project Denizen-For-Bukkit by DenizenScript.
the class HoverFormatHelper method processHoverInput.
public static boolean processHoverInput(HoverEvent.Action action, TextComponent hoverableText, String input) {
Content content;
if (action == HoverEvent.Action.SHOW_ITEM) {
ItemTag item = ItemTag.valueOf(FormattedTextHelper.unescape(input), CoreUtilities.noDebugContext);
if (item == null) {
return true;
}
// TODO: Why is there not a direct conversion method for Spigot ItemStack -> BungeeChat Item?
String itemNbt = NMSHandler.getItemHelper().getRawHoverText(item.getItemStack());
content = new Item(item.getBukkitMaterial().getKey().toString(), item.getAmount(), net.md_5.bungee.api.chat.ItemTag.ofNbt(itemNbt));
} else if (action == HoverEvent.Action.SHOW_ENTITY) {
EntityTag entity = EntityTag.valueOf(FormattedTextHelper.unescape(input), CoreUtilities.basicContext);
if (entity == null) {
return true;
}
BaseComponent name = null;
if (entity.getBukkitEntity() != null && entity.getBukkitEntity().isCustomNameVisible()) {
name = new TextComponent();
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), ChatColor.WHITE));
}
hoverableText.setHoverEvent(new HoverEvent(action, content));
return false;
}
Aggregations