Skip to content

Commit a3502f6

Browse files
committed
use cloud-requirements
1 parent ed16043 commit a3502f6

File tree

9 files changed

+66
-148
lines changed

9 files changed

+66
-148
lines changed

Bukkit/src/main/java/com/plotsquared/bukkit/inject/CloudModule.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
import com.plotsquared.bukkit.BukkitPlatform;
3030
import com.plotsquared.bukkit.commands.BukkitSenderMapper;
3131
import com.plotsquared.bukkit.util.BukkitUtil;
32+
import com.plotsquared.core.commands.CommandRequirement;
3233
import com.plotsquared.core.commands.PlotSquaredCaptionProvider;
33-
import com.plotsquared.core.commands.processing.CommandRequirementPostprocessor;
34+
import com.plotsquared.core.commands.PlotSquaredRequirementFailureHandler;
3435
import com.plotsquared.core.configuration.caption.TranslatableCaption;
3536
import com.plotsquared.core.player.ConsolePlayer;
3637
import com.plotsquared.core.player.PlotPlayer;
@@ -40,6 +41,7 @@
4041
import org.bukkit.command.CommandSender;
4142
import org.bukkit.entity.Player;
4243
import org.checkerframework.checker.nullness.qual.NonNull;
44+
import org.incendo.cloud.requirement.RequirementPostprocessor;
4345

4446
public class CloudModule extends AbstractModule {
4547

@@ -80,7 +82,8 @@ protected void configure() {
8082
commandManager.registerAsynchronousCompletions();
8183
}
8284

83-
final CommandRequirementPostprocessor requirementPostprocessor = new CommandRequirementPostprocessor();
85+
final RequirementPostprocessor<PlotPlayer<?>, CommandRequirement> requirementPostprocessor =
86+
RequirementPostprocessor.of(CommandRequirement.REQUIREMENTS_KEY, new PlotSquaredRequirementFailureHandler());
8487
commandManager.registerCommandPostProcessor(requirementPostprocessor);
8588

8689
// TODO(City): Override parsing errors using MM parsing.

Core/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies {
1818
// Cloud
1919
api(libs.cloud)
2020
api(libs.cloudMinecraftExtras)
21+
api(libs.cloudRequirements)
2122

2223
// Guice
2324
api(libs.guice) {

Core/src/main/java/com/plotsquared/core/commands/CommandRequirement.java

+16-20
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,39 @@
1919
package com.plotsquared.core.commands;
2020

2121
import cloud.commandframework.context.CommandContext;
22+
import cloud.commandframework.keys.CloudKey;
2223
import com.plotsquared.core.configuration.caption.TranslatableCaption;
2324
import com.plotsquared.core.permissions.Permission;
2425
import com.plotsquared.core.player.PlotPlayer;
26+
import io.leangen.geantyref.TypeToken;
2527
import net.kyori.adventure.text.minimessage.tag.Tag;
2628
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
2729
import org.checkerframework.checker.nullness.qual.NonNull;
30+
import org.incendo.cloud.requirement.Requirement;
31+
import org.incendo.cloud.requirement.Requirements;
2832

2933
import java.util.List;
3034

3135
/**
3236
* Something that is required for a command to be executed.
3337
*/
34-
public interface CommandRequirement {
38+
public interface CommandRequirement extends Requirement<PlotPlayer<?>, CommandRequirement> {
3539

3640
/**
37-
* Returns the caption sent when the requirement is not met.
38-
*
39-
* @return the caption
41+
* The key used to store the requirements in the {@link cloud.commandframework.meta.CommandMeta}.
4042
*/
41-
@NonNull TranslatableCaption failureCaption();
43+
CloudKey<Requirements<PlotPlayer<?>, CommandRequirement>> REQUIREMENTS_KEY = CloudKey.of(
44+
"requirements",
45+
new TypeToken<Requirements<PlotPlayer<?>, CommandRequirement>>() {
46+
}
47+
);
4248

4349
/**
44-
* Evaluates whether the requirement is met.
50+
* Returns the caption sent when the requirement is not met.
4551
*
46-
* @param context command context to evaluate
47-
* @return {@code true} if the requirement is met, else {@code false}
52+
* @return the caption
4853
*/
49-
boolean evaluate(final @NonNull CommandContext<PlotPlayer<?>> context);
54+
@NonNull TranslatableCaption failureCaption();
5055

5156
/**
5257
* Returns the placeholder values.
@@ -57,15 +62,6 @@ public interface CommandRequirement {
5762
return new TagResolver[0];
5863
}
5964

60-
/**
61-
* Returns the list of parent requirements that should be evaluated before this requirement.
62-
*
63-
* @return the requirements
64-
*/
65-
default @NonNull List<@NonNull CommandRequirement> parents() {
66-
return List.of();
67-
}
68-
6965
/**
7066
* Returns a requirement that evaluates to {@code true} if the sender has the given {@code permission} or if
7167
* this requirement evaluates to {@code true}.
@@ -94,8 +90,8 @@ public interface CommandRequirement {
9490
}
9591

9692
@Override
97-
public boolean evaluate(final @NonNull CommandContext<PlotPlayer<?>> context) {
98-
return context.sender().hasPermission(permission) || thisRequirement.evaluate(context);
93+
public boolean evaluateRequirement(final @NonNull CommandContext<PlotPlayer<?>> context) {
94+
return context.sender().hasPermission(permission) || thisRequirement.evaluateRequirement(context);
9995
}
10096
};
10197
}

Core/src/main/java/com/plotsquared/core/commands/CommandRequirements.java

-72
This file was deleted.

Core/src/main/java/com/plotsquared/core/commands/CommonCommandRequirement.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public enum CommonCommandRequirement implements CommandRequirement {
7575
}
7676

7777
@Override
78-
public boolean evaluate(final @NonNull CommandContext<PlotPlayer<?>> context) {
78+
public boolean evaluateRequirement(final @NonNull CommandContext<PlotPlayer<?>> context) {
7979
return this.predicate.test(context);
8080
}
8181
}

Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandBean.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
import com.plotsquared.core.command.CommandCategory;
2525
import com.plotsquared.core.player.PlotPlayer;
2626
import org.checkerframework.checker.nullness.qual.NonNull;
27+
import org.incendo.cloud.requirement.RequirementApplicable;
28+
import org.incendo.cloud.requirement.Requirements;
2729

2830
import java.util.List;
2931

3032
public abstract class PlotSquaredCommandBean extends CommandBean<PlotPlayer<?>> {
3133

34+
private final RequirementApplicable.RequirementApplicableFactory<PlotPlayer<?>, CommandRequirement>
35+
requirementApplicableFactory = RequirementApplicable.factory(CommandRequirement.REQUIREMENTS_KEY);
36+
3237
/**
3338
* Returns the category of the command.
3439
*
@@ -64,7 +69,7 @@ public abstract class PlotSquaredCommandBean extends CommandBean<PlotPlayer<?>>
6469
@Override
6570
protected final Command.@NonNull Builder<PlotPlayer<?>> configure(final Command.@NonNull Builder<PlotPlayer<?>> builder) {
6671
return this.configurePlotCommand(this.prepare(builder.meta(PlotSquaredCommandMeta.META_CATEGORY, this.category())))
67-
.meta(CommandRequirements.REQUIREMENTS_KEY, CommandRequirements.create(this.requirements()));
72+
.apply(this.requirementApplicableFactory.create(Requirements.of(this.requirements())));
6873
}
6974

7075
protected abstract Command.@NonNull Builder<PlotPlayer<?>> configurePlotCommand(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* PlotSquared, a land and world management plugin for Minecraft.
3+
* Copyright (C) IntellectualSites <https://intellectualsites.com>
4+
* Copyright (C) IntellectualSites team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package com.plotsquared.core.commands;
20+
21+
import cloud.commandframework.context.CommandContext;
22+
import com.plotsquared.core.player.PlotPlayer;
23+
import org.checkerframework.checker.nullness.qual.NonNull;
24+
import org.incendo.cloud.requirement.RequirementFailureHandler;
25+
26+
public final class PlotSquaredRequirementFailureHandler implements RequirementFailureHandler<PlotPlayer<?>, CommandRequirement> {
27+
28+
@Override
29+
public void handleFailure(
30+
final @NonNull CommandContext<PlotPlayer<?>> context,
31+
final @NonNull CommandRequirement requirement
32+
) {
33+
context.sender().sendMessage(requirement.failureCaption(), requirement.tagResolvers());
34+
}
35+
}

Core/src/main/java/com/plotsquared/core/commands/processing/CommandRequirementPostprocessor.java

-52
This file was deleted.

gradle/libs.versions.toml

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ informative-annotations = "1.4"
3232
vault = "1.7.1"
3333
serverlib = "2.3.4"
3434
cloud = "2.0.0-SNAPSHOT"
35+
cloudRequirements = "1.0.0-SNAPSHOT"
3536

3637
# Gradle plugins
3738
shadow = "8.1.1"
@@ -81,6 +82,7 @@ serverlib = { group = "dev.notmyfault.serverlib", name = "ServerLib", version.re
8182
cloud = { group = "cloud.commandframework", name = "cloud-core", version.ref = "cloud" }
8283
cloudPaper = { group = "cloud.commandframework", name = "cloud-paper", version.ref = "cloud" }
8384
cloudMinecraftExtras = { group = "cloud.commandframework", name = "cloud-minecraft-extras", version.ref = "cloud" }
85+
cloudRequirements = { group = "org.incendo", name = "cloud-requirements", version.ref = "cloudRequirements" }
8486

8587
[plugins]
8688
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "shadow" }

0 commit comments

Comments
 (0)