use of com.github.gustav9797.PowerfulPermsAPI.PowerfulPermsPlugin in project LuckPerms by lucko.
the class MigrationPowerfulPerms method execute.
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List<String> args, String label) {
ProgressLogger log = new ProgressLogger("PowerfulPerms");
log.addListener(plugin.getConsoleSender());
log.addListener(sender);
log.log("Starting.");
if (!Bukkit.getPluginManager().isPluginEnabled("PowerfulPerms")) {
log.logError("Plugin not loaded.");
return CommandResult.STATE_ERROR;
}
String method = plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
StorageType type = StorageType.parse(method);
if (type == null || type != StorageType.MYSQL) {
// We need to load the Hikari/MySQL stuff.
plugin.getDependencyManager().loadStorageDependencies(ImmutableSet.of(StorageType.MYSQL));
}
String address = args.get(0);
String database = args.get(1);
String username = args.get(2);
String password = args.get(3);
String dbTable = args.get(4);
// Find a list of UUIDs
log.log("Getting a list of UUIDs to migrate.");
Set<UUID> uuids = new HashSet<>();
try (HikariSupplier hikari = new HikariSupplier(address, database, username, password)) {
hikari.setup("powerfulperms-migrator-pool");
try (Connection c = hikari.getConnection()) {
DatabaseMetaData meta = c.getMetaData();
try (ResultSet rs = meta.getTables(null, null, dbTable, null)) {
if (!rs.next()) {
log.log("Error - Couldn't find table.");
return CommandResult.FAILURE;
}
}
}
try (Connection c = hikari.getConnection()) {
try (PreparedStatement ps = c.prepareStatement("SELECT COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=?")) {
ps.setString(1, dbTable);
try (ResultSet rs = ps.executeQuery()) {
log.log("Found table: " + dbTable);
while (rs.next()) {
log.log("" + rs.getString("COLUMN_NAME") + " - " + rs.getString("COLUMN_TYPE"));
}
}
}
try (PreparedStatement ps = c.prepareStatement("SELECT `uuid` FROM " + dbTable)) {
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
uuids.add(UUID.fromString(rs.getString("uuid")));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (uuids.isEmpty()) {
log.logError("Unable to find any UUIDs to migrate.");
return CommandResult.FAILURE;
}
log.log("Found " + uuids.size() + " uuids. Starting migration.");
PowerfulPermsPlugin ppPlugin = (PowerfulPermsPlugin) Bukkit.getPluginManager().getPlugin("PowerfulPerms");
PermissionManager pm = ppPlugin.getPermissionManager();
Collection<Group> groups = pm.getGroups().values();
AtomicInteger maxWeight = new AtomicInteger(0);
// Groups first.
log.log("Starting group migration.");
AtomicInteger groupCount = new AtomicInteger(0);
Iterators.iterate(groups, g -> {
maxWeight.set(Math.max(maxWeight.get(), g.getRank()));
String groupName = MigrationUtils.standardizeName(g.getName());
me.lucko.luckperms.common.model.Group group = plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join();
MigrationUtils.setGroupWeight(group, g.getRank());
for (Permission p : g.getOwnPermissions()) {
applyPerm(group, p);
}
for (Group parent : g.getParents()) {
group.setPermission(NodeFactory.buildGroupNode(parent.getName().toLowerCase()).build());
}
// server --> prefix afaik
for (Map.Entry<String, String> prefix : g.getPrefixes().entrySet()) {
if (prefix.getValue().isEmpty())
continue;
String server = prefix.getKey().toLowerCase();
if (prefix.getKey().equals("*") || prefix.getKey().equals("all")) {
server = null;
}
if (server != null) {
group.setPermission(NodeFactory.buildPrefixNode(g.getRank(), prefix.getValue()).setServer(server).build());
} else {
group.setPermission(NodeFactory.buildPrefixNode(g.getRank(), prefix.getValue()).build());
}
}
for (Map.Entry<String, String> suffix : g.getSuffixes().entrySet()) {
if (suffix.getValue().isEmpty())
continue;
String server = suffix.getKey().toLowerCase();
if (suffix.getKey().equals("*") || suffix.getKey().equals("all")) {
server = null;
}
if (server != null) {
group.setPermission(NodeFactory.buildSuffixNode(g.getRank(), suffix.getValue()).setServer(server).build());
} else {
group.setPermission(NodeFactory.buildSuffixNode(g.getRank(), suffix.getValue()).build());
}
}
plugin.getStorage().saveGroup(group);
log.logAllProgress("Migrated {} groups so far.", groupCount.incrementAndGet());
});
log.log("Migrated " + groupCount.get() + " groups");
// Migrate all users
log.log("Starting user migration.");
AtomicInteger userCount = new AtomicInteger(0);
// Increment the max weight from the group migrations. All user meta should override.
maxWeight.addAndGet(5);
// Migrate all users and their groups
Iterators.iterate(uuids, uuid -> {
// Create a LuckPerms user for the UUID
User user = plugin.getStorage().loadUser(uuid, null).join();
List<Permission> permissions = joinFuture(pm.getPlayerOwnPermissions(uuid));
for (Permission p : permissions) {
applyPerm(user, p);
}
// server --> list of groups
Map<String, List<CachedGroup>> parents = joinFuture(pm.getPlayerOwnGroups(uuid));
for (Map.Entry<String, List<CachedGroup>> parent : parents.entrySet()) {
String server = parent.getKey().toLowerCase();
if (parent.getKey().equals("*") || parent.getKey().equals("all")) {
server = null;
}
for (CachedGroup group : parent.getValue()) {
applyGroup(pm, user, group, server);
}
}
String prefix = joinFuture(pm.getPlayerOwnPrefix(uuid));
String suffix = joinFuture(pm.getPlayerOwnSuffix(uuid));
if (prefix != null && !prefix.isEmpty()) {
user.setPermission(NodeFactory.buildPrefixNode(maxWeight.get(), prefix).build());
}
if (suffix != null && !suffix.isEmpty()) {
user.setPermission(NodeFactory.buildSuffixNode(maxWeight.get(), suffix).build());
}
Group primaryGroup = joinFuture(pm.getPlayerPrimaryGroup(uuid));
if (primaryGroup != null && primaryGroup.getName() != null) {
String primary = primaryGroup.getName().toLowerCase();
if (!primary.equals(NodeFactory.DEFAULT_GROUP_NAME)) {
user.setPermission(NodeFactory.buildGroupNode(primary).build());
user.getPrimaryGroup().setStoredValue(primary);
}
}
plugin.getUserManager().cleanup(user);
plugin.getStorage().saveUser(user);
log.logProgress("Migrated {} users so far.", userCount.incrementAndGet());
});
log.log("Migrated " + userCount.get() + " users.");
log.log("Success! Migration complete.");
return CommandResult.SUCCESS;
}
Aggregations