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;
}
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));
}
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());
}
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());
}
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;
}
Aggregations