1212import net .minecraft .world .InteractionHand ;
1313import net .minecraft .world .entity .ai .village .poi .PoiType ;
1414import net .minecraft .world .entity .ai .village .poi .PoiTypes ;
15- import net .minecraft .world .item .ItemStack ;
1615import net .minecraft .world .level .block .Block ;
1716import net .minecraft .world .level .block .EntityBlock ;
1817import net .minecraft .world .level .block .entity .BlockEntity ;
2625import org .bukkit .block .BlockFace ;
2726import org .bukkit .craftbukkit .v1_20_R2 .block .CraftBlock ;
2827import org .bukkit .craftbukkit .v1_20_R2 .entity .CraftPlayer ;
28+ import org .bukkit .craftbukkit .v1_20_R2 .event .CraftEventFactory ;
2929import org .bukkit .entity .Player ;
3030import org .bukkit .event .block .Action ;
31+ import org .bukkit .event .block .BlockBreakEvent ;
3132import org .bukkit .event .player .PlayerInteractEvent ;
3233import org .bukkit .plugin .messaging .PluginMessageListener ;
3334import org .jetbrains .annotations .NotNull ;
3839import java .util .Map ;
3940import java .util .Objects ;
4041import java .util .Optional ;
41- import java .util .logging .Level ;
4242
4343public class SetBlockPacketListener implements PluginMessageListener {
4444
@@ -62,9 +62,8 @@ public SetBlockPacketListener(AxiomPaper plugin) {
6262
6363 @ Override
6464 public void onPluginMessageReceived (@ NotNull String channel , @ NotNull Player bukkitPlayer , @ NotNull byte [] message ) {
65- if (!bukkitPlayer .hasPermission ("axiom.*" )) {
65+ if (!bukkitPlayer .hasPermission ("axiom.*" ))
6666 return ;
67- }
6867
6968 // Check if player is allowed to modify this world
7069 AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent (bukkitPlayer , bukkitPlayer .getWorld ());
@@ -82,7 +81,9 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
8281 InteractionHand hand = friendlyByteBuf .readEnum (InteractionHand .class );
8382 int sequenceId = friendlyByteBuf .readVarInt ();
8483
85- ServerPlayer player = ((CraftPlayer )bukkitPlayer ).getHandle ();
84+ ServerPlayer player = ((CraftPlayer ) bukkitPlayer ).getHandle ();
85+
86+ Action interactAction = breaking ? Action .LEFT_CLICK_BLOCK : Action .RIGHT_CLICK_BLOCK ;
8687
8788 org .bukkit .inventory .ItemStack heldItem ;
8889 if (hand == InteractionHand .MAIN_HAND ) {
@@ -92,21 +93,33 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
9293 }
9394
9495 org .bukkit .block .Block blockClicked = bukkitPlayer .getWorld ().getBlockAt (blockHit .getBlockPos ().getX (),
95- blockHit .getBlockPos ().getY (), blockHit .getBlockPos ().getZ ());
96+ blockHit .getBlockPos ().getY (), blockHit .getBlockPos ().getZ ());
9697
9798 BlockFace blockFace = CraftBlock .notchToBlockFace (blockHit .getDirection ());
9899
99100 // Call interact event
100- PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent (bukkitPlayer ,
101- breaking ? Action .LEFT_CLICK_BLOCK : Action .RIGHT_CLICK_BLOCK , heldItem , blockClicked , blockFace );
102- if (!playerInteractEvent .callEvent ()) {
103- if (sequenceId >= 0 ) {
104- player .connection .ackBlockChangesUpTo (sequenceId );
101+ if (new PlayerInteractEvent (bukkitPlayer , interactAction , heldItem , blockClicked , blockFace ).callEvent ()) {
102+ updateBlocks (player , updateNeighbors , blocks );
103+
104+ org .bukkit .block .Block bukkitBlock = bukkitPlayer .getWorld ().getBlockAt (blockClicked .getX (), blockClicked .getY (), blockClicked .getZ ());
105+
106+ boolean cancelled ;
107+ if (interactAction .isLeftClick ()) {
108+ cancelled = !new BlockBreakEvent (bukkitBlock , bukkitPlayer ).callEvent ();
109+ } else {
110+ cancelled = CraftEventFactory .callBlockPlaceEvent (player .serverLevel (), player , player .getUsedItemHand (), bukkitBlock .getState (), blockClicked .getX (), blockClicked .getY (), blockClicked .getZ ()).isCancelled ();
105111 }
106- return ;
112+
113+ if (cancelled )
114+ updateBlocks (player , updateNeighbors , blocks );
115+ }
116+
117+ if (sequenceId >= 0 ) {
118+ player .connection .ackBlockChangesUpTo (sequenceId );
107119 }
120+ }
108121
109- // Update blocks
122+ private void updateBlocks ( ServerPlayer player , boolean updateNeighbors , Map < BlockPos , BlockState > blocks ) {
110123 if (updateNeighbors ) {
111124 for (Map .Entry <BlockPos , BlockState > entry : blocks .entrySet ()) {
112125 player .level ().setBlock (entry .getKey (), entry .getValue (), 3 );
@@ -143,7 +156,8 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
143156 case OCEAN_FLOOR -> oceanFloor = heightmap .getValue ();
144157 case MOTION_BLOCKING -> motionBlocking = heightmap .getValue ();
145158 case MOTION_BLOCKING_NO_LEAVES -> motionBlockingNoLeaves = heightmap .getValue ();
146- default -> {}
159+ default -> {
160+ }
147161 }
148162 }
149163
@@ -160,7 +174,7 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
160174
161175 if (blockEntity == null ) {
162176 // There isn't a block entity here, create it!
163- blockEntity = ((EntityBlock )block ).newBlockEntity (blockPos , blockState );
177+ blockEntity = ((EntityBlock ) block ).newBlockEntity (blockPos , blockState );
164178 if (blockEntity != null ) {
165179 chunk .addAndRegisterBlockEntity (blockEntity );
166180 }
@@ -178,7 +192,7 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
178192 // Block entity type isn't correct, we need to recreate it
179193 chunk .removeBlockEntity (blockPos );
180194
181- blockEntity = ((EntityBlock )block ).newBlockEntity (blockPos , blockState );
195+ blockEntity = ((EntityBlock ) block ).newBlockEntity (blockPos , blockState );
182196 if (blockEntity != null ) {
183197 chunk .addAndRegisterBlockEntity (blockEntity );
184198 }
@@ -201,7 +215,7 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
201215 Optional <Holder <PoiType >> oldPoi = PoiTypes .forState (old );
202216 if (!Objects .equals (oldPoi , newPoi )) {
203217 if (oldPoi .isPresent ()) level .getPoiManager ().remove (blockPos );
204- if ( newPoi .isPresent ()) level .getPoiManager ().add (blockPos , newPoi .get ());
218+ newPoi .ifPresent ( poiTypeHolder -> level .getPoiManager ().add (blockPos , newPoi .get () ));
205219 }
206220 }
207221
@@ -211,10 +225,6 @@ public void onPluginMessageReceived(@NotNull String channel, @NotNull Player buk
211225 }
212226 }
213227 }
214-
215- if (sequenceId >= 0 ) {
216- player .connection .ackBlockChangesUpTo (sequenceId );
217- }
218228 }
219229
220230}
0 commit comments