use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.
the class UserDemote method execute.
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args, String label) throws CommandException {
if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
final String trackName = args.get(0).toLowerCase();
if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
Message.TRACK_INVALID_ENTRY.send(sender, trackName);
return CommandResult.INVALID_ARGS;
}
Track track = StorageAssistant.loadTrack(trackName, sender, plugin);
if (track == null) {
return CommandResult.LOADING_ERROR;
}
if (track.getSize() <= 1) {
Message.TRACK_EMPTY.send(sender, track.getName());
return CommandResult.STATE_ERROR;
}
boolean silent = args.remove("-s");
MutableContextSet context = ArgumentParser.parseContext(1, args, plugin);
if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
// Load applicable groups
Set<Node> nodes = user.getEnduringNodes().values().stream().filter(Node::isGroupNode).filter(Node::getValuePrimitive).filter(node -> node.getFullContexts().makeImmutable().equals(context.makeImmutable())).collect(Collectors.toSet());
nodes.removeIf(g -> !track.containsGroup(g.getGroupName()));
if (nodes.isEmpty()) {
Message.USER_TRACK_ERROR_NOT_CONTAIN_GROUP.send(sender, user.getFriendlyName(), track.getName());
return CommandResult.FAILURE;
}
if (nodes.size() != 1) {
Message.TRACK_AMBIGUOUS_CALL.send(sender, user.getFriendlyName());
return CommandResult.FAILURE;
}
final Node oldNode = Iterables.getFirst(nodes, null);
final String old = oldNode.getGroupName();
final String previous;
try {
previous = track.getPrevious(old);
} catch (IllegalArgumentException e) {
Message.TRACK_DOES_NOT_CONTAIN.send(sender, track.getName(), old);
return CommandResult.STATE_ERROR;
}
if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), oldNode.getGroupName())) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
if (previous == null) {
user.unsetPermission(oldNode);
Message.USER_DEMOTE_ENDOFTRACK.send(sender, track.getName(), user.getFriendlyName(), old);
ExtendedLogEntry.build().actor(sender).acted(user).action("demote", track.getName(), context).build().submit(plugin, sender);
StorageAssistant.save(user, sender, plugin);
plugin.getEventFactory().handleUserDemote(user, track, old, null, sender);
return CommandResult.SUCCESS;
}
Group previousGroup = plugin.getStorage().loadGroup(previous).join().orElse(null);
if (previousGroup == null) {
Message.USER_DEMOTE_ERROR_MALFORMED.send(sender, previous);
return CommandResult.LOADING_ERROR;
}
user.unsetPermission(oldNode);
user.setPermission(NodeFactory.buildGroupNode(previousGroup.getName()).withExtraContext(context).build());
if (context.isEmpty() && user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME).equalsIgnoreCase(old)) {
user.getPrimaryGroup().setStoredValue(previousGroup.getName());
}
Message.USER_DEMOTE_SUCCESS.send(sender, user.getFriendlyName(), track.getName(), old, previousGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
if (!silent) {
Message.EMPTY.send(sender, MessageUtils.listToArrowSep(track.getGroups(), previousGroup.getName(), old, true));
}
ExtendedLogEntry.build().actor(sender).acted(user).action("demote", track.getName(), context).build().submit(plugin, sender);
StorageAssistant.save(user, sender, plugin);
plugin.getEventFactory().handleUserDemote(user, track, old, previousGroup.getName(), sender);
return CommandResult.SUCCESS;
}
use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.
the class UserPromote method execute.
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args, String label) throws CommandException {
if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), user)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
final String trackName = args.get(0).toLowerCase();
if (!DataConstraints.TRACK_NAME_TEST.test(trackName)) {
Message.TRACK_INVALID_ENTRY.send(sender, trackName);
return CommandResult.INVALID_ARGS;
}
Track track = StorageAssistant.loadTrack(trackName, sender, plugin);
if (track == null) {
return CommandResult.LOADING_ERROR;
}
if (track.getSize() <= 1) {
Message.TRACK_EMPTY.send(sender, track.getName());
return CommandResult.STATE_ERROR;
}
boolean silent = args.remove("-s");
MutableContextSet context = ArgumentParser.parseContext(1, args, plugin);
if (ArgumentPermissions.checkContext(plugin, sender, getPermission().get(), context)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
// Load applicable groups
Set<Node> nodes = user.getEnduringNodes().values().stream().filter(Node::isGroupNode).filter(Node::getValuePrimitive).filter(node -> node.getFullContexts().makeImmutable().equals(context.makeImmutable())).collect(Collectors.toSet());
nodes.removeIf(g -> !track.containsGroup(g.getGroupName()));
if (nodes.isEmpty()) {
String first = track.getGroups().get(0);
Group nextGroup = plugin.getGroupManager().getIfLoaded(first);
if (nextGroup == null) {
Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, first);
return CommandResult.LOADING_ERROR;
}
if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), nextGroup.getName())) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
user.setPermission(NodeFactory.buildGroupNode(nextGroup.getId()).withExtraContext(context).build());
Message.USER_TRACK_ADDED_TO_FIRST.send(sender, user.getFriendlyName(), nextGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
ExtendedLogEntry.build().actor(sender).acted(user).action("promote", track.getName(), context).build().submit(plugin, sender);
StorageAssistant.save(user, sender, plugin);
plugin.getEventFactory().handleUserPromote(user, track, null, first, sender);
return CommandResult.SUCCESS;
}
if (nodes.size() != 1) {
Message.TRACK_AMBIGUOUS_CALL.send(sender, user.getFriendlyName());
return CommandResult.FAILURE;
}
final Node oldNode = nodes.stream().findAny().get();
final String old = oldNode.getGroupName();
final String next;
try {
next = track.getNext(old);
} catch (IllegalArgumentException e) {
Message.TRACK_DOES_NOT_CONTAIN.send(sender, track.getName(), old);
return CommandResult.STATE_ERROR;
}
if (next == null) {
Message.USER_PROMOTE_ERROR_ENDOFTRACK.send(sender, track.getName(), user.getFriendlyName());
return CommandResult.STATE_ERROR;
}
if (!plugin.getStorage().loadGroup(next).join().isPresent()) {
Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, next);
return CommandResult.STATE_ERROR;
}
Group nextGroup = plugin.getGroupManager().getIfLoaded(next);
if (nextGroup == null) {
Message.USER_PROMOTE_ERROR_MALFORMED.send(sender, next);
return CommandResult.LOADING_ERROR;
}
if (ArgumentPermissions.checkArguments(plugin, sender, getPermission().get(), track.getName(), nextGroup.getName())) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
user.unsetPermission(oldNode);
user.setPermission(NodeFactory.buildGroupNode(nextGroup.getName()).withExtraContext(context).build());
if (context.isEmpty() && user.getPrimaryGroup().getStoredValue().orElse(NodeFactory.DEFAULT_GROUP_NAME).equalsIgnoreCase(old)) {
user.getPrimaryGroup().setStoredValue(nextGroup.getName());
}
Message.USER_PROMOTE_SUCCESS.send(sender, user.getFriendlyName(), track.getName(), old, nextGroup.getFriendlyName(), MessageUtils.contextSetToString(context));
if (!silent) {
Message.EMPTY.send(sender, MessageUtils.listToArrowSep(track.getGroups(), old, nextGroup.getName(), false));
}
ExtendedLogEntry.build().actor(sender).acted(user).action("promote", track.getName(), context).build().submit(plugin, sender);
StorageAssistant.save(user, sender, plugin);
plugin.getEventFactory().handleUserPromote(user, track, old, nextGroup.getName(), sender);
return CommandResult.SUCCESS;
}
use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.
the class TrackInsert method execute.
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Track track, List<String> args, String label) {
String groupName = args.get(0).toLowerCase();
if (!DataConstraints.GROUP_NAME_TEST.test(groupName)) {
sendDetailedUsage(sender, label);
return CommandResult.INVALID_ARGS;
}
int pos;
try {
pos = Integer.parseInt(args.get(1));
} catch (NumberFormatException e) {
Message.TRACK_INSERT_ERROR_NUMBER.send(sender, args.get(1));
return CommandResult.INVALID_ARGS;
}
Group group = StorageAssistant.loadGroup(groupName, sender, plugin, false);
if (group == null) {
return CommandResult.LOADING_ERROR;
}
try {
DataMutateResult result = track.insertGroup(group, pos - 1);
if (result.asBoolean()) {
Message.TRACK_INSERT_SUCCESS.send(sender, group.getName(), track.getName(), pos);
if (track.getGroups().size() > 1) {
Message.EMPTY.send(sender, MessageUtils.listToArrowSep(track.getGroups(), group.getName()));
}
ExtendedLogEntry.build().actor(sender).acted(track).action("insert", group.getName(), pos).build().submit(plugin, sender);
StorageAssistant.save(track, sender, plugin);
return CommandResult.SUCCESS;
} else {
Message.TRACK_ALREADY_CONTAINS.send(sender, track.getName(), group.getName());
return CommandResult.STATE_ERROR;
}
} catch (IndexOutOfBoundsException e) {
Message.TRACK_INSERT_ERROR_INVALID_POS.send(sender, pos);
return CommandResult.INVALID_ARGS;
}
}
use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.
the class HolderSubject method getParents.
@Override
public ImmutableList<LPSubjectReference> getParents(ImmutableContextSet contexts) {
InheritanceGraph graph = this.plugin.getInheritanceHandler().getGraph(this.plugin.getContextManager().formContexts(contexts));
Iterable<PermissionHolder> traversal = graph.traverse(TraversalAlgorithm.DEPTH_FIRST_PRE_ORDER, this.parent);
ImmutableList.Builder<LPSubjectReference> subjects = ImmutableList.builder();
for (PermissionHolder parent : traversal) {
if (!(parent instanceof Group)) {
continue;
}
subjects.add(((SpongeGroup) parent).sponge().toReference());
}
return subjects.build();
}
use of me.lucko.luckperms.common.model.Group in project LuckPerms by lucko.
the class MigrationBPermissions method execute.
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List<String> args, String label) {
ProgressLogger log = new ProgressLogger("bPermissions");
log.addListener(plugin.getConsoleSender());
log.addListener(sender);
log.log("Starting.");
WorldManager worldManager = WorldManager.getInstance();
if (worldManager == null) {
log.logError("Plugin not loaded.");
return CommandResult.STATE_ERROR;
}
log.log("Forcing the plugin to load all data. This could take a while.");
for (World world : worldManager.getAllWorlds()) {
log.log("Loading users in world " + world.getName());
YamlConfiguration yamlWorldUsers = null;
try {
yamlWorldUsers = (YamlConfiguration) UCONFIG_FIELD.get(world);
} catch (Throwable t) {
t.printStackTrace();
}
if (yamlWorldUsers == null) {
continue;
}
ConfigurationSection configSection = yamlWorldUsers.getConfigurationSection("users");
if (configSection == null) {
continue;
}
Set<String> users = configSection.getKeys(false);
if (users == null) {
log.logError("Couldn't get a list of users.");
return CommandResult.FAILURE;
}
AtomicInteger userLoadCount = new AtomicInteger(0);
users.forEach(s -> {
world.loadOne(s, CalculableType.USER);
log.logProgress("Forcefully loaded {} users so far.", userLoadCount.incrementAndGet());
});
}
log.log("Forcefully loaded all users.");
// Migrate one world at a time.
log.log("Starting world migration.");
Iterators.iterate(worldManager.getAllWorlds(), world -> {
log.log("Migrating world: " + world.getName());
// Migrate all groups
log.log("Starting group migration in world " + world.getName() + ".");
AtomicInteger groupCount = new AtomicInteger(0);
Iterators.iterate(world.getAll(CalculableType.GROUP), group -> {
String groupName = MigrationUtils.standardizeName(group.getName());
if (group.getName().equalsIgnoreCase(world.getDefaultGroup())) {
groupName = NodeFactory.DEFAULT_GROUP_NAME;
}
// Make a LuckPerms group for the one being migrated.
Group lpGroup = plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join();
MigrationUtils.setGroupWeight(lpGroup, group.getPriority());
migrateHolder(world, group, lpGroup);
plugin.getStorage().saveGroup(lpGroup);
log.logAllProgress("Migrated {} groups so far.", groupCount.incrementAndGet());
});
log.log("Migrated " + groupCount.get() + " groups in world " + world.getName() + ".");
// Migrate all users
log.log("Starting user migration in world " + world.getName() + ".");
AtomicInteger userCount = new AtomicInteger(0);
Iterators.iterate(world.getAll(CalculableType.USER), user -> {
// There is no mention of UUIDs in the API. I assume that name = uuid. idk?
UUID uuid = BukkitMigrationUtils.lookupUuid(log, user.getName());
if (uuid == null) {
return;
}
// Make a LuckPerms user for the one being migrated.
User lpUser = plugin.getStorage().loadUser(uuid, null).join();
migrateHolder(world, user, lpUser);
plugin.getStorage().saveUser(lpUser);
plugin.getUserManager().cleanup(lpUser);
log.logProgress("Migrated {} users so far.", userCount.incrementAndGet());
});
log.log("Migrated " + userCount.get() + " users in world " + world.getName() + ".");
});
log.log("Success! Migration complete.");
return CommandResult.SUCCESS;
}
Aggregations