-
-
Notifications
You must be signed in to change notification settings - Fork 772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: use cloud v2 for commands #4283
Draft
Citymonstret
wants to merge
13
commits into
main
Choose a base branch
from
feature/cloud-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6088dd8
feature: use cloud v2 for commands
Citymonstret 2ac2f66
port over `flag remove`
Citymonstret b62a237
port over remaining flag commands
Citymonstret 24d7fe6
improve the requirement system
Citymonstret 1a277bb
add requirement inheritance
Citymonstret 3a1b373
don't use async suggestions with brigadier
Citymonstret 92a00ef
use cloud-requirements
Citymonstret 780c65e
migrate to cloud-processors-requirements
Citymonstret 2a0ad92
use cloud snapshot
Citymonstret a659832
update cloud
SirYwell e0c7c58
fix remaining change
SirYwell 628c520
fix remaining change (2)
SirYwell c8e5cf1
fix remaining change (3)
SirYwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
Bukkit/src/main/java/com/plotsquared/bukkit/commands/BukkitSenderMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.bukkit.commands; | ||
|
||
import com.plotsquared.bukkit.util.BukkitUtil; | ||
import com.plotsquared.core.player.ConsolePlayer; | ||
import com.plotsquared.core.player.PlotPlayer; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.incendo.cloud.SenderMapper; | ||
|
||
/** | ||
* Mapper between {@link CommandSender} and {@link PlotPlayer}. | ||
*/ | ||
public final class BukkitSenderMapper implements SenderMapper<CommandSender, PlotPlayer<?>> { | ||
|
||
@Override | ||
public @NonNull PlotPlayer<?> map(final @NonNull CommandSender base) { | ||
if (base instanceof Player player) { | ||
return BukkitUtil.adapt(player); | ||
} | ||
return ConsolePlayer.getConsole(); | ||
} | ||
|
||
@Override | ||
public @NonNull CommandSender reverse(final @NonNull PlotPlayer<?> mapped) { | ||
if (mapped instanceof ConsolePlayer) { | ||
return Bukkit.getConsoleSender(); | ||
} | ||
return (Player) mapped.getPlatformPlayer(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
Bukkit/src/main/java/com/plotsquared/bukkit/inject/CloudModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* 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.bukkit.inject; | ||
|
||
import com.google.inject.AbstractModule; | ||
import com.google.inject.Key; | ||
import com.google.inject.TypeLiteral; | ||
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.PlotSquaredRequirementFailureHandler; | ||
import com.plotsquared.core.configuration.caption.TranslatableCaption; | ||
import com.plotsquared.core.player.ConsolePlayer; | ||
import com.plotsquared.core.player.PlotPlayer; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
import org.incendo.cloud.CommandManager; | ||
import org.incendo.cloud.bukkit.CloudBukkitCapabilities; | ||
import org.incendo.cloud.execution.ExecutionCoordinator; | ||
import org.incendo.cloud.minecraft.extras.MinecraftExceptionHandler; | ||
import org.incendo.cloud.paper.PaperCommandManager; | ||
import org.incendo.cloud.processors.requirements.RequirementPostprocessor; | ||
|
||
public class CloudModule extends AbstractModule { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + CloudModule.class.getSimpleName()); | ||
|
||
private static @NonNull CommandSender convert(final @NonNull PlotPlayer<?> player) { | ||
if (player instanceof ConsolePlayer) { | ||
return Bukkit.getConsoleSender(); | ||
} | ||
return (Player) player.getPlatformPlayer(); | ||
} | ||
|
||
private static @NonNull PlotPlayer<?> convert (final @NonNull CommandSender sender) { | ||
if (sender instanceof Player player) { | ||
return BukkitUtil.adapt(player); | ||
} | ||
return ConsolePlayer.getConsole(); | ||
} | ||
|
||
private final BukkitPlatform bukkitPlatform; | ||
|
||
public CloudModule(final @NonNull BukkitPlatform bukkitPlatform) { | ||
this.bukkitPlatform = bukkitPlatform; | ||
} | ||
|
||
@Override | ||
protected void configure() { | ||
final PaperCommandManager<PlotPlayer<?>> commandManager = new PaperCommandManager<PlotPlayer<?>>( | ||
this.bukkitPlatform, | ||
ExecutionCoordinator.asyncCoordinator(), | ||
new BukkitSenderMapper() | ||
); | ||
commandManager.captionRegistry().registerProvider(new PlotSquaredCaptionProvider()); | ||
|
||
if (commandManager.hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) { | ||
commandManager.registerBrigadier(); | ||
} else if (commandManager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) { | ||
commandManager.registerAsynchronousCompletions(); | ||
} | ||
|
||
final RequirementPostprocessor<PlotPlayer<?>, CommandRequirement> requirementPostprocessor = | ||
RequirementPostprocessor.of(CommandRequirement.REQUIREMENTS_KEY, new PlotSquaredRequirementFailureHandler()); | ||
commandManager.registerCommandPostProcessor(requirementPostprocessor); | ||
|
||
// TODO(City): Override parsing errors using MM parsing. | ||
MinecraftExceptionHandler.<PlotPlayer<?>>create(PlotPlayer::getAudience) | ||
.defaultHandlers() | ||
.decorator((ctx, component) -> TranslatableCaption.of("core.prefix"). | ||
toComponent(ctx.context().sender()) | ||
.append(component)) | ||
.registerTo(commandManager); | ||
|
||
bind(Key.get(new TypeLiteral<CommandManager<PlotPlayer<?>>>() {})).toInstance(commandManager); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
Core/src/main/java/com/plotsquared/core/commands/CommandRequirement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* 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 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.context.CommandContext; | ||
import org.incendo.cloud.key.CloudKey; | ||
import org.incendo.cloud.processors.requirements.Requirement; | ||
import org.incendo.cloud.processors.requirements.Requirements; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Something that is required for a command to be executed. | ||
*/ | ||
public interface CommandRequirement extends Requirement<PlotPlayer<?>, CommandRequirement> { | ||
|
||
/** | ||
* The key used to store the requirements in the {@link org.incendo.cloud.meta.CommandMeta}. | ||
*/ | ||
CloudKey<Requirements<PlotPlayer<?>, CommandRequirement>> REQUIREMENTS_KEY = CloudKey.of( | ||
"requirements", | ||
new TypeToken<Requirements<PlotPlayer<?>, CommandRequirement>>() { | ||
} | ||
); | ||
|
||
/** | ||
* Returns the caption sent when the requirement is not met. | ||
* | ||
* @return the caption | ||
*/ | ||
@NonNull TranslatableCaption failureCaption(); | ||
|
||
/** | ||
* Returns the placeholder values. | ||
* | ||
* @return placeholder values | ||
*/ | ||
default @NonNull TagResolver @NonNull[] tagResolvers() { | ||
return new TagResolver[0]; | ||
} | ||
|
||
/** | ||
* Returns a requirement that evaluates to {@code true} if the sender has the given {@code permission} or if | ||
* this requirement evaluates to {@code true}. | ||
* | ||
* @param permission the override permission | ||
* @return the new requirement | ||
*/ | ||
default @NonNull CommandRequirement withPermissionOverride(final @NonNull Permission permission) { | ||
final CommandRequirement thisRequirement = this; | ||
return new CommandRequirement() { | ||
@Override | ||
public @NonNull TranslatableCaption failureCaption() { | ||
return TranslatableCaption.of("permission.no_permission"); | ||
} | ||
|
||
@Override | ||
public @NonNull TagResolver @NonNull [] tagResolvers() { | ||
return new TagResolver[] { | ||
TagResolver.resolver("node", Tag.inserting(Permission.PERMISSION_SET_FLAG_OTHER)) | ||
}; | ||
} | ||
|
||
@Override | ||
public @NonNull List<@NonNull CommandRequirement> parents() { | ||
return thisRequirement.parents(); | ||
} | ||
|
||
@Override | ||
public boolean evaluateRequirement(final @NonNull CommandContext<PlotPlayer<?>> context) { | ||
return context.sender().hasPermission(permission) || thisRequirement.evaluateRequirement(context); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove these now that we have the sender mapper