Skip to content

Commit 773d78c

Browse files
Add additional command event values
1 parent fba8623 commit 773d78c

9 files changed

Lines changed: 190 additions & 46 deletions

File tree

src/main/java/ch/njol/skript/expressions/ExprCommandSender.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/main/java/org/skriptlang/skript/bukkit/command/CommandModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ protected void loadSelf(SkriptAddon addon) {
3434
ExprArgument::register,
3535
ExprCmdCooldownInfo::register,
3636
ExprCommand::register,
37+
ExprCommandExecutor::register,
3738
ExprCommandInfo::register,
39+
ExprCommandSender::register,
3840
syntaxRegistry -> StructCommand.register(addon, syntaxRegistry)
3941
);
4042
}

src/main/java/org/skriptlang/skript/bukkit/command/custom/ScriptCommandEvent.java

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package org.skriptlang.skript.bukkit.command.custom;
22

3+
import io.papermc.paper.command.brigadier.CommandSourceStack;
4+
import org.bukkit.Location;
35
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Entity;
47
import org.bukkit.event.Event;
58
import org.bukkit.event.HandlerList;
69
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
711

12+
import java.util.Collections;
813
import java.util.HashMap;
914
import java.util.Map;
1015

@@ -15,34 +20,78 @@ public class ScriptCommandEvent extends Event {
1520

1621
private final String label;
1722
private final String rawInput;
23+
private final ScriptCommandExecutor commandExecutor;
1824
private final CommandSender sender;
19-
private final ScriptCommandExecutor executor;
25+
private final @Nullable Entity executor;
26+
private final Location location;
2027

2128
final Map<String, Object> arguments = new HashMap<>();
2229

23-
public ScriptCommandEvent(String label, String rawInput, CommandSender sender, ScriptCommandExecutor executor) {
30+
public ScriptCommandEvent(String label, String rawInput, ScriptCommandExecutor commandExecutor, CommandSourceStack source) {
2431
this.label = label;
2532
this.rawInput = rawInput;
26-
this.sender = sender;
27-
this.executor = executor;
33+
this.commandExecutor = commandExecutor;
34+
this.sender = source.getSender();
35+
this.executor = source.getExecutor();
36+
this.location = source.getLocation();
2837
}
2938

39+
/**
40+
* @return The label of the executed command.
41+
*/
3042
public String getLabel() {
3143
return label;
3244
}
3345

46+
/**
47+
* @return The full raw input being executed.
48+
*/
3449
public String getRawInput() {
3550
return rawInput;
3651
}
3752

53+
/**
54+
* @return The sender is the thing that initiated/triggered the execution of the command.
55+
* It differs to {@link #getExecutor()} in that the executor can be changed by a command, e.g. {@code /execute}.
56+
*/
3857
public CommandSender getSender() {
3958
return sender;
4059
}
4160

42-
public ScriptCommandExecutor getExecutor() {
61+
/**
62+
* @return The entity that executes the command.
63+
* May not always be {@link #getSender()} as the executor of a command can be changed to a different entity than the one that triggered the command.
64+
*/
65+
public @Nullable Entity getExecutor() {
4366
return executor;
4467
}
4568

69+
/**
70+
* @return The location that the command is being executed at.
71+
*/
72+
public Location getLocation() {
73+
return location;
74+
}
75+
76+
/**
77+
* @return The command executor being used to execute this command (perform logic).
78+
*/
79+
public ScriptCommandExecutor getCommandExecutor() {
80+
return commandExecutor;
81+
}
82+
83+
/**
84+
* @return A map of all available arguments and their values.
85+
*/
86+
public Map<String, Object> getArguments() {
87+
return Collections.unmodifiableMap(arguments);
88+
}
89+
90+
/**
91+
* Obtains an argument by its name.
92+
* @param name The name of the argument.
93+
* @return The value of the argument.
94+
*/
4695
public Object getArgument(String name) {
4796
return arguments.get(name);
4897
}

src/main/java/org/skriptlang/skript/bukkit/command/custom/ScriptCommandExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ public ScriptCommandExecutor(Trigger trigger, List<ArgumentData<?>> arguments, @
5252
}
5353

5454
public int execute(CommandContext<CommandSourceStack> context) throws CommandSyntaxException {
55-
CommandSender sender = context.getSource().getSender();
55+
CommandSourceStack source = context.getSource();
5656
ScriptCommandEvent commandEvent =
57-
new ScriptCommandEvent(context.getNodes().getFirst().getNode().getName(), context.getInput(), sender, this);
57+
new ScriptCommandEvent(context.getNodes().getFirst().getNode().getName(), context.getInput(), this, source);
5858

5959
// final validations
60-
if (cooldownManager != null && !cooldownManager.checkExecution(commandEvent, sender)) {
60+
if (cooldownManager != null && !cooldownManager.checkExecution(commandEvent, source.getSender())) {
6161
return Command.SINGLE_SUCCESS;
6262
}
6363

src/main/java/org/skriptlang/skript/bukkit/command/elements/effects/EffCancelCooldown.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public Class<? extends Event>[] supportedEvents() {
6464
@Override
6565
protected void execute(Event event) {
6666
if (event instanceof ScriptCommandEvent commandEvent) {
67-
assert commandEvent.getExecutor().getCooldownManager() != null;
68-
commandEvent.getExecutor().getCooldownManager()
67+
assert commandEvent.getCommandExecutor().getCooldownManager() != null;
68+
commandEvent.getCommandExecutor().getCooldownManager()
6969
.setStartDate(event, commandEvent.getSender(), cancel ? null : Date.now());
7070
}
7171
}

src/main/java/org/skriptlang/skript/bukkit/command/elements/expressions/ExprCmdCooldownInfo.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ protected Object[] get(Event event) {
7979
return (Object[]) Array.newInstance(getReturnType(), 0);
8080
}
8181

82-
CooldownManager cooldownManager = commandEvent.getExecutor().getCooldownManager();
82+
CooldownManager cooldownManager = commandEvent.getCommandExecutor().getCooldownManager();
8383
assert cooldownManager != null;
8484

8585
return switch (type) {
@@ -130,7 +130,7 @@ public void change(Event event, Object @Nullable [] delta, ChangeMode mode) {
130130
return;
131131
}
132132

133-
CooldownManager cooldownManager = commandEvent.getExecutor().getCooldownManager();
133+
CooldownManager cooldownManager = commandEvent.getCommandExecutor().getCooldownManager();
134134
assert cooldownManager != null;
135135

136136
Date startDate = cooldownManager.getStartDate(event, commandEvent.getSender());
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.skriptlang.skript.bukkit.command.elements.expressions;
2+
3+
import ch.njol.skript.doc.Description;
4+
import ch.njol.skript.doc.Example;
5+
import ch.njol.skript.doc.Name;
6+
import ch.njol.skript.doc.Since;
7+
import ch.njol.skript.lang.EventRestrictedSyntax;
8+
import ch.njol.skript.lang.Expression;
9+
import ch.njol.skript.lang.SkriptParser.ParseResult;
10+
import ch.njol.skript.lang.util.SimpleExpression;
11+
import ch.njol.util.Kleenean;
12+
import org.bukkit.entity.Entity;
13+
import org.bukkit.event.Event;
14+
import org.jetbrains.annotations.Nullable;
15+
import org.skriptlang.skript.bukkit.command.custom.ScriptCommandEvent;
16+
import org.skriptlang.skript.registration.SyntaxInfo;
17+
import org.skriptlang.skript.registration.SyntaxRegistry;
18+
19+
@Name("Command Executor")
20+
@Description("""
21+
The executor of a command.
22+
This differs from the command sender in that the executor may not be the thing that triggered/initiated the command.
23+
The executor of a command is often changed to be different from the command sender by using a vanilla command such as "/execute".
24+
""")
25+
@Example("""
26+
command /balance:
27+
# This works if you do "/execute as <player> run balance"
28+
# It will send the output to the command sender, but it will be as if "<player>" was the thing executing it.
29+
send "Your balance is %{balance::%uuid of the executor%}%" to the sender
30+
""")
31+
@Since("INSERT VERSION")
32+
public class ExprCommandExecutor extends SimpleExpression<Entity> implements EventRestrictedSyntax {
33+
34+
public static void register(SyntaxRegistry syntaxRegistry) {
35+
syntaxRegistry.register(SyntaxRegistry.EXPRESSION,
36+
SyntaxInfo.Expression.simple(ExprCommandExecutor.class, ExprCommandExecutor::new, Entity.class,
37+
"[the] [command['s]] executor"));
38+
}
39+
40+
@Override
41+
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
42+
return true;
43+
}
44+
45+
@Override
46+
public Class<? extends Event>[] supportedEvents() {
47+
//noinspection unchecked
48+
return new Class[]{ScriptCommandEvent.class};
49+
}
50+
51+
@Override
52+
protected Entity[] get(Event event) {
53+
Entity executor = null;
54+
if (event instanceof ScriptCommandEvent scriptCommandEvent) {
55+
executor = scriptCommandEvent.getExecutor();
56+
}
57+
return executor == null ? new Entity[0] : new Entity[]{executor};
58+
}
59+
60+
@Override
61+
public boolean isSingle() {
62+
return true;
63+
}
64+
65+
@Override
66+
public Class<? extends Entity> getReturnType() {
67+
return Entity.class;
68+
}
69+
70+
@Override
71+
public String toString(@Nullable Event event, boolean debug) {
72+
return "the command executor";
73+
}
74+
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.skriptlang.skript.bukkit.command.elements.expressions;
2+
3+
import org.bukkit.command.CommandSender;
4+
5+
import ch.njol.skript.doc.Description;
6+
import ch.njol.skript.doc.Events;
7+
import ch.njol.skript.doc.Example;
8+
import ch.njol.skript.doc.Name;
9+
import ch.njol.skript.doc.Since;
10+
import ch.njol.skript.expressions.base.EventValueExpression;
11+
import org.skriptlang.skript.registration.SyntaxRegistry;
12+
13+
@Name("Command Sender")
14+
@Description("""
15+
The sender of a command. \
16+
This differs from the command executor in that this is always the thing that originally triggered/initiated the command. \
17+
It cannot be changed by commands like "/execute". \
18+
""")
19+
@Example("make the command sender execute \"/say hi!\"")
20+
@Example("""
21+
on command:
22+
log "%sender% used command /%command% %arguments%" to "commands.log"
23+
""")
24+
@Since("2.0")
25+
@Events("command")
26+
public class ExprCommandSender extends EventValueExpression<CommandSender> {
27+
28+
public static void register(SyntaxRegistry syntaxRegistry) {
29+
syntaxRegistry.register(SyntaxRegistry.EXPRESSION,
30+
infoBuilder(ExprCommandSender.class, CommandSender.class, "[command['s]] sender")
31+
.supplier(ExprCommandSender::new)
32+
.build());
33+
}
34+
35+
public ExprCommandSender() {
36+
super(CommandSender.class);
37+
}
38+
39+
}

src/main/java/org/skriptlang/skript/bukkit/command/elements/structures/StructCommand.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
import ch.njol.skript.config.SectionNode;
55
import ch.njol.skript.lang.Literal;
66
import ch.njol.skript.lang.SkriptParser.ParseResult;
7+
import ch.njol.skript.registrations.Classes;
78
import com.mojang.brigadier.tree.LiteralCommandNode;
89
import io.papermc.paper.command.brigadier.CommandSourceStack;
10+
import org.bukkit.Location;
11+
import org.bukkit.World;
12+
import org.bukkit.block.Block;
13+
import org.bukkit.command.BlockCommandSender;
914
import org.bukkit.command.CommandSender;
1015
import org.bukkit.event.Event;
1116
import org.jetbrains.annotations.Nullable;
@@ -33,6 +38,14 @@ public static void register(SkriptAddon addon, SyntaxRegistry syntaxRegistry) {
3338

3439
EventValueRegistry evRegistry = addon.registry(EventValueRegistry.class);
3540
evRegistry.register(EventValue.simple(ScriptCommandEvent.class, CommandSender.class, ScriptCommandEvent::getSender));
41+
evRegistry.register(EventValue.simple(ScriptCommandEvent.class, String[].class,
42+
event -> event.getArguments().values().stream()
43+
.map(Classes::toString)
44+
.toArray(String[]::new)));
45+
evRegistry.register(EventValue.simple(ScriptCommandEvent.class, Block.class,
46+
event -> event.getSender() instanceof BlockCommandSender sender ? sender.getBlock() : null));
47+
evRegistry.register(EventValue.simple(ScriptCommandEvent.class, Location.class, ScriptCommandEvent::getLocation));
48+
evRegistry.register(EventValue.simple(ScriptCommandEvent.class, World.class, event -> event.getLocation().getWorld()));
3649
}
3750

3851
private static final SubCommandEntryData ROOT_ENTRY_DATA =

0 commit comments

Comments
 (0)