Skip to content

Commit 0ff73b4

Browse files
committed
Add generation support for book meta
1 parent f2b77ca commit 0ff73b4

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
lines changed

src/main/java/com/laytonsmith/abstraction/MCBookMeta.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,15 @@ public interface MCBookMeta extends MCItemMeta {
2727
void setPages(List<String> pages);
2828

2929
void setPages(String... pages);
30+
31+
Generation getGeneration();
32+
33+
void setGeneration(Generation gen);
34+
35+
enum Generation {
36+
ORIGINAL,
37+
COPY_OF_ORIGINAL,
38+
COPY_OF_COPY,
39+
TATTERED
40+
}
3041
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCBookMeta.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,18 @@ public void setPages(String... pages) {
8383
bm.setPages(pages);
8484
}
8585

86+
@Override
87+
public Generation getGeneration() {
88+
BookMeta.Generation gen = bm.getGeneration();
89+
if(gen == null) {
90+
return Generation.ORIGINAL;
91+
}
92+
return Generation.valueOf(gen.name());
93+
}
94+
95+
@Override
96+
public void setGeneration(Generation gen) {
97+
bm.setGeneration(BookMeta.Generation.valueOf(gen.name()));
98+
}
99+
86100
}

src/main/java/com/laytonsmith/core/ObjectGenerator.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -645,23 +645,23 @@ public Construct itemMeta(MCItemStack is, Target t) {
645645
} else if(meta instanceof MCLeatherArmorMeta) {
646646
CArray color = color(((MCLeatherArmorMeta) meta).getColor(), t);
647647
ma.set("color", color, t);
648-
} else if(meta instanceof MCBookMeta) {
648+
} else if(meta instanceof MCBookMeta bookMeta) {
649649
Construct title;
650650
Construct author;
651651
Construct pages;
652-
if(((MCBookMeta) meta).hasTitle()) {
653-
title = new CString(((MCBookMeta) meta).getTitle(), t);
652+
if(bookMeta.hasTitle()) {
653+
title = new CString(bookMeta.getTitle(), t);
654654
} else {
655655
title = CNull.NULL;
656656
}
657-
if(((MCBookMeta) meta).hasAuthor()) {
658-
author = new CString(((MCBookMeta) meta).getAuthor(), t);
657+
if(bookMeta.hasAuthor()) {
658+
author = new CString(bookMeta.getAuthor(), t);
659659
} else {
660660
author = CNull.NULL;
661661
}
662-
if(((MCBookMeta) meta).hasPages()) {
662+
if(bookMeta.hasPages()) {
663663
pages = new CArray(t);
664-
for(String p : ((MCBookMeta) meta).getPages()) {
664+
for(String p : bookMeta.getPages()) {
665665
((CArray) pages).push(new CString(p, t), t);
666666
}
667667
} else {
@@ -670,6 +670,7 @@ public Construct itemMeta(MCItemStack is, Target t) {
670670
ma.set("title", title, t);
671671
ma.set("author", author, t);
672672
ma.set("pages", pages, t);
673+
ma.set("generation", bookMeta.getGeneration().name(), t);
673674
} else if(meta instanceof MCSkullMeta) {
674675
MCPlayerProfile profile = ((MCSkullMeta) meta).getProfile();
675676
// If a profile doesn't exist, it either doesn't have one (plain head) or it's not supported by server.
@@ -1241,19 +1242,26 @@ public MCItemMeta itemMeta(Mixed c, MCMaterial mat, Target t) throws ConfigRunti
12411242
throw new CREFormatException("Color was expected to be an array.", t);
12421243
}
12431244
}
1244-
} else if(meta instanceof MCBookMeta) {
1245+
} else if(meta instanceof MCBookMeta bookMeta) {
1246+
// written books must have a title and author
1247+
bookMeta.setTitle("");
1248+
bookMeta.setAuthor("");
12451249
if(ma.containsKey("title")) {
12461250
Mixed title = ma.get("title", t);
12471251
if(!(title instanceof CNull)) {
1248-
((MCBookMeta) meta).setTitle(title.val());
1252+
bookMeta.setTitle(title.val());
12491253
}
12501254
}
12511255
if(ma.containsKey("author")) {
12521256
Mixed author = ma.get("author", t);
12531257
if(!(author instanceof CNull)) {
1254-
((MCBookMeta) meta).setAuthor(author.val());
1258+
bookMeta.setAuthor(author.val());
12551259
}
12561260
}
1261+
if(ma.containsKey("generation")) {
1262+
Mixed generation = ma.get("generation", t);
1263+
bookMeta.setGeneration(MCBookMeta.Generation.valueOf(generation.val()));
1264+
}
12571265
if(ma.containsKey("pages")) {
12581266
Mixed pages = ma.get("pages", t);
12591267
if(pages instanceof CNull) {
@@ -1264,7 +1272,7 @@ public MCItemMeta itemMeta(Mixed c, MCMaterial mat, Target t) throws ConfigRunti
12641272
for(int j = 0; j < pa.size(); j++) {
12651273
pl.add(pa.get(j, t).val());
12661274
}
1267-
((MCBookMeta) meta).setPages(pl);
1275+
bookMeta.setPages(pl);
12681276
} else {
12691277
throw new CREFormatException("Pages field was expected to be an array.", t);
12701278
}

src/main/resources/functionDocs/get_itemmeta

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ The entity's custom name is derived from the item '''"display"''' string. All ot
4444
|-
4545
| Books
4646
|
47-
The "generation" tag is not yet supported. As pages are plain text, they do not yet support some advanced text components.
47+
As pages are plain text, they do not yet support some advanced text components.
4848
* '''title''' : (string) The title of the book.
4949
* '''author''' : (string) The author of the book.
5050
* '''pages''' : (array) An array of pages as strings. New lines supported. 256 character limit per page. 50 page limit.
51+
* '''generation''' : (string) The generation of the book. Can be ORIGINAL, COPY_OF_ORIGINAL, COPY_OF_COPY, or TATTERED (the last two cannot be copied).
5152
|-
5253
| Brewing Stands
5354
|

0 commit comments

Comments
 (0)