Skip to content

Commit

Permalink
Call BlockRedstoneEvent for more interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulu13022002 committed Mar 5, 2025
1 parent 8de7e35 commit c546c62
Show file tree
Hide file tree
Showing 32 changed files with 323 additions and 319 deletions.
Original file line number Diff line number Diff line change
@@ -1,58 +1,70 @@
package org.bukkit.event.block;

import com.google.common.base.Preconditions;
import org.bukkit.block.Block;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.checkerframework.common.value.qual.IntRange;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;

/**
* Called when a redstone current changes
* Called when a redstone current changes.
* <p>
* It includes the relevant mutation of the {@code powered} and {@code power}
* properties even if the block is not able to produce a redstone signal.
* For the {@code powered} property, a high state will be considered as
* a current of 15 and a low state as 0. Setting the new current to a different
* value will prevent most action in this case.
*/
@NullMarked
public class BlockRedstoneEvent extends BlockEvent {
private static final HandlerList handlers = new HandlerList();

private static final HandlerList HANDLER_LIST = new HandlerList();

private final int oldCurrent;
private int newCurrent;

public BlockRedstoneEvent(@NotNull final Block block, final int oldCurrent, final int newCurrent) {
@ApiStatus.Internal
public BlockRedstoneEvent(final Block block, final int oldCurrent, final int newCurrent) {
super(block);
this.oldCurrent = oldCurrent;
this.newCurrent = newCurrent;
}

/**
* Gets the old current of this block
* Gets the old current of this block.
*
* @return The previous current
* @return the previous current
*/
public int getOldCurrent() {
return oldCurrent;
public @IntRange(from = 0, to = 15) int getOldCurrent() {
return this.oldCurrent;
}

/**
* Gets the new current of this block
* Gets the new current of this block.
*
* @return The new current
* @return the new current
*/
public int getNewCurrent() {
return newCurrent;
public @IntRange(from = 0, to = 15) int getNewCurrent() {
return this.newCurrent;
}

/**
* Sets the new current of this block
* Sets the new current of this block.
*
* @param newCurrent The new current to set
* @param newCurrent the new current to set
*/
public void setNewCurrent(int newCurrent) {
public void setNewCurrent(@IntRange(from = 0, to = 15) int newCurrent) {
Preconditions.checkArgument(newCurrent >= 0 && newCurrent <= 15, "New current must be a redstone signal between 0 and 15 (was %s)", newCurrent);
this.newCurrent = newCurrent;
}

@NotNull
@Override
public HandlerList getHandlers() {
return handlers;
return HANDLER_LIST;
}

@NotNull
public static HandlerList getHandlerList() {
return handlers;
return HANDLER_LIST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ Co-authored-by: egg82 <[email protected]>

diff --git a/io/papermc/paper/redstone/RedstoneWireTurbo.java b/io/papermc/paper/redstone/RedstoneWireTurbo.java
new file mode 100644
index 0000000000000000000000000000000000000000..ff747a1ecdf3c888bca0d69de4f85dcd810b6139
index 0000000000000000000000000000000000000000..759211f717ad117b4d4ab00445a811eccba30915
--- /dev/null
+++ b/io/papermc/paper/redstone/RedstoneWireTurbo.java
@@ -0,0 +1,954 @@
@@ -0,0 +1,951 @@
+package io.papermc.paper.redstone;
+
+import java.util.List;
Expand All @@ -36,8 +36,7 @@ index 0000000000000000000000000000000000000000..ff747a1ecdf3c888bca0d69de4f85dcd
+import net.minecraft.world.level.block.Block;
+import net.minecraft.world.level.block.RedStoneWireBlock;
+import net.minecraft.world.level.block.state.BlockState;
+import org.bukkit.craftbukkit.block.CraftBlock;
+import org.bukkit.event.block.BlockRedstoneEvent;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
Expand Down Expand Up @@ -917,9 +916,7 @@ index 0000000000000000000000000000000000000000..ff747a1ecdf3c888bca0d69de4f85dcd
+ // egg82's amendment
+ // Adding Bukkit's BlockRedstoneEvent - er.. event.
+ if (i != j) {
+ BlockRedstoneEvent event = new BlockRedstoneEvent(CraftBlock.at(worldIn, upd.self), i, j);
+ worldIn.getCraftServer().getPluginManager().callEvent(event);
+ j = event.getNewCurrent();
+ j = CraftEventFactory.callRedstoneChange(worldIn, upd.self, i, j).getNewCurrent();
+ }
+
+ if (i != j) {
Expand Down Expand Up @@ -978,10 +975,10 @@ index 0000000000000000000000000000000000000000..ff747a1ecdf3c888bca0d69de4f85dcd
+ }
+}
diff --git a/net/minecraft/world/level/block/RedStoneWireBlock.java b/net/minecraft/world/level/block/RedStoneWireBlock.java
index 84e6c986917128d4488afa23d29c689cadb4f55d..f02232ce97779db0d12a5d5da1d767326d78ea4c 100644
index 84e6c986917128d4488afa23d29c689cadb4f55d..ebabc60f1be221536ba082d606fac3dd20a069da 100644
--- a/net/minecraft/world/level/block/RedStoneWireBlock.java
+++ b/net/minecraft/world/level/block/RedStoneWireBlock.java
@@ -290,6 +290,60 @@ public class RedStoneWireBlock extends Block {
@@ -290,6 +290,57 @@ public class RedStoneWireBlock extends Block {
return state.isFaceSturdy(level, pos, Direction.UP) || state.is(Blocks.HOPPER);
}

Expand Down Expand Up @@ -1021,10 +1018,7 @@ index 84e6c986917128d4488afa23d29c689cadb4f55d..f02232ce97779db0d12a5d5da1d76732
+ int oldPower = state.getValue(POWER);
+ int newPower = ((DefaultRedstoneWireEvaluator) evaluator).calculateTargetStrength(level, pos);
+ if (oldPower != newPower) {
+ org.bukkit.event.block.BlockRedstoneEvent event = new org.bukkit.event.block.BlockRedstoneEvent(org.bukkit.craftbukkit.block.CraftBlock.at(level, pos), oldPower, newPower);
+ level.getCraftServer().getPluginManager().callEvent(event);
+
+ newPower = event.getNewCurrent();
+ newPower = org.bukkit.craftbukkit.event.CraftEventFactory.callRedstoneChange(level, pos, oldPower, newPower).getNewCurrent();
+
+ if (level.getBlockState(pos) == state) {
+ state = state.setValue(POWER, newPower);
Expand All @@ -1042,7 +1036,7 @@ index 84e6c986917128d4488afa23d29c689cadb4f55d..f02232ce97779db0d12a5d5da1d76732
private void updatePowerStrength(Level level, BlockPos pos, BlockState state, @Nullable Orientation orientation, boolean updateShape) {
if (useExperimentalEvaluator(level)) {
new ExperimentalRedstoneWireEvaluator(this).updatePowerStrength(level, pos, state, orientation, updateShape);
@@ -318,7 +372,7 @@ public class RedStoneWireBlock extends Block {
@@ -318,7 +369,7 @@ public class RedStoneWireBlock extends Block {
@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean isMoving) {
if (!oldState.is(state.getBlock()) && !level.isClientSide) {
Expand All @@ -1051,7 +1045,7 @@ index 84e6c986917128d4488afa23d29c689cadb4f55d..f02232ce97779db0d12a5d5da1d76732

for (Direction direction : Direction.Plane.VERTICAL) {
level.updateNeighborsAt(pos.relative(direction), this);
@@ -337,7 +391,7 @@ public class RedStoneWireBlock extends Block {
@@ -337,7 +388,7 @@ public class RedStoneWireBlock extends Block {
level.updateNeighborsAt(pos.relative(direction), this);
}

Expand All @@ -1060,7 +1054,7 @@ index 84e6c986917128d4488afa23d29c689cadb4f55d..f02232ce97779db0d12a5d5da1d76732
this.updateNeighborsOfNeighboringWires(level, pos);
}
}
@@ -363,7 +417,7 @@ public class RedStoneWireBlock extends Block {
@@ -363,7 +414,7 @@ public class RedStoneWireBlock extends Block {
if (!level.isClientSide) {
if (neighborBlock != this || !useExperimentalEvaluator(level)) {
if (state.canSurvive(level, pos)) {
Expand All @@ -1070,10 +1064,10 @@ index 84e6c986917128d4488afa23d29c689cadb4f55d..f02232ce97779db0d12a5d5da1d76732
dropResources(state, level, pos);
level.removeBlock(pos, false);
diff --git a/net/minecraft/world/level/redstone/DefaultRedstoneWireEvaluator.java b/net/minecraft/world/level/redstone/DefaultRedstoneWireEvaluator.java
index 380fc51a252022195e178daccd8aa53dd1d71a2e..2d77780b6727f82ffc3cb216ca5f2d6483496cfd 100644
index d5e8e5fbcd6f3429ff081da6cd08acfc40091765..71ec6c89f1254ed625ddf7da96c32fcd2a0f6ea3 100644
--- a/net/minecraft/world/level/redstone/DefaultRedstoneWireEvaluator.java
+++ b/net/minecraft/world/level/redstone/DefaultRedstoneWireEvaluator.java
@@ -44,7 +44,7 @@ public class DefaultRedstoneWireEvaluator extends RedstoneWireEvaluator {
@@ -41,7 +41,7 @@ public class DefaultRedstoneWireEvaluator extends RedstoneWireEvaluator {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ Alternate Current's wire handler.

diff --git a/alternate/current/wire/LevelHelper.java b/alternate/current/wire/LevelHelper.java
new file mode 100644
index 0000000000000000000000000000000000000000..ff663d3089627e75221aa128aff4bf5cc459addb
index 0000000000000000000000000000000000000000..1f25361857a31afcb4df33123a9ef9e0a7038dbb
--- /dev/null
+++ b/alternate/current/wire/LevelHelper.java
@@ -0,0 +1,66 @@
@@ -0,0 +1,60 @@
+package alternate.current.wire;
+
+import org.bukkit.craftbukkit.block.CraftBlock;
+import org.bukkit.event.block.BlockRedstoneEvent;
+
+import net.minecraft.core.BlockPos;
+import net.minecraft.server.level.ServerLevel;
+import net.minecraft.world.level.block.Block;
Expand All @@ -42,10 +39,7 @@ index 0000000000000000000000000000000000000000..ff663d3089627e75221aa128aff4bf5c
+class LevelHelper {
+
+ static int doRedstoneEvent(ServerLevel level, BlockPos pos, int prevPower, int newPower) {
+ BlockRedstoneEvent event = new BlockRedstoneEvent(CraftBlock.at(level, pos), prevPower, newPower);
+ level.getCraftServer().getPluginManager().callEvent(event);
+
+ return event.getNewCurrent();
+ return org.bukkit.craftbukkit.event.CraftEventFactory.callRedstoneChange(level, pos, prevPower, newPower).getNewCurrent();
+ }
+
+ /**
Expand Down Expand Up @@ -2326,7 +2320,7 @@ index 0000000000000000000000000000000000000000..298076a0db4e6ee6e4775ac43bf749d9
+ }
+}
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
index a293d1481b5f4a1d18addc3e518486c639223f09..5bf38ab129451e867b638cfbd2d7be59cbf7f38d 100644
index a275b17d0852d9d9bc850614713244e580ae81f1..cb48a68ad6ba722276e0e5d5ab9b0da3301caac2 100644
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
@@ -214,6 +214,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
Expand Down Expand Up @@ -2374,7 +2368,7 @@ index a768f041fd16d253ec4ab5eb75288b1771d5cb00..1dbe7c7c1051c3972105534a07ce50d4
NONE("none"),
BLOCK("block"),
diff --git a/net/minecraft/world/level/block/RedStoneWireBlock.java b/net/minecraft/world/level/block/RedStoneWireBlock.java
index f02232ce97779db0d12a5d5da1d767326d78ea4c..12c9d60314c99fb65e640d255a2d0c6b7790ad4d 100644
index ebabc60f1be221536ba082d606fac3dd20a069da..2d2169caa5e8ba47f69e8b0f4138a0caa32f1f1a 100644
--- a/net/minecraft/world/level/block/RedStoneWireBlock.java
+++ b/net/minecraft/world/level/block/RedStoneWireBlock.java
@@ -290,7 +290,7 @@ public class RedStoneWireBlock extends Block {
Expand All @@ -2386,7 +2380,7 @@ index f02232ce97779db0d12a5d5da1d767326d78ea4c..12c9d60314c99fb65e640d255a2d0c6b
// The bulk of the new functionality is found in RedstoneWireTurbo.java
io.papermc.paper.redstone.RedstoneWireTurbo turbo = new io.papermc.paper.redstone.RedstoneWireTurbo(this);

@@ -372,7 +372,13 @@ public class RedStoneWireBlock extends Block {
@@ -369,7 +369,13 @@ public class RedStoneWireBlock extends Block {
@Override
protected void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean isMoving) {
if (!oldState.is(state.getBlock()) && !level.isClientSide) {
Expand All @@ -2401,7 +2395,7 @@ index f02232ce97779db0d12a5d5da1d767326d78ea4c..12c9d60314c99fb65e640d255a2d0c6b

for (Direction direction : Direction.Plane.VERTICAL) {
level.updateNeighborsAt(pos.relative(direction), this);
@@ -391,7 +397,13 @@ public class RedStoneWireBlock extends Block {
@@ -388,7 +394,13 @@ public class RedStoneWireBlock extends Block {
level.updateNeighborsAt(pos.relative(direction), this);
}

Expand All @@ -2416,7 +2410,7 @@ index f02232ce97779db0d12a5d5da1d767326d78ea4c..12c9d60314c99fb65e640d255a2d0c6b
this.updateNeighborsOfNeighboringWires(level, pos);
}
}
@@ -415,9 +427,15 @@ public class RedStoneWireBlock extends Block {
@@ -412,9 +424,15 @@ public class RedStoneWireBlock extends Block {
@Override
protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, @Nullable Orientation orientation, boolean movedByPiston) {
if (!level.isClientSide) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@

public CommandSourceStack(
CommandSource source,
@@ -187,6 +_,30 @@
this.chatMessageChainer
@@ -188,6 +_,30 @@
);
}
+

+ // Paper start - Expose 'with' functions from the CommandSourceStack
+ @Override
+ public CommandSourceStack withLocation(org.bukkit.Location location) {
Expand All @@ -46,9 +45,10 @@
+ );
+ }
+ // Paper end - Expose 'with' functions from the CommandSourceStack
+
public CommandSourceStack withRotation(Vec2 rotation) {
return this.rotation.equals(rotation)
? this
@@ -391,9 +_,44 @@

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--- a/net/minecraft/world/level/block/AbstractSkullBlock.java
+++ b/net/minecraft/world/level/block/AbstractSkullBlock.java
@@ -75,6 +_,11 @@
if (!level.isClientSide) {
boolean hasNeighborSignal = level.hasNeighborSignal(pos);
if (hasNeighborSignal != state.getValue(POWERED)) {
+ // Paper start - Call BlockRedstoneEvent
+ if (!org.bukkit.craftbukkit.event.CraftEventFactory.callBinaryRedstoneChange(level, pos, hasNeighborSignal)) {
+ return;
+ }
+ // Paper end - Call BlockRedstoneEvent
level.setBlock(pos, state.setValue(POWERED, Boolean.valueOf(hasNeighborSignal)), 2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,20 @@
if (!level.isClientSide) {
int signalForState = this.getSignalForState(state);
if (signalForState == 0) {
@@ -93,6 +_,19 @@
@@ -93,6 +_,16 @@
int signalStrength = this.getSignalStrength(level, pos);
boolean flag = currentSignal > 0;
boolean flag1 = signalStrength > 0;
+
+ // CraftBukkit start - Interact Pressure Plate
+ org.bukkit.World bworld = level.getWorld();
+ org.bukkit.plugin.PluginManager manager = level.getCraftServer().getPluginManager();
+
+ if (flag != flag1) {
+ org.bukkit.event.block.BlockRedstoneEvent eventRedstone = new org.bukkit.event.block.BlockRedstoneEvent(bworld.getBlockAt(pos.getX(), pos.getY(), pos.getZ()), currentSignal, signalStrength);
+ manager.callEvent(eventRedstone);
+ // Paper start - Call BlockRedstoneEvent
+ if (currentSignal != signalStrength) {
+ org.bukkit.event.block.BlockRedstoneEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callRedstoneChange(level, pos, currentSignal, signalStrength);
+
+ flag1 = eventRedstone.getNewCurrent() > 0;
+ signalStrength = eventRedstone.getNewCurrent();
+ signalStrength = event.getNewCurrent();
+ flag1 = signalStrength > 0;
+ }
+ // CraftBukkit end
+ // Paper end - Call BlockRedstoneEvent
+
if (currentSignal != signalStrength) {
BlockState blockState = this.setSignalForState(state, signalStrength);
level.setBlock(pos, blockState, 2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
--- a/net/minecraft/world/level/block/BellBlock.java
+++ b/net/minecraft/world/level/block/BellBlock.java
@@ -75,6 +_,11 @@
protected void neighborChanged(BlockState state, Level level, BlockPos pos, Block neighborBlock, @Nullable Orientation orientation, boolean movedByPiston) {
boolean hasNeighborSignal = level.hasNeighborSignal(pos);
if (hasNeighborSignal != state.getValue(POWERED)) {
+ // Paper start - Call BlockRedstoneEvent
+ if (!org.bukkit.craftbukkit.event.CraftEventFactory.callBinaryRedstoneChange(level, pos, hasNeighborSignal)) {
+ return;
+ }
+ // Paper end - Call BlockRedstoneEvent
if (hasNeighborSignal) {
this.attemptToRing(level, pos, null);
}
@@ -142,6 +_,11 @@
direction = level.getBlockState(pos).getValue(FACING);
}
Expand Down
Loading

0 comments on commit c546c62

Please sign in to comment.