Skip to content

Commit 8e7061b

Browse files
committed
Add length check on title
Fixes #15
1 parent 5afc423 commit 8e7061b

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>xyz.upperlevel.spigot.book</groupId>
88
<artifactId>spigot-book-api</artifactId>
9-
<version>1.4</version>
9+
<version>1.4.1</version>
1010

1111
<name>BookApi</name>
1212
<description>A low-level API for book control on spigot</description>

src/main/java/xyz/upperlevel/spigot/book/BookUtil.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,17 @@ public BookBuilder(ItemStack book) {
8989
}
9090

9191
/**
92-
* Sets the title of the book
92+
* Sets the title of the book.
93+
*
94+
* It should always be at most 32 characters.
95+
*
9396
* @param title the title of the book
9497
* @return the BookBuilder's calling instance
9598
*/
9699
public BookBuilder title(String title) {
100+
if (title.length() > 32) {
101+
throw new IllegalArgumentException("The book title must be at most 32 characters");
102+
}
97103
meta.setTitle(title);
98104
return this;
99105
}
@@ -149,7 +155,7 @@ public BookBuilder pages(List<BaseComponent[]> pages) {
149155
}
150156

151157
/**
152-
* Sets the generation of the book
158+
* Sets the generation of the book.
153159
* Only works from MC 1.10
154160
* @param generation the Book generation
155161
* @return the BookBuilder calling instance

src/test/java/xyz/upperlevel/spigot/book/example/Main.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,20 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
2626
if (sender instanceof Player) {
2727
ItemStack book = createGeneralBook((Player) sender, args.length < 1 ? "" : args[0]);
2828

29-
BookUtil.openPlayer((Player) sender, book);
29+
if (book != null) {
30+
BookUtil.openPlayer((Player) sender, book);
31+
} else {
32+
sender.sendMessage("Subcommands: color, command, link, game, general or none");
33+
}
3034
}
3135

3236
return true;
3337
}
3438

3539
private ItemStack createGeneralBook(Player p, String type) {
3640
switch (type.toLowerCase()) {
41+
case "":
42+
return createPresentationBook(p);
3743
case "color":
3844
return createColorBook(p);
3945
case "command":
@@ -45,7 +51,7 @@ private ItemStack createGeneralBook(Player p, String type) {
4551
case "general":
4652
return createGeneralBook(p);
4753
default:
48-
return createPresentationBook(p);
54+
return null;
4955
}
5056
}
5157

@@ -128,7 +134,7 @@ private ItemStack createCommandBook(Player p) {
128134
new BookUtil.PageBuilder()
129135
.add(
130136
BookUtil.TextBuilder.of("Suicide")
131-
.onHover(BookUtil.HoverAction.showText("Wannt to try?"))
137+
.onHover(BookUtil.HoverAction.showText("Wanna try?"))
132138
.onClick(BookUtil.ClickAction.runCommand("/kill"))
133139
.color(ChatColor.RED)
134140
.build()

0 commit comments

Comments
 (0)