From eb5391a496b27ffefd93720fccf9cd87c13907f8 Mon Sep 17 00:00:00 2001 From: QwQ-dev Date: Sun, 26 May 2024 18:38:53 +0800 Subject: [PATCH] Add SlimefunItem#getOptionalById(String) and SlimefunItem#getOptionalByItem(ItemStack). --- .../slimefun4/api/items/SlimefunItem.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/SlimefunItem.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/SlimefunItem.java index 3593dfa8d5..f9e23e222a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/SlimefunItem.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/items/SlimefunItem.java @@ -1233,6 +1233,17 @@ public final int hashCode() { return Slimefun.getRegistry().getSlimefunItemIds().get(id); } + /** + * Retrieve a {@link Optional} {@link SlimefunItem} by its id. + * + * @param id + * The id of the {@link SlimefunItem} + * @return The {@link Optional} {@link SlimefunItem} associated with that id. Empty if non-existent + */ + public static @Nonnull Optional getOptionalById(@Nonnull String id) { + return Optional.ofNullable(getById(id)); + } + /** * Retrieve a {@link SlimefunItem} from an {@link ItemStack}. * @@ -1258,6 +1269,17 @@ public final int hashCode() { return null; } + /** + * Retrieve a {@link Optional} {@link SlimefunItem} from an {@link ItemStack}. + * + * @param item + * The {@link ItemStack} to check + * @return The {@link Optional} {@link SlimefunItem} associated with this {@link ItemStack} if present, otherwise empty + */ + public static @Nonnull Optional getOptionalByItem(@Nullable ItemStack item) { + return Optional.ofNullable(getByItem(item)); + } + /** * Should load the {@link SlimefunBlockData} by default. * If return false, only the item with {@link BlockTicker} will be loaded with {@link ChunkLoadEvent}.