From 55a18437f4f19c6a39e0c57f574ef2a4df663f30 Mon Sep 17 00:00:00 2001 From: srnyx <25808801+srnyx@users.noreply.github.com> Date: Wed, 29 Nov 2023 17:26:38 -0500 Subject: [PATCH] Added `AnnoyingSender#getPlayerOrNull()` --- .../annoyingapi/command/AnnoyingSender.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/xyz/srnyx/annoyingapi/command/AnnoyingSender.java b/api/src/main/java/xyz/srnyx/annoyingapi/command/AnnoyingSender.java index 19f04d3..d29dd87 100644 --- a/api/src/main/java/xyz/srnyx/annoyingapi/command/AnnoyingSender.java +++ b/api/src/main/java/xyz/srnyx/annoyingapi/command/AnnoyingSender.java @@ -119,9 +119,13 @@ public boolean checkPermission(@NotNull String permission) { /** * Casts the {@link CommandSender} to a {@link Player} - *
Only use this if you know that the {@link CommandSender} is a {@link Player} + *
Only use this if you know that the {@link CommandSender} is a {@link Player}, otherwise use {@link #getPlayerOrNull()}
*
- * @return the {@link Player} that was used
+ * @return the {@link Player} that was used
+ *
+ * @throws IllegalStateException if the {@link CommandSender} is not a {@link Player}
+ *
+ * @see #getPlayerOrNull()
*/
@NotNull
public Player getPlayer() {
@@ -129,6 +133,19 @@ public Player getPlayer() {
return (Player) cmdSender;
}
+ /**
+ * If the {@link CommandSender} is a {@link Player}, it returns it as one. Otherwise, it returns {@code null}
+ *
If you know that the {@link CommandSender} is a {@link Player}, use {@link #getPlayer()} instead
+ *
+ * @return the {@link Player}, or {@code null} if the {@link CommandSender} is not a {@link Player}
+ *
+ * @see #getPlayer()
+ */
+ @Nullable
+ public Player getPlayerOrNull() {
+ return isPlayer ? (Player) cmdSender : null;
+ }
+
/**
* Sends the invalid argument message, replacing {@code %argument%} with the specified argument
*