Skip to content

Commit

Permalink
add PermissionComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Mar 4, 2024
1 parent fe51edb commit bdc5461
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
21 changes: 21 additions & 0 deletions plugin/plugin-permission/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.github.projectunified</groupId>
<artifactId>minelib-plugin</artifactId>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>minelib-plugin-permission</artifactId>

<dependencies>
<dependency>
<groupId>io.github.projectunified</groupId>
<artifactId>minelib-plugin-base</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.github.projectunified.minelib.plugin.permission;

import io.github.projectunified.minelib.plugin.base.BasePlugin;
import io.github.projectunified.minelib.plugin.base.Loadable;
import org.bukkit.Bukkit;
import org.bukkit.permissions.Permission;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* A component that handles the registration and unregistration of {@link Permission}
*/
public class PermissionComponent implements Loadable {
private final BasePlugin plugin;
private final Set<Permission> registeredPermissions = new HashSet<>();

/**
* Create a new instance
*
* @param plugin the plugin
*/
public PermissionComponent(BasePlugin plugin) {
this.plugin = plugin;
}

private List<Permission> getPermissions() {
List<Permission> permissions = new ArrayList<>();

Class<?>[] classes = {
getClass(),
plugin.getClass()
};

for (Class<?> clazz : classes) {
for (Field field : clazz.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (!field.getType().equals(Permission.class) || !Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers))
continue;

try {
Permission permission = (Permission) field.get(null);
permissions.add(permission);
} catch (IllegalAccessException e) {
plugin.getLogger().warning("Failed to access permission field: " + field.getName());
}
}
}

return permissions;
}

@Override
public void enable() {
List<Permission> permissions = getPermissions();
for (Permission permission : permissions) {
Bukkit.getPluginManager().addPermission(permission);
registeredPermissions.add(permission);
}
}

@Override
public void disable() {
for (Permission permission : registeredPermissions) {
Bukkit.getPluginManager().removePermission(permission);
}
}
}
1 change: 1 addition & 0 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<module>plugin-base</module>
<module>plugin-listener</module>
<module>plugin-postenable</module>
<module>plugin-permission</module>
</modules>
</project>

0 comments on commit bdc5461

Please sign in to comment.