Fixes the sign prefix used on the first line Makes TemporaryPermission comparable Replaces some usage of OfflinePlayer with Player Only removes a permissions if owned by the player Adds a new thread that looks for expired temporary permissions
79 lines
3.0 KiB
Java
79 lines
3.0 KiB
Java
package net.knarcraft.permissionsigns.manager;
|
|
|
|
import net.knarcraft.permissionsigns.container.TemporaryPermission;
|
|
import net.milkbowl.vault.permission.Permission;
|
|
import org.bukkit.entity.Player;
|
|
|
|
import java.util.Queue;
|
|
import java.util.concurrent.PriorityBlockingQueue;
|
|
|
|
/**
|
|
* A manager that performs all Permission tasks
|
|
*/
|
|
public class PermissionManager {
|
|
|
|
private static Permission permission;
|
|
private static final Queue<TemporaryPermission> temporaryPermissions = new PriorityBlockingQueue<>();
|
|
|
|
/**
|
|
* Initializes the permission manager
|
|
*
|
|
* @param permission <p>The permission object to use for everything permission-related</p>
|
|
*/
|
|
public static void initialize(Permission permission) {
|
|
PermissionManager.permission = permission;
|
|
}
|
|
|
|
/**
|
|
* Gets the registered temporary permissions
|
|
*
|
|
* @return <p>The registered temporary permissions</p>
|
|
*/
|
|
public static Queue<TemporaryPermission> getTemporaryPermissions() {
|
|
return temporaryPermissions;
|
|
}
|
|
|
|
/**
|
|
* Grants a permanent permission to a player
|
|
*
|
|
* @param player <p>The player to grant the permission to</p>
|
|
* @param permissionNode <p>The permission node to grant to the player</p>
|
|
*/
|
|
public static void addPermission(Player player, String permissionNode) {
|
|
//TODO: Account for world when granting permissions, if wanted
|
|
permission.playerAdd(null, player, permissionNode);
|
|
}
|
|
|
|
/**
|
|
* Grants a temporary permission to a player
|
|
*
|
|
* @param player <p>The player to give the permission to</p>
|
|
* @param permissionNode <p>The temporary permission to grant</p>
|
|
* @param duration <p>The duration for which the player should keep the given permission</p>
|
|
*/
|
|
public static void addTemporaryPermission(Player player, String permissionNode, int duration) {
|
|
permission.playerAddTransient(null, player, permissionNode);
|
|
temporaryPermissions.add(new TemporaryPermission(player, permissionNode, duration));
|
|
//TODO: Create and store a temporary permission
|
|
// Check all stored temporary permissions on startup:
|
|
// * Remove expired temporary permissions
|
|
// * Grant transient permissions
|
|
// In a separate thread, remove any expired permissions, checking at least once every seconds. Might want to
|
|
// store the granted permissions in a priority queue where the priority is the least duration left.
|
|
// How to store temporary permissions? Identifier as Player + permission granted time?
|
|
}
|
|
|
|
/**
|
|
* Removes a temporary permission
|
|
*
|
|
* @param player <p>The player to remove the permission from</p>
|
|
* @param permissionNode <p>The permission node to remove from the player</p>
|
|
*/
|
|
public static void removeTemporaryPermission(Player player, String permissionNode) {
|
|
if (player.hasPermission(permissionNode)) {
|
|
permission.playerRemoveTransient(null, player, permissionNode);
|
|
}
|
|
}
|
|
|
|
}
|