Skip to content

Commit cb960d3

Browse files
committed
Update for cloud-core access check changes
1 parent 460fdbd commit cb960d3

5 files changed

Lines changed: 60 additions & 35 deletions

File tree

cloud-brigadier/src/main/java/org/incendo/cloud/brigadier/permission/BrigadierPermissionPredicate.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import io.leangen.geantyref.GenericTypeReflector;
2727
import java.lang.reflect.Type;
2828
import java.util.Collections;
29-
import java.util.Set;
29+
import java.util.Map;
3030
import java.util.function.Predicate;
3131
import org.apiguardian.api.API;
3232
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -60,23 +60,15 @@ public BrigadierPermissionPredicate(
6060
}
6161

6262
@Override
63-
@SuppressWarnings("unchecked")
6463
public boolean test(final @NonNull S source) {
65-
final Permission permission = (Permission) this.node.nodeMeta().getOrDefault(
66-
CommandNode.META_KEY_PERMISSION,
67-
Permission.empty()
68-
);
69-
final Set<Type> senderTypes = (Set<Type>) this.node.nodeMeta().getOrDefault(
70-
CommandNode.META_KEY_SENDER_TYPES,
71-
Collections.emptySet()
72-
);
7364
final C cloudSender = this.senderMapper.map(source);
74-
if (senderTypes.isEmpty()) {
75-
return this.permissionChecker.hasPermission(cloudSender, permission);
76-
}
77-
for (final Type senderType : senderTypes) {
78-
if (GenericTypeReflector.isSuperType(senderType, cloudSender.getClass())) {
79-
return this.permissionChecker.hasPermission(cloudSender, permission);
65+
final Map<Type, Permission> accessMap =
66+
this.node.nodeMeta().getOrDefault(CommandNode.META_KEY_ACCESS, Collections.emptyMap());
67+
for (final Map.Entry<Type, Permission> entry : accessMap.entrySet()) {
68+
if (GenericTypeReflector.isSuperType(entry.getKey(), cloudSender.getClass())) {
69+
if (this.permissionChecker.hasPermission(cloudSender, entry.getValue())) {
70+
return true;
71+
}
8072
}
8173
}
8274
return false;

cloud-bukkit/src/main/java/org/incendo/cloud/bukkit/BukkitCommand.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
//
2424
package org.incendo.cloud.bukkit;
2525

26+
import io.leangen.geantyref.GenericTypeReflector;
27+
import java.lang.reflect.Type;
2628
import java.util.Collections;
2729
import java.util.List;
30+
import java.util.Map;
2831
import java.util.Objects;
2932
import java.util.Optional;
3033
import java.util.logging.Level;
@@ -155,11 +158,17 @@ public boolean testPermissionSilent(final @NonNull CommandSender target) {
155158
return false;
156159
}
157160

158-
final Permission permission = (Permission) node
159-
.nodeMeta()
160-
.getOrDefault(CommandNode.META_KEY_PERMISSION, Permission.empty());
161-
162-
return this.manager.testPermission(this.manager.senderMapper().map(target), permission).allowed();
161+
final Map<Type, Permission> accessMap =
162+
node.nodeMeta().getOrDefault(CommandNode.META_KEY_ACCESS, Collections.emptyMap());
163+
final C cloudSender = this.manager.senderMapper().map(target);
164+
for (final Map.Entry<Type, Permission> entry : accessMap.entrySet()) {
165+
if (GenericTypeReflector.isSuperType(entry.getKey(), cloudSender.getClass())) {
166+
if (this.manager.testPermission(cloudSender, entry.getValue()).allowed()) {
167+
return true;
168+
}
169+
}
170+
}
171+
return false;
163172
}
164173

165174
@API(status = API.Status.INTERNAL, since = "1.7.0")

cloud-bungee/src/main/java/org/incendo/cloud/bungee/BungeeCommand.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
//
2424
package org.incendo.cloud.bungee;
2525

26+
import io.leangen.geantyref.GenericTypeReflector;
27+
import java.lang.reflect.Type;
28+
import java.util.Collections;
29+
import java.util.Map;
2630
import java.util.Objects;
2731
import java.util.stream.Collectors;
2832
import net.md_5.bungee.api.CommandSender;
@@ -74,11 +78,17 @@ public boolean hasPermission(final CommandSender sender) {
7478
return false;
7579
}
7680

77-
final Permission permission = (Permission) node
78-
.nodeMeta()
79-
.getOrDefault(CommandNode.META_KEY_PERMISSION, Permission.empty());
80-
81-
return this.manager.testPermission(this.manager.senderMapper().map(sender), permission).allowed();
81+
final Map<Type, Permission> accessMap =
82+
node.nodeMeta().getOrDefault(CommandNode.META_KEY_ACCESS, Collections.emptyMap());
83+
final C cloudSender = this.manager.senderMapper().map(sender);
84+
for (final Map.Entry<Type, Permission> entry : accessMap.entrySet()) {
85+
if (GenericTypeReflector.isSuperType(entry.getKey(), cloudSender.getClass())) {
86+
if (this.manager.testPermission(cloudSender, entry.getValue()).allowed()) {
87+
return true;
88+
}
89+
}
90+
}
91+
return false;
8292
}
8393

8494
private @Nullable CommandNode<C> namedNode() {

cloud-minecraft-extras/src/main/java/org/incendo/cloud/minecraft/extras/MinecraftExceptionHandler.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@
3030
import java.util.Objects;
3131
import java.util.function.Consumer;
3232
import java.util.function.Function;
33+
import java.util.stream.Collectors;
3334
import net.kyori.adventure.audience.Audience;
3435
import net.kyori.adventure.text.Component;
3536
import net.kyori.adventure.text.ComponentLike;
37+
import net.kyori.adventure.text.JoinConfiguration;
3638
import net.kyori.adventure.text.event.ClickEvent;
3739
import net.kyori.adventure.text.event.HoverEvent;
3840
import net.kyori.adventure.text.format.NamedTextColor;
@@ -99,14 +101,26 @@ public static <C> MessageFactory<C, InvalidSyntaxException> createDefaultInvalid
99101
*/
100102
@API(status = API.Status.STABLE, since = "2.0.0")
101103
public static <C> MessageFactory<C, InvalidCommandSenderException> createDefaultInvalidSenderHandler() {
102-
return (formatter, ctx) -> text()
103-
.color(NamedTextColor.RED)
104-
.append(ctx.context().formatCaption(
105-
formatter,
106-
StandardCaptionKeys.EXCEPTION_INVALID_SENDER,
107-
RichVariable.of("actual", text(TypeUtils.simpleName(ctx.context().sender().getClass()), NamedTextColor.GRAY)),
108-
RichVariable.of("expected", text(TypeUtils.simpleName(ctx.exception().requiredSender()), NamedTextColor.GRAY))
109-
));
104+
return (formatter, ctx) -> {
105+
final boolean multiple = ctx.exception().requiredSenderTypes().size() > 1;
106+
final Component expected = multiple
107+
? Component.join(
108+
JoinConfiguration.commas(true),
109+
ctx.exception().requiredSenderTypes().stream()
110+
.map(TypeUtils::simpleName)
111+
.map(name -> text(name, NamedTextColor.GRAY))
112+
.collect(Collectors.toList())
113+
)
114+
: text(TypeUtils.simpleName(ctx.exception().requiredSenderTypes().iterator().next()), NamedTextColor.GRAY);
115+
return text()
116+
.color(NamedTextColor.RED)
117+
.append(ctx.context().formatCaption(
118+
formatter,
119+
multiple ? StandardCaptionKeys.EXCEPTION_INVALID_SENDER_LIST : StandardCaptionKeys.EXCEPTION_INVALID_SENDER,
120+
RichVariable.of("actual", text(TypeUtils.simpleName(ctx.context().sender().getClass()), NamedTextColor.GRAY)),
121+
RichVariable.of("expected", expected)
122+
));
123+
};
110124
}
111125

112126
/**

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ktlint = "0.50.0"
77
errorprone = "2.27.1"
88
run-task = "2.3.0"
99

10-
cloudCore = "2.0.0-beta.5"
10+
cloudCore = "2.0.0-SNAPSHOT"
1111

1212
immutables = "2.10.1"
1313

0 commit comments

Comments
 (0)