From f2da7cbdc989f1f0c789c0f364aa8aab13639108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Sat, 4 Mar 2023 12:57:14 +0100 Subject: [PATCH] implement the command requirement logic --- .../plotsquared/core/commands/CommandAdd.java | 24 ++++- .../core/commands/PlotSquaredCaptionKeys.java | 18 ++++ .../commands/PlotSquaredCommandContainer.java | 18 ++++ .../commands/PlotSquaredCommandManager.java | 5 ++ .../core/commands/arguments/PlotMember.java | 18 ++++ .../commands/parsers/PlotMemberParser.java | 18 ++++ .../requirements/CommandRequirement.java | 88 +++++++++++++++++++ .../CommandRequirementBuilderModifier.java | 61 +++++++++++++ .../CommandRequirementPostProcessor.java | 54 ++++++++++++ .../commands/requirements/Requirement.java | 20 ++++- .../requirements/RequirementType.java | 36 -------- .../commands/requirements/Requirements.java | 18 ++++ .../plotsquared/core/player/PlotPlayer.java | 2 +- 13 files changed, 339 insertions(+), 41 deletions(-) create mode 100644 Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirement.java create mode 100644 Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirementBuilderModifier.java create mode 100644 Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirementPostProcessor.java delete mode 100644 Core/src/main/java/com/plotsquared/core/commands/requirements/RequirementType.java diff --git a/Core/src/main/java/com/plotsquared/core/commands/CommandAdd.java b/Core/src/main/java/com/plotsquared/core/commands/CommandAdd.java index 2f1723783..1aa519fc2 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/CommandAdd.java +++ b/Core/src/main/java/com/plotsquared/core/commands/CommandAdd.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ package com.plotsquared.core.commands; import cloud.commandframework.annotations.Argument; @@ -6,7 +24,7 @@ import cloud.commandframework.annotations.CommandPermission; import com.google.inject.Inject; import com.plotsquared.core.commands.arguments.PlotMember; import com.plotsquared.core.commands.requirements.Requirement; -import com.plotsquared.core.commands.requirements.RequirementType; +import com.plotsquared.core.commands.requirements.CommandRequirement; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.caption.TranslatableCaption; import com.plotsquared.core.permissions.Permission; @@ -28,8 +46,8 @@ class CommandAdd implements PlotSquaredCommandContainer { this.eventDispatcher = eventDispatcher; } - @Requirement(RequirementType.PLAYER) - @Requirement(RequirementType.IS_OWNER) + @Requirement(CommandRequirement.PLAYER) + @Requirement(CommandRequirement.IS_OWNER) @CommandPermission("plots.add") @CommandMethod("plot add [target]") public void commandAdd( diff --git a/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCaptionKeys.java b/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCaptionKeys.java index 57d732676..efa0ba40d 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCaptionKeys.java +++ b/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCaptionKeys.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ package com.plotsquared.core.commands; import cloud.commandframework.captions.Caption; diff --git a/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandContainer.java b/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandContainer.java index 8476e5b6a..bc80bd7a5 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandContainer.java +++ b/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandContainer.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ package com.plotsquared.core.commands; /** diff --git a/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.java b/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.java index 39218761c..79fd070f0 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.java +++ b/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.java @@ -24,6 +24,9 @@ import cloud.commandframework.meta.SimpleCommandMeta; import com.google.inject.Inject; import com.google.inject.Injector; import com.plotsquared.core.commands.parsers.PlotMemberParser; +import com.plotsquared.core.commands.requirements.CommandRequirementBuilderModifier; +import com.plotsquared.core.commands.requirements.CommandRequirementPostProcessor; +import com.plotsquared.core.commands.requirements.Requirements; import com.plotsquared.core.player.PlotPlayer; import io.leangen.geantyref.TypeToken; import org.checkerframework.checker.nullness.qual.NonNull; @@ -49,6 +52,8 @@ public class PlotSquaredCommandManager { }, parameters -> SimpleCommandMeta.empty() ); + this.annotationParser.registerBuilderModifier(Requirements.class, new CommandRequirementBuilderModifier()); + this.commandManager.registerCommandPostProcessor(new CommandRequirementPostProcessor()); } /** diff --git a/Core/src/main/java/com/plotsquared/core/commands/arguments/PlotMember.java b/Core/src/main/java/com/plotsquared/core/commands/arguments/PlotMember.java index 0545b4a6c..52f57fa76 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/arguments/PlotMember.java +++ b/Core/src/main/java/com/plotsquared/core/commands/arguments/PlotMember.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ package com.plotsquared.core.commands.arguments; import cloud.commandframework.context.CommandContext; diff --git a/Core/src/main/java/com/plotsquared/core/commands/parsers/PlotMemberParser.java b/Core/src/main/java/com/plotsquared/core/commands/parsers/PlotMemberParser.java index 5554aa139..f29fe3e4a 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/parsers/PlotMemberParser.java +++ b/Core/src/main/java/com/plotsquared/core/commands/parsers/PlotMemberParser.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ package com.plotsquared.core.commands.parsers; import cloud.commandframework.annotations.parsers.Parser; diff --git a/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirement.java b/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirement.java new file mode 100644 index 000000000..5d2fd3b8c --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirement.java @@ -0,0 +1,88 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ +package com.plotsquared.core.commands.requirements; + +import cloud.commandframework.meta.CommandMeta; +import com.plotsquared.core.configuration.caption.Caption; +import com.plotsquared.core.configuration.caption.TranslatableCaption; +import com.plotsquared.core.player.ConsolePlayer; +import com.plotsquared.core.player.PlotPlayer; +import io.leangen.geantyref.TypeToken; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.Arrays; +import java.util.Collections; +import java.util.EnumSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.function.Predicate; + +public enum CommandRequirement { + PLAYER( + "", + player -> !(player instanceof ConsolePlayer) + ), + IN_PLOT( + "errors.not_in_plot", + player -> player.getCurrentPlot() != null + ), + PLOT_HAS_OWNER( + "info.plot_unowned", + player -> Objects.requireNonNull(player.getCurrentPlot()).hasOwner(), + IN_PLOT + ), + IS_OWNER( + "permission.no_plot_perms", + player -> Objects.requireNonNull(player.getCurrentPlot()).isOwner(player.getUUID()), + PLOT_HAS_OWNER + ); + + public static final CommandMeta.Key> COMMAND_REQUIREMENTS_KEY = CommandMeta.Key.of( + new TypeToken>() { + }, + "command_requirements" + ); + + private final @NonNull Caption caption; + private final @NonNull Predicate<@NonNull PlotPlayer> requirementPredicate; + private final @NonNull Set<@NonNull CommandRequirement> inheritedRequirements; + + CommandRequirement( + final @NonNull String caption, + final @NonNull Predicate<@NonNull PlotPlayer> requirementPredicate, + final @NonNull CommandRequirement... inheritedRequirements + ) { + this.caption = TranslatableCaption.of(caption); + this.requirementPredicate = requirementPredicate; + this.inheritedRequirements = EnumSet.copyOf(Arrays.asList(inheritedRequirements)); + } + + public @NonNull Set<@NonNull CommandRequirement> inheritedRequirements() { + return Collections.unmodifiableSet(this.inheritedRequirements); + } + + public @NonNull Caption caption() { + return this.caption; + } + + public boolean checkRequirement(final @NonNull PlotPlayer player) { + return this.requirementPredicate.test(player); + } +} diff --git a/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirementBuilderModifier.java b/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirementBuilderModifier.java new file mode 100644 index 000000000..b6dbd9db1 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirementBuilderModifier.java @@ -0,0 +1,61 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ +package com.plotsquared.core.commands.requirements; + +import cloud.commandframework.Command; +import com.plotsquared.core.player.PlotPlayer; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.EnumSet; +import java.util.List; +import java.util.Set; +import java.util.function.BiFunction; + +public class CommandRequirementBuilderModifier implements BiFunction< + @NonNull Requirements, + Command.@NonNull Builder>, + Command.@NonNull Builder> +> { + + @Override + public Command.@NonNull Builder> apply( + final @NonNull Requirements requirements, + final Command.@NonNull Builder> builder + ) { + // We use a list, because we want them to be evaluated in order. + final Set commandRequirements = EnumSet.noneOf(CommandRequirement.class); + for (final Requirement requirement : requirements.value()) { + this.addRequirements(commandRequirements, requirement.value()); + } + // We then sort the requirements. + final List sortedRequirements = commandRequirements.stream().sorted().toList(); + // We then register all the types in the command metadata. + return builder.meta(CommandRequirement.COMMAND_REQUIREMENTS_KEY, sortedRequirements); + } + + private void addRequirements( + final @NonNull Set<@NonNull CommandRequirement> requirements, + final @NonNull CommandRequirement requirement + ) { + for (final CommandRequirement inheritedRequirement : requirement.inheritedRequirements()) { + addRequirements(requirements, inheritedRequirement); + } + requirements.add(requirement); + } +} diff --git a/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirementPostProcessor.java b/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirementPostProcessor.java new file mode 100644 index 000000000..4a2d7e6e5 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/commands/requirements/CommandRequirementPostProcessor.java @@ -0,0 +1,54 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ +package com.plotsquared.core.commands.requirements; + +import cloud.commandframework.Command; +import cloud.commandframework.execution.postprocessor.CommandPostprocessingContext; +import cloud.commandframework.execution.postprocessor.CommandPostprocessor; +import cloud.commandframework.services.types.ConsumerService; +import com.plotsquared.core.player.PlotPlayer; +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.List; + +public class CommandRequirementPostProcessor implements CommandPostprocessor> { + + @Override + public void accept( + @NonNull final CommandPostprocessingContext> context + ) { + final Command> command = context.getCommand(); + final PlotPlayer player = context.getCommandContext().getSender();; + final List commandRequirements = command.getCommandMeta().get( + CommandRequirement.COMMAND_REQUIREMENTS_KEY + ).orElse(List.of()); + + for (final CommandRequirement requirement : commandRequirements) { + if (requirement.checkRequirement(player)) { + continue; + } + + // They failed the requirement =( + player.sendMessage(requirement.caption()); + + // Then we interrupt to make sure the command isn't executed. + ConsumerService.interrupt(); + } + } +} diff --git a/Core/src/main/java/com/plotsquared/core/commands/requirements/Requirement.java b/Core/src/main/java/com/plotsquared/core/commands/requirements/Requirement.java index adb38d8b9..330c3bf85 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/requirements/Requirement.java +++ b/Core/src/main/java/com/plotsquared/core/commands/requirements/Requirement.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ package com.plotsquared.core.commands.requirements; import org.checkerframework.checker.nullness.qual.NonNull; @@ -13,5 +31,5 @@ import java.lang.annotation.Target; @Target(ElementType.METHOD) public @interface Requirement { - @NonNull RequirementType value(); + @NonNull CommandRequirement value(); } diff --git a/Core/src/main/java/com/plotsquared/core/commands/requirements/RequirementType.java b/Core/src/main/java/com/plotsquared/core/commands/requirements/RequirementType.java deleted file mode 100644 index 0b4123735..000000000 --- a/Core/src/main/java/com/plotsquared/core/commands/requirements/RequirementType.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.plotsquared.core.commands.requirements; - -import com.plotsquared.core.configuration.caption.Caption; -import com.plotsquared.core.configuration.caption.TranslatableCaption; -import org.checkerframework.checker.nullness.qual.NonNull; - -import java.util.Arrays; -import java.util.Collections; -import java.util.EnumSet; -import java.util.Set; - -public enum RequirementType { - PLAYER(""), - IN_PLOT("errors.not_in_plot"), - PLOT_HAS_OWNER("info.plot_unowned", IN_PLOT), - IS_OWNER("permission.no_plot_perms", PLOT_HAS_OWNER); - - private final Caption caption; - private @NonNull Set<@NonNull RequirementType> inheritedRequirements; - - RequirementType( - final String caption, - final @NonNull RequirementType... inheritedRequirements - ) { - this.caption = TranslatableCaption.of(caption); - this.inheritedRequirements = EnumSet.copyOf(Arrays.asList(inheritedRequirements)); - } - - public @NonNull Set<@NonNull RequirementType> inheritedRequirements() { - return Collections.unmodifiableSet(this.inheritedRequirements); - } - - public @NonNull Caption caption() { - return this.caption; - } -} diff --git a/Core/src/main/java/com/plotsquared/core/commands/requirements/Requirements.java b/Core/src/main/java/com/plotsquared/core/commands/requirements/Requirements.java index 6a8d1fb95..00d6e8542 100644 --- a/Core/src/main/java/com/plotsquared/core/commands/requirements/Requirements.java +++ b/Core/src/main/java/com/plotsquared/core/commands/requirements/Requirements.java @@ -1,3 +1,21 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * 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 . + */ package com.plotsquared.core.commands.requirements; import org.checkerframework.checker.nullness.qual.NonNull; diff --git a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java index 6bb9e1bad..53de62ced 100644 --- a/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java +++ b/Core/src/main/java/com/plotsquared/core/player/PlotPlayer.java @@ -288,7 +288,7 @@ public abstract class PlotPlayer

implements CommandCaller, OfflinePlotPlayer, * * @return the plot the player is standing on or null if standing on a road or not in a {@link PlotArea} */ - public Plot getCurrentPlot() { + public @Nullable Plot getCurrentPlot() { try (final MetaDataAccess lastPlotAccess = this.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) { if (lastPlotAccess.get().orElse(null) == null && !Settings.Enabled_Components.EVENTS) {