Skip to content

Commit

Permalink
Added AnnoyingSender#getPlayerOrNull()
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Nov 29, 2023
1 parent 66643ea commit 55a1843
Showing 1 changed file with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,33 @@ public boolean checkPermission(@NotNull String permission) {

/**
* Casts the {@link CommandSender} to a {@link Player}
* <p>Only use this if you know that the {@link CommandSender} is a {@link Player}
* <p>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() {
if (!isPlayer) throw new IllegalStateException("CommandSender is not a Player");
return (Player) cmdSender;
}

/**
* If the {@link CommandSender} is a {@link Player}, it returns it as one. Otherwise, it returns {@code null}
* <br>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
*
Expand Down

0 comments on commit 55a1843

Please sign in to comment.