Don't attempt to migrate empty users in PEX migration (#2102)

This commit is contained in:
Luck 2020-03-26 22:32:53 +00:00
parent 24304a61a7
commit 3553a0f9fe
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -67,6 +67,7 @@ import ru.tehkode.permissions.events.PermissionEvent;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -78,6 +79,7 @@ import java.util.stream.Collectors;
public class MigrationPermissionsEx extends ChildCommand<Object> { public class MigrationPermissionsEx extends ChildCommand<Object> {
private static final Method GET_DATA_METHOD; private static final Method GET_DATA_METHOD;
private static final Method GET_USER_METHOD;
private static final Field TIMED_PERMISSIONS_FIELD; private static final Field TIMED_PERMISSIONS_FIELD;
private static final Field TIMED_PERMISSIONS_TIME_FIELD; private static final Field TIMED_PERMISSIONS_TIME_FIELD;
private static final Field NATIVE_INTERFACE_FIELD; private static final Field NATIVE_INTERFACE_FIELD;
@ -86,6 +88,9 @@ public class MigrationPermissionsEx extends ChildCommand<Object> {
GET_DATA_METHOD = PermissionEntity.class.getDeclaredMethod("getData"); GET_DATA_METHOD = PermissionEntity.class.getDeclaredMethod("getData");
GET_DATA_METHOD.setAccessible(true); GET_DATA_METHOD.setAccessible(true);
GET_USER_METHOD = PermissionManager.class.getDeclaredMethod("getUser", String.class, String.class, boolean.class);
GET_USER_METHOD.setAccessible(true);
TIMED_PERMISSIONS_FIELD = PermissionEntity.class.getDeclaredField("timedPermissions"); TIMED_PERMISSIONS_FIELD = PermissionEntity.class.getDeclaredField("timedPermissions");
TIMED_PERMISSIONS_FIELD.setAccessible(true); TIMED_PERMISSIONS_FIELD.setAccessible(true);
@ -181,8 +186,14 @@ public class MigrationPermissionsEx extends ChildCommand<Object> {
// Increment the max weight from the group migrations. All user meta should override. // Increment the max weight from the group migrations. All user meta should override.
int userWeight = maxWeight + 5; int userWeight = maxWeight + 5;
Iterators.tryIterate(manager.getUsers(), user -> { Collection<String> userIdentifiers = manager.getBackend().getUserIdentifiers();
UUID u = BukkitUuids.lookupUuid(log, user.getIdentifier()); Iterators.tryIterate(userIdentifiers, id -> {
PermissionUser user = (PermissionUser) GET_USER_METHOD.invoke(manager, id, null, false);
if (isUserEmpty(user)) {
return;
}
UUID u = BukkitUuids.lookupUuid(log, id);
if (u == null) { if (u == null) {
return; return;
} }
@ -221,6 +232,28 @@ public class MigrationPermissionsEx extends ChildCommand<Object> {
} }
} }
private static boolean isUserEmpty(PermissionUser user) {
for (List<String> permissions : user.getAllPermissions().values()) {
if (!permissions.isEmpty()) {
return false;
}
}
for (List<PermissionGroup> parents : user.getAllParents().values()) {
if (!parents.isEmpty()) {
return false;
}
}
for (Map<String, String> options : user.getAllOptions().values()) {
if (!options.isEmpty()) {
return false;
}
}
return true;
}
private static void migrateEntity(PermissionEntity entity, PermissionHolder holder, int weight) { private static void migrateEntity(PermissionEntity entity, PermissionHolder holder, int weight) {
// migrate permanent permissions // migrate permanent permissions
for (Map.Entry<String, List<String>> worldData : getPermanentPermissions(entity).entrySet()) { for (Map.Entry<String, List<String>> worldData : getPermanentPermissions(entity).entrySet()) {