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

ExprCommandInfo Enhancements #5889

Merged
merged 6 commits into from
Oct 16, 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
55 changes: 42 additions & 13 deletions src/main/java/ch/njol/skript/expressions/ExprCommandInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@

import ch.njol.skript.command.ScriptCommand;
import ch.njol.skript.command.ScriptCommandEvent;
import ch.njol.skript.util.Utils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
Expand All @@ -55,7 +58,16 @@
"aliases of command \"bukkit:help\"",
"permission of command \"/op\"",
"command \"op\"'s permission message",
"command \"sk\"'s plugin owner"
"command \"sk\"'s plugin owner",
"",
"command /greet <player>:",
"\tusage: /greet <target>",
"\ttrigger:",
"\t\tif arg-1 is sender:",
"\t\t\tsend \"&cYou can't greet yourself! Usage: %the usage%\"",
"\t\t\tstop",
"\t\tsend \"%sender% greets you!\" to arg-1",
"\t\tsend \"You greeted %arg-1%!\""
})
@Since("2.6")
public class ExprCommandInfo extends SimpleExpression<String> {
Expand Down Expand Up @@ -111,26 +123,18 @@ private enum InfoType {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
commandName = (Expression<String>) exprs[0];
if (commandName == null && !getParser().isCurrentEvent(ScriptCommandEvent.class))
if (commandName == null && !getParser().isCurrentEvent(ScriptCommandEvent.class, PlayerCommandPreprocessEvent.class, ServerCommandEvent.class)) {
Skript.error("There's no command in " + Utils.a(getParser().getCurrentEventName()) + " event. Please provide a command");
return false;
UnderscoreTud marked this conversation as resolved.
Show resolved Hide resolved
}
type = InfoType.values()[Math.floorDiv(matchedPattern, 2)];
return true;
}

@Nullable
@Override
protected String[] get(Event event) {
Command[] commands;
if (commandName == null) {
if (!(event instanceof ScriptCommandEvent))
return new String[0];
commands = new Command[] {((ScriptCommandEvent) event).getScriptCommand().getBukkitCommand()};
} else {
CommandMap map = Commands.getCommandMap();
if (map == null)
return new String[0];
commands = commandName.stream(event).map(map::getCommand).filter(Objects::nonNull).toArray(Command[]::new);
}
Command[] commands = getCommands(event);
if (type == InfoType.ALIASES) {
ArrayList<String> result = new ArrayList<>();
for (Command command : commands)
Expand Down Expand Up @@ -159,6 +163,31 @@ public String toString(@Nullable Event event, boolean debug) {
(commandName == null ? "" : " of command " + commandName.toString(event, debug));
}

@Nullable
private Command[] getCommands(Event event) {
if (event instanceof ScriptCommandEvent && commandName == null)
return new Command[] {((ScriptCommandEvent) event).getScriptCommand().getBukkitCommand()};

CommandMap map = Commands.getCommandMap();
if (map == null)
return null;

if (commandName != null)
return commandName.stream(event).map(map::getCommand).filter(Objects::nonNull).toArray(Command[]::new);

String commandName;
if (event instanceof ServerCommandEvent) {
commandName = ((ServerCommandEvent) event).getCommand();
} else if (event instanceof PlayerCommandPreprocessEvent) {
commandName = ((PlayerCommandPreprocessEvent) event).getMessage().substring(1);
} else {
return null;
}
commandName = commandName.split(":")[0];
Command command = map.getCommand(commandName);
return command != null ? new Command[] {command} : null;
AyhamAl-Ali marked this conversation as resolved.
Show resolved Hide resolved
}

private static List<String> getAliases(Command command) {
if (!(command instanceof PluginCommand) || ((PluginCommand) command).getPlugin() != Skript.getInstance())
return command.getAliases();
Expand Down