Add block and fluid tick events

This commit is contained in:
William Blake Galbreath
2019-10-19 00:01:14 -05:00
parent ea1b41e33f
commit fa015d7a24
2 changed files with 278 additions and 0 deletions

View File

@@ -0,0 +1,179 @@
From 857970166f62c14fc6bc58a0a60e5fc68d81c091 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Fri, 18 Oct 2019 23:58:29 -0500
Subject: [PATCH] Add block and fluid tick events
---
.../purpur/event/block/BlockTickEvent.java | 55 +++++++++++++++++++
.../purpur/event/block/FluidTickEvent.java | 55 +++++++++++++++++++
src/main/java/org/bukkit/World.java | 30 ++++++++++
3 files changed, 140 insertions(+)
create mode 100644 src/main/java/net/pl3x/purpur/event/block/BlockTickEvent.java
create mode 100644 src/main/java/net/pl3x/purpur/event/block/FluidTickEvent.java
diff --git a/src/main/java/net/pl3x/purpur/event/block/BlockTickEvent.java b/src/main/java/net/pl3x/purpur/event/block/BlockTickEvent.java
new file mode 100644
index 000000000..02787304f
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/event/block/BlockTickEvent.java
@@ -0,0 +1,55 @@
+package net.pl3x.purpur.event.block;
+
+import org.bukkit.World;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.block.BlockEvent;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called when a block is ticked by the server.
+ */
+public class BlockTickEvent extends BlockEvent implements Cancellable {
+ private static final HandlerList handlers = new HandlerList();
+ private final boolean isRandomTick;
+ private boolean cancelled;
+
+ public BlockTickEvent(@NotNull World world, int x, int y, int z) {
+ this(world, x, y, z, false);
+ }
+
+ public BlockTickEvent(@NotNull World world, int x, int y, int z, boolean isRandomTick) {
+ super(world.getBlockAt(x, y, z));
+ this.isRandomTick = isRandomTick;
+ }
+
+ /**
+ * Check if this is a random tick
+ *
+ * @return True if random
+ */
+ public boolean isRandomTick() {
+ return isRandomTick;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ cancelled = cancel;
+ }
+
+ @Override
+ @NotNull
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
diff --git a/src/main/java/net/pl3x/purpur/event/block/FluidTickEvent.java b/src/main/java/net/pl3x/purpur/event/block/FluidTickEvent.java
new file mode 100644
index 000000000..1a8f0b674
--- /dev/null
+++ b/src/main/java/net/pl3x/purpur/event/block/FluidTickEvent.java
@@ -0,0 +1,55 @@
+package net.pl3x.purpur.event.block;
+
+import org.bukkit.World;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.block.BlockEvent;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Called when a fluid is ticked by the server.
+ */
+public class FluidTickEvent extends BlockEvent implements Cancellable {
+ private static final HandlerList handlers = new HandlerList();
+ private final boolean isRandomTick;
+ private boolean cancelled;
+
+ public FluidTickEvent(@NotNull World world, int x, int y, int z) {
+ this(world, x, y, z, false);
+ }
+
+ public FluidTickEvent(@NotNull World world, int x, int y, int z, boolean isRandomTick) {
+ super(world.getBlockAt(x, y, z));
+ this.isRandomTick = isRandomTick;
+ }
+
+ /**
+ * Check if this is a random tick
+ *
+ * @return True if random
+ */
+ public boolean isRandomTick() {
+ return isRandomTick;
+ }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ cancelled = cancel;
+ }
+
+ @Override
+ @NotNull
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+}
diff --git a/src/main/java/org/bukkit/World.java b/src/main/java/org/bukkit/World.java
index 2d22ab30a..e1f25bdd1 100644
--- a/src/main/java/org/bukkit/World.java
+++ b/src/main/java/org/bukkit/World.java
@@ -2988,6 +2988,36 @@ public interface World extends PluginMessageRecipient, Metadatable {
@NotNull
public List<Raid> getRaids();
+ // Purpur start
+ /**
+ * Get if {@link net.pl3x.purpur.event.block.BlockTickEvent} is enabled for this world
+ *
+ * @return True if enabled
+ */
+ boolean isBlockTickEventEnabled();
+
+ /**
+ * Set if {@link net.pl3x.purpur.event.block.BlockTickEvent} is enabled for this world
+ *
+ * @param enabled True to enable
+ */
+ void setBlockTickEventEnabled(boolean enabled);
+
+ /**
+ * Get if {@link net.pl3x.purpur.event.block.FluidTickEvent} is enabled for this world
+ *
+ * @return True if enabled
+ */
+ boolean isFluidTickEventEnabled();
+
+ /**
+ * Set if {@link net.pl3x.purpur.event.block.FluidTickEvent} is enabled for this world
+ *
+ * @param enabled True to enable
+ */
+ void setFluidTickEventEnabled(boolean enabled);
+ // Purpur end
+
/**
* Represents various map environment types that a world may be
*/
--
2.23.0.rc1

View File

@@ -0,0 +1,99 @@
From 217e5c6ce7a757f659a3546a6fe8af34d41e5ed9 Mon Sep 17 00:00:00 2001
From: William Blake Galbreath <blake.galbreath@gmail.com>
Date: Fri, 18 Oct 2019 23:58:56 -0500
Subject: [PATCH] Add block and fluid tick events
---
.../java/net/minecraft/server/WorldServer.java | 6 ++++--
.../net/pl3x/purpur/PurpurWorldConfig.java | 7 +++++++
.../org/bukkit/craftbukkit/CraftWorld.java | 18 ++++++++++++++++++
3 files changed, 29 insertions(+), 2 deletions(-)
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
index 56133d54fa..8b82c19aeb 100644
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
@@ -570,7 +570,7 @@ public class WorldServer extends World {
gameprofilerfiller.enter("randomTick");
IBlockData iblockdata = chunksection.getType(blockposition2.getX() - j, blockposition2.getY() - j1, blockposition2.getZ() - k);
- if (iblockdata.q()) {
+ if (iblockdata.q() && (!purpurConfig.blockTickEvent || new net.pl3x.purpur.event.block.BlockTickEvent(getWorld(), blockposition2.x, blockposition2.y, blockposition2.z, true).callEvent())) { // Purpur
iblockdata.getBlock().randomTick = true; // Paper - fix MC-113809
iblockdata.b((World) this, blockposition2, this.random);
iblockdata.getBlock().randomTick = false; // Paper - fix MC-113809
@@ -578,7 +578,7 @@ public class WorldServer extends World {
Fluid fluid = iblockdata.p();
- if (fluid.h()) {
+ if (fluid.h() && (!purpurConfig.fluidTickEvent || new net.pl3x.purpur.event.block.FluidTickEvent(getWorld(), blockposition2.x, blockposition2.y, blockposition2.z, true).callEvent())) { // Purpur
fluid.b(this, blockposition2, this.random);
}
@@ -667,6 +667,7 @@ public class WorldServer extends World {
Fluid fluid = this.getFluid(nextticklistentry.a);
if (fluid.getType() == nextticklistentry.b()) {
+ if (purpurConfig.fluidTickEvent && !new net.pl3x.purpur.event.block.FluidTickEvent(getWorld(), nextticklistentry.a.x, nextticklistentry.a.y, nextticklistentry.a.z).callEvent()) return; // Purpur
fluid.a((World) this, nextticklistentry.a);
}
@@ -676,6 +677,7 @@ public class WorldServer extends World {
IBlockData iblockdata = this.getType(nextticklistentry.a);
if (iblockdata.getBlock() == nextticklistentry.b()) {
+ if (purpurConfig.blockTickEvent && !new net.pl3x.purpur.event.block.BlockTickEvent(getWorld(), nextticklistentry.a.x, nextticklistentry.a.y, nextticklistentry.a.z).callEvent()) return; // Purpur
iblockdata.a((World) this, nextticklistentry.a, this.random);
}
diff --git a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
index f687444604..3ad3415c4a 100644
--- a/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
+++ b/src/main/java/net/pl3x/purpur/PurpurWorldConfig.java
@@ -62,6 +62,13 @@ public class PurpurWorldConfig {
return config.getString("world-settings." + worldName + "." + path, config.getString("world-settings.default." + path));
}
+ public boolean blockTickEvent = false;
+ public boolean fluidTickEvent = false;
+ private void tickEvents() {
+ blockTickEvent = getBoolean("block-tick-events", false);
+ fluidTickEvent = getBoolean("fluid-tick-events", false);
+ }
+
public boolean editableSigns = false;
private void editableSigns() {
editableSigns = getBoolean("editable-signs", editableSigns);
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
index 243722b672..0f33688cd5 100644
--- a/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftWorld.java
@@ -2345,6 +2345,24 @@ public class CraftWorld implements World {
return persistentRaid.a.values().stream().map(CraftRaid::new).collect(Collectors.toList());
}
+ // Purpur start
+ public boolean isBlockTickEventEnabled() {
+ return getHandle().purpurConfig.blockTickEvent;
+ }
+
+ public void setBlockTickEventEnabled(boolean enabled) {
+ getHandle().purpurConfig.blockTickEvent = enabled;
+ }
+
+ public boolean isFluidTickEventEnabled() {
+ return getHandle().purpurConfig.fluidTickEvent;
+ }
+
+ public void setFluidTickEventEnabled(boolean enabled) {
+ getHandle().purpurConfig.fluidTickEvent = enabled;
+ }
+ // Purpur end
+
// Paper start
@Override
public CompletableFuture<Chunk> getChunkAtAsync(int x, int z, boolean gen) {
--
2.23.0.rc1