Skip to content

Commit 407e919

Browse files
committed
BlockCanBuildEvent
1 parent 5dd5cf4 commit 407e919

File tree

4 files changed

+118
-67
lines changed

4 files changed

+118
-67
lines changed
Lines changed: 16 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package org.bukkit.event.block;
22

33
import org.bukkit.Material;
4-
import org.bukkit.block.Block;
54
import org.bukkit.block.data.BlockData;
65
import org.bukkit.entity.Player;
76
import org.bukkit.event.HandlerList;
8-
import org.jetbrains.annotations.ApiStatus;
9-
import org.jetbrains.annotations.NotNull;
10-
import org.jetbrains.annotations.Nullable;
7+
import org.bukkit.inventory.EquipmentSlot;
8+
import org.jspecify.annotations.Nullable;
119

1210
/**
1311
* Called when we try to place a block, to see if we can build it here or not.
@@ -20,35 +18,7 @@
2018
* #getMaterial()} instead.
2119
* </ul>
2220
*/
23-
public class BlockCanBuildEvent extends BlockEvent {
24-
25-
private static final HandlerList HANDLER_LIST = new HandlerList();
26-
27-
private final Player player;
28-
private final org.bukkit.inventory.EquipmentSlot hand;
29-
protected BlockData blockData;
30-
protected boolean buildable;
31-
32-
@Deprecated(since = "1.13.2", forRemoval = true)
33-
@ApiStatus.Internal
34-
public BlockCanBuildEvent(@NotNull final Block block, @NotNull final BlockData type, final boolean canBuild) {
35-
this(block, null, type, canBuild, org.bukkit.inventory.EquipmentSlot.HAND); // Paper - expose hand
36-
}
37-
38-
@Deprecated(forRemoval = true)
39-
@ApiStatus.Internal
40-
public BlockCanBuildEvent(@NotNull final Block block, @Nullable final Player player, @NotNull final BlockData type, final boolean canBuild) {
41-
this(block, player, type, canBuild, org.bukkit.inventory.EquipmentSlot.HAND); // Paper start - expose hand
42-
}
43-
44-
@ApiStatus.Internal
45-
public BlockCanBuildEvent(@NotNull final Block block, @Nullable final Player player, @NotNull final BlockData type, final boolean canBuild, @NotNull final org.bukkit.inventory.EquipmentSlot hand) { // Paper end - expose hand
46-
super(block);
47-
this.player = player;
48-
this.buildable = canBuild;
49-
this.blockData = type;
50-
this.hand = hand;
51-
}
21+
public interface BlockCanBuildEvent extends BlockEventNew {
5222

5323
/**
5424
* Gets the player who placed the block involved in this event.
@@ -57,40 +27,28 @@ public BlockCanBuildEvent(@NotNull final Block block, @Nullable final Player pla
5727
*
5828
* @return The Player who placed the block involved in this event
5929
*/
60-
@Nullable
61-
public Player getPlayer() {
62-
return this.player;
63-
}
30+
@Nullable Player getPlayer();
6431

6532
/**
6633
* Gets the Material that we are trying to place.
6734
*
6835
* @return The Material that we are trying to place
6936
*/
70-
@NotNull
71-
public Material getMaterial() {
72-
return this.blockData.getMaterial();
73-
}
37+
Material getMaterial();
7438

7539
/**
7640
* Gets the BlockData that we are trying to place.
7741
*
7842
* @return The BlockData that we are trying to place
7943
*/
80-
@NotNull
81-
public BlockData getBlockData() {
82-
return this.blockData.clone();
83-
}
44+
BlockData getBlockData();
8445

8546
/**
8647
* Gets the hand the player will use to place the block
8748
*
88-
* @return the EquipmentSlot representing the players hand.
49+
* @return the {@link EquipmentSlot} representing the players hand.
8950
*/
90-
@NotNull
91-
public org.bukkit.inventory.EquipmentSlot getHand() {
92-
return this.hand;
93-
}
51+
EquipmentSlot getHand();
9452

9553
/**
9654
* Gets whether the block can be built here.
@@ -100,28 +58,20 @@ public org.bukkit.inventory.EquipmentSlot getHand() {
10058
*
10159
* @return boolean whether the block can be built
10260
*/
103-
public boolean isBuildable() {
104-
return this.buildable;
105-
}
61+
boolean isBuildable();
10662

10763
/**
10864
* Sets whether the block can be built here or not.
10965
*
110-
* @param cancel {@code true} if you want to allow the block to be built here
66+
* @param buildable {@code true} if you want to allow the block to be built here
11167
* despite Minecraft's default behaviour
11268
*/
113-
public void setBuildable(boolean cancel) {
114-
this.buildable = cancel;
115-
}
116-
117-
@NotNull
118-
@Override
119-
public HandlerList getHandlers() {
120-
return HANDLER_LIST;
121-
}
69+
void setBuildable(boolean buildable);
12270

123-
@NotNull
124-
public static HandlerList getHandlerList() {
125-
return HANDLER_LIST;
71+
static HandlerList getHandlerList() {
72+
final class Holder {
73+
private static final HandlerList HANDLER_LIST = new HandlerList();
74+
}
75+
return Holder.HANDLER_LIST;
12676
}
12777
}

paper-server/patches/features/0032-Events-to-interfaces.patch

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,19 @@ index e21d06007b6d207397e878768e243deded9486aa..880be975b8d5cb0436c090b2b794d401
10571057
if (!event.callEvent()) {
10581058
player.containerMenu.sendAllDataToRemote();
10591059
return false;
1060+
diff --git a/net/minecraft/world/item/BlockItem.java b/net/minecraft/world/item/BlockItem.java
1061+
index 73ce7c82c0bd28c2e43ca40ba35c4603b21375ad..d0fb3e30eda4bb2187011a2ca9d7588cf20ffb67 100644
1062+
--- a/net/minecraft/world/item/BlockItem.java
1063+
+++ b/net/minecraft/world/item/BlockItem.java
1064+
@@ -172,7 +172,7 @@ public class BlockItem extends Item {
1065+
boolean canBuild = (!this.mustSurvive() || state.canSurvive(world, context.getClickedPos())) && world.checkEntityCollision(state, player, CollisionContext.placementContext(player), context.getClickedPos(), true); // Paper - Cancel hit for vanished players
1066+
org.bukkit.entity.Player bukkitPlayer = (context.getPlayer() instanceof ServerPlayer) ? (org.bukkit.entity.Player) context.getPlayer().getBukkitEntity() : null;
1067+
1068+
- org.bukkit.event.block.BlockCanBuildEvent event = new org.bukkit.event.block.BlockCanBuildEvent(
1069+
+ org.bukkit.event.block.BlockCanBuildEvent event = new org.bukkit.craftbukkit.event.block.CraftBlockCanBuildEvent(
1070+
org.bukkit.craftbukkit.block.CraftBlock.at(world, context.getClickedPos()), bukkitPlayer,
1071+
org.bukkit.craftbukkit.block.data.CraftBlockData.fromData(state), canBuild, org.bukkit.craftbukkit.CraftEquipmentSlot.getHand(context.getHand())
1072+
);
10601073
diff --git a/net/minecraft/world/item/BucketItem.java b/net/minecraft/world/item/BucketItem.java
10611074
index 9d6485148393292006ed4453e0ed228b6197b239..c2339ab0794afcbfa23e44063d680ee0717a5819 100644
10621075
--- a/net/minecraft/world/item/BucketItem.java
@@ -1230,6 +1243,19 @@ index a5bfd497a07f2e2311db8eca5b8ba8af9f4d1080..6b3905581ea35562baa8e8bd3e70227b
12301243
if (event.callEvent() && snowball.attemptSpawn()) {
12311244
player.awardStat(Stats.ITEM_USED.get(this));
12321245
if (event.shouldConsume()) {
1246+
diff --git a/net/minecraft/world/item/StandingAndWallBlockItem.java b/net/minecraft/world/item/StandingAndWallBlockItem.java
1247+
index d75c95d728efc0b4064f562ca80b68b23883feaf..88c54281c4263033948ec4caf84bf0b36a891bd6 100644
1248+
--- a/net/minecraft/world/item/StandingAndWallBlockItem.java
1249+
+++ b/net/minecraft/world/item/StandingAndWallBlockItem.java
1250+
@@ -47,7 +47,7 @@ public class StandingAndWallBlockItem extends BlockItem {
1251+
boolean defaultReturn = level.isUnobstructed(blockState, clickedPos, CollisionContext.empty());
1252+
org.bukkit.entity.Player player = (context.getPlayer() instanceof net.minecraft.server.level.ServerPlayer serverPlayer) ? serverPlayer.getBukkitEntity() : null;
1253+
1254+
- org.bukkit.event.block.BlockCanBuildEvent event = new org.bukkit.event.block.BlockCanBuildEvent(org.bukkit.craftbukkit.block.CraftBlock.at(context.getLevel(), clickedPos), player, org.bukkit.craftbukkit.block.data.CraftBlockData.fromData(blockState), defaultReturn, org.bukkit.craftbukkit.CraftEquipmentSlot.getHand(context.getHand())); // Paper - Expose hand in BlockCanBuildEvent
1255+
+ org.bukkit.event.block.BlockCanBuildEvent event = new org.bukkit.craftbukkit.event.block.CraftBlockCanBuildEvent(org.bukkit.craftbukkit.block.CraftBlock.at(context.getLevel(), clickedPos), player, org.bukkit.craftbukkit.block.data.CraftBlockData.fromData(blockState), defaultReturn, org.bukkit.craftbukkit.CraftEquipmentSlot.getHand(context.getHand())); // Paper - Expose hand in BlockCanBuildEvent
1256+
context.getLevel().getCraftServer().getPluginManager().callEvent(event);
1257+
1258+
return (event.isBuildable()) ? blockState : null;
12331259
diff --git a/net/minecraft/world/item/ThrowablePotionItem.java b/net/minecraft/world/item/ThrowablePotionItem.java
12341260
index 2707c2e3c6e32bb7c497808dceb34690e27a567a..e538185e1897f7ba1c0c291cf6a0f6d8cabcbc3b 100644
12351261
--- a/net/minecraft/world/item/ThrowablePotionItem.java
@@ -1359,6 +1385,19 @@ index 0284c631e8e2b303f4f95927d99513423dad4b0c..727066a79520bda4a08f5c7ce301559d
13591385
world1.getCraftServer().getPluginManager().callEvent(event);
13601386

13611387
if (!event.isCancelled()) {
1388+
diff --git a/net/minecraft/world/level/block/FireBlock.java b/net/minecraft/world/level/block/FireBlock.java
1389+
index 14a4189166b862b66a8383046f2abfe02ccbccd8..acdfcd25c7949609b96eb48f67ca7635a423b895 100644
1390+
--- a/net/minecraft/world/level/block/FireBlock.java
1391+
+++ b/net/minecraft/world/level/block/FireBlock.java
1392+
@@ -270,7 +270,7 @@ public class FireBlock extends BaseFireBlock {
1393+
org.bukkit.block.Block burnBlock = org.bukkit.craftbukkit.block.CraftBlock.at(level, pos);
1394+
org.bukkit.block.Block sourceBlock = org.bukkit.craftbukkit.block.CraftBlock.at(level, sourcePos);
1395+
1396+
- org.bukkit.event.block.BlockBurnEvent event = new org.bukkit.event.block.BlockBurnEvent(burnBlock, sourceBlock);
1397+
+ org.bukkit.event.block.BlockBurnEvent event = new org.bukkit.craftbukkit.event.block.CraftBlockBurnEvent(burnBlock, sourceBlock);
1398+
if (!event.callEvent()) {
1399+
return;
1400+
}
13621401
diff --git a/net/minecraft/world/level/block/FlowerPotBlock.java b/net/minecraft/world/level/block/FlowerPotBlock.java
13631402
index b9cd84b1305cae67762eebde52aeab15ac6f0435..965c72dd4d7ee0afdd1c634cec3d0b0599258d17 100644
13641403
--- a/net/minecraft/world/level/block/FlowerPotBlock.java

paper-server/src/main/java/io/papermc/paper/plugin/bytecode/EventToInterfaceMigration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ public final class EventToInterfaceMigration {
187187
EntityBlockFormEvent.class,
188188
BlockSpreadEvent.class,
189189
BellResonateEvent.class,
190-
BlockBurnEvent.class
190+
BlockBurnEvent.class,
191+
BlockCanBuildEvent.class
191192
//</editor-fold>
192193
);
193194

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.bukkit.craftbukkit.event.block;
2+
3+
import org.bukkit.Material;
4+
import org.bukkit.block.Block;
5+
import org.bukkit.block.data.BlockData;
6+
import org.bukkit.entity.Player;
7+
import org.bukkit.event.HandlerList;
8+
import org.bukkit.event.block.BlockCanBuildEvent;
9+
import org.bukkit.inventory.EquipmentSlot;
10+
import org.jspecify.annotations.Nullable;
11+
12+
public class CraftBlockCanBuildEvent extends CraftBlockEvent implements BlockCanBuildEvent {
13+
14+
private final Player player;
15+
private final EquipmentSlot hand;
16+
protected BlockData blockData;
17+
protected boolean buildable;
18+
19+
public CraftBlockCanBuildEvent(final Block block, final @Nullable Player player, final BlockData blockData, final boolean canBuild, final EquipmentSlot hand) {
20+
super(block);
21+
this.player = player;
22+
this.buildable = canBuild;
23+
this.blockData = blockData;
24+
this.hand = hand;
25+
}
26+
27+
@Override
28+
public @Nullable Player getPlayer() {
29+
return this.player;
30+
}
31+
32+
@Override
33+
public Material getMaterial() {
34+
return this.blockData.getMaterial();
35+
}
36+
37+
@Override
38+
public BlockData getBlockData() {
39+
return this.blockData.clone();
40+
}
41+
42+
@Override
43+
public EquipmentSlot getHand() {
44+
return this.hand;
45+
}
46+
47+
@Override
48+
public boolean isBuildable() {
49+
return this.buildable;
50+
}
51+
52+
@Override
53+
public void setBuildable(final boolean buildable) {
54+
this.buildable = buildable;
55+
}
56+
57+
@Override
58+
public HandlerList getHandlers() {
59+
return BlockCanBuildEvent.getHandlerList();
60+
}
61+
}

0 commit comments

Comments
 (0)