Skip to content

Commit

Permalink
minor :: added 'runas' command
Browse files Browse the repository at this point in the history
  • Loading branch information
avolgha committed Aug 21, 2024
1 parent dc49deb commit 4c7982c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void bootstrap(@NotNull BootstrapContext context) {
registry.register(new MaintenanceCommand().node(), "Toggle maintenance mode");
registry.register(new PlayerGuiCommand().node(), "Toggles PlayerGUI mode");
registry.register(new QuickGuiCommand().node(), "Quickly open any Minecraft-native GUI");
registry.register(new RunAsCommand().node(), "Run commands as another player");
registry.register(new StaffChatCommand().node(), "Sends messages to a staff only chat");
registry.register(new TeleportHereCommand().node(), "Teleports a player to you");
registry.register(new TeleportToCommand().node(), "Teleports you to a player or to a coordinate");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.marius.map.spigot.commands;

import com.mojang.brigadier.tree.LiteralCommandNode;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
import io.papermc.paper.command.brigadier.argument.SignedMessageResolver;
import io.papermc.paper.command.brigadier.argument.resolvers.selector.PlayerSelectorArgumentResolver;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.JoinConfiguration;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.entity.Player;

import java.util.List;
import java.util.Objects;

@SuppressWarnings("UnstableApiUsage")
public class RunAsCommand extends BaseCommand {
@Override
public LiteralCommandNode<CommandSourceStack> node() {
return Commands.literal("runas")
.requires(source -> source.getExecutor() instanceof Player player && player.isOp())
.then(Commands.argument("target", ArgumentTypes.player())
.then(Commands.argument("command", ArgumentTypes.signedMessage())
.executes(context -> {
PlayerSelectorArgumentResolver targetResolver = context.getArgument("target", PlayerSelectorArgumentResolver.class);
SignedMessageResolver commandResolver = context.getArgument("command", SignedMessageResolver.class);

String command = commandResolver.content();
List<Player> targets = targetResolver.resolve(context.getSource());

if (targets.size() > 1) {
this.message(Objects.requireNonNull(context.getSource().getExecutor()), "Found too many target players.");
return 2;
}

boolean result = targets.getFirst().performCommand(command);
this.message(Objects.requireNonNull(context.getSource().getExecutor()), Component.join(
JoinConfiguration.spaces(),
Component.text("Command was performed")
.color(NamedTextColor.GRAY),
Component.text((result ? "" : "un") + "successfully")
.color(result ? NamedTextColor.GREEN : NamedTextColor.RED)
.append(Component.text("!")
.color(NamedTextColor.GRAY))
));

return SUCCESS;
})))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public LiteralCommandNode<CommandSourceStack> node() {
PlayerSelectorArgumentResolver resolver = context.getArgument("target", PlayerSelectorArgumentResolver.class);
List<Player> targets = resolver.resolve(context.getSource());
if (targets.size() > 1) {
this.message(Objects.requireNonNull(context.getSource().getExecutor()), "Found two many target players.");
this.message(Objects.requireNonNull(context.getSource().getExecutor()), "Found too many target players.");
return 2;
}
Objects.requireNonNull((Player) context.getSource().getExecutor()).teleport(targets.getFirst(), PlayerTeleportEvent.TeleportCause.PLUGIN);
Expand Down

0 comments on commit 4c7982c

Please sign in to comment.