Skip to content

Commit

Permalink
Add BukkitUtility#getPermissionValue(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
srnyx committed Dec 16, 2023
1 parent 06fd2ad commit 2d9a664
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions api/src/main/java/xyz/srnyx/annoyingapi/utility/BukkitUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -88,6 +89,29 @@ public static boolean toggleScoreboardTag(@NotNull Entity entity, @NotNull Strin
return false;
}

/**
* Gets the value of a permission node from a {@link Player}
* <br><br>
* <b>Example:</b> {@code player} has {@code friends.max.5} permission, {@code getPermissionValue(player, "friends.max.")} would return {@code 5}
*
* @param player the {@link Player} to get the permission value from
* @param permission the permission node to get the value of
*
* @return the value of the permission node, or null if not found
*/
@Nullable
public static Integer getPermissionValue(@NotNull Player player, @NotNull String permission) {
for (final PermissionAttachmentInfo info : player.getEffectivePermissions()) {
final String perm = info.getPermission();
if (perm.startsWith(permission)) try {
return Integer.parseInt(perm.substring(permission.length()));
} catch (final NumberFormatException e) {
e.printStackTrace();
}
}
return null;
}

/**
* Gets an {@link OfflinePlayer} from the specified name
*
Expand Down

0 comments on commit 2d9a664

Please sign in to comment.