mirror of
https://github.com/IntellectualSites/PlotSquared.git
synced 2024-11-21 20:56:45 +01:00
implement the command requirement logic
This commit is contained in:
parent
f175c24523
commit
f2da7cbdc9
@ -1,3 +1,21 @@
|
||||
/*
|
||||
* 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.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(
|
||||
|
@ -1,3 +1,21 @@
|
||||
/*
|
||||
* 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.captions.Caption;
|
||||
|
@ -1,3 +1,21 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,3 +1,21 @@
|
||||
/*
|
||||
* 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.arguments;
|
||||
|
||||
import cloud.commandframework.context.CommandContext;
|
||||
|
@ -1,3 +1,21 @@
|
||||
/*
|
||||
* 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.parsers;
|
||||
|
||||
import cloud.commandframework.annotations.parsers.Parser;
|
||||
|
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.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<List<CommandRequirement>> COMMAND_REQUIREMENTS_KEY = CommandMeta.Key.of(
|
||||
new TypeToken<List<CommandRequirement>>() {
|
||||
},
|
||||
"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);
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.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<PlotPlayer<?>>,
|
||||
Command.@NonNull Builder<PlotPlayer<?>>
|
||||
> {
|
||||
|
||||
@Override
|
||||
public Command.@NonNull Builder<PlotPlayer<?>> apply(
|
||||
final @NonNull Requirements requirements,
|
||||
final Command.@NonNull Builder<PlotPlayer<?>> builder
|
||||
) {
|
||||
// We use a list, because we want them to be evaluated in order.
|
||||
final Set<CommandRequirement> commandRequirements = EnumSet.noneOf(CommandRequirement.class);
|
||||
for (final Requirement requirement : requirements.value()) {
|
||||
this.addRequirements(commandRequirements, requirement.value());
|
||||
}
|
||||
// We then sort the requirements.
|
||||
final List<CommandRequirement> 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);
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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.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<PlotPlayer<?>> {
|
||||
|
||||
@Override
|
||||
public void accept(
|
||||
@NonNull final CommandPostprocessingContext<PlotPlayer<?>> context
|
||||
) {
|
||||
final Command<PlotPlayer<?>> command = context.getCommand();
|
||||
final PlotPlayer<?> player = context.getCommandContext().getSender();;
|
||||
final List<CommandRequirement> 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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +1,21 @@
|
||||
/*
|
||||
* 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.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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,3 +1,21 @@
|
||||
/*
|
||||
* 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.requirements;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
@ -288,7 +288,7 @@ public abstract class PlotPlayer<P> 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<Plot> lastPlotAccess =
|
||||
this.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
|
||||
if (lastPlotAccess.get().orElse(null) == null && !Settings.Enabled_Components.EVENTS) {
|
||||
|
Loading…
Reference in New Issue
Block a user