Implement command block events

This commit is contained in:
Allink 2023-06-02 21:08:56 +01:00
parent cbfc1beb6a
commit 5e4f765c02
No known key found for this signature in database
2 changed files with 249 additions and 0 deletions

View File

@ -0,0 +1,163 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Allink <arclicious@vivaldi.net>
Date: Fri, 2 Jun 2023 20:42:02 +0100
Subject: [PATCH] Add command block player edit event
diff --git a/src/main/java/me/totalfreedom/scissors/event/block/CommandBlockPlayerEditEvent.java b/src/main/java/me/totalfreedom/scissors/event/block/CommandBlockPlayerEditEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..ce6f6cbbdc82dc0b4e9c9d1f35fa79465dd97867
--- /dev/null
+++ b/src/main/java/me/totalfreedom/scissors/event/block/CommandBlockPlayerEditEvent.java
@@ -0,0 +1,72 @@
+package me.totalfreedom.scissors.event.block;
+
+import org.bukkit.block.CommandBlock;
+import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called when a command block is modified by a player
+ */
+public class CommandBlockPlayerEditEvent extends Event implements Cancellable
+{
+ private static final HandlerList handlers = new HandlerList();
+ private boolean cancelled;
+ private final Player player;
+ private final String oldCommand;
+ private String newCommand;
+ private final CommandBlock commandBlock;
+
+ public CommandBlockPlayerEditEvent(Player player, String oldCommand, String newCommand, CommandBlock commandBlock)
+ {
+ this.player = player;
+ this.oldCommand = oldCommand;
+ this.newCommand = newCommand;
+ this.commandBlock = commandBlock;
+ }
+
+ public String getNewCommand()
+ {
+ return this.newCommand;
+ }
+
+ public String getOldCommand()
+ {
+ return this.oldCommand;
+ }
+
+ public void setNewCommand(final String newCommand)
+ {
+ this.newCommand = newCommand;
+ }
+
+ public Player getPlayer()
+ {
+ return this.player;
+ }
+
+ public CommandBlock getCommandBlock()
+ {
+ return this.commandBlock;
+ }
+
+ @Override
+ public boolean isCancelled()
+ {
+ return this.cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel)
+ {
+ this.cancelled = cancel;
+ }
+
+ @Override
+ public @NotNull HandlerList getHandlers()
+ {
+ return handlers;
+ }
+}
diff --git a/src/main/java/me/totalfreedom/scissors/event/block/CommandMinecartPlayerEditEvent.java b/src/main/java/me/totalfreedom/scissors/event/block/CommandMinecartPlayerEditEvent.java
new file mode 100644
index 0000000000000000000000000000000000000000..655814b19c1e2e8b25e133190664a250867ad6f2
--- /dev/null
+++ b/src/main/java/me/totalfreedom/scissors/event/block/CommandMinecartPlayerEditEvent.java
@@ -0,0 +1,73 @@
+package me.totalfreedom.scissors.event.block;
+
+import org.bukkit.block.CommandBlock;
+import org.bukkit.entity.Player;
+import org.bukkit.entity.minecart.CommandMinecart;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called when a command block is modified by a player
+ */
+public class CommandMinecartPlayerEditEvent extends Event implements Cancellable
+{
+ private static final HandlerList handlers = new HandlerList();
+ private boolean cancelled;
+ private final Player player;
+ private final String oldCommand;
+ private String newCommand;
+ private final CommandMinecart commandMinecart;
+
+ public CommandMinecartPlayerEditEvent(Player player, String oldCommand, String newCommand, CommandMinecart commandMinecart)
+ {
+ this.player = player;
+ this.oldCommand = oldCommand;
+ this.newCommand = newCommand;
+ this.commandMinecart = commandMinecart;
+ }
+
+ public String getNewCommand()
+ {
+ return this.newCommand;
+ }
+
+ public String getOldCommand()
+ {
+ return this.oldCommand;
+ }
+
+ public void setNewCommand(final String newCommand)
+ {
+ this.newCommand = newCommand;
+ }
+
+ public Player getPlayer()
+ {
+ return this.player;
+ }
+
+ public CommandMinecart getCommandMinecart()
+ {
+ return this.commandMinecart;
+ }
+
+ @Override
+ public boolean isCancelled()
+ {
+ return this.cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel)
+ {
+ this.cancelled = cancel;
+ }
+
+ @Override
+ public @NotNull HandlerList getHandlers()
+ {
+ return handlers;
+ }
+}

View File

@ -0,0 +1,86 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Allink <arclicious@vivaldi.net>
Date: Fri, 2 Jun 2023 20:55:18 +0100
Subject: [PATCH] Implement command block events
diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
index 2549ff41d1d63ba0dbdaee3e526a44f953945b6d..2748949301ce9b2c3b098dd2a7d2caee331980a7 100644
--- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
+++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java
@@ -1,6 +1,8 @@
package net.minecraft.server.network;
import me.totalfreedom.scissors.ScissorsConfig;
+import me.totalfreedom.scissors.event.block.CommandBlockPlayerEditEvent;
+import me.totalfreedom.scissors.event.block.CommandMinecartPlayerEditEvent;
import me.totalfreedom.scissors.event.player.SpectatorTeleportEvent;
import com.google.common.collect.Lists;
import com.google.common.primitives.Floats;
@@ -160,6 +162,7 @@ import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.ProfilePublicKey;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.vehicle.Boat;
+import net.minecraft.world.entity.vehicle.MinecartCommandBlock;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.Item;
@@ -188,6 +191,7 @@ import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
+import org.bukkit.entity.minecart.CommandMinecart;
import org.slf4j.Logger;
// CraftBukkit start
@@ -994,6 +998,21 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
this.player.level.getChunkAt(blockposition).setBlockEntity(tileentity);
}
+ // Scissors start - Implement command block events
+ if (commandblocklistenerabstract instanceof org.bukkit.block.CommandBlock commandBlock)
+ {
+ CommandBlockPlayerEditEvent event = new CommandBlockPlayerEditEvent(this.getCraftPlayer(), Objects.requireNonNullElse(commandblocklistenerabstract.getCommand(), ""), s, commandBlock);
+ event.callEvent();
+
+ if (event.isCancelled()) {
+ return;
+ }
+
+ s = event.getNewCommand();
+ }
+
+ // Scissors end
+
commandblocklistenerabstract.setCommand(s);
commandblocklistenerabstract.setTrackOutput(flag);
if (!flag) {
@@ -1025,7 +1044,27 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic
BaseCommandBlock commandblocklistenerabstract = packet.getCommandBlock(this.player.level);
if (commandblocklistenerabstract != null) {
- commandblocklistenerabstract.setCommand(packet.getCommand());
+ // Scissors start - Implement command block events
+ String command = packet.getCommand();
+
+ if (commandblocklistenerabstract instanceof CommandMinecart commandMinecart)
+ {
+ CommandMinecartPlayerEditEvent event = new CommandMinecartPlayerEditEvent(this.getCraftPlayer(), Objects.requireNonNullElse(commandblocklistenerabstract.getCommand(), ""), command, commandMinecart);
+ event.callEvent();
+
+ if (event.isCancelled()) {
+ return;
+ }
+
+ command = event.getNewCommand();
+ }
+
+ commandblocklistenerabstract.setCommand(command);
+
+ // commandblocklistenerabstract.setCommand(packet.getCommand());
+
+ // Scissors end
+
commandblocklistenerabstract.setTrackOutput(packet.isTrackOutput());
if (!packet.isTrackOutput()) {
commandblocklistenerabstract.setLastOutput((Component) null);