Skip to content

Commit

Permalink
Update LuckPerms to v5.
Browse files Browse the repository at this point in the history
  • Loading branch information
Staartvin committed Jan 24, 2020
1 parent 8f24f04 commit a88bb97
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 62 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@
</dependency>

<dependency>
<groupId>me.lucko.luckperms</groupId>
<artifactId>luckperms-api</artifactId>
<version>4.1</version>
<groupId>net.luckperms</groupId>
<artifactId>api</artifactId>
<version>5.0</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

import me.armar.plugins.autorank.Autorank;
import me.armar.plugins.autorank.permissions.PermissionsHandler;
import me.lucko.luckperms.LuckPerms;
import me.lucko.luckperms.api.LuckPermsApi;
import me.lucko.luckperms.api.Node;
import me.lucko.luckperms.api.User;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import net.luckperms.api.node.types.InheritanceNode;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

/**
* @author Staartvin This is a special permission handler that handles all work
* from LuckPerms
*/
public class LuckPermsHandler extends PermissionsHandler {

private LuckPermsApi luckPermsApi;
private LuckPerms luckPermsApi;

public LuckPermsHandler(final Autorank plugin) {
super(plugin);
Expand Down Expand Up @@ -78,14 +83,9 @@ public boolean removeGroup(final Player player, final String world, final String
*/
@Override
public Collection<String> getGroups() {

List<String> groupNames = new ArrayList<>();

for (me.lucko.luckperms.api.Group luckPermGroup : luckPermsApi.getGroups()) {
groupNames.add(luckPermGroup.getName());
}

return Collections.unmodifiableCollection(groupNames);
return Collections.unmodifiableCollection(
luckPermsApi.getGroupManager().getLoadedGroups().stream()
.map(Group::getName).collect(Collectors.toList()));
}

/*
Expand All @@ -100,58 +100,23 @@ public String getName() {

@Override
public Collection<String> getPlayerGroups(final Player player) {
User user = luckPermsApi.getUser(player.getUniqueId());

User user = luckPermsApi.getUserManager().getUser(player.getUniqueId());

if (user == null) {
return new ArrayList<>();
}

List<String> groupNames = new ArrayList<>();

for (Node luckPermsNode : user.getAllNodes()) {
if (luckPermsNode.isGroupNode()) {
groupNames.add(luckPermsNode.getGroupName());
}
}

return Collections.unmodifiableCollection(groupNames);
return Collections.unmodifiableCollection(user.getDistinctNodes().parallelStream()
.filter(node -> node instanceof InheritanceNode)
.map(node -> ((InheritanceNode) node).getGroupName())
.collect(Collectors.toList()));
}

@Override
public Collection<String> getWorldGroups(final Player player, final String world) {
User user = luckPermsApi.getUser(player.getUniqueId());

if (user == null) {
return new ArrayList<>();
}

List<String> groupNames = new ArrayList<>();

for (Node luckPermsNode : user.getAllNodes()) {
if (luckPermsNode.isGroupNode()) {

// If group is not world specific, it also applies to the given world and hence should be added.
if (!luckPermsNode.isWorldSpecific()) {
groupNames.add(luckPermsNode.getGroupName());
continue;
}

Optional<String> validOnWorld = luckPermsNode.getWorld();

// World is unknown and so not world specific.
if (!validOnWorld.isPresent()) {
groupNames.add(luckPermsNode.getGroupName());
} else {
// World is not unknown and we should check if it matches the given world.
if (validOnWorld.get().equals(world)) {
groupNames.add(luckPermsNode.getGroupName());
}
}

}
}

return Collections.unmodifiableCollection(groupNames);
// The API is not very clear on how to check for world groups, so I'm ignoring that for now.
return this.getPlayerGroups(player);
}


Expand All @@ -163,9 +128,9 @@ public boolean replaceGroup(final Player player, final String world, final Strin

@Override
public boolean setupPermissionsHandler() {
Optional<LuckPermsApi> optional = LuckPerms.getApiSafe();
RegisteredServiceProvider<LuckPerms> provider = Bukkit.getServicesManager().getRegistration(LuckPerms.class);

optional.ifPresent(luckPermsApi1 -> luckPermsApi = luckPermsApi1);
if (provider != null) luckPermsApi = provider.getProvider();

return luckPermsApi != null;
}
Expand Down

0 comments on commit a88bb97

Please sign in to comment.