Makes temporary permissions work

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
This commit is contained in:
Kristian Knarvik 2022-01-14 20:33:08 +01:00
parent 339aedf2e9
commit 112fd0d53d
7 changed files with 127 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import net.knarcraft.permissionsigns.command.PermissionSignsTabCompleter;
import net.knarcraft.permissionsigns.container.PermissionSign;
import net.knarcraft.permissionsigns.container.SignCreationRequest;
import net.knarcraft.permissionsigns.formatting.Translator;
import net.knarcraft.permissionsigns.listener.PlayerListener;
import net.knarcraft.permissionsigns.listener.SignListener;
import net.knarcraft.permissionsigns.manager.EconomyManager;
import net.knarcraft.permissionsigns.manager.PermissionManager;
@ -149,6 +150,10 @@ public final class PermissionSigns extends JavaPlugin {
//TODO: Check for existence of old permission signs when clicked and register them as new permission signs. If
// it has the permissionSigns header and a name matching contents in signs.yml, add it.
//TODO: Implement config file
//TODO: Account for per-world permissions if enabled (perhaps allow world specification as world:permission?)
//TODO: Allow for custom language files. Perhaps just look for strings.yml in the folder
//Check if vault is loaded
ServicesManager servicesManager = this.getServer().getServicesManager();
RegisteredServiceProvider<Permission> permissionProvider = servicesManager.getRegistration(Permission.class);
@ -160,6 +165,7 @@ public final class PermissionSigns extends JavaPlugin {
throw new IllegalStateException("[PermissionSigns] Error: Vault could not be loaded");
}
getServer().getPluginManager().registerEvents(new SignListener(), this);
getServer().getPluginManager().registerEvents(new PlayerListener(), this);
Translator.loadLanguages("en");
registerCommands();
@ -167,6 +173,7 @@ public final class PermissionSigns extends JavaPlugin {
scheduler.runTaskTimer(this, new SignCreationRequestTimeoutThread(signCreationRequests), 0L, 100L);
scheduler.runTaskTimer(this, new PermissionTimeoutThread(), 0L, 25L);
SignManager.loadSigns();
PermissionManager.loadTemporaryPermissions();
}
/**

View File

@ -1,5 +1,6 @@
package net.knarcraft.permissionsigns.container;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
@ -9,7 +10,7 @@ import org.jetbrains.annotations.NotNull;
public class TemporaryPermission implements Comparable<TemporaryPermission> {
private final long grantedTime;
private final Player grantedPlayer;
private final OfflinePlayer grantedPlayer;
private final String permissionNode;
private final int duration;
@ -37,7 +38,7 @@ public class TemporaryPermission implements Comparable<TemporaryPermission> {
* @param grantedTime <p>The time this temporary permission was granted</p>
* @param duration <p>The duration, in seconds, the temporary permission should last</p>
*/
public TemporaryPermission(Player player, String permissionNode, long grantedTime, int duration) {
public TemporaryPermission(OfflinePlayer player, String permissionNode, long grantedTime, int duration) {
this.grantedPlayer = player;
this.permissionNode = permissionNode;
this.grantedTime = grantedTime;
@ -67,7 +68,7 @@ public class TemporaryPermission implements Comparable<TemporaryPermission> {
*
* @return <p>The player this temporary permission was granted to</p>
*/
public Player getGrantedPlayer() {
public OfflinePlayer getGrantedPlayer() {
return grantedPlayer;
}

View File

@ -0,0 +1,26 @@
package net.knarcraft.permissionsigns.listener;
import net.knarcraft.permissionsigns.container.TemporaryPermission;
import net.knarcraft.permissionsigns.manager.PermissionManager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import java.util.PriorityQueue;
import java.util.Queue;
/**
* A listener listening to player events
*/
public class PlayerListener implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
//Give the joining player any temporary permissions containing their UUID
Queue<TemporaryPermission> temporaryPermissions = new PriorityQueue<>(PermissionManager.getTemporaryPermissions());
temporaryPermissions.removeIf((item) -> !item.getGrantedPlayer().getUniqueId().equals(event.getPlayer().getUniqueId()));
temporaryPermissions.forEach((item) -> PermissionManager.grantTemporaryPermission(event.getPlayer(),
item.getPermissionNode()));
}
}

View File

@ -1,11 +1,20 @@
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
@ -13,7 +22,8 @@ import java.util.concurrent.PriorityBlockingQueue;
public class PermissionManager {
private static Permission permission;
private static final Queue<TemporaryPermission> temporaryPermissions = new PriorityBlockingQueue<>();
private static Queue<TemporaryPermission> temporaryPermissions = new PriorityBlockingQueue<>();
private static final File permissionsFile = new File(PermissionSigns.getInstance().getDataFolder(), "data.yml");
/**
* Initializes the permission manager
@ -44,6 +54,16 @@ public class PermissionManager {
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
*
@ -52,27 +72,77 @@ public class PermissionManager {
* @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);
grantTemporaryPermission(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?
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 player <p>The player to remove the permission from</p>
* @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(Player player, String permissionNode) {
if (player.hasPermission(permissionNode)) {
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) {
}
}
}

View File

@ -28,7 +28,7 @@ import java.util.logging.Level;
public class SignManager {
private static Map<Location, PermissionSign> managedSigns = new HashMap<>();
private static final File signsFile = new File(PermissionSigns.getInstance().getDataFolder(), "signs.yml");
private static final File signsFile = new File(PermissionSigns.getInstance().getDataFolder(), "data.yml");
/**
* Gets the permission sign at the given location
@ -89,7 +89,7 @@ public class SignManager {
ConfigurationSection signSection = configuration.getConfigurationSection("signs");
managedSigns = new HashMap<>();
if (signSection == null) {
PermissionSigns.getInstance().getLogger().log(Level.WARNING, "Signs section not found in signs.yml");
PermissionSigns.getInstance().getLogger().log(Level.WARNING, "Signs section not found in data.yml");
return;
}
for (String key : signSection.getKeys(false)) {

View File

@ -0,0 +1,5 @@
# The language to use for the plugin
language: en
# Whether to only give permissions for a single world, instead of granting permissions for all worlds
perWorldPermissions: false

View File

@ -13,7 +13,7 @@ en:
COMMAND_PERMISSION_DENIED: "&7You do not have necessary permissions to perform this command"
CREATION_REQUEST_CREATED: "&7Permission Sign request created. Right-click an empty sign within 60 seconds to create the new permission sign"
CANNOT_AFFORD: "&7You cannot afford to use this permission sign"
ALREADY_HAS_PERMISSIONS: "&7You already have all permissions sold by this permission sign"
ALREADY_HAS_PERMISSIONS: "&7You already have all permissions given by this permission sign"
PERMISSIONS_GRANTED: "&7You have been granted the following permission nodes: {permissions} for {time}"
PERMISSION_SIGN_CREATED: "&7Permission sign successfully created!"
CREATION_REQUEST_CANCELLED: "&7Your last permission sign creation request has been cancelled"