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,10 +1,12 @@
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 io.papermc.paper.world.damagesource.PaperCombatEntryWrapper;
import io.papermc.paper.world.damagesource.PaperCombatTrackerWrapper;
import net.minecraft.Optionull;
import net.minecraft.commands.PermissionSource;
import net.minecraft.world.damagesource.FallLocation;
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.block.CraftBiome;
@@ -16,6 +18,7 @@ import org.bukkit.damage.DamageSource;
import org.bukkit.entity.LivingEntity;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.function.Predicate;
@NullMarked
public class PaperServerInternalAPIBridge implements InternalAPIBridge {
@@ -71,4 +74,16 @@ public class PaperServerInternalAPIBridge implements InternalAPIBridge {
damageSource, damage, fallLocation, fallDistance
));
}
@Override
public Predicate<CommandSourceStack> restricted(final Predicate<CommandSourceStack> predicate) {
record RestrictedPredicate(Predicate<CommandSourceStack> predicate) implements Predicate<CommandSourceStack>, PermissionSource.RestrictedMarker {
@Override
public boolean test(final CommandSourceStack commandSourceStack) {
return this.predicate.test(commandSourceStack);
}
}
return new RestrictedPredicate(predicate);
}
}

View File

@@ -20,6 +20,7 @@ import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
import org.bukkit.command.Command;
import org.bukkit.craftbukkit.command.VanillaCommandWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -96,7 +97,7 @@ public class BukkitBrigForwardingMap extends HashMap<String, Command> {
public Command put(String key, Command value) {
Command old = this.get(key);
this.getDispatcher().getRoot().removeCommand(key); // Override previous command
if (value instanceof PluginVanillaCommandWrapper wrapper && wrapper.getName().equals(key)) {
if (value instanceof VanillaCommandWrapper wrapper && wrapper.getName().equals(key)) {
// Don't break when some plugin tries to remove and add back a plugin command registered with modern API...
this.getDispatcher().getRoot().addChild((CommandNode) wrapper.vanillaCommand);
} else {

View File

@@ -302,7 +302,6 @@ public final class CraftServer implements Server {
public CraftDataPackManager dataPackManager;
private final CraftServerTickManager serverTickManager;
private final CraftServerLinks serverLinks;
public boolean playerCommandState;
private boolean printSaveWarning;
private CraftIconCache icon;
private boolean overrideAllCommandBlockCommands = false;
@@ -973,41 +972,13 @@ public final class CraftServer implements Server {
return this.playerList;
}
// NOTE: Should only be called from DedicatedServer.ah()
public boolean dispatchServerCommand(CommandSender sender, ConsoleInput serverCommand) {
if (sender instanceof Conversable) {
Conversable conversable = (Conversable) sender;
if (conversable.isConversing()) {
conversable.acceptConversationInput(serverCommand.msg);
return true;
}
}
try {
this.playerCommandState = true;
return this.dispatchCommand(sender, serverCommand.msg);
} catch (Exception ex) {
this.getLogger().log(Level.WARNING, "Unexpected exception while parsing console command \"" + serverCommand.msg + '"', ex);
return false;
} finally {
this.playerCommandState = false;
}
}
@Override
public boolean dispatchCommand(CommandSender sender, String commandLine) {
Preconditions.checkArgument(sender != null, "sender cannot be null");
public boolean dispatchCommand(CommandSender rawSender, String commandLine) {
Preconditions.checkArgument(rawSender != null, "sender cannot be null");
Preconditions.checkArgument(commandLine != null, "commandLine cannot be null");
org.spigotmc.AsyncCatcher.catchOp("Command Dispatched Async: " + commandLine); // Spigot // Paper - Include command in error message
CommandSourceStack sourceStack = VanillaCommandWrapper.getListener(rawSender);
if (this.commandMap.dispatch(sender, commandLine)) {
return true;
}
return this.dispatchCommand(VanillaCommandWrapper.getListener(sender), commandLine);
}
public boolean dispatchCommand(CommandSourceStack sourceStack, String commandLine) {
net.minecraft.commands.Commands commands = this.getHandle().getServer().getCommands();
com.mojang.brigadier.CommandDispatcher<CommandSourceStack> dispatcher = commands.getDispatcher();
com.mojang.brigadier.ParseResults<CommandSourceStack> results = dispatcher.parse(commandLine, sourceStack);
@@ -1017,7 +988,12 @@ public final class CraftServer implements Server {
Command target = this.commandMap.getCommand(args[0].toLowerCase(java.util.Locale.ENGLISH));
try {
commands.performCommand(results, commandLine, commandLine, true);
if (results.getContext().getNodes().isEmpty()) {
return false;
}
Commands.validateParseResults(results);
commands.performCommand(results, commandLine, true);
return true;
} catch (CommandException ex) {
new com.destroystokyo.paper.event.server.ServerExceptionEvent(new com.destroystokyo.paper.exception.ServerCommandException(ex, target, sender, args)).callEvent(); // Paper
throw ex;
@@ -1026,9 +1002,6 @@ public final class CraftServer implements Server {
new com.destroystokyo.paper.event.server.ServerExceptionEvent(new com.destroystokyo.paper.exception.ServerCommandException(ex, target, sender, args)).callEvent(); // Paper
throw new CommandException(msg, ex);
}
// Paper end
return false;
}
@Override
@@ -2608,7 +2581,7 @@ public final class CraftServer implements Server {
}
public void checkSaveState() {
if (this.playerCommandState || this.printSaveWarning || this.console.autosavePeriod <= 0) {
if (this.printSaveWarning || this.console.autosavePeriod <= 0) {
return;
}
this.printSaveWarning = true;

View File

@@ -50,7 +50,7 @@ public class VanillaCommandWrapper extends BukkitCommand { // Paper
if (!this.testPermission(sender)) return true;
CommandSourceStack source = VanillaCommandWrapper.getListener(sender);
this.commands().performPrefixedCommand(source, this.toDispatcher(args, this.getName()), this.toDispatcher(args, commandLabel)); // Paper
this.commands().performPrefixedCommand(source, this.toDispatcher(args, this.getName()));
return true;
}