Skip to content

Commit

Permalink
[Code] Ajout d'un préfixe de permission commun (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ri1ongithub authored Jul 4, 2024
2 parents 232832c + 1c56adb commit 6c63d0c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

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 {

Expand All @@ -16,10 +18,26 @@ public void onEnable() {
motdChanger = new MOTDChanger();
motdChanger.startMOTDChanger(this);



this.getCommand("version").setExecutor(new VersionCommand(this));
}

@Override
public void onDisable() {
}

/**
* Format a permission with the permission prefix.
*
* @param category the permission category
* @param suffix the permission suffix
* @return The formatted permission.
* @see #PERMISSION_PREFIX
*/
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,13 +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 @@ -19,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("ayw.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 6c63d0c

Please sign in to comment.