From 0ff73b4e15c414e5c776fa184aa20fdd5416d572 Mon Sep 17 00:00:00 2001 From: PseudoKnight Date: Sun, 10 Dec 2023 14:49:33 -0800 Subject: [PATCH] Add generation support for book meta --- .../laytonsmith/abstraction/MCBookMeta.java | 11 +++++++ .../abstraction/bukkit/BukkitMCBookMeta.java | 14 +++++++++ .../com/laytonsmith/core/ObjectGenerator.java | 30 ++++++++++++------- src/main/resources/functionDocs/get_itemmeta | 3 +- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/laytonsmith/abstraction/MCBookMeta.java b/src/main/java/com/laytonsmith/abstraction/MCBookMeta.java index 6c02e97e03..eda3e01fdd 100644 --- a/src/main/java/com/laytonsmith/abstraction/MCBookMeta.java +++ b/src/main/java/com/laytonsmith/abstraction/MCBookMeta.java @@ -27,4 +27,15 @@ public interface MCBookMeta extends MCItemMeta { void setPages(List pages); void setPages(String... pages); + + Generation getGeneration(); + + void setGeneration(Generation gen); + + enum Generation { + ORIGINAL, + COPY_OF_ORIGINAL, + COPY_OF_COPY, + TATTERED + } } diff --git a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCBookMeta.java b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCBookMeta.java index 75a92c4566..6e00f7bb8d 100644 --- a/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCBookMeta.java +++ b/src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCBookMeta.java @@ -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())); + } + } diff --git a/src/main/java/com/laytonsmith/core/ObjectGenerator.java b/src/main/java/com/laytonsmith/core/ObjectGenerator.java index 4bc9a1ebce..14351f491d 100644 --- a/src/main/java/com/laytonsmith/core/ObjectGenerator.java +++ b/src/main/java/com/laytonsmith/core/ObjectGenerator.java @@ -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 { @@ -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. @@ -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) { @@ -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); } diff --git a/src/main/resources/functionDocs/get_itemmeta b/src/main/resources/functionDocs/get_itemmeta index 91fbb84332..848f4e67b9 100644 --- a/src/main/resources/functionDocs/get_itemmeta +++ b/src/main/resources/functionDocs/get_itemmeta @@ -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 |