Copy usernames in the GroupManager migration (#883)

This commit is contained in:
Luck 2018-04-02 13:34:30 +01:00
parent c8bb85a06b
commit 3453f05aca
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 36 additions and 14 deletions

View File

@ -39,9 +39,11 @@ import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.NodeFactory; import me.lucko.luckperms.common.node.NodeFactory;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.references.UserIdentifier;
import me.lucko.luckperms.common.sender.Sender; import me.lucko.luckperms.common.sender.Sender;
import me.lucko.luckperms.common.utils.Iterators; import me.lucko.luckperms.common.utils.Iterators;
import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.Predicates;
import me.lucko.luckperms.common.utils.Uuids;
import org.anjocaido.groupmanager.GlobalGroups; import org.anjocaido.groupmanager.GlobalGroups;
import org.anjocaido.groupmanager.GroupManager; import org.anjocaido.groupmanager.GroupManager;
@ -112,7 +114,7 @@ public class MigrationGroupManager extends SubCommand<Object> {
log.log("Migrated " + globalGroupCount.get() + " global groups"); log.log("Migrated " + globalGroupCount.get() + " global groups");
// Collect data // Collect data
Map<UUID, Set<Node>> users = new HashMap<>(); Map<UserIdentifier, Set<Node>> users = new HashMap<>();
Map<UUID, String> primaryGroups = new HashMap<>(); Map<UUID, String> primaryGroups = new HashMap<>();
Map<String, Set<Node>> groups = new HashMap<>(); Map<String, Set<Node>> groups = new HashMap<>();
@ -166,16 +168,23 @@ public class MigrationGroupManager extends SubCommand<Object> {
return; return;
} }
users.putIfAbsent(uuid, new HashSet<>()); String lastName = user.getLastName();
if (Uuids.parse(lastName).isPresent()) {
lastName = null;
}
UserIdentifier id = UserIdentifier.of(uuid, lastName);
users.putIfAbsent(id, new HashSet<>());
for (String node : user.getPermissionList()) { for (String node : user.getPermissionList()) {
if (node.isEmpty()) continue; if (node.isEmpty()) continue;
users.get(uuid).add(MigrationUtils.parseNode(node, true).setWorld(worldMappingFunc.apply(world)).build()); users.get(id).add(MigrationUtils.parseNode(node, true).setWorld(worldMappingFunc.apply(world)).build());
} }
// Collect sub groups // Collect sub groups
String finalWorld = worldMappingFunc.apply(world); String finalWorld = worldMappingFunc.apply(world);
users.get(uuid).addAll(user.subGroupListStringCopy().stream() users.get(id).addAll(user.subGroupListStringCopy().stream()
.filter(n -> !n.isEmpty()) .filter(n -> !n.isEmpty())
.map(n -> NodeFactory.groupNode(MigrationUtils.standardizeName(n))) .map(n -> NodeFactory.groupNode(MigrationUtils.standardizeName(n)))
.map(n -> NodeFactory.make(n, true, null, finalWorld)) .map(n -> NodeFactory.make(n, true, null, finalWorld))
@ -194,9 +203,9 @@ public class MigrationGroupManager extends SubCommand<Object> {
if (key.equals(NodeFactory.PREFIX_KEY) || key.equals(NodeFactory.SUFFIX_KEY)) { if (key.equals(NodeFactory.PREFIX_KEY) || key.equals(NodeFactory.SUFFIX_KEY)) {
ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase()); ChatMetaType type = ChatMetaType.valueOf(key.toUpperCase());
users.get(uuid).add(NodeFactory.buildChatMetaNode(type, 100, value).setWorld(worldMappingFunc.apply(world)).build()); users.get(id).add(NodeFactory.buildChatMetaNode(type, 100, value).setWorld(worldMappingFunc.apply(world)).build());
} else { } else {
users.get(uuid).add(NodeFactory.buildMetaNode(key, value).setWorld(worldMappingFunc.apply(world)).build()); users.get(id).add(NodeFactory.buildMetaNode(key, value).setWorld(worldMappingFunc.apply(world)).build());
} }
} }
@ -225,13 +234,13 @@ public class MigrationGroupManager extends SubCommand<Object> {
log.log("Starting user migration."); log.log("Starting user migration.");
AtomicInteger userCount = new AtomicInteger(0); AtomicInteger userCount = new AtomicInteger(0);
Iterators.iterate(users.entrySet(), e -> { Iterators.iterate(users.entrySet(), e -> {
User user = plugin.getStorage().loadUser(e.getKey(), null).join(); User user = plugin.getStorage().loadUser(e.getKey().getUuid(), e.getKey().getUsername().orElse(null)).join();
for (Node node : e.getValue()) { for (Node node : e.getValue()) {
user.setPermission(node); user.setPermission(node);
} }
String primaryGroup = primaryGroups.get(e.getKey()); String primaryGroup = primaryGroups.get(e.getKey().getUuid());
if (primaryGroup != null && !primaryGroup.isEmpty()) { if (primaryGroup != null && !primaryGroup.isEmpty()) {
user.setPermission(NodeFactory.buildGroupNode(primaryGroup).build()); user.setPermission(NodeFactory.buildGroupNode(primaryGroup).build());
user.getPrimaryGroup().setStoredValue(primaryGroup); user.getPrimaryGroup().setStoredValue(primaryGroup);

View File

@ -27,14 +27,27 @@ package me.lucko.luckperms.common.references;
import me.lucko.luckperms.common.model.User; import me.lucko.luckperms.common.model.User;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* Used to identify a specific {@link User}. * Used to identify a specific {@link User}.
*/ */
public final class UserIdentifier implements Identifiable<UUID> { public final class UserIdentifier implements Identifiable<UUID> {
public static UserIdentifier of(UUID uuid, String username) {
/**
* Creates a {@link UserIdentifier}.
*
* @param uuid the uuid of the user
* @param username the username of the user, nullable
* @return
*/
public static UserIdentifier of(@Nonnull UUID uuid, @Nullable String username) {
Objects.requireNonNull(uuid, "uuid");
if (username == null || username.equalsIgnoreCase("null") || username.isEmpty()) { if (username == null || username.equalsIgnoreCase("null") || username.isEmpty()) {
username = null; username = null;
} }
@ -50,6 +63,10 @@ public final class UserIdentifier implements Identifiable<UUID> {
this.username = username; this.username = username;
} }
public UUID getUuid() {
return this.uuid;
}
@Override @Override
public UUID getId() { public UUID getId() {
return getUuid(); return getUuid();
@ -74,10 +91,6 @@ public final class UserIdentifier implements Identifiable<UUID> {
@Override @Override
public String toString() { public String toString() {
return "UserIdentifier(uuid=" + this.uuid + ", username=" + this.getUsername() + ")"; return "UserIdentifier(uuid=" + this.uuid + ", username=" + this.username + ")";
}
public UUID getUuid() {
return this.uuid;
} }
} }