Skip to content
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

Add the ability to execute as a bungeecord command #5811

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 48 additions & 28 deletions src/main/java/ch/njol/skript/effects/EffCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -33,63 +34,82 @@
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.util.StringMode;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;

/**
* @author Peter Güttinger
*/
@Name("Command")
@Description("Executes a command. This can be useful to use other plugins in triggers.")
@Examples({"make player execute command \"/suicide\"",
"execute console command \"/say Hello everyone!\""})
@Since("1.0")
@Description({
"Executes a command. This can be useful to use other plugins in triggers.",
"If the command is a bungeecord side command, " +
"you can use the [bungeecord] option to execute command on the proxy."
})
@Examples({
"make player execute command \"/home\"",
"execute console command \"/say Hello everyone!\"",
"execute player bungeecord command \"/alert &6Testing Announcement!\""
})
@Since("1.0, INSERT VERSION (bungeecord command)")
public class EffCommand extends Effect {

public static final String MESSAGE_CHANNEL = "Message";

static {
Skript.registerEffect(EffCommand.class,
"[execute] [the] command %strings% [by %-commandsenders%]",
"[execute] [the] %commandsenders% command %strings%",
"(let|make) %commandsenders% execute [[the] command] %strings%");
"[execute] [the] [bungee:bungee[cord]] command %strings% [by %-commandsenders%]",
"[execute] [the] %commandsenders% [bungee:bungee[cord]] command %strings%",
"(let|make) %commandsenders% execute [[the] [bungee:bungee[cord]] command] %strings%");
}

@Nullable
private Expression<CommandSender> senders;
@SuppressWarnings("null")
private Expression<String> commands;

@SuppressWarnings({"unchecked", "null"})
private boolean bungeecord;

@Override
public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (matchedPattern == 0) {
commands = (Expression<String>) vars[0];
senders = (Expression<CommandSender>) vars[1];
commands = (Expression<String>) exprs[0];
senders = (Expression<CommandSender>) exprs[1];
} else {
senders = (Expression<CommandSender>) vars[0];
commands = (Expression<String>) vars[1];
senders = (Expression<CommandSender>) exprs[0];
commands = (Expression<String>) exprs[1];
}
bungeecord = parseResult.hasTag("bungee");
if (bungeecord && senders == null) {
Skript.error("The commandsenders expression cannot be omitted when using the bungeecord option");
return false;
}
commands = VariableString.setStringMode(commands, StringMode.COMMAND);
return true;
}

@Override
public void execute(final Event e) {
for (String command : commands.getArray(e)) {
public void execute(Event event) {
for (String command : commands.getArray(event)) {
assert command != null;
if (command.startsWith("/"))
command = "" + command.substring(1);
if (senders != null) {
for (final CommandSender sender : senders.getArray(e)) {
assert sender != null;
for (CommandSender sender : senders.getArray(event)) {
if (bungeecord) {
if (!(sender instanceof Player))
continue;
Player player = (Player) sender;
Utils.sendPluginMessage(player, EffConnect.BUNGEE_CHANNEL, MESSAGE_CHANNEL, player.getName(), "/" + command);
continue;
}
Skript.dispatchCommand(sender, command);
}
} else {
Skript.dispatchCommand(Bukkit.getConsoleSender(), command);
}
}
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return "make " + (senders != null ? senders.toString(e, debug) : "the console") + " execute the command " + commands.toString(e, debug);
public String toString(@Nullable Event event, boolean debug) {
return "make " + (senders != null ? senders.toString(event, debug) : "the console") + " execute the command " + commands.toString(event, debug);
TheLimeGlass marked this conversation as resolved.
Show resolved Hide resolved
}

}