Skip to content

Commit c8350b5

Browse files
authored
Merge pull request #1346 from PaperMC/feature/book-like
feature(api): Add BookLike interface
2 parents b6bd83b + d4c8bcc commit c8350b5

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

api/src/main/java/net/kyori/adventure/audience/Audience.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import net.kyori.adventure.chat.SignedMessage;
3939
import net.kyori.adventure.dialog.DialogLike;
4040
import net.kyori.adventure.inventory.Book;
41+
import net.kyori.adventure.inventory.BookLike;
4142
import net.kyori.adventure.pointer.Pointered;
4243
import net.kyori.adventure.resource.ResourcePackInfo;
4344
import net.kyori.adventure.resource.ResourcePackInfoLike;
@@ -514,7 +515,21 @@ default void stopSound(final SoundStop stop) {
514515
*/
515516
@ForwardingAudienceOverrideNotRequired
516517
default void openBook(final Book.Builder book) {
517-
this.openBook(book.build());
518+
this.openBook(Objects.requireNonNull(book, "book").build());
519+
}
520+
521+
/**
522+
* Opens a book.
523+
*
524+
* <p>When possible, no item should persist after closing the book.</p>
525+
*
526+
* @param book a book
527+
* @see Book
528+
* @since 5.0.0
529+
*/
530+
@ForwardingAudienceOverrideNotRequired
531+
default void openBook(final BookLike book) {
532+
this.openBook(Objects.requireNonNull(book, "book").asBook());
518533
}
519534

520535
/**

api/src/main/java/net/kyori/adventure/inventory/Book.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* @see Audience#openBook(Book)
4444
* @since 4.0.0
4545
*/
46-
public sealed interface Book extends Buildable<Book.Builder> permits BookImpl {
46+
public sealed interface Book extends Buildable<Book.Builder>, BookLike permits BookImpl {
4747
/**
4848
* Creates a book.
4949
*

api/src/main/java/net/kyori/adventure/inventory/BookImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public Builder toBuilder() {
7676
.pages(this.pages);
7777
}
7878

79+
@Override
80+
public Book asBook() {
81+
return this;
82+
}
83+
7984
static final class BuilderImpl implements Builder {
8085
private Component title = Component.empty();
8186
private Component author = Component.empty();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of adventure, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2017-2025 KyoriPowered
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package net.kyori.adventure.inventory;
25+
26+
import org.jetbrains.annotations.Contract;
27+
28+
/**
29+
* Something that can be represented as a {@link Book}.
30+
*
31+
* @since 5.0.0
32+
*/
33+
@FunctionalInterface
34+
public interface BookLike {
35+
/**
36+
* Gets a {@link Book} representation of this object.
37+
*
38+
* @return a book
39+
* @since 5.0.0
40+
*/
41+
@Contract(pure = true)
42+
Book asBook();
43+
}

0 commit comments

Comments
 (0)