From c7336dbcadd28c1b2f4fe1b67ba06d09ad6144d5 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 25 Jul 2016 20:20:51 +0100 Subject: [PATCH] Change Vault hook to use new per-world perms system --- .../me/lucko/luckperms/LPBukkitPlugin.java | 2 +- .../vault}/VaultChatHook.java | 2 +- .../{vaulthooks => api/vault}/VaultHook.java | 2 +- .../api/vault/VaultPermissionHook.java | 197 ++++++++++++++++++ .../vaulthooks/VaultPermissionHook.java | 152 -------------- .../me/lucko/luckperms/users/UserManager.java | 11 - 6 files changed, 200 insertions(+), 166 deletions(-) rename bukkit/src/main/java/me/lucko/luckperms/{vaulthooks => api/vault}/VaultChatHook.java (98%) rename bukkit/src/main/java/me/lucko/luckperms/{vaulthooks => api/vault}/VaultHook.java (96%) create mode 100644 bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java delete mode 100644 bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultPermissionHook.java diff --git a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java index f09f27e6..461d0993 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java @@ -3,6 +3,7 @@ package me.lucko.luckperms; import lombok.Getter; import me.lucko.luckperms.api.LuckPermsApi; import me.lucko.luckperms.api.implementation.ApiProvider; +import me.lucko.luckperms.api.vault.VaultHook; import me.lucko.luckperms.data.Datastore; import me.lucko.luckperms.data.MySQLConfiguration; import me.lucko.luckperms.data.methods.FlatfileDatastore; @@ -16,7 +17,6 @@ import me.lucko.luckperms.users.BukkitUserManager; import me.lucko.luckperms.users.UserManager; import me.lucko.luckperms.utils.LPConfiguration; import me.lucko.luckperms.utils.UuidCache; -import me.lucko.luckperms.vaulthooks.VaultHook; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; diff --git a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultChatHook.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultChatHook.java similarity index 98% rename from bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultChatHook.java rename to bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultChatHook.java index 56dcdd88..ee0520d1 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultChatHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultChatHook.java @@ -1,4 +1,4 @@ -package me.lucko.luckperms.vaulthooks; +package me.lucko.luckperms.api.vault; import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.permission.Permission; diff --git a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultHook.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultHook.java similarity index 96% rename from bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultHook.java rename to bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultHook.java index d35286c0..819066e9 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultHook.java @@ -1,4 +1,4 @@ -package me.lucko.luckperms.vaulthooks; +package me.lucko.luckperms.api.vault; import me.lucko.luckperms.LPBukkitPlugin; import net.milkbowl.vault.chat.Chat; diff --git a/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java new file mode 100644 index 00000000..d23ba125 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java @@ -0,0 +1,197 @@ +package me.lucko.luckperms.api.vault; + +import lombok.NonNull; +import lombok.Setter; +import me.lucko.luckperms.LPBukkitPlugin; +import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; +import me.lucko.luckperms.exceptions.ObjectLacksException; +import me.lucko.luckperms.groups.Group; +import me.lucko.luckperms.users.User; +import me.lucko.luckperms.utils.PermissionObject; +import net.milkbowl.vault.permission.Permission; + +/** + * + */ +class VaultPermissionHook extends Permission { + + @Setter + private LPBukkitPlugin plugin; + + @Override + public String getName() { + return "LuckPerms"; + } + + @Override + public boolean isEnabled() { + return plugin.getDatastore().isAcceptingLogins(); + } + + @Override + public boolean hasSuperPermsCompat() { + return true; + } + + private boolean objectHas(String world, PermissionObject object, String permission) { + if (object == null) return false; + + if (world != null && !world.equals("")) { + return object.hasPermission(permission, true, "global", world); + } else { + return object.hasPermission(permission, true); + } + } + + private boolean objectAdd(String world, PermissionObject object, String permission) { + if (object == null) return false; + + try { + if (world != null && !world.equals("")) { + object.setPermission(permission, true, "global", world); + } else { + object.setPermission(permission, true); + } + } catch (ObjectAlreadyHasException ignored) {} + + objectSave(object); + return true; + } + + private boolean objectRemove(String world, PermissionObject object, String permission) { + if (object == null) return false; + + try { + if (world != null && !world.equals("")) { + object.unsetPermission(permission, "global", world); + } else { + object.unsetPermission(permission); + } + } catch (ObjectLacksException ignored) {} + + objectSave(object); + return true; + } + + private void objectSave(PermissionObject t) { + if (t instanceof User) { + ((User) t).refreshPermissions(); + plugin.getDatastore().saveUser(((User) t), aBoolean -> {}); + } + if (t instanceof Group) { + plugin.getDatastore().saveGroup(((Group) t), aBoolean -> plugin.runUpdateTask()); + } + } + + @Override + public boolean playerHas(String world, @NonNull String player, @NonNull String permission) { + return objectHas(world, plugin.getUserManager().getUser(player), permission); + } + + @Override + public boolean playerAdd(String world, @NonNull String player, @NonNull String permission) { + final User user = plugin.getUserManager().getUser(player); + return objectAdd(world, user, permission); + } + + @Override + public boolean playerRemove(String world, @NonNull String player, @NonNull String permission) { + final User user = plugin.getUserManager().getUser(player); + return objectRemove(world, user, permission); + } + + @Override + public boolean groupHas(String world, @NonNull String groupName, @NonNull String permission) { + final Group group = plugin.getGroupManager().getGroup(groupName); + return objectHas(world, group, permission); + } + + @Override + public boolean groupAdd(String world, @NonNull String groupName, @NonNull String permission) { + final Group group = plugin.getGroupManager().getGroup(groupName); + return objectAdd(world, group, permission); + } + + @Override + public boolean groupRemove(String world, @NonNull String groupName, @NonNull String permission) { + final Group group = plugin.getGroupManager().getGroup(groupName); + return objectRemove(world, group, permission); + } + + @Override + public boolean playerInGroup(String world, @NonNull String player, @NonNull String group) { + final User user = plugin.getUserManager().getUser(player); + if (user == null) return false; + + final Group group1 = plugin.getGroupManager().getGroup(group); + if (group1 == null) return false; + + if (world != null && !world.equals("")) { + return user.isInGroup(group1, "global", world); + } else { + return user.isInGroup(group1); + } + } + + @Override + public boolean playerAddGroup(String world, @NonNull String player, @NonNull String groupName) { + final User user = plugin.getUserManager().getUser(player); + if (user == null) return false; + + final Group group = plugin.getGroupManager().getGroup(groupName); + if (group == null) return false; + + try { + if (world != null && !world.equals("")) { + user.addGroup(group, "global", world); + } else { + user.addGroup(group); + } + } catch (ObjectAlreadyHasException ignored) {} + objectSave(user); + return true; + } + + @Override + public boolean playerRemoveGroup(String world, @NonNull String player, @NonNull String groupName) { + final User user = plugin.getUserManager().getUser(player); + if (user == null) return false; + + final Group group = plugin.getGroupManager().getGroup(groupName); + if (group == null) return false; + + try { + if (world != null && !world.equals("")) { + user.removeGroup(group, "global", world); + } else { + user.removeGroup(group); + } + } catch (ObjectLacksException ignored) {} + objectSave(user); + return true; + } + + @Override + public String[] getPlayerGroups(String world, @NonNull String player) { + final User user = plugin.getUserManager().getUser(player); + return (user == null) ? new String[0] : + world != null && !world.equals("") ? user.getLocalGroups("global", world).toArray(new String[0]) : + user.getGroupNames().toArray(new String[0]); + } + + @Override + public String getPrimaryGroup(String world, @NonNull String player) { + final User user = plugin.getUserManager().getUser(player); + return (user == null) ? null : user.getPrimaryGroup(); + } + + @Override + public String[] getGroups() { + return plugin.getGroupManager().getGroups().keySet().toArray(new String[0]); + } + + @Override + public boolean hasGroupSupport() { + return true; + } +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultPermissionHook.java deleted file mode 100644 index b50aa33a..00000000 --- a/bukkit/src/main/java/me/lucko/luckperms/vaulthooks/VaultPermissionHook.java +++ /dev/null @@ -1,152 +0,0 @@ -package me.lucko.luckperms.vaulthooks; - -import lombok.Setter; -import me.lucko.luckperms.LPBukkitPlugin; -import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; -import me.lucko.luckperms.exceptions.ObjectLacksException; -import me.lucko.luckperms.groups.Group; -import me.lucko.luckperms.users.User; -import net.milkbowl.vault.permission.Permission; - -class VaultPermissionHook extends Permission { - - @Setter - private LPBukkitPlugin plugin; - - @Override - public String getName() { - return "LuckPerms"; - } - - @Override - public boolean isEnabled() { - return plugin.getDatastore().isAcceptingLogins(); - } - - @Override - public boolean hasSuperPermsCompat() { - // Idk??? - return true; - } - - @Override - public boolean playerHas(String world, String player, String permission) { - final User user = plugin.getUserManager().getUser(player); - return user != null && user.hasPermission(permission, true); - } - - @Override - public boolean playerAdd(String world, String player, String permission) { - final User user = plugin.getUserManager().getUser(player); - if (user == null) return false; - - try { - user.setPermission(permission, true); - } catch (ObjectAlreadyHasException ignored) {} - plugin.getUserManager().saveUser(user, plugin.getDatastore()); - return true; - } - - @Override - public boolean playerRemove(String world, String player, String permission) { - final User user = plugin.getUserManager().getUser(player); - if (user == null) return false; - - try { - user.unsetPermission(permission); - } catch (ObjectLacksException ignored) {} - plugin.getUserManager().saveUser(user, plugin.getDatastore()); - return true; - } - - @Override - public boolean groupHas(String world, String groupName, String permission) { - final Group group = plugin.getGroupManager().getGroup(groupName); - return group != null && group.hasPermission(permission, true); - } - - @Override - public boolean groupAdd(String world, String groupName, String permission) { - final Group group = plugin.getGroupManager().getGroup(groupName); - if (group == null) return false; - - try { - group.setPermission(permission, true); - } catch (ObjectAlreadyHasException ignored) {} - plugin.runUpdateTask(); - return true; - } - - @Override - public boolean groupRemove(String world, String groupName, String permission) { - final Group group = plugin.getGroupManager().getGroup(groupName); - if (group == null) return false; - - try { - group.unsetPermission(permission); - } catch (ObjectLacksException ignored) {} - plugin.runUpdateTask(); - return true; - } - - @Override - public boolean playerInGroup(String world, String player, String group) { - final User user = plugin.getUserManager().getUser(player); - if (user == null) return false; - - final Group group1 = plugin.getGroupManager().getGroup(group); - return group1 != null && user.isInGroup(group1); - } - - @Override - public boolean playerAddGroup(String world, String player, String groupName) { - final User user = plugin.getUserManager().getUser(player); - if (user == null) return false; - - final Group group = plugin.getGroupManager().getGroup(groupName); - if (group == null) return false; - - try { - user.addGroup(group); - } catch (ObjectAlreadyHasException ignored) {} - plugin.getUserManager().saveUser(user, plugin.getDatastore()); - return true; - } - - @Override - public boolean playerRemoveGroup(String world, String player, String groupName) { - final User user = plugin.getUserManager().getUser(player); - if (user == null) return false; - - final Group group = plugin.getGroupManager().getGroup(groupName); - if (group == null) return false; - - try { - user.removeGroup(group); - } catch (ObjectLacksException ignored) {} - plugin.getUserManager().saveUser(user, plugin.getDatastore()); - return true; - } - - @Override - public String[] getPlayerGroups(String world, String player) { - final User user = plugin.getUserManager().getUser(player); - return (user == null) ? new String[0] : user.getGroupNames().toArray(new String[0]); - } - - @Override - public String getPrimaryGroup(String world, String player) { - final User user = plugin.getUserManager().getUser(player); - return (user == null) ? null : user.getPrimaryGroup(); - } - - @Override - public String[] getGroups() { - return plugin.getGroupManager().getGroups().keySet().toArray(new String[0]); - } - - @Override - public boolean hasGroupSupport() { - return true; - } -} diff --git a/common/src/main/java/me/lucko/luckperms/users/UserManager.java b/common/src/main/java/me/lucko/luckperms/users/UserManager.java index 28aff2c2..40352196 100644 --- a/common/src/main/java/me/lucko/luckperms/users/UserManager.java +++ b/common/src/main/java/me/lucko/luckperms/users/UserManager.java @@ -3,7 +3,6 @@ package me.lucko.luckperms.users; import lombok.Getter; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.LuckPermsPlugin; -import me.lucko.luckperms.data.Datastore; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import java.util.Map; @@ -74,16 +73,6 @@ public abstract class UserManager { } } - /** - * Saves a user object in the datastore - * @param user the user to save - * @param datastore the datastore - */ - public void saveUser(User user, Datastore datastore) { - user.refreshPermissions(); - datastore.saveUser(user, success -> {}); - } - /** * Set a user to the default group * @param user the user to give to