Allow For Default Titles in InventoryView Builders (#12013)

This commit is contained in:
Miles
2025-02-16 22:07:00 +00:00
committed by GitHub
parent 84609dc046
commit 8eb8e44ac3
18 changed files with 201 additions and 70 deletions

View File

@@ -1,5 +1,6 @@
package org.bukkit.inventory;
import net.kyori.adventure.text.Component;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
@@ -18,12 +19,14 @@ import org.bukkit.inventory.view.builder.InventoryViewBuilder;
import org.bukkit.inventory.view.builder.LocationInventoryViewBuilder;
import org.bukkit.inventory.view.builder.MerchantInventoryViewBuilder;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
/**
* Represents different kinds of views, also known as menus, which can be
* created and viewed by the player.
*/
@NullMarked
@ApiStatus.Experimental
public interface MenuType extends Keyed, io.papermc.paper.world.flag.FeatureDependant { // Paper - make FeatureDependant
@@ -138,6 +141,20 @@ public interface MenuType extends Keyed, io.papermc.paper.world.flag.FeatureDepe
*/
interface Typed<V extends InventoryView, B extends InventoryViewBuilder<V>> extends MenuType {
/**
* Creates a view of the specified menu type.
* <p>
* The player provided to create this view must be the player the view
* is opened for. See {@link HumanEntity#openInventory(InventoryView)}
* for more information.
*
* @param player the player the view belongs to
* @return the created {@link InventoryView}
*/
default V create(HumanEntity player) {
return create(player, (Component) null);
}
/**
* Creates a view of the specified menu type.
* <p>
@@ -148,11 +165,10 @@ public interface MenuType extends Keyed, io.papermc.paper.world.flag.FeatureDepe
* @param player the player the view belongs to
* @param title the title of the view
* @return the created {@link InventoryView}
* @deprecated Use {@link #create(HumanEntity, net.kyori.adventure.text.Component)} instead.
* @deprecated Use {@link #create(HumanEntity, Component)} instead.
*/
@NotNull
@Deprecated(since = "1.21") // Paper - adventure
V create(@NotNull HumanEntity player, @NotNull String title);
V create(HumanEntity player, @Nullable String title);
// Paper start - adventure
/**
@@ -166,11 +182,9 @@ public interface MenuType extends Keyed, io.papermc.paper.world.flag.FeatureDepe
* @param title the title of the view
* @return the created {@link InventoryView}
*/
@NotNull
V create(@NotNull HumanEntity player, @NotNull net.kyori.adventure.text.Component title);
V create(HumanEntity player, @Nullable Component title);
// Paper end - adventure
@NotNull
B builder();
}
@@ -186,8 +200,7 @@ public interface MenuType extends Keyed, io.papermc.paper.world.flag.FeatureDepe
* @param title the title of the view
* @return the created {@link InventoryView}
*/
@NotNull
InventoryView create(@NotNull HumanEntity player, @NotNull net.kyori.adventure.text.Component title);
InventoryView create(HumanEntity player, @Nullable Component title);
// Paper end - adventure
/**
@@ -196,7 +209,6 @@ public interface MenuType extends Keyed, io.papermc.paper.world.flag.FeatureDepe
*
* @return the typed MenuType.
*/
@NotNull
MenuType.Typed<InventoryView, InventoryViewBuilder<InventoryView>> typed();
/**
@@ -213,19 +225,16 @@ public interface MenuType extends Keyed, io.papermc.paper.world.flag.FeatureDepe
* @throws IllegalArgumentException if the provided viewClass cannot be
* typed to this MenuType
*/
@NotNull
<V extends InventoryView, B extends InventoryViewBuilder<V>> MenuType.Typed<V, B> typed(@NotNull final Class<V> viewClass) throws IllegalArgumentException;
<V extends InventoryView, B extends InventoryViewBuilder<V>> MenuType.Typed<V, B> typed(final Class<V> viewClass) throws IllegalArgumentException;
/**
* Gets the {@link InventoryView} class of this MenuType.
*
* @return the {@link InventoryView} class of this MenuType
*/
@NotNull
Class<? extends InventoryView> getInventoryViewClass();
@NotNull
private static <T extends MenuType> T get(@NotNull final String key) {
private static <T extends MenuType> T get(final String key) {
return (T) Registry.MENU.getOrThrow(NamespacedKey.minecraft(key));
}
}

View File

@@ -4,6 +4,7 @@ import net.kyori.adventure.text.Component;
import org.bukkit.entity.HumanEntity;
import org.bukkit.inventory.InventoryView;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.Nullable;
/**
* Generic Builder for InventoryView's with no special attributes or parameters
@@ -23,10 +24,10 @@ public interface InventoryViewBuilder<V extends InventoryView> {
/**
* Sets the title of the builder
*
* @param title the title
* @param title the title, or null for a default title
* @return this builder
*/
InventoryViewBuilder<V> title(final Component title);
InventoryViewBuilder<V> title(@Nullable final Component title);
/**
* Builds this builder into a InventoryView

View File

@@ -4,7 +4,7 @@ import net.kyori.adventure.text.Component;
import org.bukkit.Location;
import org.bukkit.inventory.InventoryView;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.Nullable;
/**
* An InventoryViewBuilder that can be bound by location within the world
@@ -18,7 +18,7 @@ public interface LocationInventoryViewBuilder<V extends InventoryView> extends I
LocationInventoryViewBuilder<V> copy();
@Override
LocationInventoryViewBuilder<V> title(final @NotNull Component title);
LocationInventoryViewBuilder<V> title(final @Nullable Component title);
/**
* Determines whether or not the server should check if the player can reach

View File

@@ -5,7 +5,7 @@ import org.bukkit.Server;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.Merchant;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.Nullable;
/**
* An InventoryViewBuilder for creating merchant views
@@ -19,7 +19,7 @@ public interface MerchantInventoryViewBuilder<V extends InventoryView> extends I
MerchantInventoryViewBuilder<V> copy();
@Override
MerchantInventoryViewBuilder<V> title(final @NotNull Component title);
MerchantInventoryViewBuilder<V> title(final @Nullable Component title);
/**
* Adds a merchant to this builder