use of me.lucko.luckperms.api.context.MutableContextSet 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.api.context.MutableContextSet 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.api.context.MutableContextSet in project LuckPerms by lucko.
the class ContextSetJsonSerializer method deserializeContextSet.
public static ContextSet deserializeContextSet(JsonElement element) {
Preconditions.checkArgument(element.isJsonObject());
JsonObject data = element.getAsJsonObject();
if (data.entrySet().isEmpty()) {
return ContextSet.empty();
}
MutableContextSet map = MutableContextSet.create();
for (Map.Entry<String, JsonElement> e : data.entrySet()) {
String k = e.getKey();
JsonElement v = e.getValue();
if (v.isJsonArray()) {
JsonArray values = v.getAsJsonArray();
for (JsonElement value : values) {
map.add(k, value.getAsString());
}
} else {
map.add(k, v.getAsString());
}
}
return map;
}
use of me.lucko.luckperms.api.context.MutableContextSet in project LuckPerms by lucko.
the class VaultPermissionHook method contextForLookup.
// utility method for getting a contexts instance for a given vault lookup.
Contexts contextForLookup(User user, String world) {
MutableContextSet context;
Player player = user == null ? null : this.plugin.getBootstrap().getPlayer(user.getUuid());
if (player != null) {
context = this.plugin.getContextManager().getApplicableContext(player).mutableCopy();
} else {
context = this.plugin.getContextManager().getStaticContext().mutableCopy();
}
// if world is not null, we want to do a lookup in that specific world
if (world != null && !world.isEmpty()) {
// remove already accumulated worlds
context.removeAll(Contexts.WORLD_KEY);
// add the vault world
context.add(Contexts.WORLD_KEY, world.toLowerCase());
}
// if we're using a special vault server
if (useVaultServer()) {
// remove the normal server context from the set
context.remove(Contexts.SERVER_KEY, getServer());
// add the vault specific server
if (!getVaultServer().equals("global")) {
context.add(Contexts.SERVER_KEY, getVaultServer());
}
}
return Contexts.of(context, isIncludeGlobal(), true, true, true, true, false);
}
use of me.lucko.luckperms.api.context.MutableContextSet in project LuckPerms by lucko.
the class MetaClear method execute.
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label, CommandPermission permission) throws CommandException {
if (ArgumentPermissions.checkModifyPerms(plugin, sender, permission, holder)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
MetaType type = null;
if (!args.isEmpty()) {
String typeId = args.get(0).toLowerCase();
if (typeId.equals("any") || typeId.equals("all") || typeId.equals("*")) {
type = MetaType.ANY;
}
if (typeId.equals("chat") || typeId.equals("chatmeta")) {
type = MetaType.CHAT;
}
if (typeId.equals(NodeFactory.META_KEY)) {
type = MetaType.META;
}
if (typeId.equals(NodeFactory.PREFIX_KEY) || typeId.equals("prefixes")) {
type = MetaType.PREFIX;
}
if (typeId.equals(NodeFactory.SUFFIX_KEY) || typeId.equals("suffixes")) {
type = MetaType.SUFFIX;
}
if (type != null) {
args.remove(0);
}
}
if (type == null) {
type = MetaType.ANY;
}
int before = holder.getEnduringNodes().size();
MutableContextSet context = ArgumentParser.parseContext(0, args, plugin);
if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return CommandResult.NO_PERMISSION;
}
if (context.isEmpty()) {
holder.clearMeta(type);
} else {
holder.clearMeta(type, context);
}
int changed = before - holder.getEnduringNodes().size();
if (changed == 1) {
Message.META_CLEAR_SUCCESS_SINGULAR.send(sender, holder.getFriendlyName(), type.name().toLowerCase(), MessageUtils.contextSetToString(context), changed);
} else {
Message.META_CLEAR_SUCCESS.send(sender, holder.getFriendlyName(), type.name().toLowerCase(), MessageUtils.contextSetToString(context), changed);
}
ExtendedLogEntry.build().actor(sender).acted(holder).action("meta", "clear", context).build().submit(plugin, sender);
StorageAssistant.save(holder, sender, plugin);
return CommandResult.SUCCESS;
}
Aggregations