Add bPermissions migration

This commit is contained in:
Luck 2016-09-03 18:15:52 +01:00
parent cee96d7daa
commit 7970e2a1c6
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
13 changed files with 278 additions and 59 deletions

View File

@ -217,6 +217,16 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
return getServer().getServicesManager().load(clazz); return getServer().getServicesManager().load(clazz);
} }
@SuppressWarnings("deprecation")
@Override
public UUID getUUID(String playerName) {
try {
return getServer().getOfflinePlayer(playerName).getUniqueId();
} catch (Exception e) {
return null;
}
}
@Override @Override
public boolean isPluginLoaded(String name) { public boolean isPluginLoaded(String name) {
return getServer().getPluginManager().isPluginEnabled(name); return getServer().getPluginManager().isPluginEnabled(name);

View File

@ -2,7 +2,7 @@ name: LuckPerms
author: Luck author: Luck
version: ${release.version}.${git.closest.tag.commit.count} version: ${release.version}.${git.closest.tag.commit.count}
main: me.lucko.luckperms.LPBukkitPlugin main: me.lucko.luckperms.LPBukkitPlugin
softdepend: [Vault, PermissionsEx, GroupManager, PowerfulPerms, zPermissions] # For migration softdepend: [Vault, PermissionsEx, GroupManager, PowerfulPerms, zPermissions, bPermissions] # For migration
description: A permissions plugin description: A permissions plugin
commands: commands:
luckperms: luckperms:

View File

@ -185,6 +185,11 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
return null; return null;
} }
@Override
public UUID getUUID(String playerName) {
return null; // Not needed on Bungee
}
@Override @Override
public boolean isPluginLoaded(String name) { public boolean isPluginLoaded(String name) {
return getProxy().getPluginManager().getPlugins().stream() return getProxy().getPluginManager().getPlugins().stream()

View File

@ -132,6 +132,12 @@
<version>3.0-dev-52</version> <version>3.0-dev-52</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>de.bananaco</groupId>
<artifactId>bpermissions-api</artifactId>
<version>2.12</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -136,6 +136,13 @@ public interface LuckPermsPlugin {
*/ */
Object getService(Class clazz); Object getService(Class clazz);
/**
* Used as a backup for migration
* @param playerName the players name
* @return a uuid if found, or null if not
*/
UUID getUUID(String playerName);
/** /**
* Checks if a plugin is loaded on the platform * Checks if a plugin is loaded on the platform
* @param name the name of the plugin * @param name the name of the plugin

View File

@ -66,6 +66,11 @@ public class MigrationMainCommand extends MainCommand<Object> {
subCommands.add(new MigrationBungeePerms()); subCommands.add(new MigrationBungeePerms());
} catch (Throwable ignored) {} } catch (Throwable ignored) {}
try {
Class.forName("de.bananaco.bpermissions.api.WorldManager");
subCommands.add(new MigrationBPermissions());
} catch (Throwable ignored) {}
} }
@Override @Override

View File

@ -0,0 +1,231 @@
package me.lucko.luckperms.commands.migration.subcommands;
import de.bananaco.bpermissions.api.*;
import me.lucko.luckperms.LuckPermsPlugin;
import me.lucko.luckperms.api.Logger;
import me.lucko.luckperms.commands.CommandResult;
import me.lucko.luckperms.commands.Predicate;
import me.lucko.luckperms.commands.Sender;
import me.lucko.luckperms.commands.SubCommand;
import me.lucko.luckperms.constants.Constants;
import me.lucko.luckperms.core.PermissionHolder;
import me.lucko.luckperms.data.LogEntry;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.utils.ArgumentChecker;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import static me.lucko.luckperms.constants.Permission.MIGRATION;
public class MigrationBPermissions extends SubCommand<Object> {
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");
uConfigField.setAccessible(true);
} catch (Throwable t) {
t.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private static Set<String> 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<String>) getKeysMethod.invoke(configSection, false);
} catch (Throwable t) {
t.printStackTrace();
return null;
}
}
public MigrationBPermissions() {
super("bpermissions", "Migration from bPermissions", "/%s migration bpermissions", MIGRATION, Predicate.alwaysFalse());
}
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List<String> args, String label) {
final Logger log = plugin.getLog();
WorldManager worldManager = WorldManager.getInstance();
if (worldManager == null) {
log.severe("bPermissions Migration: Error -> bPermissions is not loaded.");
return CommandResult.STATE_ERROR;
}
log.info("bPermissions Migration: Forcing the plugin to load all data. This could take a while.");
for (World world : worldManager.getAllWorlds()) {
Set<String> users = getUsers(world);
if (users == null) {
log.severe("bPermissions Migration: Couldn't get a list of users.");
return CommandResult.FAILURE;
}
users.forEach(s -> world.loadOne(s, CalculableType.USER));
}
// Migrate one world at a time.
log.info("bPermissions Migration: Starting world migration.");
for (World world : worldManager.getAllWorlds()) {
log.info("bPermissions Migration: Migrating world: " + world.getName());
// Migrate all groups
log.info("bPermissions Migration: Starting group migration in world " + world.getName() + ".");
int groupCount = 0;
for (Calculable group : world.getAll(CalculableType.GROUP)) {
groupCount++;
String groupName = group.getName().toLowerCase();
if (group.getName().equalsIgnoreCase(world.getDefaultGroup())) {
groupName = "default";
}
// Make a LuckPerms group for the one being migrated.
plugin.getDatastore().createAndLoadGroup(groupName);
me.lucko.luckperms.groups.Group lpGroup = plugin.getGroupManager().get(groupName);
try {
LogEntry.build()
.actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName())
.acted(lpGroup).action("create")
.build().submit(plugin);
} catch (Exception ex) {
ex.printStackTrace();
}
migrateHolder(plugin, world, group, lpGroup);
plugin.getDatastore().saveGroup(lpGroup);
}
log.info("bPermissions Migration: Migrated " + groupCount + " groups in world " + world.getName() + ".");
// Migrate all users
log.info("bPermissions Migration: Starting user migration in world " + world.getName() + ".");
int userCount = 0;
for (Calculable user : world.getAll(CalculableType.USER)) {
userCount++;
// There is no mention of UUIDs in the API. I assume that name = uuid. idk?
UUID uuid;
try {
uuid = UUID.fromString(user.getName());
} catch (IllegalArgumentException e) {
uuid = plugin.getUUID(user.getName());
}
if (uuid == null) {
log.info("bPermissions Migration: Unable to migrate user " + user.getName() + ". Unable to get UUID.");
continue;
}
// Make a LuckPerms user for the one being migrated.
plugin.getDatastore().loadUser(uuid, "null");
me.lucko.luckperms.users.User lpUser = plugin.getUserManager().get(uuid);
migrateHolder(plugin, world, user, lpUser);
plugin.getDatastore().saveUser(lpUser);
plugin.getUserManager().cleanup(lpUser);
}
log.info("bPermissions Migration: Migrated " + userCount + " users in world " + world.getName() + ".");
}
log.info("bPermissions Migration: Success! Completed without any errors.");
return CommandResult.SUCCESS;
}
private static void migrateHolder(LuckPermsPlugin plugin, 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());
LogEntry.build()
.actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName())
.acted(holder).action("set " + p.name() + " " + p.isTrue() + " global " + world.getName())
.build().submit(plugin);
} catch (Exception ex) {
if (!(ex instanceof ObjectAlreadyHasException)) {
ex.printStackTrace();
}
}
// Include any child permissions
for (Map.Entry<String, Boolean> child : p.getChildren().entrySet()) {
try {
holder.setPermission(child.getKey(), child.getValue(), "global", world.getName());
LogEntry.build()
.actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName())
.acted(holder).action("set " + child.getKey() + " " + child.getValue() + " global " + world.getName())
.build().submit(plugin);
} catch (Exception ex) {
if (!(ex instanceof ObjectAlreadyHasException)) {
ex.printStackTrace();
}
}
}
}
// Migrate any inherited groups
for (Group parent : c.getGroups()) {
try {
holder.setPermission("group." + parent.getName(), true, "global", world.getName());
LogEntry.build()
.actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName())
.acted(holder).action("setinherit " + parent.getName() + " global " + world.getName())
.build().submit(plugin);
} catch (Exception ex) {
if (!(ex instanceof ObjectAlreadyHasException)) {
ex.printStackTrace();
}
}
}
// Migrate existing meta
for (Map.Entry<String, String> meta : c.getMeta().entrySet()) {
if (meta.getKey().equalsIgnoreCase("prefix") || meta.getKey().equalsIgnoreCase("suffix")) {
String chatMeta = ArgumentChecker.escapeCharacters(meta.getValue());
try {
holder.setPermission(meta.getKey().toLowerCase() + "." + c.getPriority() + "." + chatMeta, true);
LogEntry.build()
.actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName())
.acted(holder).action("set " + meta.getKey().toLowerCase() + "." + c.getPriority() + "." + chatMeta + " true")
.build().submit(plugin);
} catch (Exception ex) {
if (!(ex instanceof ObjectAlreadyHasException)) {
ex.printStackTrace();
}
}
continue;
}
try {
holder.setPermission("meta." + meta.getKey() + "." + meta.getValue(), true, "global", world.getName());
LogEntry.build()
.actor(Constants.getConsoleUUID()).actorName(Constants.getConsoleName())
.acted(holder).action("set meta." + meta.getKey() + "." + meta.getValue() + " true global " + world.getName())
.build().submit(plugin);
} catch (Exception ex) {
if (!(ex instanceof ObjectAlreadyHasException)) {
ex.printStackTrace();
}
}
}
}
}

View File

@ -43,41 +43,11 @@ import ru.tehkode.permissions.bukkit.PermissionsEx;
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.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class MigrationPermissionsEx extends SubCommand<Object> { public class MigrationPermissionsEx extends SubCommand<Object> {
private static Class<?> bukkitPlayer = null;
private static Method getUniqueIdMethod = null;
private static Method getPlayerMethod = null;
static {
try {
bukkitPlayer = Class.forName("org.bukkit.entity.Player");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (bukkitPlayer != null) {
try {
getUniqueIdMethod = bukkitPlayer.getMethod("getUniqueId");
getUniqueIdMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
try {
getPlayerMethod = PermissionUser.class.getMethod("getPlayer");
getPlayerMethod.setAccessible(true);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public MigrationPermissionsEx() { public MigrationPermissionsEx() {
super("permissionsex", "Migration from PermissionsEx", "/%s migration permissionsex [world names]", super("permissionsex", "Migration from PermissionsEx", "/%s migration permissionsex [world names]",
Permission.MIGRATION, Predicate.alwaysFalse()); Permission.MIGRATION, Predicate.alwaysFalse());
@ -273,19 +243,9 @@ public class MigrationPermissionsEx extends SubCommand<Object> {
try { try {
u = UUID.fromString(user.getIdentifier()); u = UUID.fromString(user.getIdentifier());
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
u = ni.nameToUUID(user.getIdentifier()); u = ni.nameToUUID(user.getIdentifier());
if (u == null) { if (u == null) {
if (bukkitPlayer != null) { u = plugin.getUUID(user.getIdentifier());
try {
Object playerObj = getPlayerMethod.invoke(user);
u = (UUID) getUniqueIdMethod.invoke(playerObj);
} catch (Throwable t) {
t.printStackTrace();
}
}
} }
} }

View File

@ -30,14 +30,12 @@ import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.event.events.GroupAddEvent; import me.lucko.luckperms.api.event.events.GroupAddEvent;
import me.lucko.luckperms.api.implementation.internal.GroupLink; import me.lucko.luckperms.api.implementation.internal.GroupLink;
import me.lucko.luckperms.api.implementation.internal.PermissionHolderLink; import me.lucko.luckperms.api.implementation.internal.PermissionHolderLink;
import me.lucko.luckperms.constants.Patterns;
import me.lucko.luckperms.core.PermissionHolder; import me.lucko.luckperms.core.PermissionHolder;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.exceptions.ObjectLacksException; import me.lucko.luckperms.exceptions.ObjectLacksException;
import me.lucko.luckperms.utils.Identifiable; import me.lucko.luckperms.utils.Identifiable;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ToString(of = {"name"}) @ToString(of = {"name"})

View File

@ -26,20 +26,13 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import lombok.Cleanup; import lombok.Cleanup;
import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.LuckPermsPlugin;
import me.lucko.luckperms.api.LogEntry;
import me.lucko.luckperms.constants.Constants;
import me.lucko.luckperms.data.Log;
import me.lucko.luckperms.groups.Group; import me.lucko.luckperms.groups.Group;
import me.lucko.luckperms.storage.Datastore;
import me.lucko.luckperms.tracks.Track; import me.lucko.luckperms.tracks.Track;
import me.lucko.luckperms.users.User; import me.lucko.luckperms.users.User;
import me.lucko.luckperms.utils.Node; import me.lucko.luckperms.utils.Node;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.*;
import java.util.logging.Formatter;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static me.lucko.luckperms.core.PermissionHolder.exportToLegacy; import static me.lucko.luckperms.core.PermissionHolder.exportToLegacy;

View File

@ -31,16 +31,13 @@ import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.event.events.GroupAddEvent; import me.lucko.luckperms.api.event.events.GroupAddEvent;
import me.lucko.luckperms.api.implementation.internal.GroupLink; import me.lucko.luckperms.api.implementation.internal.GroupLink;
import me.lucko.luckperms.api.implementation.internal.PermissionHolderLink; import me.lucko.luckperms.api.implementation.internal.PermissionHolderLink;
import me.lucko.luckperms.constants.Patterns;
import me.lucko.luckperms.core.PermissionHolder; import me.lucko.luckperms.core.PermissionHolder;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.exceptions.ObjectLacksException; import me.lucko.luckperms.exceptions.ObjectLacksException;
import me.lucko.luckperms.groups.Group; import me.lucko.luckperms.groups.Group;
import me.lucko.luckperms.utils.Identifiable; import me.lucko.luckperms.utils.Identifiable;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;

View File

@ -244,6 +244,15 @@ public class LPSpongePlugin implements LuckPermsPlugin {
return Sponge.getServiceManager().provideUnchecked(clazz); return Sponge.getServiceManager().provideUnchecked(clazz);
} }
@Override
public UUID getUUID(String playerName) {
try {
return game.getServer().getPlayer(playerName).get().getUniqueId();
} catch (Exception e) {
return null;
}
}
@Override @Override
public boolean isPluginLoaded(String name) { public boolean isPluginLoaded(String name) {
return game.getPluginManager().isLoaded(name); return game.getPluginManager().isLoaded(name);
@ -251,17 +260,17 @@ public class LPSpongePlugin implements LuckPermsPlugin {
@Override @Override
public void runUpdateTask() { public void runUpdateTask() {
scheduler.createTaskBuilder().async().execute(new UpdateTask(this)).submit(LPSpongePlugin.this); scheduler.createTaskBuilder().async().execute(new UpdateTask(this)).submit(this);
} }
@Override @Override
public void doAsync(Runnable r) { public void doAsync(Runnable r) {
scheduler.createTaskBuilder().async().execute(r).submit(LPSpongePlugin.this); scheduler.createTaskBuilder().async().execute(r).submit(this);
} }
@Override @Override
public void doSync(Runnable r) { public void doSync(Runnable r) {
scheduler.createTaskBuilder().execute(r).submit(LPSpongePlugin.this); scheduler.createTaskBuilder().execute(r).submit(this);
} }
private void registerPermission(PermissionService p, String node) { private void registerPermission(PermissionService p, String node) {

View File

@ -23,8 +23,6 @@
package me.lucko.luckperms.users; package me.lucko.luckperms.users;
import me.lucko.luckperms.LPSpongePlugin; import me.lucko.luckperms.LPSpongePlugin;
import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent;
import me.lucko.luckperms.api.implementation.internal.UserLink;
import java.util.UUID; import java.util.UUID;