-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
72 changes: 72 additions & 0 deletions
72
...src/main/java/io/github/projectunified/minelib/plugin/permission/PermissionComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters