4040import java .util .concurrent .ConcurrentHashMap ;
4141import java .util .stream .Collectors ;
4242
43+ import org .bukkit .event .block .BlockBreakEvent ;
44+ import org .bukkit .event .block .BlockPlaceEvent ;
45+ import org .bukkit .event .block .BlockPhysicsEvent ;
46+ import org .bukkit .event .EventPriority ;
47+
48+ import org .bukkit .ChunkSnapshot ;
49+ import org .bukkit .Chunk ;
50+
4351public class TuffX extends JavaPlugin implements Listener , PluginMessageListener , TabCompleter {
4452
4553 public static final String CHANNEL = "eagler:below_y0" ;
@@ -224,9 +232,14 @@ private void handleInteraction(Player player, Location loc, String action) {
224232
225233 switch (action .toLowerCase ()) {
226234 case "break" :
227- getLogger (). info ( "breaking block at " + loc . getX ()+ "," + loc . getY ()+ "," + loc . getZ () );
235+ ItemStack tool = player . getInventory (). getItemInMainHand ( );
228236 if (player .getGameMode () == GameMode .SURVIVAL ) {
229- block .breakNaturally ();
237+ BlockBreakEvent breakEvent = new BlockBreakEvent (block , player );
238+ getServer ().getPluginManager ().callEvent (breakEvent );
239+
240+ if (!breakEvent .isCancelled ()) {
241+ block .breakNaturally (player .getInventory ().getItemInMainHand ());
242+ }
230243 } else {
231244 block .setType (Material .AIR );
232245 }
@@ -290,7 +303,7 @@ private byte[] createWelcomePayload(String message, int someNumber) {
290303 }
291304 }
292305
293- private byte [] createSectionPayload (World world , int cx , int cz , int sectionY ) throws IOException {
306+ /* private byte[] createSectionPayload(World world, int cx, int cz, int sectionY) throws IOException {
294307 try (ByteArrayOutputStream bout = new ByteArrayOutputStream(8200); DataOutputStream out = new DataOutputStream(bout)) {
295308 out.writeUTF("chunk_data");
296309 out.writeInt(cx);
@@ -312,6 +325,37 @@ private byte[] createSectionPayload(World world, int cx, int cz, int sectionY) t
312325 }
313326 return bout.toByteArray();
314327 }
328+ }*/
329+
330+ private byte [] createSectionPayload (World world , int cx , int cz , int sectionY ) throws IOException {
331+ Chunk chunk = world .getChunkAt (cx , cz );
332+ ChunkSnapshot snapshot = chunk .getChunkSnapshot (true , false , false ); // avoid lighting overhead
333+
334+ try (ByteArrayOutputStream bout = new ByteArrayOutputStream (8200 );
335+ DataOutputStream out = new DataOutputStream (bout )) {
336+
337+ out .writeUTF ("chunk_data" );
338+ out .writeInt (cx );
339+ out .writeInt (cz );
340+ out .writeInt (sectionY );
341+
342+ int baseY = sectionY * 16 ;
343+ for (int y = 0 ; y < 16 ; y ++) {
344+ for (int z = 0 ; z < 16 ; z ++) {
345+ for (int x = 0 ; x < 16 ; x ++) {
346+ int worldY = baseY + y ;
347+
348+ Material type ;
349+ type = snapshot .getBlockType (x , worldY , z );
350+ if (type == null ) type = Material .AIR ;
351+
352+ short legacyId = LegacyBlockIdManager .getLegacyShort (type );
353+ out .writeShort (legacyId );
354+ }
355+ }
356+ }
357+ return bout .toByteArray ();
358+ }
315359 }
316360
317361 @ EventHandler (priority = EventPriority .HIGHEST )
@@ -416,4 +460,44 @@ public void run() {
416460
417461 private record ChunkSectionKey (UUID playerId , String worldName , int cx , int cz , int sectionY ) {
418462 }
463+
464+
465+
466+ @ EventHandler (priority = EventPriority .MONITOR , ignoreCancelled = true )
467+ public void onBlockBreak (BlockBreakEvent event ) {
468+ Block block = event .getBlock ();
469+ sendBlockUpdateToNearby (block .getLocation (), Material .AIR );
470+ //sendChunkSectionIfBelowY0(event.getPlayer(), block);
471+ }
472+
473+ @ EventHandler (priority = EventPriority .MONITOR , ignoreCancelled = true )
474+ public void onBlockPlace (BlockPlaceEvent event ) {
475+ Block block = event .getBlock ();
476+ sendBlockUpdateToNearby (block .getLocation (), block .getType ());
477+ //sendChunkSectionIfBelowY0(event.getPlayer(), block);
478+ }
479+
480+ @ EventHandler (priority = EventPriority .MONITOR , ignoreCancelled = true )
481+ public void onBlockPhysics (BlockPhysicsEvent event ) {
482+ Block block = event .getBlock ();
483+ sendBlockUpdateToNearby (block .getLocation (), block .getType ());
484+ //sendChunkSectionIfBelowY0(null, block);
485+ }
486+
487+ private void sendChunkSectionIfBelowY0 (Player player , Block block ) {
488+ if (block .getY () >= 0 ) return ;
489+
490+ int sectionY = block .getY () >> 4 ;
491+ Chunk chunk = block .getChunk ();
492+
493+ if (player != null ) {
494+ sendChunkSectionsAsync (player , chunk );
495+ } else {
496+ for (Player p : chunk .getWorld ().getPlayers ()) {
497+ if (isPlayerInChunkRange (p , chunk , getServer ().getViewDistance ())) {
498+ sendChunkSectionsAsync (p , chunk );
499+ }
500+ }
501+ }
502+ }
419503}
0 commit comments