|
| 1 | +package com.shanebeestudios.skbee.elements.other.effects; |
| 2 | + |
| 3 | +import ch.njol.skript.Skript; |
| 4 | +import ch.njol.skript.doc.Description; |
| 5 | +import ch.njol.skript.doc.Examples; |
| 6 | +import ch.njol.skript.doc.Name; |
| 7 | +import ch.njol.skript.doc.Since; |
| 8 | +import ch.njol.skript.lang.Expression; |
| 9 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 10 | +import ch.njol.skript.lang.SyntaxStringBuilder; |
| 11 | +import ch.njol.util.Kleenean; |
| 12 | +import com.shanebeestudios.skbee.SkBee; |
| 13 | +import com.shanebeestudios.skbee.api.skript.base.Effect; |
| 14 | +import org.bukkit.Bukkit; |
| 15 | +import org.bukkit.command.CommandSender; |
| 16 | +import org.bukkit.event.Event; |
| 17 | +import org.jetbrains.annotations.Nullable; |
| 18 | + |
| 19 | +@Name("Dispatch Command") |
| 20 | +@Description({"Similar to Skript's command effect, with the option to use any command sender", |
| 21 | + "(vs. being restricted to only player/console), and the option to temporarily attach a permission.", |
| 22 | + "The attached permission will only last for 1 tick, and automatically remove. This is not persistent."}) |
| 23 | +@Examples({"dispatch player command \"give %player% stick\" with permission \"minecraft.command.give\"", |
| 24 | + "dispatch (random element of all mobs) command \"/tell %player% hi\""}) |
| 25 | +@Since("INSERT VERSION") |
| 26 | +public class EffDispatchCommand extends Effect { |
| 27 | + |
| 28 | + private static final SkBee PLUGIN = SkBee.getPlugin(); |
| 29 | + |
| 30 | + static { |
| 31 | + Skript.registerEffect(EffDispatchCommand.class, |
| 32 | + "dispatch %commandsender% command %string% [with permission %-string%]"); |
| 33 | + } |
| 34 | + |
| 35 | + private Expression<CommandSender> commandSender; |
| 36 | + private Expression<String> command; |
| 37 | + private Expression<String> permission; |
| 38 | + |
| 39 | + @SuppressWarnings("unchecked") |
| 40 | + @Override |
| 41 | + public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
| 42 | + this.commandSender = (Expression<CommandSender>) exprs[0]; |
| 43 | + this.command = (Expression<String>) exprs[1]; |
| 44 | + this.permission = (Expression<String>) exprs[2]; |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected void execute(Event event) { |
| 50 | + CommandSender commandSender = this.commandSender.getSingle(event); |
| 51 | + String command = this.command.getSingle(event); |
| 52 | + if (commandSender == null || command == null) return; |
| 53 | + |
| 54 | + if (this.permission != null) { |
| 55 | + String permission = this.permission.getSingle(event); |
| 56 | + if (permission == null) return; |
| 57 | + |
| 58 | + commandSender.addAttachment(PLUGIN, permission, true, 1); |
| 59 | + } |
| 60 | + if (command.startsWith("/")) command = command.substring(1); |
| 61 | + |
| 62 | + Bukkit.dispatchCommand(commandSender, command); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String toString(@Nullable Event e, boolean d) { |
| 67 | + String perm = this.permission == null ? "" : " with permission " + this.permission.getSingle(e); |
| 68 | + return new SyntaxStringBuilder(e, d) |
| 69 | + .append("dispatch", this.commandSender, "command", this.command) |
| 70 | + .append(perm) |
| 71 | + .toString(); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments