-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
EffCommand.java
115 lines (105 loc) · 3.95 KB
/
EffCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.effects;
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;
import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
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;
@Name("Command")
@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, 2.8.0 (bungeecord command)")
public class EffCommand extends Effect {
public static final String MESSAGE_CHANNEL = "Message";
static {
Skript.registerEffect(EffCommand.class,
"[execute] [the] [bungee:bungee[cord]] command[s] %strings% [by %-commandsenders%]",
"[execute] [the] %commandsenders% [bungee:bungee[cord]] command[s] %strings%",
"(let|make) %commandsenders% execute [[the] [bungee:bungee[cord]] command[s]] %strings%");
}
@Nullable
private Expression<CommandSender> senders;
private Expression<String> commands;
private boolean bungeecord;
@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
if (matchedPattern == 0) {
commands = (Expression<String>) exprs[0];
senders = (Expression<CommandSender>) exprs[1];
} else {
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(Event event) {
for (String command : commands.getArray(event)) {
assert command != null;
if (command.startsWith("/"))
command = "" + command.substring(1);
if (senders != 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(@Nullable Event event, boolean debug) {
return "make " + (senders != null ? senders.toString(event, debug) : "the console") + " execute " + (bungeecord ? "bungeecord " : "") + "command " + commands.toString(event, debug);
}
}