From dbc909a317463961bdcec6f4aaec21e465f04f7c Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 13 Mar 2017 19:10:36 +0000 Subject: [PATCH] Cleanup migration commands --- .../api/context/ImmutableContextSet.java | 2 +- .../api/context/MutableContextSet.java | 6 +- .../migration/MigrationBPermissions.java | 101 ++++------ .../migration/MigrationGroupManager.java | 179 +++++++----------- .../migration/MigrationPermissionsEx.java | 125 ++---------- .../migration/MigrationPowerfulPerms.java | 14 +- .../migration/MigrationZPermissions.java | 71 ++++--- .../migration/MigrationBungeePerms.java | 65 ++----- .../migration/MigrationMainCommand.java | 65 ++----- .../commands/migration/MigrationUtils.java | 55 ++++++ .../luckperms/common/constants/Patterns.java | 4 +- .../common/core/model/ImmutableNode.java | 11 +- .../migration/MigrationPermissionManager.java | 9 +- .../migration/MigrationPermissionsEx.java | 11 +- ...onUtils.java => SpongeMigrationUtils.java} | 20 +- 15 files changed, 295 insertions(+), 443 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/commands/migration/MigrationUtils.java rename sponge/src/main/java/me/lucko/luckperms/sponge/migration/{MigrationUtils.java => SpongeMigrationUtils.java} (89%) diff --git a/api/src/main/java/me/lucko/luckperms/api/context/ImmutableContextSet.java b/api/src/main/java/me/lucko/luckperms/api/context/ImmutableContextSet.java index ff851df07..8f17528dc 100644 --- a/api/src/main/java/me/lucko/luckperms/api/context/ImmutableContextSet.java +++ b/api/src/main/java/me/lucko/luckperms/api/context/ImmutableContextSet.java @@ -72,7 +72,7 @@ public final class ImmutableContextSet implements ContextSet { ImmutableMultimap.Builder b = ImmutableMultimap.builder(); for (Map.Entry e : map.entrySet()) { - b.put(e.getKey(), e.getValue()); + b.put(e.getKey().toLowerCase(), e.getValue()); } return new ImmutableContextSet(b.build()); diff --git a/api/src/main/java/me/lucko/luckperms/api/context/MutableContextSet.java b/api/src/main/java/me/lucko/luckperms/api/context/MutableContextSet.java index de4674c5d..1e259f337 100644 --- a/api/src/main/java/me/lucko/luckperms/api/context/MutableContextSet.java +++ b/api/src/main/java/me/lucko/luckperms/api/context/MutableContextSet.java @@ -284,7 +284,7 @@ public final class MutableContextSet implements ContextSet { */ public void addAll(Iterable> iterable) { if (iterable == null) { - throw new NullPointerException("contexts"); + throw new NullPointerException("iterable"); } for (Map.Entry e : iterable) { @@ -300,7 +300,7 @@ public final class MutableContextSet implements ContextSet { */ public void addAll(Map map) { if (map == null) { - throw new NullPointerException("contexts"); + throw new NullPointerException("map"); } addAll(map.entrySet()); } @@ -334,7 +334,7 @@ public final class MutableContextSet implements ContextSet { throw new NullPointerException("value"); } - map.entries().removeIf(entry -> entry.getKey().equalsIgnoreCase(key) && entry.getValue().equals(value)); + map.entries().removeIf(entry -> entry.getKey().equals(key) && entry.getValue().equals(value)); } /** diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java index f753bf4e3..7923f70ee 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationBPermissions.java @@ -29,12 +29,13 @@ import de.bananaco.bpermissions.api.Permission; import de.bananaco.bpermissions.api.World; import de.bananaco.bpermissions.api.WorldManager; -import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.core.model.PermissionHolder; import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; @@ -42,10 +43,10 @@ import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.ProgressLogger; import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; @@ -56,9 +57,6 @@ import static me.lucko.luckperms.common.constants.Permission.MIGRATION; public class MigrationBPermissions extends SubCommand { private static Field uConfigField; - private static Method getConfigurationSectionMethod = null; - private static Method getKeysMethod = null; - static { try { uConfigField = Class.forName("de.bananaco.bpermissions.imp.YamlWorld").getDeclaredField("uconfig"); @@ -89,7 +87,24 @@ public class MigrationBPermissions extends SubCommand { log.log("Forcing the plugin to load all data. This could take a while."); for (World world : worldManager.getAllWorlds()) { log.log("Loading users in world " + world.getName()); - Set users = getUsers(world); + + YamlConfiguration yamlWorldUsers = null; + try { + yamlWorldUsers = (YamlConfiguration) uConfigField.get(world); + } catch (Throwable t) { + t.printStackTrace(); + } + + if (yamlWorldUsers == null) { + continue; + } + + ConfigurationSection configSection = yamlWorldUsers.getConfigurationSection("users"); + if (configSection == null) { + continue; + } + + Set users = configSection.getKeys(false); if (users == null) { log.logErr("Couldn't get a list of users."); return CommandResult.FAILURE; @@ -111,7 +126,7 @@ public class MigrationBPermissions extends SubCommand { log.log("Starting group migration in world " + world.getName() + "."); AtomicInteger groupCount = new AtomicInteger(0); for (Calculable group : world.getAll(CalculableType.GROUP)) { - String groupName = group.getName().toLowerCase(); + String groupName = MigrationUtils.standardizeName(group.getName()); if (group.getName().equalsIgnoreCase(world.getDefaultGroup())) { groupName = "default"; } @@ -120,13 +135,16 @@ public class MigrationBPermissions extends SubCommand { plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join(); me.lucko.luckperms.common.core.model.Group lpGroup = plugin.getGroupManager().getIfLoaded(groupName); - migrateHolder(log, world, group, lpGroup); + MigrationUtils.setGroupWeight(lpGroup, group.getPriority()); + migrateHolder(world, group, lpGroup); + plugin.getStorage().saveGroup(lpGroup); log.logAllProgress("Migrated {} groups so far.", groupCount.incrementAndGet()); } log.log("Migrated " + groupCount.get() + " groups in world " + world.getName() + "."); + // Migrate all users log.log("Starting user migration in world " + world.getName() + "."); AtomicInteger userCount = new AtomicInteger(0); @@ -153,7 +171,7 @@ public class MigrationBPermissions extends SubCommand { plugin.getStorage().loadUser(uuid, "null").join(); User lpUser = plugin.getUserManager().get(uuid); - migrateHolder(log, world, user, lpUser); + migrateHolder(world, user, lpUser); plugin.getStorage().saveUser(lpUser); plugin.getUserManager().cleanup(lpUser); @@ -168,78 +186,35 @@ public class MigrationBPermissions extends SubCommand { return CommandResult.SUCCESS; } - @SuppressWarnings("unchecked") - private static Set getUsers(World world) { - try { - Object yamlWorldUsers = uConfigField.get(world); - if (getConfigurationSectionMethod == null) { - getConfigurationSectionMethod = yamlWorldUsers.getClass().getMethod("getConfigurationSection", String.class); - getConfigurationSectionMethod.setAccessible(true); - } - - Object configSection = getConfigurationSectionMethod.invoke(yamlWorldUsers, "users"); - if (configSection == null) { - return Collections.emptySet(); - } - - if (getKeysMethod == null) { - getKeysMethod = configSection.getClass().getMethod("getKeys", boolean.class); - getKeysMethod.setAccessible(true); - } - - return (Set) getKeysMethod.invoke(configSection, false); - } catch (Throwable t) { - t.printStackTrace(); - return null; - } - } - - private static void migrateHolder(ProgressLogger log, World world, Calculable c, PermissionHolder holder) { + private static void migrateHolder(World world, Calculable c, PermissionHolder holder) { // Migrate the groups permissions in this world for (Permission p : c.getPermissions()) { - try { - holder.setPermission(p.name(), p.isTrue(), "global", world.getName()); - } catch (Exception ex) { - log.handleException(ex); - } + holder.setPermissionUnchecked(NodeFactory.make(p.name(), p.isTrue(), "global", world.getName())); // Include any child permissions for (Map.Entry child : p.getChildren().entrySet()) { - try { - holder.setPermission(child.getKey(), child.getValue(), "global", world.getName()); - } catch (Exception ex) { - log.handleException(ex); - } + holder.setPermissionUnchecked(NodeFactory.make(child.getKey(), child.getValue(), "global", world.getName())); } } // Migrate any inherited groups for (Group parent : c.getGroups()) { - try { - holder.setPermission("group." + parent.getName(), true, "global", world.getName()); - } catch (Exception ex) { - log.handleException(ex); + String parentName = MigrationUtils.standardizeName(parent.getName()); + if (parent.getName().equalsIgnoreCase(world.getDefaultGroup())) { + parentName = "default"; } + + holder.setPermissionUnchecked(NodeFactory.make("group." + parentName, true, "global", world.getName())); } // Migrate existing meta for (Map.Entry meta : c.getMeta().entrySet()) { if (meta.getKey().equalsIgnoreCase("prefix") || meta.getKey().equalsIgnoreCase("suffix")) { - String chatMeta = MetaUtils.escapeCharacters(meta.getValue()); - try { - holder.setPermission(meta.getKey().toLowerCase() + "." + c.getPriority() + "." + chatMeta, true); - } catch (Exception ex) { - log.handleException(ex); - } + holder.setPermissionUnchecked(NodeFactory.makeChatMetaNode(meta.getKey().equalsIgnoreCase("prefix"), c.getPriority(), meta.getValue()).setWorld(world.getName()).build()); continue; } - try { - holder.setPermission("meta." + meta.getKey() + "." + meta.getValue(), true, "global", world.getName()); - } catch (Exception ex) { - log.handleException(ex); - } + holder.setPermissionUnchecked(NodeFactory.makeMetaNode(meta.getKey(), meta.getValue()).setWorld(world.getName()).build()); } } - } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java index fdcae01ee..16b5371ca 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationGroupManager.java @@ -22,20 +22,19 @@ package me.lucko.luckperms.bukkit.migration; -import com.google.common.collect.Maps; - +import me.lucko.luckperms.api.Node; import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.common.commands.Arg; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.constants.Permission; +import me.lucko.luckperms.common.core.NodeFactory; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.ProgressLogger; -import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; -import me.lucko.luckperms.exceptions.ObjectLacksException; import org.anjocaido.groupmanager.GlobalGroups; import org.anjocaido.groupmanager.GroupManager; @@ -47,17 +46,16 @@ import org.bukkit.Bukkit; import org.bukkit.World; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Function; import java.util.stream.Collectors; public class MigrationGroupManager extends SubCommand { - // group manager global groups contain the ":" character, which is not allowed in windows file names - private static final Function GROUP_RENAME_FUNCTION = s -> s.replace(':', '-').toLowerCase(); - public MigrationGroupManager() { super("groupmanager", "Migration from GroupManager", Permission.MIGRATION, Predicates.is(0), Arg.list(Arg.create("migrate as global", true, "if world permissions should be ignored, and just migrated as global")) @@ -76,8 +74,8 @@ public class MigrationGroupManager extends SubCommand { log.logErr("Was expecting true/false, but got " + args.get(0) + " instead."); return CommandResult.STATE_ERROR; } - boolean migrateAsGlobal = Boolean.parseBoolean(args.get(0)); - final Function worldMappingFunc = s -> migrateAsGlobal ? "" : s; + final boolean migrateAsGlobal = Boolean.parseBoolean(args.get(0)); + final Function worldMappingFunc = s -> migrateAsGlobal ? null : s; if (!Bukkit.getPluginManager().isPluginEnabled("GroupManager")) { log.logErr("Plugin not loaded."); @@ -85,7 +83,6 @@ public class MigrationGroupManager extends SubCommand { } List worlds = Bukkit.getWorlds().stream().map(World::getName).map(String::toLowerCase).collect(Collectors.toList()); - GroupManager gm = (GroupManager) Bukkit.getPluginManager().getPlugin("GroupManager"); // Migrate Global Groups @@ -94,34 +91,17 @@ public class MigrationGroupManager extends SubCommand { AtomicInteger globalGroupCount = new AtomicInteger(0); for (Group g : gg.getGroupList()) { - String name = GROUP_RENAME_FUNCTION.apply(g.getName()); + String groupName = MigrationUtils.standardizeName(g.getName()); - plugin.getStorage().createAndLoadGroup(name, CreationCause.INTERNAL).join(); - me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(name); + plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join(); + me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(groupName); for (String node : g.getPermissionList()) { - boolean value = true; - if (node.startsWith("!") || node.startsWith("-")) { - node = node.substring(1); - value = false; - } else if (node.startsWith("+")) { - node = node.substring(1); - value = true; - } - - try { - group.setPermission(node, value); - } catch (Exception ex) { - log.handleException(ex); - } + group.setPermissionUnchecked(MigrationUtils.parseNode(node, true).build()); } for (String s : g.getInherits()) { - try { - group.setPermission("group." + GROUP_RENAME_FUNCTION.apply(s), true); - } catch (Exception ex) { - log.handleException(ex); - } + group.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(s))); } plugin.getStorage().saveGroup(group); @@ -130,14 +110,9 @@ public class MigrationGroupManager extends SubCommand { log.log("Migrated " + globalGroupCount.get() + " global groups"); // Collect data - - // UUID --> Map, Boolean> where k=world, v = node - Map, Boolean>> users = new HashMap<>(); - // UUID --> primary group name + Map> users = new HashMap<>(); Map primaryGroups = new HashMap<>(); - - // String --> Map, Boolean> where k=world, v = node - Map, Boolean>> groups = new HashMap<>(); + Map> groups = new HashMap<>(); WorldsHolder wh = gm.getWorldsHolder(); @@ -150,27 +125,36 @@ public class MigrationGroupManager extends SubCommand { WorldDataHolder wdh = wh.getWorldData(world); AtomicInteger groupWorldCount = new AtomicInteger(0); - for (Group g : wdh.getGroupList()) { - String name = GROUP_RENAME_FUNCTION.apply(g.getName()); + for (Group group : wdh.getGroupList()) { + String groupName = MigrationUtils.standardizeName(group.getName()); - groups.putIfAbsent(name, new HashMap<>()); + groups.putIfAbsent(groupName, new HashSet<>()); - for (String node : g.getPermissionList()) { - boolean value = true; - if (node.startsWith("!") || node.startsWith("-")) { - node = node.substring(1); - value = false; - } else if (node.startsWith("+")) { - node = node.substring(1); - value = true; + for (String node : group.getPermissionList()) { + groups.get(groupName).add(MigrationUtils.parseNode(node, true).setWorld(worldMappingFunc.apply(world)).build()); + } + + for (String s : group.getInherits()) { + groups.get(groupName).add(NodeFactory.make("group." + MigrationUtils.standardizeName(s), true, null, worldMappingFunc.apply(world))); + } + + + String[] metaKeys = group.getVariables().getVarKeyList(); + for (String key : metaKeys) { + String value = group.getVariables().getVarString(key); + key = key.toLowerCase(); + + if (key.equals("build")) { + continue; } - groups.get(name).put(Maps.immutableEntry(worldMappingFunc.apply(world), node), value); + if (key.equals("prefix") || key.equals("suffix")) { + groups.get(groupName).add(NodeFactory.makeChatMetaNode(key.equals("prefix"), 50, value).setWorld(worldMappingFunc.apply(world)).build()); + } else { + groups.get(groupName).add(NodeFactory.makeMetaNode(key, value).setWorld(worldMappingFunc.apply(world)).build()); + } } - for (String s : g.getInherits()) { - groups.get(name).put(Maps.immutableEntry(worldMappingFunc.apply(world), "group." + GROUP_RENAME_FUNCTION.apply(s)), true); - } log.logAllProgress("Migrated {} groups so far in world " + world, groupWorldCount.incrementAndGet()); } log.log("Migrated " + groupWorldCount.get() + " groups in world " + world); @@ -185,35 +169,42 @@ public class MigrationGroupManager extends SubCommand { continue; } - users.putIfAbsent(uuid, new HashMap<>()); + users.putIfAbsent(uuid, new HashSet<>()); for (String node : user.getPermissionList()) { - boolean value = true; - if (node.startsWith("!") || node.startsWith("-")) { - node = node.substring(1); - value = false; - } else if (node.startsWith("+")) { - node = node.substring(1); - value = true; - } - - users.get(uuid).put(Maps.immutableEntry(worldMappingFunc.apply(world), node), value); + users.get(uuid).add(MigrationUtils.parseNode(node, true).setWorld(worldMappingFunc.apply(world)).build()); } - String finalWorld = worldMappingFunc.apply(world); - // Collect sub groups - users.get(uuid).putAll(user.subGroupListStringCopy().stream() - .map(n -> "group." + GROUP_RENAME_FUNCTION.apply(n)) - .map(n -> Maps.immutableEntry(finalWorld, n)) - .collect(Collectors.toMap(n -> n, n -> true)) + String finalWorld = worldMappingFunc.apply(world); + users.get(uuid).addAll(user.subGroupListStringCopy().stream() + .map(n -> "group." + MigrationUtils.standardizeName(n)) + .map(n -> NodeFactory.make(n, true, null, finalWorld)) + .collect(Collectors.toSet()) ); - primaryGroups.put(uuid, GROUP_RENAME_FUNCTION.apply(user.getGroupName())); + + // Get primary group + primaryGroups.put(uuid, MigrationUtils.standardizeName(user.getGroupName())); + + String[] metaKeys = user.getVariables().getVarKeyList(); + for (String key : metaKeys) { + String value = user.getVariables().getVarString(key); + key = key.toLowerCase(); + + if (key.equals("build")) { + continue; + } + + if (key.equals("prefix") || key.equals("suffix")) { + users.get(uuid).add(NodeFactory.makeChatMetaNode(key.equals("prefix"), 100, value).setWorld(worldMappingFunc.apply(world)).build()); + } else { + users.get(uuid).add(NodeFactory.makeMetaNode(key, value).setWorld(worldMappingFunc.apply(world)).build()); + } + } log.logProgress("Migrated {} users so far in world " + world, userWorldCount.incrementAndGet()); } log.log("Migrated " + userWorldCount.get() + " users in world " + world); - } log.log("All data has now been processed, now starting the import process."); @@ -221,23 +212,12 @@ public class MigrationGroupManager extends SubCommand { log.log("Starting group migration."); AtomicInteger groupCount = new AtomicInteger(0); - for (Map.Entry, Boolean>> e : groups.entrySet()) { + for (Map.Entry> e : groups.entrySet()) { plugin.getStorage().createAndLoadGroup(e.getKey(), CreationCause.INTERNAL).join(); me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(e.getKey()); - for (Map.Entry, Boolean> n : e.getValue().entrySet()) { - // n.key.key = world - // n.key.value = node - // n.value = true/false - try { - if (n.getKey().getKey().equals("")) { - group.setPermission(n.getKey().getValue(), n.getValue()); - } else { - group.setPermission(n.getKey().getValue(), n.getValue(), "global", n.getKey().getKey()); - } - } catch (Exception ex) { - log.handleException(ex); - } + for (Node node : e.getValue()) { + group.setPermissionUnchecked(node); } plugin.getStorage().saveGroup(group); @@ -247,34 +227,19 @@ public class MigrationGroupManager extends SubCommand { log.log("Starting user migration."); AtomicInteger userCount = new AtomicInteger(0); - for (Map.Entry, Boolean>> e : users.entrySet()) { + for (Map.Entry> e : users.entrySet()) { plugin.getStorage().loadUser(e.getKey(), "null").join(); me.lucko.luckperms.common.core.model.User user = plugin.getUserManager().get(e.getKey()); - for (Map.Entry, Boolean> n : e.getValue().entrySet()) { - // n.key.key = world - // n.key.value = node - // n.value = true/false - try { - if (n.getKey().getKey().equals("")) { - user.setPermission(n.getKey().getValue(), n.getValue()); - } else { - user.setPermission(n.getKey().getValue(), n.getValue(), "global", n.getKey().getKey()); - } - } catch (Exception ex) { - log.handleException(ex); - } + for (Node node : e.getValue()) { + user.setPermissionUnchecked(node); } String primaryGroup = primaryGroups.get(e.getKey()); if (primaryGroup != null) { - try { - user.setPermission("group." + primaryGroup, true); - } catch (ObjectAlreadyHasException ignored) {} + user.setPermissionUnchecked(NodeFactory.make("group." + primaryGroup)); user.setPrimaryGroup(primaryGroup); - try { - user.unsetPermission("group.default"); - } catch (ObjectLacksException ignored) {} + user.unsetPermissionUnchecked(NodeFactory.make("group.default")); } plugin.getStorage().saveUser(user); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java index d57618ee8..7343adaa7 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPermissionsEx.java @@ -22,11 +22,11 @@ package me.lucko.luckperms.bukkit.migration; -import me.lucko.luckperms.api.MetaUtils; import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeFactory; @@ -35,8 +35,6 @@ import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.ProgressLogger; -import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; -import me.lucko.luckperms.exceptions.ObjectLacksException; import org.bukkit.Bukkit; import org.bukkit.World; @@ -100,26 +98,15 @@ public class MigrationPermissionsEx extends SubCommand { for (PermissionGroup group : manager.getGroupList()) { int groupWeight = maxWeight - group.getRank(); - final String name = group.getName().toLowerCase(); - plugin.getStorage().createAndLoadGroup(name, CreationCause.INTERNAL).join(); - Group lpGroup = plugin.getGroupManager().getIfLoaded(name); + final String groupName = MigrationUtils.standardizeName(group.getName()); + plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join(); + Group lpGroup = plugin.getGroupManager().getIfLoaded(groupName); - lpGroup.removeIf(n -> n.getPermission().startsWith("weight.")); - lpGroup.setPermissionUnchecked(NodeFactory.make("weight." + groupWeight, true)); + MigrationUtils.setGroupWeight(lpGroup, groupWeight); try { for (String node : group.getOwnPermissions(null)) { - boolean value = true; - if (node.startsWith("-")) { - node = node.substring(1); - value = false; - } - - try { - lpGroup.setPermission(node, value); - } catch (Exception ex) { - log.handleException(ex); - } + lpGroup.setPermissionUnchecked(MigrationUtils.parseNode(node, true).build()); } } catch (NullPointerException ignored) { // No docs on if #getOwnPermissions(null) is ok. Should be fine though. @@ -127,35 +114,17 @@ public class MigrationPermissionsEx extends SubCommand { for (String world : worlds) { for (String node : group.getOwnPermissions(world)) { - boolean value = true; - if (node.startsWith("-")) { - node = node.substring(1); - value = false; - } - - try { - lpGroup.setPermission(node, value, "global", world.toLowerCase()); - } catch (Exception ex) { - log.handleException(ex); - } + lpGroup.setPermissionUnchecked(MigrationUtils.parseNode(node, true).setWorld(world.toLowerCase()).build()); } } for (PermissionGroup g : group.getParents()) { - try { - lpGroup.setPermission("group." + g.getName().toLowerCase(), true); - } catch (Exception ex) { - log.handleException(ex); - } + lpGroup.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(g.getName()))); } for (String world : worlds) { for (PermissionGroup g : group.getParents(world)) { - try { - lpGroup.setPermission("group." + g.getName().toLowerCase(), true, "global", world.toLowerCase()); - } catch (Exception ex) { - log.handleException(ex); - } + lpGroup.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(g.getName()), true, "global", world.toLowerCase())); } } @@ -163,21 +132,11 @@ public class MigrationPermissionsEx extends SubCommand { String suffix = group.getOwnSuffix(); if (prefix != null && !prefix.equals("")) { - prefix = MetaUtils.escapeCharacters(prefix); - try { - lpGroup.setPermission("prefix." + groupWeight + "." + prefix, true); - } catch (Exception ex) { - log.handleException(ex); - } + lpGroup.setPermissionUnchecked(NodeFactory.makePrefixNode(groupWeight, prefix).build()); } if (suffix != null && !suffix.equals("")) { - suffix = MetaUtils.escapeCharacters(suffix); - try { - lpGroup.setPermission("suffix." + groupWeight + "." + suffix, true); - } catch (Exception ex) { - log.handleException(ex); - } + lpGroup.setPermissionUnchecked(NodeFactory.makeSuffixNode(groupWeight, suffix).build()); } plugin.getStorage().saveGroup(lpGroup); @@ -217,17 +176,7 @@ public class MigrationPermissionsEx extends SubCommand { try { for (String node : user.getOwnPermissions(null)) { - boolean value = true; - if (node.startsWith("-")) { - node = node.substring(1); - value = false; - } - - try { - lpUser.setPermission(node, value); - } catch (Exception ex) { - log.handleException(ex); - } + lpUser.setPermissionUnchecked(MigrationUtils.parseNode(node, true).build()); } } catch (NullPointerException ignored) { // No docs on if #getOwnPermissions(null) is ok. Should be fine though. @@ -235,35 +184,17 @@ public class MigrationPermissionsEx extends SubCommand { for (String world : worlds) { for (String node : user.getOwnPermissions(world)) { - boolean value = true; - if (node.startsWith("-")) { - node = node.substring(1); - value = false; - } - - try { - lpUser.setPermission(node, value, "global", world.toLowerCase()); - } catch (Exception ex) { - log.handleException(ex); - } + lpUser.setPermissionUnchecked(MigrationUtils.parseNode(node, true).setWorld(world.toLowerCase()).build()); } } - for (String s : user.getGroupNames()) { - try { - lpUser.setPermission("group." + s.toLowerCase(), true); - } catch (Exception ex) { - log.handleException(ex); - } + for (String g : user.getGroupNames()) { + lpUser.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(g))); } for (String world : worlds) { - for (String s : user.getGroupNames(world)) { - try { - lpUser.setPermission("group." + s.toLowerCase(), true, "global", world.toLowerCase()); - } catch (Exception ex) { - log.handleException(ex); - } + for (String g : user.getGroupNames(world)) { + lpUser.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(g), true, "global", world.toLowerCase())); } } @@ -271,21 +202,11 @@ public class MigrationPermissionsEx extends SubCommand { String suffix = user.getOwnSuffix(); if (prefix != null && !prefix.equals("")) { - prefix = MetaUtils.escapeCharacters(prefix); - try { - lpUser.setPermission("prefix." + maxWeight + "." + prefix, true); - } catch (Exception ex) { - log.handleException(ex); - } + lpUser.setPermissionUnchecked(NodeFactory.makePrefixNode(maxWeight, prefix).build()); } if (suffix != null && !suffix.equals("")) { - suffix = MetaUtils.escapeCharacters(suffix); - try { - lpUser.setPermission("suffix." + maxWeight + "." + suffix, true); - } catch (Exception ex) { - log.handleException(ex); - } + lpUser.setPermissionUnchecked(NodeFactory.makeSuffixNode(maxWeight, suffix).build()); } // Lowest rank is the highest group #logic @@ -299,13 +220,9 @@ public class MigrationPermissionsEx extends SubCommand { } if (primary != null && !primary.equalsIgnoreCase("default")) { - try { - lpUser.setPermission("group." + primary.toLowerCase(), true); - } catch (ObjectAlreadyHasException ignored) {} + lpUser.setPermissionUnchecked(NodeFactory.make("group." + primary.toLowerCase())); lpUser.setPrimaryGroup(primary); - try { - lpUser.unsetPermission("group.default"); - } catch (ObjectLacksException ignored) {} + lpUser.unsetPermissionUnchecked(NodeFactory.make("group.default")); } plugin.getUserManager().cleanup(lpUser); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java index 4e46b0ae4..713870656 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationPowerfulPerms.java @@ -36,6 +36,7 @@ import me.lucko.luckperms.common.commands.Arg; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.core.NodeFactory; @@ -88,7 +89,7 @@ public class MigrationPowerfulPerms extends SubCommand { log.log("Starting."); if (!Bukkit.getPluginManager().isPluginEnabled("PowerfulPerms")) { - log.logErr("PowerfulPerms is not loaded."); + log.logErr("Plugin not loaded."); return CommandResult.STATE_ERROR; } @@ -168,12 +169,11 @@ public class MigrationPowerfulPerms extends SubCommand { for (Group g : groups) { maxWeight = Math.max(maxWeight, g.getRank()); - final String name = g.getName().toLowerCase(); - plugin.getStorage().createAndLoadGroup(name, CreationCause.INTERNAL).join(); - final me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(name); + final String groupName = MigrationUtils.standardizeName(g.getName()); + plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join(); + final me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(groupName); - group.removeIf(n -> n.getPermission().startsWith("weight.")); - group.setPermissionUnchecked(NodeFactory.make("weight." + g.getRank(), true)); + MigrationUtils.setGroupWeight(group, g.getRank()); for (Permission p : g.getOwnPermissions()) { applyPerm(group, p); @@ -321,7 +321,7 @@ public class MigrationPowerfulPerms extends SubCommand { private void applyGroup(PermissionManager pm, PermissionHolder holder, CachedGroup g, String server) { Group group = pm.getGroup(g.getGroupId()); - String node = "group." + group.getName(); + String node = "group." + MigrationUtils.standardizeName(group.getName()); long expireAt = 0L; if (g.willExpire()) { diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java index cd2c6c20e..c449fcfc6 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/migration/MigrationZPermissions.java @@ -26,6 +26,7 @@ import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeFactory; @@ -36,7 +37,6 @@ import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.ProgressLogger; -import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import org.bukkit.Bukkit; import org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService; @@ -66,12 +66,12 @@ public class MigrationZPermissions extends SubCommand { log.log("Starting."); if (!Bukkit.getPluginManager().isPluginEnabled("zPermissions")) { - log.logErr("zPermissions is not loaded."); + log.logErr("Plugin not loaded."); return CommandResult.STATE_ERROR; } if (!Bukkit.getServicesManager().isProvidedFor(ZPermissionsService.class)) { - log.logErr("zPermissions is not loaded."); + log.logErr("Plugin not loaded."); return CommandResult.STATE_ERROR; } @@ -91,8 +91,10 @@ public class MigrationZPermissions extends SubCommand { log.log("Starting group migration."); AtomicInteger groupCount = new AtomicInteger(0); for (String g : service.getAllGroups()) { - plugin.getStorage().createAndLoadGroup(g.toLowerCase(), CreationCause.INTERNAL).join(); - Group group = plugin.getGroupManager().getIfLoaded(g.toLowerCase()); + String groupName = MigrationUtils.standardizeName(g); + + plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join(); + Group group = plugin.getGroupManager().getIfLoaded(groupName); PermissionEntity entity = internalService.getEntity(g, null, true); migrateEntity(group, entity, null); @@ -106,10 +108,13 @@ public class MigrationZPermissions extends SubCommand { log.log("Starting track migration."); AtomicInteger trackCount = new AtomicInteger(0); for (String t : service.getAllTracks()) { - plugin.getStorage().createAndLoadTrack(t.toLowerCase(), CreationCause.INTERNAL).join(); - Track track = plugin.getTrackManager().getIfLoaded(t.toLowerCase()); + String trackName = MigrationUtils.standardizeName(t); + + plugin.getStorage().createAndLoadTrack(trackName, CreationCause.INTERNAL).join(); + Track track = plugin.getTrackManager().getIfLoaded(trackName); track.setGroups(service.getTrackGroups(t)); plugin.getStorage().saveTrack(track); + log.logAllProgress("Migrated {} tracks so far.", trackCount.incrementAndGet()); } log.log("Migrated " + trackCount.get() + " tracks"); @@ -118,18 +123,17 @@ public class MigrationZPermissions extends SubCommand { log.log("Starting user migration."); AtomicInteger userCount = new AtomicInteger(0); for (UUID u : service.getAllPlayersUUID()) { - plugin.getStorage().loadUser(u, "null").join(); - User user = plugin.getUserManager().get(u); - PermissionEntity entity = internalService.getEntity(null, u, false); - migrateEntity(user, entity, internalService.getGroups(u)); - - user.setPrimaryGroup(service.getPlayerPrimaryGroup(u)); - + String username = null; if (!entity.isGroup()) { - user.setName(entity.getDisplayName()); + username = entity.getDisplayName(); } + plugin.getStorage().loadUser(u, username).join(); + User user = plugin.getUserManager().get(u); + migrateEntity(user, entity, internalService.getGroups(u)); + user.setPrimaryGroup(MigrationUtils.standardizeName(service.getPlayerPrimaryGroup(u))); + plugin.getUserManager().cleanup(user); plugin.getStorage().saveUser(user); log.logProgress("Migrated {} users so far.", userCount.incrementAndGet()); @@ -140,41 +144,36 @@ public class MigrationZPermissions extends SubCommand { return CommandResult.SUCCESS; } - private void migrateEntity(PermissionHolder group, PermissionEntity entity, List memberships) { + private void migrateEntity(PermissionHolder holder, PermissionEntity entity, List memberships) { for (Entry e : entity.getPermissions()) { - if (e.getWorld() != null) { - try { - group.setPermission(e.getPermission(), true, "global", e.getWorld().getName()); - } catch (ObjectAlreadyHasException ignored) {} + if (e.getWorld() != null && !e.getWorld().getName().equals("")) { + holder.setPermissionUnchecked(MigrationUtils.parseNode(e.getPermission(), true).setWorld(e.getWorld().getName()).build()); } else { - try { - group.setPermission(e.getPermission(), true); // TODO handle negated. - } catch (ObjectAlreadyHasException ignored) {} + holder.setPermissionUnchecked(MigrationUtils.parseNode(e.getPermission(), true).build()); } } if (entity.isGroup()) { + // entity.getMemberships() doesn't work for groups (always returns 0 records) for (Inheritance inheritance : entity.getInheritancesAsChild()) { - try { - if (!inheritance.getParent().getName().equals(group.getObjectName())) { - group.setPermission("group." + inheritance.getParent().getName(), true); - } - } catch (ObjectAlreadyHasException ignored) {} + if (!inheritance.getParent().getName().equals(holder.getObjectName())) { + holder.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(inheritance.getParent().getName()))); + } } } else { - // entity.getMemberships() doesn't work (always returns 0 records) for (Membership membership : memberships) { - try { - group.setPermission("group." + membership.getGroup().getDisplayName(), true); - } catch (ObjectAlreadyHasException ignored) {} + holder.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(membership.getGroup().getDisplayName()))); } } + int weight = entity.isGroup() ? 50 : 100; for (EntityMetadata metadata : entity.getMetadata()) { - try { - group.setPermission(NodeFactory.makeMetaNode(metadata.getName(), metadata.getStringValue()).build()); - } catch (Exception e) { - e.printStackTrace(); + String key = metadata.getName().toLowerCase(); + + if (key.equals("prefix") || key.equals("suffix")) { + holder.setPermissionUnchecked(NodeFactory.makeChatMetaNode(key.equals("prefix"), weight, metadata.getStringValue()).build()); + } else { + holder.setPermissionUnchecked(NodeFactory.makeMetaNode(key, metadata.getStringValue()).build()); } } } diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java b/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java index f8f8e7e3a..dab89cf43 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/migration/MigrationBungeePerms.java @@ -26,6 +26,7 @@ import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.core.NodeFactory; @@ -79,52 +80,34 @@ public class MigrationBungeePerms extends SubCommand { int groupWeight = maxWeight - g.getRank(); // Make a LuckPerms group for the one being migrated - plugin.getStorage().createAndLoadGroup(g.getName().toLowerCase(), CreationCause.INTERNAL).join(); - me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(g.getName().toLowerCase()); + String groupName = MigrationUtils.standardizeName(g.getName()); + plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join(); + me.lucko.luckperms.common.core.model.Group group = plugin.getGroupManager().getIfLoaded(groupName); - group.removeIf(n -> n.getPermission().startsWith("weight.")); - group.setPermissionUnchecked(NodeFactory.make("weight." + groupWeight, true)); + MigrationUtils.setGroupWeight(group, groupWeight); // Migrate global perms for (String perm : g.getPerms()) { - boolean value = true; - if (perm.startsWith("-") || perm.startsWith("!")) { - perm = perm.substring(1); - value = false; - } - - group.setPermissionUnchecked(NodeFactory.make(perm, value)); + group.setPermissionUnchecked(MigrationUtils.parseNode(perm, true).build()); } // Migrate per-server perms for (Map.Entry e : g.getServers().entrySet()) { for (String perm : e.getValue().getPerms()) { - boolean value = true; - if (perm.startsWith("-") || perm.startsWith("!")) { - perm = perm.substring(1); - value = false; - } - - group.setPermissionUnchecked(NodeFactory.make(perm, value, e.getKey())); + group.setPermissionUnchecked(MigrationUtils.parseNode(perm, true).setWorld(e.getKey()).build()); } // Migrate per-world perms for (Map.Entry we : e.getValue().getWorlds().entrySet()) { for (String perm : we.getValue().getPerms()) { - boolean value = true; - if (perm.startsWith("-") || perm.startsWith("!")) { - perm = perm.substring(1); - value = false; - } - - group.setPermissionUnchecked(NodeFactory.make(perm, value, e.getKey(), we.getKey())); + group.setPermissionUnchecked(MigrationUtils.parseNode(perm, true).setServer(e.getKey()).setWorld(we.getKey()).build()); } } } // Migrate any parent groups for (String inherit : g.getInheritances()) { - group.setPermissionUnchecked(NodeFactory.make("group." + inherit)); + group.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(inherit))); } // Migrate prefix and suffix @@ -134,7 +117,6 @@ public class MigrationBungeePerms extends SubCommand { if (prefix != null && !prefix.equals("")) { group.setPermissionUnchecked(NodeFactory.makePrefixNode(groupWeight, prefix).build()); } - if (suffix != null && !suffix.equals("")) { group.setPermissionUnchecked(NodeFactory.makeSuffixNode(groupWeight, suffix).build()); } @@ -158,49 +140,31 @@ public class MigrationBungeePerms extends SubCommand { } // Make a LuckPerms user for the one being migrated. - plugin.getStorage().loadUser(u.getUUID(), "null").join(); + plugin.getStorage().loadUser(u.getUUID(), u.getName()).join(); me.lucko.luckperms.common.core.model.User user = plugin.getUserManager().get(u.getUUID()); // Migrate global perms for (String perm : u.getPerms()) { - boolean value = true; - if (perm.startsWith("-") || perm.startsWith("!")) { - perm = perm.substring(1); - value = false; - } - - user.setPermissionUnchecked(NodeFactory.make(perm, value)); + user.setPermissionUnchecked(MigrationUtils.parseNode(perm, true).build()); } // Migrate per-server perms for (Map.Entry e : u.getServers().entrySet()) { for (String perm : e.getValue().getPerms()) { - boolean value = true; - if (perm.startsWith("-") || perm.startsWith("!")) { - perm = perm.substring(1); - value = false; - } - - user.setPermissionUnchecked(NodeFactory.make(perm, value, e.getKey())); + user.setPermissionUnchecked(MigrationUtils.parseNode(perm, true).setWorld(e.getKey()).build()); } // Migrate per-world perms for (Map.Entry we : e.getValue().getWorlds().entrySet()) { for (String perm : we.getValue().getPerms()) { - boolean value = true; - if (perm.startsWith("-") || perm.startsWith("!")) { - perm = perm.substring(1); - value = false; - } - - user.setPermissionUnchecked(NodeFactory.make(perm, value, e.getKey(), we.getKey())); + user.setPermissionUnchecked(MigrationUtils.parseNode(perm, true).setServer(e.getKey()).setWorld(we.getKey()).build()); } } } // Migrate groups for (String group : u.getGroupsString()) { - user.setPermissionUnchecked(NodeFactory.make("group." + group)); + user.setPermissionUnchecked(NodeFactory.make("group." + MigrationUtils.standardizeName(group))); } // Migrate prefix & suffix @@ -210,7 +174,6 @@ public class MigrationBungeePerms extends SubCommand { if (prefix != null && !prefix.equals("")) { user.setPermissionUnchecked(NodeFactory.makePrefixNode(maxWeight, prefix).build()); } - if (suffix != null && !suffix.equals("")) { user.setPermissionUnchecked(NodeFactory.makeSuffixNode(maxWeight, suffix).build()); } diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/migration/MigrationMainCommand.java b/common/src/main/java/me/lucko/luckperms/common/commands/migration/MigrationMainCommand.java index ab63672db..b1506abb3 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/migration/MigrationMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/migration/MigrationMainCommand.java @@ -22,6 +22,8 @@ package me.lucko.luckperms.common.commands.migration; +import com.google.common.collect.ImmutableMap; + import me.lucko.luckperms.common.commands.Command; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; @@ -35,60 +37,31 @@ import me.lucko.luckperms.common.utils.Predicates; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; public class MigrationMainCommand extends MainCommand { + private static final Map PLUGINS = ImmutableMap.builder() + .put("org.anjocaido.groupmanager.GroupManager", "me.lucko.luckperms.bukkit.migration.MigrationGroupManager") + .put("ru.tehkode.permissions.bukkit.PermissionsEx", "me.lucko.luckperms.bukkit.migration.MigrationPermissionsEx") + .put("com.github.cheesesoftware.PowerfulPermsAPI.PowerfulPermsPlugin", "me.lucko.luckperms.bukkit.migration.MigrationPowerfulPerms") + .put("org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService", "me.lucko.luckperms.bukkit.migration.MigrationZPermissions") + .put("net.alpenblock.bungeeperms.BungeePerms", "me.lucko.luckperms.bungee.migration.MigrationBungeePerms") + .put("de.bananaco.bpermissions.api.WorldManager", "me.lucko.luckperms.bukkit.migration.MigrationBPermissions") + .put("ninja.leaping.permissionsex.sponge.PermissionsExPlugin", "me.lucko.luckperms.sponge.migration.MigrationPermissionsEx") + .put("io.github.djxy.permissionmanager.PermissionManager", "me.lucko.luckperms.sponge.migration.MigrationPermissionManager") + .build(); + @SuppressWarnings("unchecked") private static List> getAvailableCommands() { List> l = new ArrayList<>(); - try { - Class.forName("org.anjocaido.groupmanager.GroupManager"); - l.add((SubCommand) Class.forName("me.lucko.luckperms.bukkit.migration.MigrationGroupManager").newInstance()); - } catch (Throwable ignored) { - } - - try { - Class.forName("ru.tehkode.permissions.bukkit.PermissionsEx"); - l.add((SubCommand) Class.forName("me.lucko.luckperms.bukkit.migration.MigrationPermissionsEx").newInstance()); - } catch (Throwable ignored) { - } - - try { - Class.forName("com.github.cheesesoftware.PowerfulPermsAPI.PowerfulPermsPlugin"); - l.add((SubCommand) Class.forName("me.lucko.luckperms.bukkit.migration.MigrationPowerfulPerms").newInstance()); - } catch (Throwable ignored) { - } - - try { - Class.forName("org.tyrannyofheaven.bukkit.zPermissions.ZPermissionsService"); - l.add((SubCommand) Class.forName("me.lucko.luckperms.bukkit.migration.MigrationZPermissions").newInstance()); - } catch (Throwable ignored) { - } - - try { - Class.forName("net.alpenblock.bungeeperms.BungeePerms"); - l.add((SubCommand) Class.forName("me.lucko.luckperms.bungee.migration.MigrationBungeePerms").newInstance()); - } catch (Throwable ignored) { - } - - try { - Class.forName("de.bananaco.bpermissions.api.WorldManager"); - l.add((SubCommand) Class.forName("me.lucko.luckperms.bukkit.migration.MigrationBPermissions").newInstance()); - } catch (Throwable ignored) { - } - - try { - Class.forName("ninja.leaping.permissionsex.sponge.PermissionsExPlugin"); - l.add((SubCommand) Class.forName("me.lucko.luckperms.sponge.migration.MigrationPermissionsEx").newInstance()); - } catch (Throwable ignored) { - } - - try { - Class.forName("io.github.djxy.permissionmanager.PermissionManager"); - l.add((SubCommand) Class.forName("me.lucko.luckperms.sponge.migration.MigrationPermissionManager").newInstance()); - } catch (Throwable ignored) { + for (Map.Entry plugin : PLUGINS.entrySet()) { + try { + Class.forName(plugin.getKey()); + l.add((SubCommand) Class.forName(plugin.getValue()).newInstance()); + } catch (Throwable ignored) {} } return l.stream().collect(Collectors.toList()); diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/migration/MigrationUtils.java b/common/src/main/java/me/lucko/luckperms/common/commands/migration/MigrationUtils.java new file mode 100644 index 000000000..5845c0679 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/commands/migration/MigrationUtils.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.luckperms.common.commands.migration; + +import lombok.experimental.UtilityClass; + +import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.common.core.NodeFactory; +import me.lucko.luckperms.common.core.model.Group; + +@UtilityClass +public class MigrationUtils { + + public static Node.Builder parseNode(String permission, boolean value) { + if (permission.startsWith("-") || permission.startsWith("!")) { + permission = permission.substring(1); + value = false; + } else if (permission.startsWith("+")) { + permission = permission.substring(1); + value = true; + } + + return NodeFactory.newBuilder(permission).setValue(value); + } + + public static void setGroupWeight(Group group, int weight) { + group.removeIf(n -> n.getPermission().startsWith("weight.")); + group.setPermissionUnchecked(NodeFactory.make("weight." + weight)); + } + + public static String standardizeName(String string) { + return string.trim().replace(':', '-').replace(' ', '-').replace('.', '-').toLowerCase(); + } + +} diff --git a/common/src/main/java/me/lucko/luckperms/common/constants/Patterns.java b/common/src/main/java/me/lucko/luckperms/common/constants/Patterns.java index 10632d424..312658105 100644 --- a/common/src/main/java/me/lucko/luckperms/common/constants/Patterns.java +++ b/common/src/main/java/me/lucko/luckperms/common/constants/Patterns.java @@ -61,8 +61,8 @@ public class Patterns { }); public static final Pattern COMMAND_SEPARATOR = Pattern.compile(" (?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)"); - public static final Pattern NON_ALPHA_NUMERIC = Pattern.compile("[\\/\\$\\.\\- ]"); - public static final Pattern NON_ALPHA_NUMERIC_SPACE = Pattern.compile("[\\/\\$\\.\\-]"); + public static final Pattern NON_ALPHA_NUMERIC = Pattern.compile("[\\/\\$\\. ]"); + public static final Pattern NON_ALPHA_NUMERIC_SPACE = Pattern.compile("[\\/\\$\\.]"); public static final Pattern NON_USERNAME = Pattern.compile("[^A-Za-z0-9_ ]"); public static final Pattern STRIP_COLOR_PATTERN = Pattern.compile("(?i)" + String.valueOf('ยง') + "[0-9A-FK-OR]"); public static final Pattern NODE_CONTEXTS = Pattern.compile("\\(.+\\).*"); diff --git a/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java b/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java index 2878b1610..31002d3eb 100644 --- a/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java +++ b/common/src/main/java/me/lucko/luckperms/common/core/model/ImmutableNode.java @@ -195,11 +195,18 @@ public class ImmutableNode implements Node { throw new IllegalArgumentException("Empty permission"); } - if (server != null && (server.equalsIgnoreCase("global") || server.equals(""))) { + if (server != null) { + server = server.toLowerCase(); + } + if (world != null) { + world = world.toLowerCase(); + } + + if (server != null && (server.equals("global") || server.equals(""))) { server = null; } - if (world != null && (world.equalsIgnoreCase("global") || world.equals(""))) { + if (world != null && (world.equals("global") || world.equals(""))) { world = null; } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java index a5e3d9180..287461c33 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionManager.java @@ -26,6 +26,7 @@ import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.Util; import me.lucko.luckperms.common.constants.Permission; @@ -50,7 +51,7 @@ import java.util.Optional; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; -import static me.lucko.luckperms.sponge.migration.MigrationUtils.migrateSubject; +import static me.lucko.luckperms.sponge.migration.SpongeMigrationUtils.migrateSubject; public class MigrationPermissionManager extends SubCommand { public MigrationPermissionManager() { @@ -88,12 +89,12 @@ public class MigrationPermissionManager extends SubCommand { // Migrate defaults log.log("Migrating default subjects."); for (SubjectCollection collection : pmService.getKnownSubjects().values()) { - MigrationUtils.migrateSubjectData( + SpongeMigrationUtils.migrateSubjectData( collection.getDefaults().getSubjectData(), lpService.getSubjects("defaults").get(collection.getIdentifier()).getSubjectData() ); } - MigrationUtils.migrateSubjectData(pmService.getDefaults().getSubjectData(), lpService.getDefaults().getSubjectData()); + SpongeMigrationUtils.migrateSubjectData(pmService.getDefaults().getSubjectData(), lpService.getDefaults().getSubjectData()); // Migrate groups log.log("Starting group migration."); @@ -108,7 +109,7 @@ public class MigrationPermissionManager extends SubCommand { AtomicInteger groupCount = new AtomicInteger(0); for (Subject pmGroup : pmService.getGroupSubjects().getAllSubjects()) { - String pmName = MigrationUtils.convertName(pmGroup.getIdentifier()); + String pmName = MigrationUtils.standardizeName(pmGroup.getIdentifier()); // Make a LuckPerms group for the one being migrated plugin.getStorage().createAndLoadGroup(pmName, CreationCause.INTERNAL).join(); diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java index c956c19c2..9db6f6fe5 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationPermissionsEx.java @@ -26,6 +26,7 @@ import me.lucko.luckperms.api.event.cause.CreationCause; import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandResult; import me.lucko.luckperms.common.commands.SubCommand; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.utils.Util; import me.lucko.luckperms.common.constants.Permission; @@ -55,7 +56,7 @@ import java.util.TreeMap; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; -import static me.lucko.luckperms.sponge.migration.MigrationUtils.migrateSubject; +import static me.lucko.luckperms.sponge.migration.SpongeMigrationUtils.migrateSubject; public class MigrationPermissionsEx extends SubCommand { public MigrationPermissionsEx() { @@ -84,12 +85,12 @@ public class MigrationPermissionsEx extends SubCommand { // Migrate defaults log.log("Migrating default subjects."); for (SubjectCollection collection : pexService.getKnownSubjects().values()) { - MigrationUtils.migrateSubjectData( + SpongeMigrationUtils.migrateSubjectData( collection.getDefaults().getSubjectData(), lpService.getSubjects("defaults").get(collection.getIdentifier()).getSubjectData() ); } - MigrationUtils.migrateSubjectData(pexService.getDefaults().getSubjectData(), lpService.getDefaults().getSubjectData()); + SpongeMigrationUtils.migrateSubjectData(pexService.getDefaults().getSubjectData(), lpService.getDefaults().getSubjectData()); log.log("Calculating group weightings."); int maxWeight = 0; @@ -110,7 +111,7 @@ public class MigrationPermissionsEx extends SubCommand { log.log("Starting group migration."); AtomicInteger groupCount = new AtomicInteger(0); for (Subject pexGroup : pexService.getGroupSubjects().getAllSubjects()) { - String pexName = MigrationUtils.convertName(pexGroup.getIdentifier()); + String pexName = MigrationUtils.standardizeName(pexGroup.getIdentifier()); Optional rankString = pexGroup.getOption("rank"); OptionalInt rank = OptionalInt.empty(); @@ -135,7 +136,7 @@ public class MigrationPermissionsEx extends SubCommand { // Pull track data Optional track = pexGroup.getOption("rank-ladder"); if (track.isPresent() && rank.isPresent()) { - String trackName = MigrationUtils.convertName(track.get()); + String trackName = MigrationUtils.standardizeName(track.get()); if (!tracks.containsKey(trackName)) { tracks.put(trackName, new TreeMap<>(Comparator.reverseOrder())); } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/SpongeMigrationUtils.java similarity index 89% rename from sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java rename to sponge/src/main/java/me/lucko/luckperms/sponge/migration/SpongeMigrationUtils.java index 74c5468f8..8db4d3592 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/migration/MigrationUtils.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/migration/SpongeMigrationUtils.java @@ -25,11 +25,12 @@ package me.lucko.luckperms.sponge.migration; import lombok.experimental.UtilityClass; import me.lucko.luckperms.api.context.ContextSet; +import me.lucko.luckperms.common.commands.migration.MigrationUtils; import me.lucko.luckperms.common.core.NodeBuilder; import me.lucko.luckperms.common.core.NodeFactory; +import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.PermissionHolder; import me.lucko.luckperms.common.utils.ExtractedContexts; -import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.sponge.service.proxy.Util; import org.spongepowered.api.service.context.Context; @@ -43,12 +44,13 @@ import java.util.Map; import java.util.Set; @UtilityClass -public class MigrationUtils { +public class SpongeMigrationUtils { public static void migrateSubject(Subject subject, PermissionHolder holder, int priority) { - holder.removeIf(n -> n.getPermission().startsWith("weight.")); - holder.setPermissionUnchecked(NodeFactory.make("weight." + priority, true)); + if (holder instanceof Group) { + MigrationUtils.setGroupWeight((Group) holder, priority); + } // Migrate permissions Map, Map> perms = subject.getSubjectData().getAllPermissions(); @@ -61,9 +63,7 @@ public class MigrationUtils { String world = extractedContexts.getWorld(); for (Map.Entry perm : e.getValue().entrySet()) { - try { - holder.setPermission(new NodeBuilder(perm.getKey()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(perm.getValue()).build()); - } catch (ObjectAlreadyHasException ignored) {} + holder.setPermissionUnchecked(new NodeBuilder(perm.getKey()).setServer(server).setWorld(world).withExtraContext(contexts).setValue(perm.getValue()).build()); } } @@ -107,7 +107,7 @@ public class MigrationUtils { continue; // LuckPerms does not support persisting other subject types. } - holder.setPermissionUnchecked(new NodeBuilder("group." + convertName(s.getIdentifier())).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); + holder.setPermissionUnchecked(new NodeBuilder("group." + MigrationUtils.standardizeName(s.getIdentifier())).setServer(server).setWorld(world).withExtraContext(contexts).setValue(true).build()); } } } @@ -132,8 +132,4 @@ public class MigrationUtils { } } - public static String convertName(String s) { - return s.replace(' ', '_').toLowerCase(); - } - }