Saves and loads temporary permissions to/from disk as necessary Gives players their temporary permissions when joining Adds an un-used config file Changes an english string in strings.yml
149 lines
5.9 KiB
Java
149 lines
5.9 KiB
Java
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<TemporaryPermission> temporaryPermissions = new PriorityBlockingQueue<>();
|
|
private static final File permissionsFile = new File(PermissionSigns.getInstance().getDataFolder(), "data.yml");
|
|
|
|
/**
|
|
* 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 the given player
|
|
*
|
|
* @param player <p>The player to add the temporary permission to</p>
|
|
* @param permissionNode <p>The permission node to grant</p>
|
|
*/
|
|
public static void grantTemporaryPermission(Player player, String permissionNode) {
|
|
permission.playerAddTransient(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) {
|
|
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 <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(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 <p>If unable to write to the file</p>
|
|
*/
|
|
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) {
|
|
}
|
|
}
|
|
|
|
}
|