add requirement inheritance

This commit is contained in:
Alexander Söderberg
2024-01-02 12:00:49 +01:00
parent 24d7fe626d
commit 1a277bbbeb
7 changed files with 125 additions and 45 deletions

View File

@ -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 cloud.commandframework.SenderMapper;
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;
/**
* 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();
}
}

View File

@ -27,6 +27,7 @@ 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.PlotSquaredCaptionProvider;
import com.plotsquared.core.commands.processing.CommandRequirementPostprocessor;
@ -66,35 +67,30 @@ public class CloudModule extends AbstractModule {
@Override
protected void configure() {
try {
final PaperCommandManager<PlotPlayer<?>> commandManager = new PaperCommandManager<PlotPlayer<?>>(
this.bukkitPlatform,
ExecutionCoordinator.asyncCoordinator(),
CloudModule::convert,
CloudModule::convert
);
commandManager.captionRegistry().registerProvider(new PlotSquaredCaptionProvider());
if (commandManager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) {
commandManager.registerAsynchronousCompletions();
}
if (commandManager.hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {
commandManager.registerBrigadier();
}
final CommandRequirementPostprocessor requirementPostprocessor = new CommandRequirementPostprocessor();
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);
} catch (final Exception e) {
LOGGER.error("Failed to configure command manager", e);
final PaperCommandManager<PlotPlayer<?>> commandManager = new PaperCommandManager<PlotPlayer<?>>(
this.bukkitPlatform,
ExecutionCoordinator.asyncCoordinator(),
new BukkitSenderMapper()
);
commandManager.captionRegistry().registerProvider(new PlotSquaredCaptionProvider());
if (commandManager.hasCapability(CloudBukkitCapabilities.ASYNCHRONOUS_COMPLETION)) {
commandManager.registerAsynchronousCompletions();
}
if (commandManager.hasCapability(CloudBukkitCapabilities.NATIVE_BRIGADIER)) {
commandManager.registerBrigadier();
}
final CommandRequirementPostprocessor requirementPostprocessor = new CommandRequirementPostprocessor();
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);
}
}