Change Vault hook to use new per-world perms system

This commit is contained in:
Luck 2016-07-25 20:20:51 +01:00
parent ebeb69dd3a
commit c7336dbcad
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
6 changed files with 200 additions and 166 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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