Skip to content

Commit

Permalink
[Code] Ajout de catégories de permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
dandan2611 committed Jul 4, 2024
1 parent 8f34193 commit 1c56adb
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
16 changes: 6 additions & 10 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

import fr.communaywen.core.utils.MOTDChanger;
import fr.communaywen.core.commands.VersionCommand;
import fr.communaywen.core.utils.PermissionCategory;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

public final class AywenCraftPlugin extends JavaPlugin {

/**
* Permission prefix.
* <br>
* Permissions in the plugin <b>SHOULD</b> be prefixed with this prefix. E.g. <code>ayw.command.prout</code>
*/
public static final @NotNull String PERMISSION_PREFIX = "ayw";

private MOTDChanger motdChanger;

@Override
Expand All @@ -36,12 +30,14 @@ public void onDisable() {
/**
* Format a permission with the permission prefix.
*
* @param suffix The permission suffix.
* @param category the permission category
* @param suffix the permission suffix
* @return The formatted permission.
* @see #PERMISSION_PREFIX
*/
public static @NotNull String formatPermission(final @NotNull String suffix) {
return PERMISSION_PREFIX + "." + suffix;
public static @NotNull String formatPermission(final @NotNull PermissionCategory category,
final @NotNull String suffix) {
return category.formatPermission(suffix);
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package fr.communaywen.core.commands;

import fr.communaywen.core.AywenCraftPlugin;
import fr.communaywen.core.utils.PermissionCategory;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class VersionCommand implements CommandExecutor {

public static final @NotNull String PERMISSION = PermissionCategory.ADMIN.formatPermission("version");

private final JavaPlugin plugin;

public VersionCommand(JavaPlugin plugin) {
Expand All @@ -20,7 +24,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
if (command.getName().equalsIgnoreCase("version")) {
if (sender instanceof Player) {
Player player = (Player) sender;
if (!player.hasPermission(AywenCraftPlugin.formatPermission("version"))) {
if (!player.hasPermission(PERMISSION)) {
player.sendMessage("Vous n'avez pas la permission d'utiliser cette commande.");
return true;
}
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/fr/communaywen/core/utils/PermissionCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package fr.communaywen.core.utils;

import org.jetbrains.annotations.NotNull;

/**
* Permission categories
*/
public enum PermissionCategory {

/**
* Administrative permissions (management commands, etc.)
*/
ADMIN("admin"),

/**
* Regular commands
*/
COMMAND("command"),
;

/**
* Permission prefix.
* <br>
* Permissions in the plugin <b>SHOULD</b> be prefixed with this prefix. E.g. <code>ayw.command.prout</code>
*/
public static final @NotNull String PERMISSION_PREFIX = "ayw";

/**
* Permission postfix.
*/
private final @NotNull String postfix;

/**
* Create a new permission category.
*
* @param postfix the permission postfix
*/
PermissionCategory(final @NotNull String postfix) {
this.postfix = postfix;
}

/**
* Get the permission postfix.
*
* @return the permission postfix
*/
public @NotNull String getPostfix() {
return postfix;
}

/**
* Format a permission with the permission prefix.
*
* @param suffix the permission suffix
* @return the formatted permission
*/
public @NotNull String formatPermission(final @NotNull String suffix) {
return PERMISSION_PREFIX + "." + postfix + "." + suffix;
}

}

0 comments on commit 1c56adb

Please sign in to comment.