2626import org .bukkit .block .BlockFace ;
2727import org .bukkit .craftbukkit .v1_20_R2 .block .CraftBlock ;
2828import org .bukkit .craftbukkit .v1_20_R2 .entity .CraftPlayer ;
29+ import org .bukkit .craftbukkit .v1_20_R2 .event .CraftEventFactory ;
2930import org .bukkit .entity .Player ;
3031import org .bukkit .event .block .Action ;
32+ import org .bukkit .event .block .BlockBreakEvent ;
3133import org .bukkit .event .player .PlayerInteractEvent ;
3234import org .bukkit .plugin .messaging .PluginMessageListener ;
3335import org .jetbrains .annotations .NotNull ;
@@ -83,6 +85,8 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
8385
8486 ServerPlayer player = ((CraftPlayer )bukkitPlayer ).getHandle ();
8587
88+ Action interactAction = breaking ? Action .LEFT_CLICK_BLOCK : Action .RIGHT_CLICK_BLOCK ;
89+
8690 org .bukkit .inventory .ItemStack heldItem ;
8791 if (hand == InteractionHand .MAIN_HAND ) {
8892 heldItem = bukkitPlayer .getInventory ().getItemInMainHand ();
@@ -91,21 +95,33 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
9195 }
9296
9397 org .bukkit .block .Block blockClicked = bukkitPlayer .getWorld ().getBlockAt (blockHit .getBlockPos ().getX (),
94- blockHit .getBlockPos ().getY (), blockHit .getBlockPos ().getZ ());
98+ blockHit .getBlockPos ().getY (), blockHit .getBlockPos ().getZ ());
9599
96100 BlockFace blockFace = CraftBlock .notchToBlockFace (blockHit .getDirection ());
97101
98102 // Call interact event
99- PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent (bukkitPlayer ,
100- breaking ? Action .LEFT_CLICK_BLOCK : Action .RIGHT_CLICK_BLOCK , heldItem , blockClicked , blockFace );
101- if (!playerInteractEvent .callEvent ()) {
102- if (sequenceId >= 0 ) {
103- player .connection .ackBlockChangesUpTo (sequenceId );
103+ if (new PlayerInteractEvent (bukkitPlayer , interactAction , heldItem , blockClicked , blockFace ).callEvent ()) {
104+ updateBlocks (player , updateNeighbors , blocks );
105+
106+ org .bukkit .block .Block bukkitBlock = bukkitPlayer .getWorld ().getBlockAt (blockClicked .getX (), blockClicked .getY (), blockClicked .getZ ());
107+
108+ boolean cancelled ;
109+ if (interactAction .isLeftClick ()) {
110+ cancelled = !new BlockBreakEvent (bukkitBlock , bukkitPlayer ).callEvent ();
111+ } else {
112+ cancelled = CraftEventFactory .callBlockPlaceEvent (player .serverLevel (), player , player .getUsedItemHand (), bukkitBlock .getState (), blockClicked .getX (), blockClicked .getY (), blockClicked .getZ ()).isCancelled ();
104113 }
105- return ;
114+
115+ if (cancelled )
116+ updateBlocks (player , updateNeighbors , blocks );
117+ }
118+
119+ if (sequenceId >= 0 ) {
120+ player .connection .ackBlockChangesUpTo (sequenceId );
106121 }
122+ }
107123
108- // Update blocks
124+ private void updateBlocks ( ServerPlayer player , boolean updateNeighbors , Map < BlockPos , BlockState > blocks ) {
109125 if (updateNeighbors ) {
110126 for (Map .Entry <BlockPos , BlockState > entry : blocks .entrySet ()) {
111127 player .level ().setBlock (entry .getKey (), entry .getValue (), 3 );
@@ -210,10 +226,6 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
210226 }
211227 }
212228 }
213-
214- if (sequenceId >= 0 ) {
215- player .connection .ackBlockChangesUpTo (sequenceId );
216- }
217229 }
218230
219231}
0 commit comments