Add ExecuteCommandEvent

This commit is contained in:
William Blake Galbreath
2019-05-31 03:20:22 -05:00
parent 2df8fe9436
commit b146d349e3

View File

@@ -0,0 +1,127 @@
From 0daba673f890f25f7e57e46700e3c03dcbb613e4 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Fri, 31 May 2019 00:08:28 -0500
Subject: [PATCH] Add ExecuteCommandEvent
---
.../purpur/event/ExecuteCommandEvent.java | 87 +++++++++++++++++++
.../org/bukkit/command/SimpleCommandMap.java | 9 ++
2 files changed, 96 insertions(+)
create mode 100644 src/main/java/net/pl3x/purpur/event/ExecuteCommandEvent.java
diff --git a/src/main/java/net/pl3x/purpur/event/ExecuteCommandEvent.java b/src/main/java/net/pl3x/purpur/event/ExecuteCommandEvent.java
new file mode 100644
index 00000000..013a8712
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/event/ExecuteCommandEvent.java
@@ -0,0 +1,87 @@
+package net.pl3x.purpur.event;
+
+import org.apache.commons.lang.Validate;
+import org.bukkit.command.Command;
+import org.bukkit.command.CommandSender;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * This event is called whenever someone runs a command
+ */
+public class ExecuteCommandEvent extends Event implements Cancellable {
+ private static final HandlerList handlers = new HandlerList();
+ private boolean cancel = false;
+ private CommandSender sender;
+ private Command command;
+
+ public ExecuteCommandEvent(@NotNull CommandSender sender, @NotNull Command command) {
+ this.sender = sender;
+ this.command = command;
+ }
+
+ /**
+ * Gets the command that the player is attempting to execute.
+ *
+ * @return Command the player is attempting to execute
+ */
+ @NotNull
+ public Command getCommand() {
+ return command;
+ }
+
+ /**
+ * Sets the command that the player will execute.
+ *
+ * @param command New command that the player will execute
+ * @throws IllegalArgumentException if command is null or empty
+ */
+ public void setCommand(@NotNull Command command) throws IllegalArgumentException {
+ Validate.notNull(command, "Command cannot be null");
+ this.command = command;
+ }
+
+ /**
+ * Gets the sender that this command will be executed as.
+ *
+ * @return Sender this command will be executed as
+ */
+ @NotNull
+ public CommandSender getSender() {
+ return sender;
+ }
+
+ /**
+ * Sets the sender that this command will be executed as.
+ *
+ * @param sender New sender which this event will execute as
+ * @throws IllegalArgumentException if the sender provided is null
+ */
+ public void setSender(@NotNull final CommandSender sender) throws IllegalArgumentException {
+ Validate.notNull(sender, "Sender cannot be null");
+ this.sender = sender;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancel;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancel = cancel;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
diff --git a/src/main/java/org/bukkit/command/SimpleCommandMap.java b/src/main/java/org/bukkit/command/SimpleCommandMap.java
index 460fda05..b87c30a3 100644
--- a/src/main/java/org/bukkit/command/SimpleCommandMap.java
+++ b/src/main/java/org/bukkit/command/SimpleCommandMap.java
@@ -147,6 +147,15 @@ public class SimpleCommandMap implements CommandMap {
return false;
}
+ // Purpur start
+ net.pl3x.purpur.event.ExecuteCommandEvent event = new net.pl3x.purpur.event.ExecuteCommandEvent(sender, target);
+ if (!event.callEvent()) {
+ return true; // cancelled
+ }
+ sender = event.getSender();
+ target = event.getCommand();
+ // Purpur end
+
// Paper start - Plugins do weird things to workaround normal registration
if (target.timings == null) {
target.timings = co.aikar.timings.TimingsManager.getCommandTiming(null, target);
--
2.20.1