Skip to content

Commit

Permalink
item book property: MapTag!
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 1, 2020
1 parent c8a3e31 commit 2d1a590
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -906,18 +906,6 @@ public ListTag asList() {
return new ListTag(values);
}

public ListTag asList(String prefix) {
if (values == null) {
ListTag toReturn = new ListTag();
toReturn.setPrefix(prefix);
if (size != 0) {
toReturn.add(firstValue);
}
return toReturn;
}
return new ListTag(values, prefix);
}

/**
* Returns a String value of the value in the specified index. If
* the value doesn't exist, "" is returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
Expand All @@ -17,6 +18,7 @@
import org.bukkit.inventory.meta.BookMeta;

import java.util.ArrayList;
import java.util.List;

public class ItemBook implements Property {

Expand All @@ -35,7 +37,7 @@ public static ItemBook getFrom(ObjectTag _item) {
}

public static final String[] handledTags = new String[] {
"book", "book_author", "book_title", "book_pages"
"book", "book_author", "book_title", "book_pages", "book_map"
};

public static final String[] handledMechs = new String[] {
Expand Down Expand Up @@ -101,29 +103,38 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
return output.getObjectAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <ItemTag.book_map>
// @returns MapTag
// @mechanism ItemTag.book
// @group properties
// @description
// Returns a MapTag of data about the book, with keys "pages" (a ListTag), and when available, "author" and "title".
// -->
if (attribute.startsWith("book_map")) {
return getBookMap().getObjectAttribute(attribute.fulfill(1));
}

if (attribute.startsWith("book")) {
Deprecations.itemBookTags.warn(attribute.context);
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
attribute = attribute.fulfill(1);

if (item.getItemStack().getType() == Material.WRITTEN_BOOK) {
if (attribute.startsWith("author")) {
Deprecations.itemBookTags.warn(attribute.context);
return new ElementTag(bookInfo.getAuthor())
.getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("title")) {
Deprecations.itemBookTags.warn(attribute.context);
return new ElementTag(bookInfo.getTitle())
.getObjectAttribute(attribute.fulfill(1));
}
}
if (attribute.startsWith("page_count")) {
Deprecations.itemBookTags.warn(attribute.context);
return new ElementTag(bookInfo.getPageCount())
.getObjectAttribute(attribute.fulfill(1));
}
if ((attribute.startsWith("page") || attribute.startsWith("get_page")) && attribute.hasContext(1)) {
Deprecations.itemBookTags.warn(attribute.context);
return new ElementTag(FormattedTextHelper.stringify(bookInfo.spigot().getPage(attribute.getIntContext(1))))
.getObjectAttribute(attribute.fulfill(1));
}
Expand All @@ -133,7 +144,6 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
.getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("pages")) {
Deprecations.itemBookTags.warn(attribute.context);
ListTag output = new ListTag();
for (BaseComponent[] page : bookInfo.spigot().getPages()) {
output.add(FormattedTextHelper.stringify(page));
Expand All @@ -148,19 +158,6 @@ public ObjectTag getObjectAttribute(Attribute attribute) {
}
return output.getObjectAttribute(attribute.fulfill(1));
}

// <--[tag]
// @attribute <ItemTag.book>
// @returns ElementTag
// @mechanism ItemTag.book
// @group properties
// @description
// Returns full information on the book item, in the format
// author|AUTHOR|title|TITLE|raw_pages|PAGE_ONE|PAGE_TWO|...
// or as raw_pages|PAGE_ONE|PAGE_TWO|...
// Pre-escaped to prevent issues.
// See <@link language Property Escaping>
// -->
String output = getOutputString();
if (output == null) {
output = "null";
Expand All @@ -174,18 +171,33 @@ public ObjectTag getObjectAttribute(Attribute attribute) {

@Override
public String getPropertyString() {
String output = getOutputString();
if (output.equals("raw_pages")) {
return null;
MapTag map = getBookMap();
return map.toString();
}

public MapTag getBookMap() {
MapTag outMap = new MapTag();
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK) && bookInfo.hasAuthor() && bookInfo.hasTitle()) {
outMap.putObject("author", new ElementTag(bookInfo.getAuthor()));
outMap.putObject("author", new ElementTag(bookInfo.getTitle()));
}
return output;
if (bookInfo.hasPages()) {
List<BaseComponent[]> pages = bookInfo.spigot().getPages();
ListTag pageList = new ListTag(pages.size());
for (BaseComponent[] page : pages) {
pageList.addObject(new ElementTag(FormattedTextHelper.stringify(page)));
}
outMap.putObject("pages", pageList);
}
return outMap;
}

@Deprecated
public String getOutputString() {
StringBuilder output = new StringBuilder();
StringBuilder output = new StringBuilder(128);
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK)
&& bookInfo.hasAuthor() && bookInfo.hasTitle()) {
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK) && bookInfo.hasAuthor() && bookInfo.hasTitle()) {
output.append("author|").append(EscapeTagBase.escape(bookInfo.getAuthor()))
.append("|title|").append(EscapeTagBase.escape(bookInfo.getTitle())).append("|");
}
Expand Down Expand Up @@ -282,10 +294,10 @@ public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name book
// @input ElementTag
// @input MapTag
// @description
// Changes the information on a book item.
// See <@link language Property Escaping>
// Should have keys "pages" (a ListTag), and optionally "title" and "author".
// @tags
// <ItemTag.is_book>
// <ItemTag.book_title>
Expand All @@ -294,6 +306,31 @@ public void adjust(Mechanism mechanism) {
// -->
if (mechanism.matches("book")) {
BookMeta meta = (BookMeta) item.getItemStack().getItemMeta();
if (mechanism.getValue().asString().startsWith("map@")) {
MapTag mapData = mechanism.valueAsType(MapTag.class);
ObjectTag author = mapData.getObject("author");
ObjectTag title = mapData.getObject("title");
if (author != null && title != null) {
if (!item.getItemStack().getType().equals(Material.WRITTEN_BOOK)) {
Debug.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 = 1; i < pageList.size(); i++) {
newPages.add(FormattedTextHelper.parse(pageList.get(i)));
}
meta.spigot().setPages(newPages);
}
item.getItemStack().setItemMeta(meta);
return;
}
ListTag data = mechanism.valueAsType(ListTag.class);
if (data.size() < 1) {
Debug.echoError("Invalid book input!");
Expand All @@ -312,19 +349,19 @@ public void adjust(Mechanism mechanism) {
meta.setAuthor(EscapeTagBase.unEscape(data.get(1)));
meta.setTitle(EscapeTagBase.unEscape(data.get(3)));
for (int i = 0; i < 4; i++) {
data.remove(0); // No .removeRange?
data.removeObject(0); // No .removeRange?
}
}
}
if (data.get(0).equalsIgnoreCase("raw_pages")) {
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
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<>();
ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
for (int i = 1; i < data.size(); i++) {
newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(data.get(i))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ public static String stringify(BaseComponent component) {
ClickEvent click = component.getClickEvent();
builder.append(ChatColor.COLOR_CHAR).append("[click=").append(click.getAction().name()).append(";").append(escape(click.getValue())).append("]");
}
if (component instanceof TranslatableComponent) {
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) {
Expand All @@ -121,9 +124,6 @@ else if (component instanceof ScoreComponent) {
.append(";").append(escape(((ScoreComponent) component).getObjective()))
.append(";").append(escape(((ScoreComponent) component).getValue())).append("]");
}
else if (component instanceof TextComponent) {
builder.append(((TextComponent) component).getText());
}
List<BaseComponent> after = component.getExtra();
if (after != null) {
for (BaseComponent afterComponent : after) {
Expand All @@ -142,7 +142,7 @@ else if (component instanceof TextComponent) {
builder.append(RESET);
String output = builder.toString();
while (output.contains(RESET + RESET)) {
output = output.replace(RESET + RESET, RESET);
output = output.replace(RESET + RESET, RESET);
}
return output;
}
Expand All @@ -163,7 +163,7 @@ public static TextComponent copyFormatToNewText(TextComponent last) {
public static BaseComponent[] parse(String str) {
str = CoreUtilities.clearNBSPs(str);
char[] chars = str.toCharArray();
List<BaseComponent> outputList = new ArrayList<>();
List<BaseComponent> outputList = new ArrayList<>(2);
int started = 0;
TextComponent nextText = new TextComponent();
TextComponent lastText = new TextComponent();
Expand Down

0 comments on commit 2d1a590

Please sign in to comment.