package net.knarcraft.permissionsigns.manager; import net.knarcraft.permissionsigns.PermissionSigns; import net.knarcraft.permissionsigns.container.TemporaryPermission; import net.milkbowl.vault.permission.Permission; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; import java.io.File; import java.io.IOException; import java.util.Queue; import java.util.UUID; import java.util.concurrent.PriorityBlockingQueue; import java.util.logging.Level; /** * A manager that performs all Permission tasks */ public class PermissionManager { private static Permission permission; private static Queue temporaryPermissions = new PriorityBlockingQueue<>(); private static final File permissionsFile = new File(PermissionSigns.getInstance().getDataFolder(), "data.yml"); /** * Initializes the permission manager * * @param permission

The permission object to use for everything permission-related

*/ public static void initialize(Permission permission) { PermissionManager.permission = permission; } /** * Gets the registered temporary permissions * * @return

The registered temporary permissions

*/ public static Queue getTemporaryPermissions() { return temporaryPermissions; } /** * Grants a permanent permission to a player * * @param player

The player to grant the permission to

* @param permissionNode

The permission node to grant to the player

*/ 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 the given player * * @param player

The player to add the temporary permission to

* @param permissionNode

The permission node to grant

*/ public static void grantTemporaryPermission(Player player, String permissionNode) { permission.playerAddTransient(null, player, permissionNode); } /** * Grants a temporary permission to a player * * @param player

The player to give the permission to

* @param permissionNode

The temporary permission to grant

* @param duration

The duration for which the player should keep the given permission

*/ public static void addTemporaryPermission(Player player, String permissionNode, int duration) { grantTemporaryPermission(player, permissionNode); temporaryPermissions.add(new TemporaryPermission(player, permissionNode, duration)); try { saveTemporaryPermissions(); } catch (IOException e) { PermissionSigns.getInstance().getLogger().log(Level.SEVERE, "Unable to save temporary permissions! " + "Players will lose their temporary permissions upon server restart!"); } } /** * Removes a temporary permission * * @param offlinePlayer

The player to remove the permission from

* @param permissionNode

The permission node to remove from the player

*/ public static void removeTemporaryPermission(OfflinePlayer offlinePlayer, String permissionNode) { Player player = offlinePlayer.getPlayer(); if (player != null && player.hasPermission(permissionNode)) { permission.playerRemoveTransient(null, player, permissionNode); } } /** * Saves all temporary permissions to a file * * @throws IOException

If unable to write to the file

*/ public static void saveTemporaryPermissions() throws IOException { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(permissionsFile); ConfigurationSection permissionSection = configuration.createSection("permissions"); temporaryPermissions.forEach((item) -> { String key = item.getGrantedPlayer().getUniqueId() + "," + item.getGrantedTime(); permissionSection.set(key + ".permissionNode", item.getPermissionNode()); permissionSection.set(key + ".duration", item.getGrantedDuration()); }); configuration.save(permissionsFile); } /** * Loads all saved temporary permissions */ public static void loadTemporaryPermissions() { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(permissionsFile); ConfigurationSection permissionSection = configuration.getConfigurationSection("permissions"); temporaryPermissions = new PriorityBlockingQueue<>(); if (permissionSection == null) { PermissionSigns.getInstance().getLogger().log(Level.WARNING, "Permission section not found in data.yml"); return; } long currentTime = System.currentTimeMillis(); for (String key : permissionSection.getKeys(false)) { String[] identifierParts = key.split(","); OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(identifierParts[0])); String permissionNode = permissionSection.getString(key + ".permissionNode"); long granted = Long.parseLong(identifierParts[1]); int duration = permissionSection.getInt(key + ".duration", 1); //Skip any expired temporary permissions if (currentTime > granted + (1000L * duration)) { continue; } //TODO: Need to wait for the player to join the server before allocating transient permissions temporaryPermissions.add(new TemporaryPermission(player, permissionNode, granted, duration)); } try { saveTemporaryPermissions(); } catch (IOException ignored) { } } }