Skip to content

Commit

Permalink
Add generation support for book meta
Browse files Browse the repository at this point in the history
  • Loading branch information
PseudoKnight committed Dec 10, 2023
1 parent f2b77ca commit 0ff73b4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/laytonsmith/abstraction/MCBookMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ public interface MCBookMeta extends MCItemMeta {
void setPages(List<String> pages);

void setPages(String... pages);

Generation getGeneration();

void setGeneration(Generation gen);

enum Generation {
ORIGINAL,
COPY_OF_ORIGINAL,
COPY_OF_COPY,
TATTERED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,18 @@ public void setPages(String... pages) {
bm.setPages(pages);
}

@Override
public Generation getGeneration() {
BookMeta.Generation gen = bm.getGeneration();
if(gen == null) {
return Generation.ORIGINAL;
}
return Generation.valueOf(gen.name());
}

@Override
public void setGeneration(Generation gen) {
bm.setGeneration(BookMeta.Generation.valueOf(gen.name()));
}

}
30 changes: 19 additions & 11 deletions src/main/java/com/laytonsmith/core/ObjectGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,23 +645,23 @@ public Construct itemMeta(MCItemStack is, Target t) {
} else if(meta instanceof MCLeatherArmorMeta) {
CArray color = color(((MCLeatherArmorMeta) meta).getColor(), t);
ma.set("color", color, t);
} else if(meta instanceof MCBookMeta) {
} else if(meta instanceof MCBookMeta bookMeta) {
Construct title;
Construct author;
Construct pages;
if(((MCBookMeta) meta).hasTitle()) {
title = new CString(((MCBookMeta) meta).getTitle(), t);
if(bookMeta.hasTitle()) {
title = new CString(bookMeta.getTitle(), t);
} else {
title = CNull.NULL;
}
if(((MCBookMeta) meta).hasAuthor()) {
author = new CString(((MCBookMeta) meta).getAuthor(), t);
if(bookMeta.hasAuthor()) {
author = new CString(bookMeta.getAuthor(), t);
} else {
author = CNull.NULL;
}
if(((MCBookMeta) meta).hasPages()) {
if(bookMeta.hasPages()) {
pages = new CArray(t);
for(String p : ((MCBookMeta) meta).getPages()) {
for(String p : bookMeta.getPages()) {
((CArray) pages).push(new CString(p, t), t);
}
} else {
Expand All @@ -670,6 +670,7 @@ public Construct itemMeta(MCItemStack is, Target t) {
ma.set("title", title, t);
ma.set("author", author, t);
ma.set("pages", pages, t);
ma.set("generation", bookMeta.getGeneration().name(), t);
} else if(meta instanceof MCSkullMeta) {
MCPlayerProfile profile = ((MCSkullMeta) meta).getProfile();
// If a profile doesn't exist, it either doesn't have one (plain head) or it's not supported by server.
Expand Down Expand Up @@ -1241,19 +1242,26 @@ public MCItemMeta itemMeta(Mixed c, MCMaterial mat, Target t) throws ConfigRunti
throw new CREFormatException("Color was expected to be an array.", t);
}
}
} else if(meta instanceof MCBookMeta) {
} else if(meta instanceof MCBookMeta bookMeta) {
// written books must have a title and author
bookMeta.setTitle("");
bookMeta.setAuthor("");
if(ma.containsKey("title")) {
Mixed title = ma.get("title", t);
if(!(title instanceof CNull)) {
((MCBookMeta) meta).setTitle(title.val());
bookMeta.setTitle(title.val());
}
}
if(ma.containsKey("author")) {
Mixed author = ma.get("author", t);
if(!(author instanceof CNull)) {
((MCBookMeta) meta).setAuthor(author.val());
bookMeta.setAuthor(author.val());
}
}
if(ma.containsKey("generation")) {
Mixed generation = ma.get("generation", t);
bookMeta.setGeneration(MCBookMeta.Generation.valueOf(generation.val()));
}
if(ma.containsKey("pages")) {
Mixed pages = ma.get("pages", t);
if(pages instanceof CNull) {
Expand All @@ -1264,7 +1272,7 @@ public MCItemMeta itemMeta(Mixed c, MCMaterial mat, Target t) throws ConfigRunti
for(int j = 0; j < pa.size(); j++) {
pl.add(pa.get(j, t).val());
}
((MCBookMeta) meta).setPages(pl);
bookMeta.setPages(pl);
} else {
throw new CREFormatException("Pages field was expected to be an array.", t);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/functionDocs/get_itemmeta
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ The entity's custom name is derived from the item '''"display"''' string. All ot
|-
| Books
|
The "generation" tag is not yet supported. As pages are plain text, they do not yet support some advanced text components.
As pages are plain text, they do not yet support some advanced text components.
* '''title''' : (string) The title of the book.
* '''author''' : (string) The author of the book.
* '''pages''' : (array) An array of pages as strings. New lines supported. 256 character limit per page. 50 page limit.
* '''generation''' : (string) The generation of the book. Can be ORIGINAL, COPY_OF_ORIGINAL, COPY_OF_COPY, or TATTERED (the last two cannot be copied).
|-
| Brewing Stands
|
Expand Down

0 comments on commit 0ff73b4

Please sign in to comment.