Advertisement
broken-arrow

Untitled

Nov 9th, 2021
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1.     @EventHandler
  2.     public void onBlockBreak(BlockBreakEvent event) {
  3.         if (!LWC.ENABLED || event.isCancelled()) {
  4.             return;
  5.         }
  6.  
  7.         LWC lwc = plugin.getLWC();
  8.         Player player = event.getPlayer();
  9.         Block block = event.getBlock();
  10.  
  11.         boolean ignoreBlockDestruction = Boolean
  12.                 .parseBoolean(lwc.resolveProtectionConfiguration(block, "ignoreBlockDestruction"));
  13.  
  14.         if (ignoreBlockDestruction) {
  15.             return;
  16.         }
  17.  
  18.         ProtectionCache cache = lwc.getProtectionCache();
  19.         String cacheKey = cache.cacheKey(block.getLocation());
  20.  
  21.         // In the event they place a block, remove any known nulls there
  22.         if (cache.isKnownNull(cacheKey)) {
  23.             cache.remove(cacheKey);
  24.         }
  25.  
  26.         Protection protection = lwc.findProtection(block.getLocation());
  27.  
  28.         if (protection == null) {
  29.             return;
  30.         }
  31.  
  32.         boolean canAccess = lwc.canAccessProtection(player, protection);
  33.         boolean canAdmin = lwc.canAdminProtection(player, protection);
  34.        
  35.         // when destroying a chest, it's possible they are also destroying a
  36.         // double chest
  37.         // in the event they're trying to destroy a double chest, we should just
  38.         // move
  39.         // the protection to the chest that is not destroyed, if it is not that
  40.         // one already.
  41.         if (protection.isOwner(player) && DoubleChestMatcher.PROTECTABLES_CHESTS.contains(block.getType())) {
  42.             Block doubleChest = lwc.findAdjacentDoubleChest(block);
  43.  
  44.             if (doubleChest != null) {
  45.                 // if they destroyed the protected block we want to move it aye?
  46.                 if (lwc.blockEquals(protection.getBlock(), block)) {
  47.                     // correct the block
  48.                     BlockCache blockCache = BlockCache.getInstance();
  49.                     protection.setBlockId(blockCache.getBlockId(doubleChest));
  50.                     protection.setX(doubleChest.getX());
  51.                     protection.setY(doubleChest.getY());
  52.                     protection.setZ(doubleChest.getZ());
  53.                     protection.saveNow();
  54.                 }
  55.  
  56.                 // Repair the cache
  57.                 protection.radiusRemoveCache();
  58.  
  59.                 if (protection.getProtectionFinder() != null) {
  60.                     protection.getProtectionFinder().removeBlock(block.getState());
  61.                 }
  62.  
  63.                 lwc.getProtectionCache().addProtection(protection);
  64.  
  65.                 return;
  66.             }
  67.         }
  68.  
  69.         try {
  70.             LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection,
  71.                     LWCProtectionDestroyEvent.Method.BLOCK_DESTRUCTION, canAccess, canAdmin);
  72.             lwc.getModuleLoader().dispatchEvent(evt);
  73.            
  74.             if ((evt.isCancelled() || !canAccess) && !plugin.getLWC().hasPermission(player, "lwc.force.destroy.block")) {
  75.                 event.setCancelled(true);
  76.             }
  77.         } catch (Exception e) {
  78.             event.setCancelled(true);
  79.             lwc.sendLocale(player, "protection.internalerror", "id", "BLOCK_BREAK");
  80.             e.printStackTrace();
  81.         }
  82.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement