Skip to content

Commit

Permalink
Add the ability to execute as a bungeecord command (SkriptLang#5811)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass authored and NotSoDelayed committed Oct 30, 2023
1 parent c023726 commit 5447879
Showing 1 changed file with 48 additions and 28 deletions.
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 " + (bungeecord ? "bungeecord " : "") + "command " + commands.toString(event, debug);
}

}

0 comments on commit 5447879

Please sign in to comment.