diff --git a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java index 7123ede168d..e4b2db87701 100644 --- a/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java +++ b/src/main/java/ch/njol/skript/classes/data/BukkitEventValues.java @@ -88,6 +88,7 @@ import org.bukkit.event.block.BlockIgniteEvent; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.command.UnknownCommandEvent; import org.bukkit.event.block.BellRingEvent; import org.bukkit.event.block.BellResonateEvent; import org.bukkit.event.enchantment.EnchantItemEvent; @@ -1904,5 +1905,14 @@ public ItemStack get(InventoryMoveItemEvent event) { return event.getItem(); } }, EventValues.TIME_NOW); + + if (Skript.classExists("org.bukkit.event.command.UnknownCommandEvent")) { + EventValues.registerEventValue(UnknownCommandEvent.class, CommandSender.class, new Getter() { + @Override + public CommandSender get(UnknownCommandEvent event) { + return event.getSender(); + } + }, EventValues.TIME_NOW); + } } } diff --git a/src/main/java/ch/njol/skript/events/SimpleEvents.java b/src/main/java/ch/njol/skript/events/SimpleEvents.java index 23f819ea890..17259c36550 100644 --- a/src/main/java/ch/njol/skript/events/SimpleEvents.java +++ b/src/main/java/ch/njol/skript/events/SimpleEvents.java @@ -45,6 +45,7 @@ import org.bukkit.event.block.BlockSpreadEvent; import org.bukkit.event.block.LeavesDecayEvent; import org.bukkit.event.block.SignChangeEvent; +import org.bukkit.event.command.UnknownCommandEvent; import org.bukkit.event.block.SpongeAbsorbEvent; import org.bukkit.event.enchantment.EnchantItemEvent; import org.bukkit.event.enchantment.PrepareItemEnchantEvent; @@ -758,6 +759,18 @@ public class SimpleEvents { ) .since("2.7"); + if (Skript.classExists("org.bukkit.event.command.UnknownCommandEvent")) { + Skript.registerEvent("Unknown Command", SimpleEvent.class, UnknownCommandEvent.class, "unknown command") + .description("Called when an unknown command is used.") + .examples( + "on unknown command:", + "\tset unknown command message to \"Hey, this command does not exist.\"" + ) + + .since("INSERT VERSION") + .requiredPlugins("Paper"); + } + { final Class eventClass; if (Skript.classExists("org.bukkit.event.block.BellRingEvent")) { @@ -798,5 +811,4 @@ public class SimpleEvents { .requiredPlugins("Spigot 1.19.4+"); } } - } diff --git a/src/main/java/ch/njol/skript/expressions/ExprArgument.java b/src/main/java/ch/njol/skript/expressions/ExprArgument.java index 622aa229a4f..4fc63ba3489 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprArgument.java +++ b/src/main/java/ch/njol/skript/expressions/ExprArgument.java @@ -22,6 +22,7 @@ import java.util.regex.MatchResult; import org.bukkit.event.Event; +import org.bukkit.event.command.UnknownCommandEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.server.ServerCommandEvent; import org.eclipse.jdt.annotation.Nullable; @@ -60,7 +61,7 @@ "add argument 1 to argument 2", "heal the last argument" }) -@Since("1.0, 2.7 (support for command events)") +@Since("1.0, 2.7 (support for command events), INSERT VERSION (unknown command)") public class ExprArgument extends SimpleExpression { static { @@ -81,13 +82,15 @@ public class ExprArgument extends SimpleExpression { private Argument argument; private int ordinal = -1; // Available in ORDINAL and sometimes CLASSINFO + private boolean SUPPORTS_UNKNOWN_COMMAND_EVENT = Skript.classExists("org.bukkit.event.command.UnknownCommandEvent"); @Override @SuppressWarnings("unchecked") - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + public boolean init(Expression[] expressions, int matchedPattern, + Kleenean isDelayed, ParseResult parseResult) { boolean scriptCommand = getParser().isCurrentEvent(ScriptCommandEvent.class); - if (!scriptCommand && !getParser().isCurrentEvent(PlayerCommandPreprocessEvent.class, ServerCommandEvent.class)) { - Skript.error("The 'argument' expression can only be used in a script command or command event"); + if (!scriptCommand && !getParser().isCurrentEvent(PlayerCommandPreprocessEvent.class, ServerCommandEvent.class, org.bukkit.event.command.UnknownCommandEvent.class)) { + Skript.error("The 'argument' expression can only be used in a script command, command event or unknown command event"); return false; } @@ -159,7 +162,7 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye Skript.error("'arguments' cannot be used for script commands. Use 'argument 1', 'argument 2', etc. instead", ErrorQuality.SEMANTIC_ERROR); return false; case CLASSINFO: - ClassInfo c = ((Literal>) exprs[0]).getSingle(); + ClassInfo c = ((Literal>) expressions[0]).getSingle(); if (parseResult.regexes.size() > 0) { ordinal = Utils.parseInt(parseResult.regexes.get(0).group()); if (ordinal > currentArguments.size()) { @@ -213,16 +216,18 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye @Override @Nullable - protected Object[] get(final Event e) { + protected Object[] get(Event event) { if (argument != null) { - return argument.getCurrent(e); + return argument.getCurrent(event); } String fullCommand; - if (e instanceof PlayerCommandPreprocessEvent) { - fullCommand = ((PlayerCommandPreprocessEvent) e).getMessage().substring(1).trim(); - } else if (e instanceof ServerCommandEvent) { // It's a ServerCommandEvent then - fullCommand = ((ServerCommandEvent) e).getCommand().trim(); + if (SUPPORTS_UNKNOWN_COMMAND_EVENT && event instanceof UnknownCommandEvent) { + fullCommand = ((UnknownCommandEvent) event).getCommandLine().trim(); + } else if (event instanceof PlayerCommandPreprocessEvent) { + fullCommand = ((PlayerCommandPreprocessEvent) event).getMessage().substring(1).trim(); + } else if (event instanceof ServerCommandEvent) { // It's a ServerCommandEvent then + fullCommand = ((ServerCommandEvent) event).getCommand().trim(); } else { return new Object[0]; } diff --git a/src/main/java/ch/njol/skript/expressions/ExprCommand.java b/src/main/java/ch/njol/skript/expressions/ExprCommand.java index 4809e9eaaa7..677e2d6954a 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprCommand.java +++ b/src/main/java/ch/njol/skript/expressions/ExprCommand.java @@ -20,6 +20,7 @@ import ch.njol.skript.command.ScriptCommandEvent; import org.bukkit.event.Event; +import org.bukkit.event.command.UnknownCommandEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.server.ServerCommandEvent; import org.eclipse.jdt.annotation.Nullable; @@ -59,11 +60,12 @@ public class ExprCommand extends SimpleExpression { } private boolean fullCommand; + private boolean SUPPORTS_UNKNOWN_COMMAND_EVENT = Skript.classExists("org.bukkit.event.command.UnknownCommandEvent"); @Override - public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { - if (!getParser().isCurrentEvent(PlayerCommandPreprocessEvent.class, ServerCommandEvent.class, ScriptCommandEvent.class)) { - Skript.error("The 'command' expression can only be used in a script command or command event"); + public boolean init(Expression[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + if (!getParser().isCurrentEvent(PlayerCommandPreprocessEvent.class, ServerCommandEvent.class, org.bukkit.event.command.UnknownCommandEvent.class, ScriptCommandEvent.class)) { + Skript.error("The 'command' expression can only be used in a script command, command event or unknown command event"); return false; } fullCommand = matchedPattern == 0; @@ -72,16 +74,18 @@ public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelaye @Override @Nullable - protected String[] get(final Event e) { + protected String[] get(Event event) { final String s; - if (e instanceof PlayerCommandPreprocessEvent) { - s = ((PlayerCommandPreprocessEvent) e).getMessage().substring(1).trim(); - } else if (e instanceof ServerCommandEvent) { - s = ((ServerCommandEvent) e).getCommand().trim(); + if (event instanceof PlayerCommandPreprocessEvent) { + s = ((PlayerCommandPreprocessEvent) event).getMessage().substring(1).trim(); + } else if (event instanceof ServerCommandEvent) { + s = ((ServerCommandEvent) event).getCommand().trim(); + } else if (SUPPORTS_UNKNOWN_COMMAND_EVENT && event instanceof UnknownCommandEvent) { + s = ((UnknownCommandEvent) event).getCommandLine().trim(); } else { // It's a script command event - ScriptCommandEvent event = (ScriptCommandEvent) e; - s = event.getCommandLabel() + " " + event.getArgsString(); + ScriptCommandEvent scriptCmdEvent = (ScriptCommandEvent) event; + s = scriptCmdEvent.getCommandLabel() + " " + scriptCmdEvent.getArgsString(); } if (fullCommand) { diff --git a/src/main/java/ch/njol/skript/expressions/ExprMessage.java b/src/main/java/ch/njol/skript/expressions/ExprMessage.java index a3b54f43c4c..abd57312777 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprMessage.java +++ b/src/main/java/ch/njol/skript/expressions/ExprMessage.java @@ -19,6 +19,7 @@ package ch.njol.skript.expressions; import org.bukkit.event.Event; +import org.bukkit.event.command.UnknownCommandEvent; import org.bukkit.event.entity.EntityDeathEvent; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.AsyncPlayerChatEvent; @@ -47,25 +48,30 @@ */ @SuppressWarnings("deprecation") @Name("Message") -@Description("The (chat) message of a chat event, the join message of a join event, the quit message of a quit event, or the death message on a death event. This expression is mostly useful for being changed.") -@Examples({"on chat:", - " player has permission \"admin\"", - " set message to \"&c%message%\"", +@Description("The (chat) message of a chat event, the join message of a join event, the quit message of a quit event, or the death message of a death event, or the unknown command message of an unknown command event. This expression is mostly useful for being changed.") +@Examples({ + "on chat:", + "\tplayer has permission \"admin\"", + "\tset message to \"&c%message%\"", "", "on first join:", - " set join message to \"Welcome %player% to our awesome server!\"", + "\tset join message to \"Welcome %player% to our awesome server!\"", "", "on join:", - " player has played before", - " set join message to \"Welcome back, %player%!\"", + "\tplayer has played before", + "\tset join message to \"Welcome back, %player%!\"", "", "on quit:", - " set quit message to \"%player% left this awesome server!\"", + "\tset quit message to \"%player% left this awesome server!\"", "", "on death:", - " set the death message to \"%player% died!\""}) -@Since("1.4.6 (chat message), 1.4.9 (join & quit messages), 2.0 (death message)") -@Events({"chat", "join", "quit", "death"}) + "\tset the death message to \"%player% died!\"", + "", + "on unknown command:", + "\tset unknown command message to \"This command does not exist.\"" +}) +@Since("1.4.6 (chat message), 1.4.9 (join & quit messages), 2.0 (death message), INSERT VERSION (unknown command message)") +@Events({"chat", "join", "quit", "death", "unknown command"}) public class ExprMessage extends SimpleExpression { @SuppressWarnings("unchecked") @@ -84,46 +90,58 @@ void set(Event event, String message) { JOIN("join", "(join|log[ ]in)( |-)message", PlayerJoinEvent.class) { @Override @Nullable - String get(final Event e) { - return ((PlayerJoinEvent) e).getJoinMessage(); + String get(Event event) { + return ((PlayerJoinEvent) event).getJoinMessage(); } @Override - void set(final Event e, final String message) { - ((PlayerJoinEvent) e).setJoinMessage(message); + void set(Event event, final String message) { + ((PlayerJoinEvent) event).setJoinMessage(message); } }, QUIT("quit", "(quit|leave|log[ ]out|kick)( |-)message", PlayerQuitEvent.class, PlayerKickEvent.class) { @Override @Nullable - String get(final Event e) { - if (e instanceof PlayerKickEvent) - return ((PlayerKickEvent) e).getLeaveMessage(); + String get(Event event) { + if (event instanceof PlayerKickEvent) + return ((PlayerKickEvent) event).getLeaveMessage(); else - return ((PlayerQuitEvent) e).getQuitMessage(); + return ((PlayerQuitEvent) event).getQuitMessage(); } @Override - void set(final Event e, final String message) { - if (e instanceof PlayerKickEvent) - ((PlayerKickEvent) e).setLeaveMessage(message); + void set(Event event, final String message) { + if (event instanceof PlayerKickEvent) + ((PlayerKickEvent) event).setLeaveMessage(message); else - ((PlayerQuitEvent) e).setQuitMessage(message); + ((PlayerQuitEvent) event).setQuitMessage(message); + } + }, + UNKNOWN("unknown command", "unknown command( |-)message", UnknownCommandEvent.class) { + @Override + @Nullable + String get(Event event) { + return ((UnknownCommandEvent) event).getMessage(); + } + + @Override + void set(Event event, final String message) { + ((UnknownCommandEvent) event).setMessage(message); } }, DEATH("death", "death( |-)message", EntityDeathEvent.class) { @Override @Nullable - String get(final Event e) { - if (e instanceof PlayerDeathEvent) - return ((PlayerDeathEvent) e).getDeathMessage(); + String get(Event event) { + if (event instanceof PlayerDeathEvent) + return ((PlayerDeathEvent) event).getDeathMessage(); return null; } @Override - void set(final Event e, final String message) { - if (e instanceof PlayerDeathEvent) - ((PlayerDeathEvent) e).setDeathMessage(message); + void set(Event event, String message) { + if (event instanceof PlayerDeathEvent) + ((PlayerDeathEvent) event).setDeathMessage(message); } }; diff --git a/src/test/skript/junit/EvtUnknownCommand.sk b/src/test/skript/junit/EvtUnknownCommand.sk new file mode 100644 index 00000000000..57be41f51ed --- /dev/null +++ b/src/test/skript/junit/EvtUnknownCommand.sk @@ -0,0 +1,2 @@ +on unknown command: + set unknown command message to "Hey, this command does not exist."