Search in sources :

Example 11 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListPlayerCommand method linkPlayersToGroups.

public static Map<String, List<Player>> linkPlayersToGroups(List<Subject> groups, Map<String, String> groupAliases, Map<Player, List<String>> players) {
    final Map<String, List<Player>> groupToPlayer = Maps.newHashMap();
    for (Subject x : groups) {
        List<Player> groupPlayerList;
        String groupName = x.getIdentifier();
        if (groupAliases.containsKey(x.getIdentifier())) {
            groupName = groupAliases.get(x.getIdentifier());
        }
        // Get the players in the group.
        Collection<Player> cp = players.entrySet().stream().filter(k -> k.getValue().contains(x.getIdentifier())).map(Map.Entry::getKey).collect(Collectors.toList());
        if (!cp.isEmpty()) {
            groupPlayerList = groupToPlayer.computeIfAbsent(groupName, g -> Lists.newArrayList());
            cp.forEach(players::remove);
            groupPlayerList.addAll(cp);
        }
    }
    return groupToPlayer;
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Keys(org.spongepowered.api.data.key.Keys) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) HashMap(java.util.HashMap) Contextual(org.spongepowered.api.service.context.Contextual) PermissionService(org.spongepowered.api.service.permission.PermissionService) Function(java.util.function.Function) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) Lists(com.google.common.collect.Lists) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) Subject(org.spongepowered.api.service.permission.Subject) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) PlayerInfoConfigAdapter(io.github.nucleuspowered.nucleus.modules.playerinfo.config.PlayerInfoConfigAdapter) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) AFKHandler(io.github.nucleuspowered.nucleus.modules.afk.handlers.AFKHandler) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) ListConfig(io.github.nucleuspowered.nucleus.modules.playerinfo.config.ListConfig) Player(org.spongepowered.api.entity.living.player.Player) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Subject(org.spongepowered.api.service.permission.Subject)

Example 12 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method testWeightedGroups.

@Test
public void testWeightedGroups() {
    Subject subject1 = createSubject("subject1");
    Subject subject2 = createSubject("subject2", subject1);
    Subject subject2a = createSubjectWithWeight("subject2a", -1, subject1);
    Subject subject3 = createSubject("subject3", subject1, subject2);
    Subject subject4 = createSubjectWithWeight("subject4", 1, subject1, subject2);
    Subject subject5 = createSubjectWithWeight("subject5", 2, subject1, subject2);
    List<Subject> subjects = Lists.newArrayList(subject1, subject2, subject2a, subject3, subject4, subject5);
    List<Subject> sorted = subjects.stream().sorted((x, y) -> ListPlayerCommand.groupComparison(ListPlayerCommand.weightingFunction, x, y)).collect(Collectors.toList());
    Assert.assertEquals(subject5, sorted.get(0));
    Assert.assertEquals(subject4, sorted.get(1));
    Assert.assertEquals(subject3, sorted.get(2));
    Assert.assertEquals(subject2, sorted.get(3));
    Assert.assertEquals(subject1, sorted.get(4));
    Assert.assertEquals(subject2a, sorted.get(5));
}
Also used : Arrays(java.util.Arrays) Context(org.spongepowered.api.service.context.Context) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Mockito(org.mockito.Mockito) List(java.util.List) Lists(com.google.common.collect.Lists) ListPlayerCommand(io.github.nucleuspowered.nucleus.modules.playerinfo.commands.ListPlayerCommand) Map(java.util.Map) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Player(org.spongepowered.api.entity.living.player.Player) Assert(org.junit.Assert) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) Subject(org.spongepowered.api.service.permission.Subject) Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Example 13 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method testPlayerNotInAnyGroupIsLeftOver.

@Test
@SuppressWarnings("all")
public void testPlayerNotInAnyGroupIsLeftOver() {
    // Create two groups.
    Subject admin = createSubjectWithWeight("admin", 1);
    Subject ace = createSubjectWithWeight("ace", 0);
    // The player is in both groups. Also, order is important.
    List<Subject> parents = Lists.newArrayList(admin, ace);
    Player player = Mockito.mock(Player.class);
    Player player2 = Mockito.mock(Player.class);
    List<SubjectReference> lsr = getSubjectReferences(parents);
    Mockito.when(player.getParents()).thenReturn(lsr);
    Mockito.when(player.getParents(Mockito.anySet())).thenReturn(lsr);
    Mockito.when(player2.getParents()).thenReturn(Lists.newArrayList());
    Mockito.when(player2.getParents(Mockito.anySet())).thenReturn(Lists.newArrayList());
    // Create our map.
    Map<Player, List<String>> map = Maps.newHashMap();
    map.put(player, Lists.newArrayList("admin", "ace"));
    map.put(player2, Lists.newArrayList());
    // No aliases.
    Map<String, String> aliases = Maps.newHashMap();
    aliases.put("admin", "Admin");
    aliases.put("ace", "Ace");
    // Now, let's run it through our method.
    Map<String, List<Player>> result = ListPlayerCommand.linkPlayersToGroups(parents, aliases, map);
    Assert.assertEquals("There should only be one entry", 1, result.size());
    List<Player> players = result.get("Admin");
    Assert.assertNotNull("Players is null", players);
    Assert.assertEquals("There should only be one player!", 1, players.size());
    Assert.assertEquals("One player should have been left over", 1, map.keySet().size());
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) List(java.util.List) Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Example 14 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method testWeightsAreBeingAppliedCorrectlyWithReversedGroupsInList.

@Test
@SuppressWarnings("all")
public void testWeightsAreBeingAppliedCorrectlyWithReversedGroupsInList() {
    // Create two groups.
    Subject admin = createSubjectWithWeight("admin", 1);
    Subject ace = createSubjectWithWeight("ace", 0);
    // Order of groups to check for.
    List<Subject> order = Lists.newArrayList(admin, ace);
    // The player is in both groups.
    List<Subject> parents = Lists.newArrayList(ace, admin);
    Player player = Mockito.mock(Player.class);
    List<SubjectReference> lsr = getSubjectReferences(parents);
    Mockito.when(player.getParents()).thenReturn(lsr);
    Mockito.when(player.getParents(Mockito.anySet())).thenReturn(lsr);
    // Create our map.
    Map<Player, List<String>> map = Maps.newHashMap();
    map.put(player, Lists.newArrayList("ace", "admin"));
    // No aliases.
    Map<String, String> aliases = Maps.newHashMap();
    // Now, let's run it through our method.
    Map<String, List<Player>> result = ListPlayerCommand.linkPlayersToGroups(order, aliases, map);
    Assert.assertEquals("There should only be one entry", 1, result.size());
    List<Player> players = result.get("admin");
    Assert.assertNotNull("Players is null", players);
    Assert.assertEquals("There should only be one player!", 1, players.size());
    Assert.assertTrue("map is not empty", map.isEmpty());
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) List(java.util.List) Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Example 15 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method createSubjectWithWeight.

private static Subject createSubjectWithWeight(String name, int weight, Subject... parents) {
    Subject subject = Mockito.mock(Subject.class);
    Mockito.when(subject.getIdentifier()).thenReturn(name);
    Mockito.when(subject.getOption(Mockito.anySetOf(Context.class), Mockito.eq("nucleus.list.weight"))).thenReturn(Optional.of(String.valueOf(weight)));
    Mockito.when(subject.getOption(Mockito.eq("nucleus.list.weight"))).thenReturn(Optional.of(String.valueOf(weight)));
    List<SubjectReference> lsr = getSubjectReferences(Arrays.asList(parents));
    Mockito.when(subject.getParents()).thenReturn(lsr);
    Mockito.when(subject.getParents(Mockito.anySetOf(Context.class))).thenReturn(lsr);
    return subject;
}
Also used : Context(org.spongepowered.api.service.context.Context) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) Subject(org.spongepowered.api.service.permission.Subject)

Aggregations

Subject (org.spongepowered.api.service.permission.Subject)28 SubjectReference (org.spongepowered.api.service.permission.SubjectReference)11 Test (org.junit.Test)10 List (java.util.List)9 Player (org.spongepowered.api.entity.living.player.Player)9 Context (org.spongepowered.api.service.context.Context)6 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 PermissionService (org.spongepowered.api.service.permission.PermissionService)5 Text (org.spongepowered.api.text.Text)5 Lists (com.google.common.collect.Lists)4 Maps (com.google.common.collect.Maps)4 Util (io.github.nucleuspowered.nucleus.Util)4 Map (java.util.Map)4 CommandSource (org.spongepowered.api.command.CommandSource)4 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)3 ListPlayerCommand (io.github.nucleuspowered.nucleus.modules.playerinfo.commands.ListPlayerCommand)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 CompletableFuture (java.util.concurrent.CompletableFuture)3