Cleanup/command dispatching (#12713)

This commit is contained in:
Owen
2025-06-21 21:44:07 -04:00
committed by GitHub
parent 186e9e331b
commit 5edcf6ddf6
10 changed files with 97 additions and 124 deletions

View File

@ -1,5 +1,6 @@
package io.papermc.paper;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.world.damagesource.CombatEntry;
import io.papermc.paper.world.damagesource.FallLocationType;
import net.kyori.adventure.util.Services;
@ -11,6 +12,8 @@ import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.function.Predicate;
/**
* Static bridge to the server internals.
* <p>
@ -73,5 +76,15 @@ public interface InternalAPIBridge {
* @return combat entry
*/
CombatEntry createCombatEntry(DamageSource damageSource, float damage, @Nullable FallLocationType fallLocationType, float fallDistance);
/**
* Causes this predicate to be considered restricted.
* Applying this to a command node prevents this command from being executed from an
* unattended context, such as click events.
*
* @param predicate wrapped predicate
* @return wrapped predicate
*/
Predicate<CommandSourceStack> restricted(Predicate<CommandSourceStack> predicate);
}

View File

@ -5,6 +5,7 @@ import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.tree.LiteralCommandNode;
import io.papermc.paper.InternalAPIBridge;
import io.papermc.paper.plugin.bootstrap.BootstrapContext;
import io.papermc.paper.plugin.bootstrap.PluginBootstrap;
import io.papermc.paper.plugin.configuration.PluginMeta;
@ -13,6 +14,7 @@ import io.papermc.paper.plugin.lifecycle.event.registrar.Registrar;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.function.Predicate;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Unmodifiable;
import org.jspecify.annotations.NullMarked;
@ -85,6 +87,22 @@ public interface Commands extends Registrar {
return RequiredArgumentBuilder.argument(name, argumentType);
}
/**
* Creates a restricted {@link Predicate} that wraps the given predicate.
* <p>
* A restricted predicate prevents execution in unattended contexts, such as from
* chat click events. A warning is shown on the client before executing the command.
* <p>
* This is used by vanilla to prevent invocation of sensitive commands (like op) from
* players without their knowledge.
*
* @param predicate the original predicate to wrap
* @return a new predicate with restricted execution behavior
*/
static Predicate<CommandSourceStack> restricted(final Predicate<CommandSourceStack> predicate) {
return InternalAPIBridge.get().restricted(predicate);
}
/**
* Gets the underlying {@link CommandDispatcher}.
*