From a1a2e189b69f50c43ae8c997ba47c3c4045387b1 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 1 Feb 2018 20:31:54 +0000 Subject: [PATCH] Refactor PEX primary group detection to exclude temporary parents --- .../migration/MigrationPermissionsEx.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) 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 0de9f1fd..a0ce44c2 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 @@ -173,22 +173,6 @@ public class MigrationPermissionsEx extends SubCommand { // migrate data migrateEntity(user, lpUser, userWeight); - // Lowest rank is the highest group #logic - String primary = null; - int weight = Integer.MAX_VALUE; - for (PermissionGroup group : user.getOwnParents()) { - if (group.getRank() < weight) { - primary = group.getName(); - weight = group.getRank(); - } - } - - if (primary != null && !primary.isEmpty() && !primary.equalsIgnoreCase(NodeFactory.DEFAULT_GROUP_NAME)) { - lpUser.setPermission(NodeFactory.buildGroupNode(primary.toLowerCase()).build()); - lpUser.getPrimaryGroup().setStoredValue(primary); - lpUser.unsetPermission(NodeFactory.buildGroupNode(NodeFactory.DEFAULT_GROUP_NAME).build()); - } - plugin.getUserManager().cleanup(lpUser); plugin.getStorage().saveUser(lpUser); log.logProgress("Migrated {} users so far.", userCount.incrementAndGet()); @@ -242,9 +226,13 @@ public class MigrationPermissionsEx extends SubCommand { } // migrate parents - Map> parents = entity.getAllParents(); - for (Map.Entry> worldData : parents.entrySet()) { + for (Map.Entry> worldData : entity.getAllParents().entrySet()) { String world = standardizeWorld(worldData.getKey()); + + // keep track of primary group + String primary = null; + int primaryWeight = Integer.MAX_VALUE; + for (PermissionGroup parent : worldData.getValue()) { String parentName = parent.getName(); long expiry = 0L; @@ -262,6 +250,20 @@ public class MigrationPermissionsEx extends SubCommand { } holder.setPermission(NodeFactory.buildGroupNode(MigrationUtils.standardizeName(parentName)).setWorld(world).setExpiry(expiry).build()); + + // migrate primary groups + if (world == null && holder instanceof User && expiry == 0) { + if (parent.getRank() < primaryWeight) { + primary = parent.getName(); + primaryWeight = parent.getRank(); + } + } + } + + if (primary != null && !primary.isEmpty() && !primary.equalsIgnoreCase(NodeFactory.DEFAULT_GROUP_NAME)) { + User user = ((User) holder); + user.getPrimaryGroup().setStoredValue(primary); + user.unsetPermission(NodeFactory.buildGroupNode(NodeFactory.DEFAULT_GROUP_NAME).build()); } } @@ -278,8 +280,7 @@ public class MigrationPermissionsEx extends SubCommand { } // migrate options - Map> options = entity.getAllOptions(); - for (Map.Entry> worldData : options.entrySet()) { + for (Map.Entry> worldData : entity.getAllOptions().entrySet()) { String world = standardizeWorld(worldData.getKey()); for (Map.Entry opt : worldData.getValue().entrySet()) { if (opt.getKey() == null || opt.getKey().isEmpty() || opt.getValue() == null || opt.getValue().isEmpty()) {