Calculate Sponge defaults using context

This commit is contained in:
Luck 2016-09-29 20:38:03 +01:00
parent becdfd6f03
commit 71b33a296c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 74 additions and 70 deletions

View File

@ -22,16 +22,20 @@
package me.lucko.luckperms.api.sponge; package me.lucko.luckperms.api.sponge;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.api.Tristate;
import me.lucko.luckperms.calculators.PermissionCalculator; import me.lucko.luckperms.calculators.PermissionCalculator;
import me.lucko.luckperms.calculators.PermissionProcessor; import me.lucko.luckperms.calculators.PermissionProcessor;
import org.spongepowered.api.service.context.Context;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
public class ContextData { public class ContextData {
private final LuckPermsUserSubject parent; private final LuckPermsUserSubject parent;
@ -48,16 +52,17 @@ public class ContextData {
this.parent = parent; this.parent = parent;
this.context = context; this.context = context;
Set<Context> contexts = context.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet());
List<PermissionProcessor> processors = new ArrayList<>(5); List<PermissionProcessor> processors = new ArrayList<>(5);
processors.add(new PermissionCalculator.MapProcessor(permissionCache)); processors.add(new PermissionCalculator.MapProcessor(permissionCache));
if (service.getPlugin().getConfiguration().isApplyingWildcards()) { if (service.getPlugin().getConfiguration().isApplyingWildcards()) {
processors.add(new LuckPermsUserSubject.SpongeWildcardProcessor(permissionCache)); processors.add(new SpongeWildcardProcessor(permissionCache));
processors.add(new PermissionCalculator.WildcardProcessor(permissionCache)); processors.add(new PermissionCalculator.WildcardProcessor(permissionCache));
} }
if (service.getPlugin().getConfiguration().isApplyingRegex()) { if (service.getPlugin().getConfiguration().isApplyingRegex()) {
processors.add(new PermissionCalculator.RegexProcessor(permissionCache)); processors.add(new PermissionCalculator.RegexProcessor(permissionCache));
} }
processors.add(new LuckPermsUserSubject.SpongeDefaultsProcessor(service)); processors.add(new SpongeDefaultsProcessor(service, contexts));
calculator = new PermissionCalculator(service.getPlugin(), parent.getUser().getName(), service.getPlugin().getConfiguration().isDebugPermissionChecks(), processors); calculator = new PermissionCalculator(service.getPlugin(), parent.getUser().getName(), service.getPlugin().getConfiguration().isDebugPermissionChecks(), processors);
} }
@ -75,4 +80,71 @@ public class ContextData {
} }
} }
@AllArgsConstructor
private static class SpongeWildcardProcessor implements PermissionProcessor {
@Getter
private final Map<String, Boolean> map;
@Override
public me.lucko.luckperms.api.Tristate hasPermission(String permission) {
String node = permission;
while (node.contains(".")) {
int endIndex = node.lastIndexOf('.');
if (endIndex == -1) {
break;
}
node = node.substring(0, endIndex);
if (!isEmpty(node)) {
if (map.containsKey(node)) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get(node));
}
}
}
if (map.containsKey("'*'")) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("'*'"));
}
if (map.containsKey("*")) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("*"));
}
return me.lucko.luckperms.api.Tristate.UNDEFINED;
}
private static boolean isEmpty(String s) {
if (s.equals("")) {
return true;
}
char[] chars = s.toCharArray();
for (char c : chars) {
if (c != '.') {
return false;
}
}
return true;
}
}
@AllArgsConstructor
private static class SpongeDefaultsProcessor implements PermissionProcessor {
private final LuckPermsService service;
private final Set<Context> contexts;
@Override
public me.lucko.luckperms.api.Tristate hasPermission(String permission) {
org.spongepowered.api.util.Tristate t = service.getDefaults().getPermissionValue(contexts, permission);
if (t != org.spongepowered.api.util.Tristate.UNDEFINED) {
return me.lucko.luckperms.api.Tristate.fromBoolean(t.asBoolean());
} else {
return me.lucko.luckperms.api.Tristate.UNDEFINED;
}
}
}
} }

View File

@ -22,12 +22,10 @@
package me.lucko.luckperms.api.sponge; package me.lucko.luckperms.api.sponge;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent; import me.lucko.luckperms.api.event.events.UserPermissionRefreshEvent;
import me.lucko.luckperms.api.implementation.internal.UserLink; import me.lucko.luckperms.api.implementation.internal.UserLink;
import me.lucko.luckperms.calculators.PermissionProcessor;
import me.lucko.luckperms.contexts.Contexts; import me.lucko.luckperms.contexts.Contexts;
import me.lucko.luckperms.users.User; import me.lucko.luckperms.users.User;
import org.spongepowered.api.Sponge; import org.spongepowered.api.Sponge;
@ -156,70 +154,4 @@ public class LuckPermsUserSubject extends LuckPermsSubject {
service.getPlugin().getContextManager().giveApplicableContext(player.get(), context); service.getPlugin().getContextManager().giveApplicableContext(player.get(), context);
return context.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet()); return context.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet());
} }
@AllArgsConstructor
static class SpongeWildcardProcessor implements PermissionProcessor {
@Getter
private final Map<String, Boolean> map;
@Override
public me.lucko.luckperms.api.Tristate hasPermission(String permission) {
String node = permission;
while (node.contains(".")) {
int endIndex = node.lastIndexOf('.');
if (endIndex == -1) {
break;
}
node = node.substring(0, endIndex);
if (!isEmpty(node)) {
if (map.containsKey(node)) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get(node));
}
}
}
if (map.containsKey("'*'")) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("'*'"));
}
if (map.containsKey("*")) {
return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("*"));
}
return me.lucko.luckperms.api.Tristate.UNDEFINED;
}
private static boolean isEmpty(String s) {
if (s.equals("")) {
return true;
}
char[] chars = s.toCharArray();
for (char c : chars) {
if (c != '.') {
return false;
}
}
return true;
}
}
@AllArgsConstructor
static class SpongeDefaultsProcessor implements PermissionProcessor {
private final LuckPermsService service;
@Override
public me.lucko.luckperms.api.Tristate hasPermission(String permission) {
Tristate t = service.getDefaults().getPermissionValue(Collections.emptySet(), permission);
if (t != Tristate.UNDEFINED) {
return me.lucko.luckperms.api.Tristate.fromBoolean(t.asBoolean());
} else {
return me.lucko.luckperms.api.Tristate.UNDEFINED;
}
}
}
} }