Skip to content

Commit

Permalink
use cloud-requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
Citymonstret committed Jan 2, 2024
1 parent ed16043 commit a3502f6
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.commands.BukkitSenderMapper;
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.core.commands.CommandRequirement;
import com.plotsquared.core.commands.PlotSquaredCaptionProvider;
import com.plotsquared.core.commands.processing.CommandRequirementPostprocessor;
import com.plotsquared.core.commands.PlotSquaredRequirementFailureHandler;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.player.PlotPlayer;
Expand All @@ -40,6 +41,7 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.requirement.RequirementPostprocessor;

public class CloudModule extends AbstractModule {

Expand Down Expand Up @@ -80,7 +82,8 @@ protected void configure() {
commandManager.registerAsynchronousCompletions();
}

final CommandRequirementPostprocessor requirementPostprocessor = new CommandRequirementPostprocessor();
final RequirementPostprocessor<PlotPlayer<?>, CommandRequirement> requirementPostprocessor =
RequirementPostprocessor.of(CommandRequirement.REQUIREMENTS_KEY, new PlotSquaredRequirementFailureHandler());
commandManager.registerCommandPostProcessor(requirementPostprocessor);

// TODO(City): Override parsing errors using MM parsing.
Expand Down
1 change: 1 addition & 0 deletions Core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
// Cloud
api(libs.cloud)
api(libs.cloudMinecraftExtras)
api(libs.cloudRequirements)

// Guice
api(libs.guice) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,39 @@
package com.plotsquared.core.commands;

import cloud.commandframework.context.CommandContext;
import cloud.commandframework.keys.CloudKey;
import com.plotsquared.core.configuration.caption.TranslatableCaption;
import com.plotsquared.core.permissions.Permission;
import com.plotsquared.core.player.PlotPlayer;
import io.leangen.geantyref.TypeToken;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.requirement.Requirement;
import org.incendo.cloud.requirement.Requirements;

import java.util.List;

/**
* Something that is required for a command to be executed.
*/
public interface CommandRequirement {
public interface CommandRequirement extends Requirement<PlotPlayer<?>, CommandRequirement> {

/**
* Returns the caption sent when the requirement is not met.
*
* @return the caption
* The key used to store the requirements in the {@link cloud.commandframework.meta.CommandMeta}.
*/
@NonNull TranslatableCaption failureCaption();
CloudKey<Requirements<PlotPlayer<?>, CommandRequirement>> REQUIREMENTS_KEY = CloudKey.of(
"requirements",
new TypeToken<Requirements<PlotPlayer<?>, CommandRequirement>>() {
}
);

/**
* Evaluates whether the requirement is met.
* Returns the caption sent when the requirement is not met.
*
* @param context command context to evaluate
* @return {@code true} if the requirement is met, else {@code false}
* @return the caption
*/
boolean evaluate(final @NonNull CommandContext<PlotPlayer<?>> context);
@NonNull TranslatableCaption failureCaption();

/**
* Returns the placeholder values.
Expand All @@ -57,15 +62,6 @@ public interface CommandRequirement {
return new TagResolver[0];
}

/**
* Returns the list of parent requirements that should be evaluated before this requirement.
*
* @return the requirements
*/
default @NonNull List<@NonNull CommandRequirement> parents() {
return List.of();
}

/**
* Returns a requirement that evaluates to {@code true} if the sender has the given {@code permission} or if
* this requirement evaluates to {@code true}.
Expand Down Expand Up @@ -94,8 +90,8 @@ public interface CommandRequirement {
}

@Override
public boolean evaluate(final @NonNull CommandContext<PlotPlayer<?>> context) {
return context.sender().hasPermission(permission) || thisRequirement.evaluate(context);
public boolean evaluateRequirement(final @NonNull CommandContext<PlotPlayer<?>> context) {
return context.sender().hasPermission(permission) || thisRequirement.evaluateRequirement(context);
}
};
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public enum CommonCommandRequirement implements CommandRequirement {
}

@Override
public boolean evaluate(final @NonNull CommandContext<PlotPlayer<?>> context) {
public boolean evaluateRequirement(final @NonNull CommandContext<PlotPlayer<?>> context) {
return this.predicate.test(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@
import com.plotsquared.core.command.CommandCategory;
import com.plotsquared.core.player.PlotPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.requirement.RequirementApplicable;
import org.incendo.cloud.requirement.Requirements;

import java.util.List;

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

private final RequirementApplicable.RequirementApplicableFactory<PlotPlayer<?>, CommandRequirement>
requirementApplicableFactory = RequirementApplicable.factory(CommandRequirement.REQUIREMENTS_KEY);

/**
* Returns the category of the command.
*
Expand Down Expand Up @@ -64,7 +69,7 @@ public abstract class PlotSquaredCommandBean extends CommandBean<PlotPlayer<?>>
@Override
protected final Command.@NonNull Builder<PlotPlayer<?>> configure(final Command.@NonNull Builder<PlotPlayer<?>> builder) {
return this.configurePlotCommand(this.prepare(builder.meta(PlotSquaredCommandMeta.META_CATEGORY, this.category())))
.meta(CommandRequirements.REQUIREMENTS_KEY, CommandRequirements.create(this.requirements()));
.apply(this.requirementApplicableFactory.create(Requirements.of(this.requirements())));
}

protected abstract Command.@NonNull Builder<PlotPlayer<?>> configurePlotCommand(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* PlotSquared, a land and world management plugin for Minecraft.
* Copyright (C) IntellectualSites <https://intellectualsites.com>
* Copyright (C) IntellectualSites team and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.plotsquared.core.commands;

import cloud.commandframework.context.CommandContext;
import com.plotsquared.core.player.PlotPlayer;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.requirement.RequirementFailureHandler;

public final class PlotSquaredRequirementFailureHandler implements RequirementFailureHandler<PlotPlayer<?>, CommandRequirement> {

@Override
public void handleFailure(
final @NonNull CommandContext<PlotPlayer<?>> context,
final @NonNull CommandRequirement requirement
) {
context.sender().sendMessage(requirement.failureCaption(), requirement.tagResolvers());
}
}

This file was deleted.

2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ informative-annotations = "1.4"
vault = "1.7.1"
serverlib = "2.3.4"
cloud = "2.0.0-SNAPSHOT"
cloudRequirements = "1.0.0-SNAPSHOT"

# Gradle plugins
shadow = "8.1.1"
Expand Down Expand Up @@ -81,6 +82,7 @@ serverlib = { group = "dev.notmyfault.serverlib", name = "ServerLib", version.re
cloud = { group = "cloud.commandframework", name = "cloud-core", version.ref = "cloud" }
cloudPaper = { group = "cloud.commandframework", name = "cloud-paper", version.ref = "cloud" }
cloudMinecraftExtras = { group = "cloud.commandframework", name = "cloud-minecraft-extras", version.ref = "cloud" }
cloudRequirements = { group = "org.incendo", name = "cloud-requirements", version.ref = "cloudRequirements" }

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

0 comments on commit a3502f6

Please sign in to comment.