Compile against 1.15 api, update luckperms to 5.0 (breaking change)

This commit is contained in:
Gabriele C 2019-12-12 16:41:44 +01:00
parent 71cda72b8a
commit e05c07799d
3 changed files with 75 additions and 75 deletions

View File

@ -64,7 +64,7 @@
<maven.minimumVersion>3.3.9</maven.minimumVersion> <maven.minimumVersion>3.3.9</maven.minimumVersion>
<!-- Dependencies versions --> <!-- Dependencies versions -->
<spigot.version>1.14.2-R0.1-SNAPSHOT</spigot.version> <spigot.version>1.15-R0.1-SNAPSHOT</spigot.version>
<!-- Versioning properties --> <!-- Versioning properties -->
<project.outputName>AuthMe</project.outputName> <project.outputName>AuthMe</project.outputName>
@ -690,9 +690,9 @@
<!-- LuckPerms plugin --> <!-- LuckPerms plugin -->
<dependency> <dependency>
<groupId>me.lucko.luckperms</groupId> <groupId>net.luckperms</groupId>
<artifactId>luckperms-api</artifactId> <artifactId>api</artifactId>
<version>4.4</version> <version>5.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>

View File

@ -4,20 +4,21 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.output.ConsoleLoggerFactory; import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PermissionsSystemType; import fr.xephi.authme.permission.PermissionsSystemType;
import me.lucko.luckperms.LuckPerms; import net.luckperms.api.LuckPerms;
import me.lucko.luckperms.api.Contexts; import net.luckperms.api.LuckPermsProvider;
import me.lucko.luckperms.api.DataMutateResult; import net.luckperms.api.cacheddata.CachedPermissionData;
import me.lucko.luckperms.api.Group; import net.luckperms.api.model.data.DataMutateResult;
import me.lucko.luckperms.api.LuckPermsApi; import net.luckperms.api.model.group.Group;
import me.lucko.luckperms.api.Node; import net.luckperms.api.model.user.User;
import me.lucko.luckperms.api.User; import net.luckperms.api.node.NodeEqualityPredicate;
import me.lucko.luckperms.api.caching.PermissionData; import net.luckperms.api.node.types.InheritanceNode;
import me.lucko.luckperms.api.caching.UserData; import net.luckperms.api.query.QueryMode;
import net.luckperms.api.query.QueryOptions;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -33,42 +34,39 @@ import java.util.stream.Collectors;
public class LuckPermsHandler implements PermissionHandler { public class LuckPermsHandler implements PermissionHandler {
private final ConsoleLogger logger = ConsoleLoggerFactory.get(LuckPermsHandler.class); private final ConsoleLogger logger = ConsoleLoggerFactory.get(LuckPermsHandler.class);
private LuckPermsApi luckPermsApi; private LuckPerms luckPerms;
public LuckPermsHandler() throws PermissionHandlerException { public LuckPermsHandler() throws PermissionHandlerException {
try { try {
luckPermsApi = LuckPerms.getApi(); luckPerms = LuckPermsProvider.get();
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
throw new PermissionHandlerException("Could not get api of LuckPerms", e); throw new PermissionHandlerException("Could not get api of LuckPerms", e);
} }
} }
private void saveUser(User user) {
luckPermsApi.getUserManager().saveUser(user)
.thenAcceptAsync(wasSuccessful -> user.refreshCachedData());
}
@Override @Override
public boolean addToGroup(OfflinePlayer player, String group) { public boolean addToGroup(OfflinePlayer player, String group) {
Group newGroup = luckPermsApi.getGroup(group); Group newGroup = luckPerms.getGroupManager().getGroup(group);
if (newGroup == null) { if (newGroup == null) {
return false; return false;
} }
User user = luckPermsApi.getUser(player.getName()); String playerName = player.getName();
if (playerName == null) {
return false;
}
User user = luckPerms.getUserManager().getUser(playerName);
if (user == null) { if (user == null) {
return false; return false;
} }
DataMutateResult result = user.setPermission( InheritanceNode node = InheritanceNode.builder(group).build();
luckPermsApi.getNodeFactory().makeGroupNode(newGroup).build()); DataMutateResult result = user.data().add(node);
if (result == DataMutateResult.FAIL) { if (result == DataMutateResult.FAIL) {
return false; return false;
} }
saveUser(user); luckPerms.getUserManager().saveUser(user);
luckPermsApi.cleanupUser(user);
return true; return true;
} }
@ -79,97 +77,102 @@ public class LuckPermsHandler implements PermissionHandler {
@Override @Override
public boolean hasPermissionOffline(String name, PermissionNode node) { public boolean hasPermissionOffline(String name, PermissionNode node) {
User user = luckPermsApi.getUser(name); User user = luckPerms.getUserManager().getUser(name);
if (user == null) { if (user == null) {
logger.warning("LuckPermsHandler: tried to check permission for offline user " logger.warning("LuckPermsHandler: tried to check permission for offline user "
+ name + " but it isn't loaded!"); + name + " but it isn't loaded!");
return false; return false;
} }
UserData userData = user.getCachedData(); CachedPermissionData permissionData = user.getCachedData()
PermissionData permissionData = userData.getPermissionData(Contexts.allowAll()); .getPermissionData(QueryOptions.builder(QueryMode.NON_CONTEXTUAL).build());
boolean result = permissionData.getPermissionValue(node.getNode()).asBoolean(); return permissionData.checkPermission(node.getNode()).asBoolean();
luckPermsApi.cleanupUser(user);
return result;
} }
@Override @Override
public boolean isInGroup(OfflinePlayer player, String group) { public boolean isInGroup(OfflinePlayer player, String group) {
User user = luckPermsApi.getUser(player.getName()); String playerName = player.getName();
if (playerName == null) {
return false;
}
User user = luckPerms.getUserManager().getUser(playerName);
if (user == null) { if (user == null) {
logger.warning("LuckPermsHandler: tried to check group for offline user " logger.warning("LuckPermsHandler: tried to check group for offline user "
+ player.getName() + " but it isn't loaded!"); + player.getName() + " but it isn't loaded!");
return false; return false;
} }
Group permissionGroup = luckPermsApi.getGroup(group); InheritanceNode inheritanceNode = InheritanceNode.builder(group).build();
boolean result = permissionGroup != null && user.inheritsGroup(permissionGroup); return user.data().contains(inheritanceNode, NodeEqualityPredicate.EXACT).asBoolean();
luckPermsApi.cleanupUser(user);
return result;
} }
@Override @Override
public boolean removeFromGroup(OfflinePlayer player, String group) { public boolean removeFromGroup(OfflinePlayer player, String group) {
User user = luckPermsApi.getUser(player.getName()); String playerName = player.getName();
if (playerName == null) {
return false;
}
User user = luckPerms.getUserManager().getUser(playerName);
if (user == null) { if (user == null) {
logger.warning("LuckPermsHandler: tried to remove group for offline user " logger.warning("LuckPermsHandler: tried to remove group for offline user "
+ player.getName() + " but it isn't loaded!"); + player.getName() + " but it isn't loaded!");
return false; return false;
} }
Group permissionGroup = luckPermsApi.getGroup(group); InheritanceNode groupNode = InheritanceNode.builder(group).build();
if (permissionGroup == null) { boolean result = user.data().remove(groupNode) != DataMutateResult.FAIL;
return false;
}
Node groupNode = luckPermsApi.getNodeFactory().makeGroupNode(permissionGroup).build(); luckPerms.getUserManager().saveUser(user);
boolean result = user.unsetPermission(groupNode) != DataMutateResult.FAIL;
luckPermsApi.cleanupUser(user);
return result; return result;
} }
@Override @Override
public boolean setGroup(OfflinePlayer player, String group) { public boolean setGroup(OfflinePlayer player, String group) {
User user = luckPermsApi.getUser(player.getName()); String playerName = player.getName();
if (playerName == null) {
return false;
}
User user = luckPerms.getUserManager().getUser(playerName);
if (user == null) { if (user == null) {
logger.warning("LuckPermsHandler: tried to set group for offline user " logger.warning("LuckPermsHandler: tried to set group for offline user "
+ player.getName() + " but it isn't loaded!"); + player.getName() + " but it isn't loaded!");
return false; return false;
} }
Group permissionGroup = luckPermsApi.getGroup(group); InheritanceNode groupNode = InheritanceNode.builder(group).build();
if (permissionGroup == null) { DataMutateResult result = user.data().add(groupNode);
return false;
}
Node groupNode = luckPermsApi.getNodeFactory().makeGroupNode(permissionGroup).build();
DataMutateResult result = user.setPermission(groupNode);
if (result == DataMutateResult.FAIL) { if (result == DataMutateResult.FAIL) {
return false; return false;
} }
user.clearMatching(node -> node.isGroupNode() && !node.getGroupName().equals(permissionGroup.getName())); user.data().clear(node -> {
if (!(node instanceof InheritanceNode)) {
return false;
}
InheritanceNode inheritanceNode = (InheritanceNode) node;
return !inheritanceNode.equals(groupNode);
});
saveUser(user); luckPerms.getUserManager().saveUser(user);
luckPermsApi.cleanupUser(user);
return true; return true;
} }
@Override @Override
public List<String> getGroups(OfflinePlayer player) { public List<String> getGroups(OfflinePlayer player) {
User user = luckPermsApi.getUser(player.getName()); String playerName = player.getName();
if (playerName == null) {
return Collections.emptyList();
}
User user = luckPerms.getUserManager().getUser(playerName);
if (user == null) { if (user == null) {
logger.warning("LuckPermsHandler: tried to get groups for offline user " logger.warning("LuckPermsHandler: tried to get groups for offline user "
+ player.getName() + " but it isn't loaded!"); + player.getName() + " but it isn't loaded!");
return Collections.emptyList(); return Collections.emptyList();
} }
List<String> result = user.getOwnNodes().stream() return user.getDistinctNodes().stream()
.filter(Node::isGroupNode) .filter(node -> node instanceof InheritanceNode)
.map(n -> luckPermsApi.getGroupSafe(n.getGroupName())) .map(node -> (InheritanceNode) node)
.filter(Optional::isPresent) .map(node -> luckPerms.getGroupManager().getGroup(node.getGroupName()))
.map(Optional::get) .filter(Objects::nonNull)
.distinct()
.sorted((o1, o2) -> { .sorted((o1, o2) -> {
if (o1.getName().equals(user.getPrimaryGroup()) || o2.getName().equals(user.getPrimaryGroup())) { if (o1.getName().equals(user.getPrimaryGroup()) || o2.getName().equals(user.getPrimaryGroup())) {
return o1.getName().equals(user.getPrimaryGroup()) ? 1 : -1; return o1.getName().equals(user.getPrimaryGroup()) ? 1 : -1;
@ -180,9 +183,6 @@ public class LuckPermsHandler implements PermissionHandler {
}) })
.map(Group::getName) .map(Group::getName)
.collect(Collectors.toList()); .collect(Collectors.toList());
luckPermsApi.cleanupUser(user);
return result;
} }
@Override @Override
@ -193,7 +193,7 @@ public class LuckPermsHandler implements PermissionHandler {
@Override @Override
public void loadUserData(UUID uuid) throws PermissionLoadUserException { public void loadUserData(UUID uuid) throws PermissionLoadUserException {
try { try {
luckPermsApi.getUserManager().loadUser(uuid).get(5, TimeUnit.SECONDS); luckPerms.getUserManager().loadUser(uuid).get(5, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) { } catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new PermissionLoadUserException("Unable to load the permission data of the user " + uuid, e); throw new PermissionLoadUserException("Unable to load the permission data of the user " + uuid, e);
} }

View File

@ -10,8 +10,8 @@ import fr.xephi.authme.permission.handlers.VaultHandler;
import fr.xephi.authme.permission.handlers.ZPermissionsHandler; import fr.xephi.authme.permission.handlers.ZPermissionsHandler;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.PluginSettings;
import me.lucko.luckperms.LuckPerms; import net.luckperms.api.LuckPerms;
import me.lucko.luckperms.api.LuckPermsApi; import net.luckperms.api.LuckPermsProvider;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Server; import org.bukkit.Server;
@ -162,8 +162,8 @@ public class PermissionsManagerInitializationTest {
private void setUpForPermissionSystemTest() { private void setUpForPermissionSystemTest() {
if (permissionsSystemType == LUCK_PERMS) { if (permissionsSystemType == LUCK_PERMS) {
LuckPermsApi api = mock(LuckPermsApi.class); LuckPerms api = mock(LuckPerms.class);
ReflectionTestUtils.setField(LuckPerms.class, null, "instance", api); ReflectionTestUtils.setField(LuckPermsProvider.class, null, "instance", api);
} else if (permissionsSystemType == PERMISSIONS_EX) { } else if (permissionsSystemType == PERMISSIONS_EX) {
throw new AssumptionViolatedException( throw new AssumptionViolatedException(
"PermissionsEx instance cannot be mocked because of missing dependencies -- skipping"); "PermissionsEx instance cannot be mocked because of missing dependencies -- skipping");