Implement default contexts - closes #241

This commit is contained in:
Luck 2017-05-09 13:36:43 +01:00
parent e835b31277
commit 1fee47e087
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
41 changed files with 139 additions and 103 deletions

View File

@ -578,7 +578,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
MutableContextSet set = MutableContextSet.create(); MutableContextSet set = MutableContextSet.create();
set.add("server", getConfiguration().get(ConfigKeys.SERVER)); set.add("server", getConfiguration().get(ConfigKeys.SERVER));
set.add("world", s); set.add("world", s);
set.addAll(configuration.getStaticContexts().getContextSet()); set.addAll(configuration.getContextsFile().getStaticContexts());
return set.makeImmutable(); return set.makeImmutable();
}) })
.collect(Collectors.toList()) .collect(Collectors.toList())
@ -593,7 +593,7 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
MutableContextSet set = MutableContextSet.create(); MutableContextSet set = MutableContextSet.create();
set.add("server", getConfiguration().get(ConfigKeys.VAULT_SERVER)); set.add("server", getConfiguration().get(ConfigKeys.VAULT_SERVER));
set.add("world", s); set.add("world", s);
set.addAll(configuration.getStaticContexts().getContextSet()); set.addAll(configuration.getContextsFile().getStaticContexts());
return set.makeImmutable(); return set.makeImmutable();
}) })
.collect(Collectors.toList()) .collect(Collectors.toList())

View File

@ -69,7 +69,7 @@ public class MetaAddChatMeta extends SharedSubCommand {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
int priority = ArgumentUtils.handlePriority(0, args); int priority = ArgumentUtils.handlePriority(0, args);
String meta = ArgumentUtils.handleString(1, args); String meta = ArgumentUtils.handleString(1, args);
MutableContextSet context = ArgumentUtils.handleContext(2, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin);
DataMutateResult result = holder.setPermission(NodeFactory.makeChatMetaNode(isPrefix, priority, meta).withExtraContext(context).build()); DataMutateResult result = holder.setPermission(NodeFactory.makeChatMetaNode(isPrefix, priority, meta).withExtraContext(context).build());
if (result.asBoolean()) { if (result.asBoolean()) {

View File

@ -76,7 +76,7 @@ public class MetaAddTempChatMeta extends SharedSubCommand {
int priority = ArgumentUtils.handlePriority(0, args); int priority = ArgumentUtils.handlePriority(0, args);
String meta = ArgumentUtils.handleString(1, args); String meta = ArgumentUtils.handleString(1, args);
long duration = ArgumentUtils.handleDuration(2, args); long duration = ArgumentUtils.handleDuration(2, args);
MutableContextSet context = ArgumentUtils.handleContext(3, args); MutableContextSet context = ArgumentUtils.handleContext(3, args, plugin);
TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR); TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR);
Map.Entry<DataMutateResult, Node> ret = holder.setPermission(NodeFactory.makeChatMetaNode(isPrefix, priority, meta).setExpiry(duration).withExtraContext(context).build(), modifier); Map.Entry<DataMutateResult, Node> ret = holder.setPermission(NodeFactory.makeChatMetaNode(isPrefix, priority, meta).setExpiry(duration).withExtraContext(context).build(), modifier);

View File

@ -55,7 +55,7 @@ public class MetaClear extends SharedSubCommand {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
int before = holder.getNodes().size(); int before = holder.getNodes().size();
MutableContextSet context = ArgumentUtils.handleContext(0, args); MutableContextSet context = ArgumentUtils.handleContext(0, args, plugin);
if (context.isEmpty()) { if (context.isEmpty()) {
holder.clearMeta(); holder.clearMeta();

View File

@ -69,7 +69,7 @@ public class MetaRemoveChatMeta extends SharedSubCommand {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
int priority = ArgumentUtils.handlePriority(0, args); int priority = ArgumentUtils.handlePriority(0, args);
String meta = ArgumentUtils.handleStringOrElse(1, args, "null"); String meta = ArgumentUtils.handleStringOrElse(1, args, "null");
MutableContextSet context = ArgumentUtils.handleContext(2, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin);
// Handle bulk removal // Handle bulk removal
if (meta.equalsIgnoreCase("null") || meta.equals("*")) { if (meta.equalsIgnoreCase("null") || meta.equals("*")) {

View File

@ -69,7 +69,7 @@ public class MetaRemoveTempChatMeta extends SharedSubCommand {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
int priority = ArgumentUtils.handlePriority(0, args); int priority = ArgumentUtils.handlePriority(0, args);
String meta = ArgumentUtils.handleStringOrElse(1, args, "null"); String meta = ArgumentUtils.handleStringOrElse(1, args, "null");
MutableContextSet context = ArgumentUtils.handleContext(2, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin);
// Handle bulk removal // Handle bulk removal
if (meta.equalsIgnoreCase("null") || meta.equals("*")) { if (meta.equalsIgnoreCase("null") || meta.equals("*")) {

View File

@ -60,7 +60,7 @@ public class MetaSet extends SharedSubCommand {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String key = args.get(0); String key = args.get(0);
String value = args.get(1); String value = args.get(1);
MutableContextSet context = ArgumentUtils.handleContext(2, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin);
Node n = NodeFactory.makeMetaNode(key, value).withExtraContext(context).build(); Node n = NodeFactory.makeMetaNode(key, value).withExtraContext(context).build();

View File

@ -65,7 +65,7 @@ public class MetaSetTemp extends SharedSubCommand {
String key = args.get(0); String key = args.get(0);
String value = args.get(1); String value = args.get(1);
long duration = ArgumentUtils.handleDuration(2, args); long duration = ArgumentUtils.handleDuration(2, args);
MutableContextSet context = ArgumentUtils.handleContext(2, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin);
TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR); TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR);
Node n = NodeFactory.makeMetaNode(key, value).withExtraContext(context).setExpiry(duration).build(); Node n = NodeFactory.makeMetaNode(key, value).withExtraContext(context).setExpiry(duration).build();

View File

@ -57,7 +57,7 @@ public class MetaUnset extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String key = args.get(0); String key = args.get(0);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
holder.clearMetaKeys(key, context, false); holder.clearMetaKeys(key, context, false);
Message.UNSET_META_SUCCESS.send(sender, key, holder.getFriendlyName(), Util.contextSetToString(context)); Message.UNSET_META_SUCCESS.send(sender, key, holder.getFriendlyName(), Util.contextSetToString(context));

View File

@ -57,7 +57,7 @@ public class MetaUnsetTemp extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String key = args.get(0); String key = args.get(0);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
holder.clearMetaKeys(key, context, true); holder.clearMetaKeys(key, context, true);
Message.UNSET_META_TEMP_SUCCESS.send(sender, key, holder.getFriendlyName(), Util.contextSetToString(context)); Message.UNSET_META_TEMP_SUCCESS.send(sender, key, holder.getFriendlyName(), Util.contextSetToString(context));

View File

@ -58,7 +58,7 @@ public class HolderClear<T extends PermissionHolder> extends SubCommand<T> {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, T holder, List<String> args, String label) throws CommandException {
int before = holder.getNodes().size(); int before = holder.getNodes().size();
MutableContextSet context = ArgumentUtils.handleContext(0, args); MutableContextSet context = ArgumentUtils.handleContext(0, args, plugin);
if (context.isEmpty()) { if (context.isEmpty()) {
holder.clearNodes(); holder.clearNodes();

View File

@ -61,7 +61,7 @@ public class ParentAdd extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String groupName = ArgumentUtils.handleName(0, args); String groupName = ArgumentUtils.handleName(0, args);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
if (!plugin.getStorage().loadGroup(groupName).join()) { if (!plugin.getStorage().loadGroup(groupName).join()) {
Message.GROUP_DOES_NOT_EXIST.send(sender); Message.GROUP_DOES_NOT_EXIST.send(sender);

View File

@ -69,7 +69,7 @@ public class ParentAddTemp extends SharedSubCommand {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String groupName = ArgumentUtils.handleName(0, args); String groupName = ArgumentUtils.handleName(0, args);
long duration = ArgumentUtils.handleDuration(1, args); long duration = ArgumentUtils.handleDuration(1, args);
MutableContextSet context = ArgumentUtils.handleContext(2, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin);
TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR); TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR);
if (!plugin.getStorage().loadGroup(groupName).join()) { if (!plugin.getStorage().loadGroup(groupName).join()) {

View File

@ -55,7 +55,7 @@ public class ParentClear extends SharedSubCommand {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
int before = holder.getNodes().size(); int before = holder.getNodes().size();
MutableContextSet context = ArgumentUtils.handleContext(0, args); MutableContextSet context = ArgumentUtils.handleContext(0, args, plugin);
if (context.isEmpty()) { if (context.isEmpty()) {
holder.clearParents(true); holder.clearParents(true);

View File

@ -63,7 +63,7 @@ public class ParentRemove extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String groupName = ArgumentUtils.handleNameWithSpace(0, args); String groupName = ArgumentUtils.handleNameWithSpace(0, args);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
if (holder instanceof User) { if (holder instanceof User) {
User user = (User) holder; User user = (User) holder;

View File

@ -61,7 +61,7 @@ public class ParentRemoveTemp extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String groupName = ArgumentUtils.handleNameWithSpace(0, args); String groupName = ArgumentUtils.handleNameWithSpace(0, args);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
DataMutateResult result = holder.unsetPermission(NodeFactory.newBuilder("group." + groupName).setExpiry(10L).withExtraContext(context).build()); DataMutateResult result = holder.unsetPermission(NodeFactory.newBuilder("group." + groupName).setExpiry(10L).withExtraContext(context).build());

View File

@ -61,7 +61,7 @@ public class ParentSet extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String groupName = ArgumentUtils.handleName(0, args); String groupName = ArgumentUtils.handleName(0, args);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
if (!plugin.getStorage().loadGroup(groupName).join()) { if (!plugin.getStorage().loadGroup(groupName).join()) {
Message.GROUP_DOES_NOT_EXIST.send(sender); Message.GROUP_DOES_NOT_EXIST.send(sender);

View File

@ -59,7 +59,7 @@ public class PermissionCheck extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String node = ArgumentUtils.handleString(0, args); String node = ArgumentUtils.handleString(0, args);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
Tristate result = holder.hasPermission(NodeFactory.newBuilder(node).withExtraContext(context).build()); Tristate result = holder.hasPermission(NodeFactory.newBuilder(node).withExtraContext(context).build());
String s = Util.formatTristate(result); String s = Util.formatTristate(result);

View File

@ -59,7 +59,7 @@ public class PermissionCheckInherits extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String node = ArgumentUtils.handleString(0, args); String node = ArgumentUtils.handleString(0, args);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
InheritanceInfo result = holder.inheritsPermissionInfo(NodeFactory.newBuilder(node).withExtraContext(context).build()); InheritanceInfo result = holder.inheritsPermissionInfo(NodeFactory.newBuilder(node).withExtraContext(context).build());

View File

@ -64,7 +64,7 @@ public class PermissionSet extends SharedSubCommand {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
boolean b = ArgumentUtils.handleBoolean(1, args); boolean b = ArgumentUtils.handleBoolean(1, args);
String node = b ? ArgumentUtils.handleNode(0, args) : ArgumentUtils.handleString(0, args); String node = b ? ArgumentUtils.handleNode(0, args) : ArgumentUtils.handleString(0, args);
MutableContextSet context = ArgumentUtils.handleContext(2, args); MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin);
DataMutateResult result = holder.setPermission(NodeFactory.newBuilder(node).setValue(b).withExtraContext(context).build()); DataMutateResult result = holder.setPermission(NodeFactory.newBuilder(node).setValue(b).withExtraContext(context).build());

View File

@ -71,7 +71,7 @@ public class PermissionSetTemp extends SharedSubCommand {
boolean b = ArgumentUtils.handleBoolean(1, args); boolean b = ArgumentUtils.handleBoolean(1, args);
String node = b ? ArgumentUtils.handleNode(0, args) : ArgumentUtils.handleString(0, args); String node = b ? ArgumentUtils.handleNode(0, args) : ArgumentUtils.handleString(0, args);
long duration = ArgumentUtils.handleDuration(2, args); long duration = ArgumentUtils.handleDuration(2, args);
MutableContextSet context = ArgumentUtils.handleContext(3, args); MutableContextSet context = ArgumentUtils.handleContext(3, args, plugin);
TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR); TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR);
Map.Entry<DataMutateResult, Node> result = holder.setPermission(NodeFactory.newBuilder(node).setValue(b).withExtraContext(context).setExpiry(duration).build(), modifier); Map.Entry<DataMutateResult, Node> result = holder.setPermission(NodeFactory.newBuilder(node).setValue(b).withExtraContext(context).setExpiry(duration).build(), modifier);

View File

@ -59,7 +59,7 @@ public class PermissionUnset extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String node = ArgumentUtils.handleString(0, args); String node = ArgumentUtils.handleString(0, args);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
DataMutateResult result; DataMutateResult result;
if (node.startsWith("group.")) { if (node.startsWith("group.")) {

View File

@ -59,7 +59,7 @@ public class PermissionUnsetTemp extends SharedSubCommand {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, PermissionHolder holder, List<String> args, String label) throws CommandException {
String node = ArgumentUtils.handleString(0, args); String node = ArgumentUtils.handleString(0, args);
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
DataMutateResult result = holder.unsetPermission(NodeFactory.newBuilder(node).setExpiry(10L).withExtraContext(context).build()); DataMutateResult result = holder.unsetPermission(NodeFactory.newBuilder(node).setExpiry(10L).withExtraContext(context).build());

View File

@ -84,7 +84,7 @@ public class UserDemote extends SubCommand<User> {
return CommandResult.STATE_ERROR; return CommandResult.STATE_ERROR;
} }
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
boolean silent = false; boolean silent = false;
if (args.contains("-s")) { if (args.contains("-s")) {

View File

@ -84,7 +84,7 @@ public class UserPromote extends SubCommand<User> {
return CommandResult.STATE_ERROR; return CommandResult.STATE_ERROR;
} }
MutableContextSet context = ArgumentUtils.handleContext(1, args); MutableContextSet context = ArgumentUtils.handleContext(1, args, plugin);
boolean silent = false; boolean silent = false;
if (args.contains("-s")) { if (args.contains("-s")) {

View File

@ -32,6 +32,7 @@ import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.constants.DataConstraints; import me.lucko.luckperms.common.constants.DataConstraints;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.DateUtil; import me.lucko.luckperms.common.utils.DateUtil;
import java.util.ArrayList; import java.util.ArrayList;
@ -137,7 +138,7 @@ public class ArgumentUtils {
return args.size() > index ? args.get(index).toLowerCase() : null; return args.size() > index ? args.get(index).toLowerCase() : null;
} }
public static MutableContextSet handleContext(int fromIndex, List<String> args) throws CommandException { public static MutableContextSet handleContext(int fromIndex, List<String> args, LuckPermsPlugin plugin) throws CommandException {
if (args.size() > fromIndex) { if (args.size() > fromIndex) {
MutableContextSet set = MutableContextSet.create(); MutableContextSet set = MutableContextSet.create();
@ -149,14 +150,6 @@ public class ArgumentUtils {
// one of the first two values, and doesn't have a key // one of the first two values, and doesn't have a key
if (i <= 1 && !pair.contains("=")) { if (i <= 1 && !pair.contains("=")) {
String key = i == 0 ? "server" : "world"; String key = i == 0 ? "server" : "world";
if (key.equals("server") && !DataConstraints.SERVER_NAME_TEST.test(pair)) {
throw new InvalidServerWorldException();
}
if (key.equals("world") && !DataConstraints.WORLD_NAME_TEST.test(pair)) {
throw new InvalidServerWorldException();
}
set.add(key, pair); set.add(key, pair);
continue; continue;
} }
@ -176,16 +169,16 @@ public class ArgumentUtils {
continue; continue;
} }
if (key.equals("server") && !DataConstraints.SERVER_NAME_TEST.test(value)) {
throw new InvalidServerWorldException();
}
if (key.equals("world") && !DataConstraints.WORLD_NAME_TEST.test(value)) {
throw new InvalidServerWorldException();
}
set.add(key, value); set.add(key, value);
} }
return sanitizeContexts(set);
} else {
return sanitizeContexts(plugin.getConfiguration().getContextsFile().getDefaultContexts().mutableCopy());
}
}
public static MutableContextSet sanitizeContexts(MutableContextSet set) throws ArgumentException {
// remove any potential "global" context mappings // remove any potential "global" context mappings
set.remove("server", "global"); set.remove("server", "global");
set.remove("world", "global"); set.remove("world", "global");
@ -212,12 +205,23 @@ public class ArgumentUtils {
} }
} }
return set; // there's either none or 1
} else { for (String server : servers) {
return MutableContextSet.create(); if (!DataConstraints.SERVER_NAME_TEST.test(server)) {
throw new InvalidServerWorldException();
} }
} }
// there's either none or 1
for (String world : worlds) {
if (!DataConstraints.WORLD_NAME_TEST.test(world)) {
throw new InvalidServerWorldException();
}
}
return set;
}
public static int handlePriority(int index, List<String> args) throws ArgumentException { public static int handlePriority(int index, List<String> args) throws ArgumentException {
try { try {
return Integer.parseInt(args.get(index)); return Integer.parseInt(args.get(index));
@ -226,7 +230,7 @@ public class ArgumentUtils {
} }
} }
public static ImmutableContextSet handleContexts(int fromIndex, List<String> args) { public static ImmutableContextSet handleContextSponge(int fromIndex, List<String> args) {
if (args.size() <= fromIndex) { if (args.size() <= fromIndex) {
return ImmutableContextSet.empty(); return ImmutableContextSet.empty();
} }

View File

@ -45,7 +45,7 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
private final LPConfigurationDelegate delegate = new LPConfigurationDelegate(this); private final LPConfigurationDelegate delegate = new LPConfigurationDelegate(this);
@Getter @Getter
private final StaticContextsFile staticContexts = new StaticContextsFile(this); private final ContextsFile contextsFile = new ContextsFile(this);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
@ -56,7 +56,7 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
@Override @Override
public void loadAll() { public void loadAll() {
ConfigKeys.getAllKeys().forEach(cache::get); ConfigKeys.getAllKeys().forEach(cache::get);
staticContexts.reload(); contextsFile.load();
} }
@Override @Override
@ -67,7 +67,7 @@ public abstract class AbstractConfiguration implements LuckPermsConfiguration {
cache.invalidateAll(toInvalidate); cache.invalidateAll(toInvalidate);
loadAll(); loadAll();
staticContexts.reload(); contextsFile.load();
getPlugin().getApiProvider().getEventFactory().handleConfigReload(); getPlugin().getApiProvider().getEventFactory().handleConfigReload();
} }
} }

View File

@ -27,6 +27,7 @@ package me.lucko.luckperms.common.config;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.Setter;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -43,35 +44,66 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
@RequiredArgsConstructor @RequiredArgsConstructor
public class StaticContextsFile { public class ContextsFile {
private final LuckPermsConfiguration configuration; private final LuckPermsConfiguration configuration;
@Getter @Getter
private ImmutableContextSet contextSet = ImmutableContextSet.empty(); @Setter
private ImmutableContextSet staticContexts = ImmutableContextSet.empty();
public void reload() { @Getter
File file = new File(configuration.getPlugin().getConfigDirectory(), "static-contexts.json"); @Setter
if (!file.exists()) { private ImmutableContextSet defaultContexts = ImmutableContextSet.empty();
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
JsonObject template = new JsonObject(); public void load() {
template.add("context", new JsonObject()); File file = new File(configuration.getPlugin().getConfigDirectory(), "contexts.json");
new GsonBuilder().setPrettyPrinting().create().toJson(template, writer); File oldFile = new File(configuration.getPlugin().getConfigDirectory(), "static-contexts.json");
} catch (IOException e) { if (oldFile.exists()) {
e.printStackTrace(); oldFile.renameTo(file);
} }
contextSet = ImmutableContextSet.empty();
if (!file.exists()) {
save();
return; return;
} }
boolean save = false;
try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) {
JsonObject data = new Gson().fromJson(reader, JsonObject.class); JsonObject data = new Gson().fromJson(reader, JsonObject.class);
if (!data.has("context") || !data.get("context").isJsonObject()) { if (data.has("context")) {
return; staticContexts = NodeModel.deserializeContextSet(data.get("context").getAsJsonObject()).makeImmutable();
save = true;
} }
JsonObject contexts = data.get("context").getAsJsonObject(); if (data.has("static-contexts")) {
contextSet = NodeModel.deserializeContextSet(contexts).makeImmutable(); staticContexts = NodeModel.deserializeContextSet(data.get("static-contexts").getAsJsonObject()).makeImmutable();
}
if (data.has("default-contexts")) {
defaultContexts = NodeModel.deserializeContextSet(data.get("default-contexts").getAsJsonObject()).makeImmutable();
}
} catch (IOException e) {
e.printStackTrace();
}
if (save) {
save();
}
}
public void save() {
File file = new File(configuration.getPlugin().getConfigDirectory(), "contexts.json");
try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
JsonObject data = new JsonObject();
data.add("static-contexts", NodeModel.serializeContextSet(staticContexts));
data.add("default-contexts", NodeModel.serializeContextSet(defaultContexts));
new GsonBuilder().setPrettyPrinting().create().toJson(data, writer);
writer.flush();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -37,7 +37,7 @@ public interface LuckPermsConfiguration {
LuckPermsPlugin getPlugin(); LuckPermsPlugin getPlugin();
StaticContextsFile getStaticContexts(); ContextsFile getContextsFile();
void init(); void init();

View File

@ -43,7 +43,7 @@ public class StaticCalculator<T> implements ContextCalculator<T> {
accumulator.add("server", server); accumulator.add("server", server);
} }
accumulator.addAll(config.getStaticContexts().getContextSet()); accumulator.addAll(config.getContextsFile().getStaticContexts());
return accumulator; return accumulator;
} }

View File

@ -51,7 +51,7 @@ public class OptionClear extends SubCommand<LPSubjectData> {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(0, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(0, args);
if (contextSet.isEmpty()) { if (contextSet.isEmpty()) {
subjectData.clearOptions(); subjectData.clearOptions();
Util.sendPluginMessage(sender, "&aCleared options matching contexts &bANY&a."); Util.sendPluginMessage(sender, "&aCleared options matching contexts &bANY&a.");

View File

@ -52,7 +52,7 @@ public class OptionInfo extends SubCommand<LPSubjectData> {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(0, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(0, args);
if (contextSet.isEmpty()) { if (contextSet.isEmpty()) {
Util.sendPluginMessage(sender, "&aShowing options matching contexts &bANY&a."); Util.sendPluginMessage(sender, "&aShowing options matching contexts &bANY&a.");
Map<ImmutableContextSet, ImmutableMap<String, String>> options = subjectData.getAllOptions(); Map<ImmutableContextSet, ImmutableMap<String, String>> options = subjectData.getAllOptions();

View File

@ -55,7 +55,7 @@ public class OptionSet extends SubCommand<LPSubjectData> {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
String key = args.get(0); String key = args.get(0);
String value = args.get(1); String value = args.get(1);
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(2, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(2, args);
if (subjectData.setOption(contextSet, key, value).join()) { if (subjectData.setOption(contextSet, key, value).join()) {
Util.sendPluginMessage(sender, "&aSet &f\"" + key + "&f\"&a to &f\"" + value + "&f\"&a in context " + SpongeUtils.contextToString(contextSet)); Util.sendPluginMessage(sender, "&aSet &f\"" + key + "&f\"&a to &f\"" + value + "&f\"&a in context " + SpongeUtils.contextToString(contextSet));

View File

@ -53,7 +53,7 @@ public class OptionUnset extends SubCommand<LPSubjectData> {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
String key = args.get(0); String key = args.get(0);
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(1, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(1, args);
if (subjectData.unsetOption(contextSet, key).join()) { if (subjectData.unsetOption(contextSet, key).join()) {
Util.sendPluginMessage(sender, "&aUnset &f\"" + key + "&f\"&a in context " + SpongeUtils.contextToString(contextSet)); Util.sendPluginMessage(sender, "&aUnset &f\"" + key + "&f\"&a in context " + SpongeUtils.contextToString(contextSet));

View File

@ -60,7 +60,7 @@ public class ParentAdd extends SubCommand<LPSubjectData> {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
String collection = args.get(0); String collection = args.get(0);
String name = args.get(1); String name = args.get(1);
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(2, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(2, args);
LuckPermsService service = Sponge.getServiceManager().provideUnchecked(LuckPermsService.class); LuckPermsService service = Sponge.getServiceManager().provideUnchecked(LuckPermsService.class);
if (service.getLoadedCollections().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(collection))) { if (service.getLoadedCollections().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(collection))) {

View File

@ -51,7 +51,7 @@ public class ParentClear extends SubCommand<LPSubjectData> {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(0, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(0, args);
if (contextSet.isEmpty()) { if (contextSet.isEmpty()) {
subjectData.clearParents(); subjectData.clearParents();
Util.sendPluginMessage(sender, "&aCleared parents matching contexts &bANY&a."); Util.sendPluginMessage(sender, "&aCleared parents matching contexts &bANY&a.");

View File

@ -53,7 +53,7 @@ public class ParentInfo extends SubCommand<LPSubjectData> {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(0, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(0, args);
if (contextSet.isEmpty()) { if (contextSet.isEmpty()) {
Util.sendPluginMessage(sender, "&aShowing parents matching contexts &bANY&a."); Util.sendPluginMessage(sender, "&aShowing parents matching contexts &bANY&a.");
Map<ImmutableContextSet, ImmutableList<SubjectReference>> parents = subjectData.getAllParents(); Map<ImmutableContextSet, ImmutableList<SubjectReference>> parents = subjectData.getAllParents();

View File

@ -60,7 +60,7 @@ public class ParentRemove extends SubCommand<LPSubjectData> {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
String collection = args.get(0); String collection = args.get(0);
String name = args.get(1); String name = args.get(1);
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(2, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(2, args);
LuckPermsService service = Sponge.getServiceManager().provideUnchecked(LuckPermsService.class); LuckPermsService service = Sponge.getServiceManager().provideUnchecked(LuckPermsService.class);
if (service.getLoadedCollections().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(collection))) { if (service.getLoadedCollections().keySet().stream().map(String::toLowerCase).noneMatch(s -> s.equalsIgnoreCase(collection))) {

View File

@ -51,7 +51,7 @@ public class PermissionClear extends SubCommand<LPSubjectData> {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(0, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(0, args);
if (contextSet.isEmpty()) { if (contextSet.isEmpty()) {
subjectData.clearPermissions(); subjectData.clearPermissions();
Util.sendPluginMessage(sender, "&aCleared permissions matching contexts &bANY&a."); Util.sendPluginMessage(sender, "&aCleared permissions matching contexts &bANY&a.");

View File

@ -52,7 +52,7 @@ public class PermissionInfo extends SubCommand<LPSubjectData> {
@Override @Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(0, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(0, args);
if (contextSet.isEmpty()) { if (contextSet.isEmpty()) {
Util.sendPluginMessage(sender, "&aShowing permissions matching contexts &bANY&a."); Util.sendPluginMessage(sender, "&aShowing permissions matching contexts &bANY&a.");
Map<ImmutableContextSet, ImmutableMap<String, Boolean>> permissions = subjectData.getAllPermissions(); Map<ImmutableContextSet, ImmutableMap<String, Boolean>> permissions = subjectData.getAllPermissions();

View File

@ -56,7 +56,7 @@ public class PermissionSet extends SubCommand<LPSubjectData> {
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException { public CommandResult execute(LuckPermsPlugin plugin, Sender sender, LPSubjectData subjectData, List<String> args, String label) throws CommandException {
String node = args.get(0); String node = args.get(0);
Tristate tristate = SpongeUtils.parseTristate(1, args); Tristate tristate = SpongeUtils.parseTristate(1, args);
ImmutableContextSet contextSet = ArgumentUtils.handleContexts(2, args); ImmutableContextSet contextSet = ArgumentUtils.handleContextSponge(2, args);
if (subjectData.setPermission(contextSet, node, tristate).join()) { if (subjectData.setPermission(contextSet, node, tristate).join()) {
Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to &b" + tristate.toString().toLowerCase() + "&a in context " + SpongeUtils.contextToString(contextSet)); Util.sendPluginMessage(sender, "&aSet &b" + node + "&a to &b" + tristate.toString().toLowerCase() + "&a in context " + SpongeUtils.contextToString(contextSet));