More fixes on handling of server contexts. Fixes #88

* Server contexts will be automatically added if not found on set.
* Flag definitions will automatically add a server context if none is
found on deserialization.
* Option and Flag GUI will fallback to no server context if none is
found.
This commit is contained in:
bloodshot 2019-12-31 22:41:10 -05:00
parent 90d7db65cc
commit 63005e72f8
13 changed files with 235 additions and 75 deletions

View File

@ -941,15 +941,24 @@ private Consumer<CommandSender> createCustomFlagConsumer(GDPermissionUser src, G
private Consumer<CommandSender> createFlagConsumer(GDPermissionUser src, GDClaim claim, Flag flag, FlagContextHolder flagHolder, Tristate newValue, Set<Context> contexts, MenuType displayType) { private Consumer<CommandSender> createFlagConsumer(GDPermissionUser src, GDClaim claim, Flag flag, FlagContextHolder flagHolder, Tristate newValue, Set<Context> contexts, MenuType displayType) {
final Player player = src.getOnlinePlayer(); final Player player = src.getOnlinePlayer();
return consumer -> { return consumer -> {
Set<Context> newContexts = new HashSet<>();
GDCauseStackManager.getInstance().pushCause(player); GDCauseStackManager.getInstance().pushCause(player);
final boolean isCustom = UIHelper.containsCustomContext(contexts); Set<Context> newContexts = new HashSet<>(contexts);
if (!isCustom && displayType == MenuType.CLAIM) { if (displayType == MenuType.CLAIM) {
final Iterator<Context> iterator = newContexts.iterator();
while (iterator.hasNext()) {
final Context context = iterator.next();
if (context.getKey().equals("gd_claim_default")) {
iterator.remove();
}
}
newContexts.add(claim.getContext()); newContexts.add(claim.getContext());
} else {
newContexts.addAll(contexts);
} }
Context serverContext = null;
final String serverName = PermissionUtil.getInstance().getServerName();
if (serverName != null) {
serverContext = new Context("server", serverName);
}
// Check server context // Check server context
final Iterator<Context> iterator = newContexts.iterator(); final Iterator<Context> iterator = newContexts.iterator();
boolean hasServerContext = false; boolean hasServerContext = false;
@ -961,11 +970,8 @@ private Consumer<CommandSender> createFlagConsumer(GDPermissionUser src, GDClaim
} }
} }
if (!hasServerContext) { if (!hasServerContext && serverContext != null) {
final String serverName = PermissionUtil.getInstance().getServerName(); newContexts.add(serverContext);
if (serverName != null) {
newContexts.add(new Context("server", serverName));
}
} }
GDFlagPermissionEvent.Set event = new GDFlagPermissionEvent.Set(this.subject, flag, newValue, newContexts); GDFlagPermissionEvent.Set event = new GDFlagPermissionEvent.Set(this.subject, flag, newValue, newContexts);
@ -976,6 +982,11 @@ private Consumer<CommandSender> createFlagConsumer(GDPermissionUser src, GDClaim
} }
PermissionResult result = PermissionUtil.getInstance().setPermissionValue(this.subject, flag, newValue, newContexts); PermissionResult result = PermissionUtil.getInstance().setPermissionValue(this.subject, flag, newValue, newContexts);
if (!result.successful()) {
// Try again without server context
newContexts.remove(serverContext);
result = PermissionUtil.getInstance().setPermissionValue(this.subject, flag, newValue, newContexts, false, true);
}
if (result.successful()) { if (result.successful()) {
showFlagPermissions(src, claim, displayType); showFlagPermissions(src, claim, displayType);
} }

View File

@ -781,14 +781,23 @@ private Consumer<CommandSender> newOptionValueConsumer(GDPermissionUser src, GDC
newValue = value == null ? "undefined" :String.valueOf(value); newValue = value == null ? "undefined" :String.valueOf(value);
} }
Set<Context> newContexts = new HashSet<>(); Set<Context> newContexts = new HashSet<>(contexts);
final boolean isCustom = this.containsCustomContext(option, contexts); if (displayType == MenuType.CLAIM) {
if (!isCustom && displayType == MenuType.CLAIM) { final Iterator<Context> iterator = newContexts.iterator();
while (iterator.hasNext()) {
final Context context = iterator.next();
if (context.getKey().equals("gd_claim_default")) {
iterator.remove();
}
}
newContexts.add(claim.getContext()); newContexts.add(claim.getContext());
} else {
newContexts.addAll(contexts);
} }
Context serverContext = null;
final String serverName = PermissionUtil.getInstance().getServerName();
if (serverName != null) {
serverContext = new Context("server", serverName);
}
// Check server context // Check server context
final Iterator<Context> iterator = newContexts.iterator(); final Iterator<Context> iterator = newContexts.iterator();
boolean hasServerContext = false; boolean hasServerContext = false;
@ -800,13 +809,15 @@ private Consumer<CommandSender> newOptionValueConsumer(GDPermissionUser src, GDC
} }
} }
if (!hasServerContext) { if (!hasServerContext && serverContext != null) {
final String serverName = PermissionUtil.getInstance().getServerName(); newContexts.add(serverContext);
if (serverName != null) {
newContexts.add(new Context("server", serverName));
}
} }
final PermissionResult result = PermissionUtil.getInstance().setOptionValue(this.subject, option.getPermission(), newValue, newContexts); final PermissionResult result = PermissionUtil.getInstance().setOptionValue(this.subject, option.getPermission(), newValue, newContexts);
if (!result.successful()) {
// Try again without server context
newContexts.remove(serverContext);
PermissionUtil.getInstance().setOptionValue(this.subject, option.getPermission(), newValue, newContexts, false);
}
if (result.successful()) { if (result.successful()) {
if (option == Options.PLAYER_WEATHER) { if (option == Options.PLAYER_WEATHER) {
CommonEntityEventHandler.getInstance().checkPlayerWeather(src, claim, claim, true); CommonEntityEventHandler.getInstance().checkPlayerWeather(src, claim, claim, true);

View File

@ -39,7 +39,7 @@
import com.griefdefender.permission.flag.GDFlagData; import com.griefdefender.permission.flag.GDFlagData;
import com.griefdefender.permission.flag.GDFlagDefinition; import com.griefdefender.permission.flag.GDFlagDefinition;
import com.griefdefender.registry.FlagRegistryModule; import com.griefdefender.registry.FlagRegistryModule;
import com.griefdefender.util.PermissionUtil;
import net.kyori.text.Component; import net.kyori.text.Component;
import net.kyori.text.TextComponent; import net.kyori.text.TextComponent;
import net.kyori.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
@ -146,6 +146,18 @@ public FlagDefinition deserialize(TypeToken<?> type, ConfigurationNode node) thr
contexts.add(new Context(key, value)); contexts.add(new Context(key, value));
} }
} }
// Check for required server context and add if not found
boolean hasServerContext = false;
for (Context context : contexts) {
if (context.getKey().equalsIgnoreCase("server")) {
hasServerContext = true;
break;
}
}
if (!hasServerContext) {
final String serverName = PermissionUtil.getInstance().getServerName() == null ? "global" : PermissionUtil.getInstance().getServerName();
contexts.add(new Context("server", serverName));
}
flagDefinition.setContexts(contexts); flagDefinition.setContexts(contexts);
} }
return flagDefinition; return flagDefinition;

View File

@ -733,8 +733,12 @@ public List<String> getOptionValueList(GDPermissionHolder holder, Option option,
return list; return list;
} }
public PermissionResult setOptionValue(GDPermissionHolder holder, String key, String value, Set<Context> contexts) { public PermissionResult setOptionValue(GDPermissionHolder holder, String key, String value, Set<Context> contexts, boolean check) {
DataMutateResult result = null; DataMutateResult result = null;
if (check) {
// If no server context exists, add global
this.checkServerContext(contexts, value);
}
ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy(); ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder); final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
if (permissionHolder == null) { if (permissionHolder == null) {
@ -768,8 +772,12 @@ public PermissionResult setOptionValue(GDPermissionHolder holder, String key, St
return new GDPermissionResult(ResultTypes.FAILURE); return new GDPermissionResult(ResultTypes.FAILURE);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean save) { public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save) {
DataMutateResult result = null; DataMutateResult result = null;
if (check) {
// If no server context exists, add global
this.checkServerContext(contexts, value.name());
}
ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy(); ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
final Node node = this.luckPermsApi.getNodeBuilderRegistry().forPermission().permission(permission).value(value.asBoolean()).context(set).build(); final Node node = this.luckPermsApi.getNodeBuilderRegistry().forPermission().permission(permission).value(value.asBoolean()).context(set).build();
final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder); final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
@ -803,6 +811,8 @@ public PermissionResult setPermissionValue(GDPermissionHolder holder, String per
} }
public void setTransientOption(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) { public void setTransientOption(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) {
// If no server context exists, add global
this.checkServerContext(contexts, value);
MutableContextSet contextSet = this.getLPContexts(contexts); MutableContextSet contextSet = this.getLPContexts(contexts);
final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder); final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
if (permissionHolder == null) { if (permissionHolder == null) {
@ -814,6 +824,8 @@ public void setTransientOption(GDPermissionHolder holder, String permission, Str
} }
public void setTransientPermission(GDPermissionHolder holder, String permission, Boolean value, Set<Context> contexts) { public void setTransientPermission(GDPermissionHolder holder, String permission, Boolean value, Set<Context> contexts) {
// If no server context exists, add global
this.checkServerContext(contexts, value.toString());
MutableContextSet contextSet = this.getLPContexts(contexts); MutableContextSet contextSet = this.getLPContexts(contexts);
final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder); final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
if (permissionHolder == null) { if (permissionHolder == null) {
@ -886,6 +898,20 @@ private void clearMeta(PermissionHolder holder, String metaKey, ContextSet set)
holder.data().clear(set, NodeType.META.predicate(node -> node.getMetaKey().equals(metaKey))); holder.data().clear(set, NodeType.META.predicate(node -> node.getMetaKey().equals(metaKey)));
} }
private void checkServerContext(Set<Context> contexts, String value) {
for (Context context : contexts) {
if (context.getKey().equalsIgnoreCase("server")) {
return;
}
}
final String serverName = this.luckPermsApi.getServerName();
if (serverName != null) {
contexts.add(new Context("server", serverName));
} else {
contexts.add(new Context("server", "global"));
}
}
private static class DefaultDataQueryOrderFunction implements DataQueryOrderFunction { private static class DefaultDataQueryOrderFunction implements DataQueryOrderFunction {
@Override @Override

View File

@ -300,9 +300,24 @@ public interface PermissionProvider {
* @param permission The permission * @param permission The permission
* @param value The value * @param value The value
* @param contexts The contexts * @param contexts The contexts
* @param check Whether to check and apply a server context if none exists
* @return The permission result * @return The permission result
*/ */
PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts); default PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) {
return this.setOptionValue(holder, permission, value, contexts, true);
}
/**
* Sets an option and value with contexts to a holder.
*
* @param holder The holder
* @param permission The permission
* @param value The value
* @param contexts The contexts
* @param check Whether to check and apply a server context if none exists
* @return The permission result
*/
PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts, boolean check);
/** /**
* Sets a permission and value with contexts to a holder. * Sets a permission and value with contexts to a holder.
@ -314,7 +329,7 @@ public interface PermissionProvider {
* @return The permission result * @return The permission result
*/ */
default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts) { default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts) {
return this.setPermissionValue(holder, flag, value, contexts, true); return this.setPermissionValue(holder, flag.getPermission(), value, contexts, true, true);
} }
/** /**
@ -324,11 +339,12 @@ default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag
* @param flag The flag to use for permission * @param flag The flag to use for permission
* @param value The value * @param value The value
* @param contexts The contexts * @param contexts The contexts
* @param check Whether to check and apply a server context if none exists
* @param save Whether a save should occur * @param save Whether a save should occur
* @return The permission result * @return The permission result
*/ */
default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts, boolean save) { default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts, boolean check, boolean save) {
return this.setPermissionValue(holder, flag.getPermission(), value, contexts, save); return this.setPermissionValue(holder, flag.getPermission(), value, contexts, check, save);
} }
/** /**
@ -341,7 +357,7 @@ default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag
* @return Whether the set permission operation was successful * @return Whether the set permission operation was successful
*/ */
default PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts) { default PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts) {
return this.setPermissionValue(holder, permission, value, contexts, true); return this.setPermissionValue(holder, permission, value, contexts, true, true);
} }
/** /**
@ -351,10 +367,11 @@ default PermissionResult setPermissionValue(GDPermissionHolder holder, String pe
* @param permission The permission * @param permission The permission
* @param value The value * @param value The value
* @param contexts The contexts * @param contexts The contexts
* @param check Whether to check and apply a server context if none exists
* @param save Whether a save should occur * @param save Whether a save should occur
* @return Whether the set permission operation was successful * @return Whether the set permission operation was successful
*/ */
PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean save); PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save);
/** /**
* Sets a transient option and value with contexts to a holder. * Sets a transient option and value with contexts to a holder.

View File

@ -407,7 +407,7 @@ public List<String> getOptionValueList(GDPermissionHolder holder, Option option,
} }
@Override @Override
public PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) { public PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts, boolean check) {
return convertResult(holderToPEXSubject(holder).data().update(data -> data.setOption(contextsGDToPEX(contexts), permission, value))).join(); return convertResult(holderToPEXSubject(holder).data().update(data -> data.setOption(contextsGDToPEX(contexts), permission, value))).join();
} }
@ -428,7 +428,7 @@ public void refreshCachedData(GDPermissionHolder holder) {
} }
@Override @Override
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean save) { public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save) {
return convertResult(holderToPEXSubject(holder).data().update(data -> data.setPermission(contextsGDToPEX(contexts), permission, intFromTristate(value)))).join(); return convertResult(holderToPEXSubject(holder).data().update(data -> data.setPermission(contextsGDToPEX(contexts), permission, intFromTristate(value)))).join();
} }

View File

@ -196,23 +196,27 @@ public List<String> getOptionValueList(GDPermissionHolder holder, Option option,
} }
public PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) { public PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) {
return PERMISSION_PROVIDER.setOptionValue(holder, permission, value, contexts); return PERMISSION_PROVIDER.setOptionValue(holder, permission, value, contexts, true);
}
public PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts, boolean check) {
return PERMISSION_PROVIDER.setOptionValue(holder, permission, value, contexts, check);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts) { public PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts) {
return PERMISSION_PROVIDER.setPermissionValue(holder, flag, value, contexts, true); return PERMISSION_PROVIDER.setPermissionValue(holder, flag, value, contexts, true, true);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts) { public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts) {
return PERMISSION_PROVIDER.setPermissionValue(holder, permission, value, contexts, true); return PERMISSION_PROVIDER.setPermissionValue(holder, permission, value, contexts, true, true);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts, boolean save) { public PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts, boolean check, boolean save) {
return PERMISSION_PROVIDER.setPermissionValue(holder, flag, value, contexts, save); return PERMISSION_PROVIDER.setPermissionValue(holder, flag, value, contexts, check, save);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean save) { public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save) {
return PERMISSION_PROVIDER.setPermissionValue(holder, permission, value, contexts, save); return PERMISSION_PROVIDER.setPermissionValue(holder, permission, value, contexts, check, save);
} }
public void setTransientOption(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) { public void setTransientOption(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) {

View File

@ -941,15 +941,24 @@ private Consumer<CommandSource> createCustomFlagConsumer(GDPermissionUser src, G
private Consumer<CommandSource> createFlagConsumer(GDPermissionUser src, GDClaim claim, Flag flag, FlagContextHolder flagHolder, Tristate newValue, Set<Context> contexts, MenuType displayType) { private Consumer<CommandSource> createFlagConsumer(GDPermissionUser src, GDClaim claim, Flag flag, FlagContextHolder flagHolder, Tristate newValue, Set<Context> contexts, MenuType displayType) {
final Player player = src.getOnlinePlayer(); final Player player = src.getOnlinePlayer();
return consumer -> { return consumer -> {
Set<Context> newContexts = new HashSet<>();
GDCauseStackManager.getInstance().pushCause(player); GDCauseStackManager.getInstance().pushCause(player);
final boolean isCustom = UIHelper.containsCustomContext(contexts); Set<Context> newContexts = new HashSet<>(contexts);
if (!isCustom && displayType == MenuType.CLAIM) { if (displayType == MenuType.CLAIM) {
final Iterator<Context> iterator = newContexts.iterator();
while (iterator.hasNext()) {
final Context context = iterator.next();
if (context.getKey().equals("gd_claim_default")) {
iterator.remove();
}
}
newContexts.add(claim.getContext()); newContexts.add(claim.getContext());
} else {
newContexts.addAll(contexts);
} }
Context serverContext = null;
final String serverName = PermissionUtil.getInstance().getServerName();
if (serverName != null) {
serverContext = new Context("server", serverName);
}
// Check server context // Check server context
final Iterator<Context> iterator = newContexts.iterator(); final Iterator<Context> iterator = newContexts.iterator();
boolean hasServerContext = false; boolean hasServerContext = false;
@ -961,11 +970,8 @@ private Consumer<CommandSource> createFlagConsumer(GDPermissionUser src, GDClaim
} }
} }
if (!hasServerContext) { if (!hasServerContext && serverContext != null) {
final String serverName = PermissionUtil.getInstance().getServerName(); newContexts.add(serverContext);
if (serverName != null) {
newContexts.add(new Context("server", serverName));
}
} }
GDFlagPermissionEvent.Set event = new GDFlagPermissionEvent.Set(this.subject, flag, newValue, newContexts); GDFlagPermissionEvent.Set event = new GDFlagPermissionEvent.Set(this.subject, flag, newValue, newContexts);
@ -976,6 +982,11 @@ private Consumer<CommandSource> createFlagConsumer(GDPermissionUser src, GDClaim
} }
PermissionResult result = PermissionUtil.getInstance().setPermissionValue(this.subject, flag, newValue, newContexts); PermissionResult result = PermissionUtil.getInstance().setPermissionValue(this.subject, flag, newValue, newContexts);
if (!result.successful()) {
// Try again without server context
newContexts.remove(serverContext);
result = PermissionUtil.getInstance().setPermissionValue(this.subject, flag, newValue, newContexts, false, true);
}
if (result.successful()) { if (result.successful()) {
showFlagPermissions(src, claim, displayType); showFlagPermissions(src, claim, displayType);
} }

View File

@ -781,33 +781,43 @@ private Consumer<CommandSource> newOptionValueConsumer(GDPermissionUser src, GDC
newValue = value == null ? "undefined" :String.valueOf(value); newValue = value == null ? "undefined" :String.valueOf(value);
} }
Set<Context> newContexts = new HashSet<>(); Set<Context> newContexts = new HashSet<>(contexts);
final boolean isCustom = this.containsCustomContext(option, contexts); if (displayType == MenuType.CLAIM) {
if (!isCustom && displayType == MenuType.CLAIM) { final Iterator<Context> iterator = newContexts.iterator();
while (iterator.hasNext()) {
final Context context = iterator.next();
if (context.getKey().equals("gd_claim_default")) {
iterator.remove();
}
}
newContexts.add(claim.getContext()); newContexts.add(claim.getContext());
} else {
newContexts.addAll(contexts);
} }
Context serverContext = null;
final String serverName = PermissionUtil.getInstance().getServerName();
if (serverName != null) {
serverContext = new Context("server", serverName);
}
// Check server context // Check server context
final Iterator<Context> iterator = newContexts.iterator(); final Iterator<Context> iterator = newContexts.iterator();
boolean hasServerContext = false; boolean hasServerContext = false;
while (iterator.hasNext()) { while (iterator.hasNext()) {
final Context context = iterator.next(); final Context context = iterator.next();
if (context.getKey().equals("server")) { if (context.getKey().equals("server")) {
//iterator.remove();
hasServerContext = true; hasServerContext = true;
break; break;
} }
} }
if (!hasServerContext) { if (!hasServerContext && serverContext != null) {
final String serverName = PermissionUtil.getInstance().getServerName(); newContexts.add(serverContext);
if (serverName != null) {
newContexts.add(new Context("server", serverName));
}
} }
final PermissionResult result = PermissionUtil.getInstance().setOptionValue(this.subject, option.getPermission(), newValue, newContexts); final PermissionResult result = PermissionUtil.getInstance().setOptionValue(this.subject, option.getPermission(), newValue, newContexts);
if (!result.successful()) {
// Try again without server context
newContexts.remove(serverContext);
PermissionUtil.getInstance().setOptionValue(this.subject, option.getPermission(), newValue, newContexts, false);
}
if (result.successful()) { if (result.successful()) {
if (option == Options.PLAYER_WEATHER) { if (option == Options.PLAYER_WEATHER) {
CommonEntityEventHandler.getInstance().checkPlayerWeather(src, claim, claim, true); CommonEntityEventHandler.getInstance().checkPlayerWeather(src, claim, claim, true);

View File

@ -39,7 +39,7 @@
import com.griefdefender.permission.flag.GDFlagData; import com.griefdefender.permission.flag.GDFlagData;
import com.griefdefender.permission.flag.GDFlagDefinition; import com.griefdefender.permission.flag.GDFlagDefinition;
import com.griefdefender.registry.FlagRegistryModule; import com.griefdefender.registry.FlagRegistryModule;
import com.griefdefender.util.PermissionUtil;
import net.kyori.text.Component; import net.kyori.text.Component;
import net.kyori.text.TextComponent; import net.kyori.text.TextComponent;
import net.kyori.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.text.serializer.legacy.LegacyComponentSerializer;
@ -146,6 +146,17 @@ public FlagDefinition deserialize(TypeToken<?> type, ConfigurationNode node) thr
contexts.add(new Context(key, value)); contexts.add(new Context(key, value));
} }
} }
boolean hasServerContext = false;
for (Context context : contexts) {
if (context.getKey().equalsIgnoreCase("server")) {
hasServerContext = true;
break;
}
}
if (!hasServerContext) {
final String serverName = PermissionUtil.getInstance().getServerName() == null ? "global" : PermissionUtil.getInstance().getServerName();
contexts.add(new Context("server", serverName));
}
flagDefinition.setContexts(contexts); flagDefinition.setContexts(contexts);
} }
return flagDefinition; return flagDefinition;

View File

@ -729,8 +729,12 @@ public List<String> getOptionValueList(GDPermissionHolder holder, Option option,
return list; return list;
} }
public PermissionResult setOptionValue(GDPermissionHolder holder, String key, String value, Set<Context> contexts) { public PermissionResult setOptionValue(GDPermissionHolder holder, String key, String value, Set<Context> contexts, boolean check) {
DataMutateResult result = null; DataMutateResult result = null;
if (check) {
// If no server context exists, add global
this.checkServerContext(contexts, value);
}
ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy(); ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder); final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
if (permissionHolder == null) { if (permissionHolder == null) {
@ -764,8 +768,12 @@ public PermissionResult setOptionValue(GDPermissionHolder holder, String key, St
return new GDPermissionResult(ResultTypes.FAILURE); return new GDPermissionResult(ResultTypes.FAILURE);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean save) { public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save) {
DataMutateResult result = null; DataMutateResult result = null;
if (check) {
// If no server context exists, add global
this.checkServerContext(contexts, value.name());
}
ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy(); ImmutableContextSet set = this.getLPContexts(contexts).immutableCopy();
final Node node = this.luckPermsApi.getNodeBuilderRegistry().forPermission().permission(permission).value(value.asBoolean()).context(set).build(); final Node node = this.luckPermsApi.getNodeBuilderRegistry().forPermission().permission(permission).value(value.asBoolean()).context(set).build();
final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder); final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
@ -799,6 +807,8 @@ public PermissionResult setPermissionValue(GDPermissionHolder holder, String per
} }
public void setTransientOption(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) { public void setTransientOption(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) {
// If no server context exists, add global
this.checkServerContext(contexts, value);
MutableContextSet contextSet = this.getLPContexts(contexts); MutableContextSet contextSet = this.getLPContexts(contexts);
final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder); final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
if (permissionHolder == null) { if (permissionHolder == null) {
@ -810,6 +820,8 @@ public void setTransientOption(GDPermissionHolder holder, String permission, Str
} }
public void setTransientPermission(GDPermissionHolder holder, String permission, Boolean value, Set<Context> contexts) { public void setTransientPermission(GDPermissionHolder holder, String permission, Boolean value, Set<Context> contexts) {
// If no server context exists, add global
this.checkServerContext(contexts, value.toString());
MutableContextSet contextSet = this.getLPContexts(contexts); MutableContextSet contextSet = this.getLPContexts(contexts);
final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder); final PermissionHolder permissionHolder = this.getLuckPermsHolder(holder);
if (permissionHolder == null) { if (permissionHolder == null) {
@ -882,6 +894,20 @@ private void clearMeta(PermissionHolder holder, String metaKey, ContextSet set)
holder.data().clear(set, NodeType.META.predicate(node -> node.getMetaKey().equals(metaKey))); holder.data().clear(set, NodeType.META.predicate(node -> node.getMetaKey().equals(metaKey)));
} }
private void checkServerContext(Set<Context> contexts, String value) {
for (Context context : contexts) {
if (context.getKey().equalsIgnoreCase("server")) {
return;
}
}
final String serverName = this.luckPermsApi.getServerName();
if (serverName != null) {
contexts.add(new Context("server", serverName));
} else {
contexts.add(new Context("server", "global"));
}
}
private static class DefaultDataQueryOrderFunction implements DataQueryOrderFunction { private static class DefaultDataQueryOrderFunction implements DataQueryOrderFunction {
@Override @Override

View File

@ -300,9 +300,24 @@ public interface PermissionProvider {
* @param permission The permission * @param permission The permission
* @param value The value * @param value The value
* @param contexts The contexts * @param contexts The contexts
* @param check Whether to check and apply a server context if none exists
* @return The permission result * @return The permission result
*/ */
PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts); default PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) {
return this.setOptionValue(holder, permission, value, contexts, true);
}
/**
* Sets an option and value with contexts to a holder.
*
* @param holder The holder
* @param permission The permission
* @param value The value
* @param contexts The contexts
* @param check Whether to check and apply a server context if none exists
* @return The permission result
*/
PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts, boolean check);
/** /**
* Sets a permission and value with contexts to a holder. * Sets a permission and value with contexts to a holder.
@ -314,7 +329,7 @@ public interface PermissionProvider {
* @return The permission result * @return The permission result
*/ */
default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts) { default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts) {
return this.setPermissionValue(holder, flag, value, contexts, true); return this.setPermissionValue(holder, flag.getPermission(), value, contexts, true, true);
} }
/** /**
@ -324,11 +339,12 @@ default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag
* @param flag The flag to use for permission * @param flag The flag to use for permission
* @param value The value * @param value The value
* @param contexts The contexts * @param contexts The contexts
* @param check Whether to check and apply a server context if none exists
* @param save Whether a save should occur * @param save Whether a save should occur
* @return The permission result * @return The permission result
*/ */
default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts, boolean save) { default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts, boolean check, boolean save) {
return this.setPermissionValue(holder, flag.getPermission(), value, contexts, save); return this.setPermissionValue(holder, flag.getPermission(), value, contexts, check, save);
} }
/** /**
@ -341,7 +357,7 @@ default PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag
* @return Whether the set permission operation was successful * @return Whether the set permission operation was successful
*/ */
default PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts) { default PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts) {
return this.setPermissionValue(holder, permission, value, contexts, true); return this.setPermissionValue(holder, permission, value, contexts, true, true);
} }
/** /**
@ -351,10 +367,11 @@ default PermissionResult setPermissionValue(GDPermissionHolder holder, String pe
* @param permission The permission * @param permission The permission
* @param value The value * @param value The value
* @param contexts The contexts * @param contexts The contexts
* @param check Whether to check and apply a server context if none exists
* @param save Whether a save should occur * @param save Whether a save should occur
* @return Whether the set permission operation was successful * @return Whether the set permission operation was successful
*/ */
PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean save); PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save);
/** /**
* Sets a transient option and value with contexts to a holder. * Sets a transient option and value with contexts to a holder.

View File

@ -190,23 +190,27 @@ public List<String> getOptionValueList(GDPermissionHolder holder, Option option,
} }
public PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) { public PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) {
return PERMISSION_PROVIDER.setOptionValue(holder, permission, value, contexts); return PERMISSION_PROVIDER.setOptionValue(holder, permission, value, contexts, true);
}
public PermissionResult setOptionValue(GDPermissionHolder holder, String permission, String value, Set<Context> contexts, boolean check) {
return PERMISSION_PROVIDER.setOptionValue(holder, permission, value, contexts, check);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts) { public PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts) {
return PERMISSION_PROVIDER.setPermissionValue(holder, flag, value, contexts, true); return PERMISSION_PROVIDER.setPermissionValue(holder, flag, value, contexts, true, true);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts) { public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts) {
return PERMISSION_PROVIDER.setPermissionValue(holder, permission, value, contexts, true); return PERMISSION_PROVIDER.setPermissionValue(holder, permission, value, contexts, true, true);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts, boolean save) { public PermissionResult setPermissionValue(GDPermissionHolder holder, Flag flag, Tristate value, Set<Context> contexts, boolean check, boolean save) {
return PERMISSION_PROVIDER.setPermissionValue(holder, flag, value, contexts, save); return PERMISSION_PROVIDER.setPermissionValue(holder, flag, value, contexts, check, save);
} }
public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean save) { public PermissionResult setPermissionValue(GDPermissionHolder holder, String permission, Tristate value, Set<Context> contexts, boolean check, boolean save) {
return PERMISSION_PROVIDER.setPermissionValue(holder, permission, value, contexts, save); return PERMISSION_PROVIDER.setPermissionValue(holder, permission, value, contexts, check, save);
} }
public void setTransientOption(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) { public void setTransientOption(GDPermissionHolder holder, String permission, String value, Set<Context> contexts) {