add permission enum

This commit is contained in:
Ryder Belserion 2024-09-22 16:06:53 -04:00
parent a610aa96d2
commit feb40131bd
No known key found for this signature in database
3 changed files with 117 additions and 37 deletions

View File

@ -118,6 +118,12 @@ public class CrazyAuctions extends Vital {
registerCommand(getCommand("crazyauctions"), new AuctionTab(), new AuctionCommand());
for (final Permissions permission : Permissions.values()) {
if (permission.shouldRegister()) {
permission.registerPermission();
}
}
this.support = new VaultSupport();
this.support.loadVault();

View File

@ -0,0 +1,111 @@
package com.badbones69.crazyauctions.api.enums.other;
import com.badbones69.crazyauctions.CrazyAuctions;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import java.util.HashMap;
import java.util.Map;
public enum Permissions {
reload("reload", "Access to /crazyauctions reload", PermissionDefault.OP, false),
help("help", "Access to /crazyauctions help", PermissionDefault.TRUE, false),
view("view", "Access to /crazyauctions view", PermissionDefault.TRUE, false),
expired("expired", "Access to /crazyauctions expired", PermissionDefault.TRUE, false),
listed("listed", "Access to /crazyauctions listed", PermissionDefault.TRUE, false),
sell("sell", "Access to /crazyauctions sell", PermissionDefault.TRUE, false),
bid("bid", "Access to /crazyauctions bid", PermissionDefault.TRUE, false),
use("use", "Access to /crazyauctions", PermissionDefault.TRUE, false),
bypass("bypass", "Bypasses most plugin restrictions", PermissionDefault.OP, true),
access("access", "Access other portions of the plugin", PermissionDefault.TRUE, true),
player_wildcard("player.*", "Access multiple player based commands", PermissionDefault.FALSE, new HashMap<>() {{
put("crazyauctions.bid", true);
put("crazyauctions.sell", true);
put("crazyauctions.access", true);
put("crazyauctions.view", true);
}}, true),
admin_wildcard("admin", "Access multiple admin based commands", PermissionDefault.FALSE, new HashMap<>() {{
put("crazyauctions.player.*", true);
put("crazyauctions.reload", true);
put("crazyauctions.bypass", true);
put("crazyauctions.view", true);
}}, true);
private final String node;
private final String description;
private final PermissionDefault isDefault;
private final Map<String, Boolean> children;
private final boolean register;
private final PluginManager manager = CrazyAuctions.getPlugin().getServer().getPluginManager();
Permissions(String node, String description, PermissionDefault isDefault, Map<String, Boolean> children, boolean register) {
this.node = node;
this.description = description;
this.isDefault = isDefault;
this.children = children;
this.register = register;
}
Permissions(String node, String description, PermissionDefault isDefault, boolean register) {
this.node = node;
this.description = description;
this.isDefault = isDefault;
this.children = new HashMap<>();
this.register = register;
}
public final String getNode() {
return "crazyauctions." + this.node;
}
public final boolean shouldRegister() {
return this.register;
}
public final String getDescription() {
return this.description;
}
public final PermissionDefault isDefault() {
return this.isDefault;
}
public final Map<String, Boolean> getChildren() {
return this.children;
}
public final boolean hasPermission(final Player player) {
return player.hasPermission(getNode());
}
public final boolean isValid() {
return this.manager.getPermission(getNode()) != null;
}
public final Permission getPermission() {
return new Permission(getNode(), getDescription(), isDefault(), getChildren().isEmpty() ? null : getChildren());
}
public void registerPermission() {
if (isValid()) return;
this.manager.addPermission(getPermission());
}
public void unregisterPermission() {
if (!isValid()) return;
this.manager.removePermission(getNode());
}
}

View File

@ -17,40 +17,3 @@ commands:
crazyauctions:
description: Opens the Crazy Auctions GUI.
aliases: [crazyauction, ah, ca]
permissions:
crazyauctions.access:
default: true
crazyauctions.test:
default: op
crazyauctions.view:
default: true
crazyauctions.reload:
default: op
crazyauctions.bypass:
default: false
crazyauctions.sell:
default: true
crazyauctions.bid:
default: true
crazyauctions.player.*:
default: false
children:
crazyauctions.bid: true
crazyauctions.sell: true
crazyauctions.access: true
crazyauctions.view: true
crazyauctions.admin:
default: false
children:
crazyauctions.test: true
crazyauctions.reload: true
crazyauctions.bypass: true