turt2live

Untitled

Feb 21st, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1.     @EventHandler (priority = EventPriority.LOW, ignoreCancelled = true)
  2.     public void onBlockBreak(BlockBreakEvent event){
  3.         Block block = event.getBlock();
  4.         if(plugin.getHookManager().checkForSignProtection(block)){
  5.             return; // Let them deal with it
  6.         }
  7.         Player player = event.getPlayer();
  8.         Location blockLocation = block.getLocation();
  9.         Location playerLocation = player.getLocation();
  10.         GameMode blockGamemode = null, attachedGamemode = null;
  11.         boolean isRegion = false, isAttached = false, isGamemode = false;
  12.         boolean illegal = false;
  13.         InteractionSettings interaction = null;
  14.         Material attachedMaterial = Material.AIR;
  15.         ASConfig c = configFor(blockLocation);
  16.  
  17.         if(c.blockBreak.contains(block.getType())){
  18.             illegal = true;
  19.         }
  20.         if(!plugin.isBlocked(player, PermissionNodes.ALLOW_BLOCK_BREAK, PermissionNodes.DENY_BLOCK_BREAK, block.getType())){
  21.             illegal = false;
  22.         }
  23.  
  24.         if(!player.hasPermission(PermissionNodes.FREE_PLACE)){
  25.             blockGamemode = plugin.getBlockManager().getType(block);
  26.             if(blockGamemode != null){
  27.                 if(!GamemodeAbstraction.isMatch(blockGamemode, player.getGameMode())){
  28.                     isGamemode = true;
  29.                     interaction = configFor(player.getGameMode(), blockGamemode, blockLocation);
  30.                 }else if(!c.naturalSettings.allowMismatchedGM){
  31.                     for(BlockFace face : ASUtils.TRUE_BLOCK_FACES){
  32.                         Block rel = block.getRelative(face);
  33.                         if(MaterialAPI.isDroppedOnBreak(rel, block, true)){
  34.                             attachedGamemode = plugin.getBlockManager().getType(rel);
  35.                             if(attachedGamemode != null){
  36.                                 if(!GamemodeAbstraction.isMatch(blockGamemode, attachedGamemode)){
  37.                                     isAttached = true;
  38.                                     interaction = configFor(player.getGameMode(), attachedGamemode, blockLocation);
  39.                                     break;
  40.                                 }
  41.                             }
  42.                         }
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.  
  48.         Region playerRegion = plugin.getRegionManager().getRegion(player.getLocation());
  49.         if(!player.hasPermission(PermissionNodes.REGION_BREAK)){
  50.             Region blockRegion = plugin.getRegionManager().getRegion(block.getLocation());
  51.             if(playerRegion != blockRegion){
  52.                 isRegion = true;
  53.                 illegal = true;
  54.             }
  55.         }
  56.  
  57.         if(!illegal && interaction != null){
  58.             illegal = interaction.deny;
  59.         }
  60.         if(illegal){
  61.             event.setCancelled(true);
  62.         }else{
  63.             plugin.getBlockManager().removeBlock(block);
  64.         }
  65.  
  66.         Action action = Action.BLOCK_BREAK;
  67.         String[] extras = null;
  68.         if(isRegion){
  69.             action = Action.REGION_BLOCK_BREAK;
  70.             extras = new String[] {playerRegion.getName()};
  71.         }else{
  72.             if(isGamemode || isAttached){
  73.                 action = Action.GAMEMODE_BLOCK_BREAK;
  74.                 extras = new String[] {(isGamemode ? blockGamemode : attachedGamemode).name()};
  75.             }
  76.         }
  77.         plugin.getMessages().notifyParties(player, action, illegal, isAttached ? attachedMaterial : block.getType(), extras);
  78.  
  79.         if(interaction != null && !interaction.deny){
  80.             block.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
  81.             plugin.getHookManager().sendBlockBreak(player.getName(), block.getLocation(), block.getType(), block.getData());
  82.             plugin.getBlockManager().removeBlock(block);
  83.             if(interaction.drop){
  84.                 block.breakNaturally();
  85.             }else{
  86.                 block.setType(Material.AIR);
  87.             }
  88.         }
  89.  
  90.         if(event.isCancelled() || player.hasPermission(PermissionNodes.BREAK_ANYTHING)){
  91.             return;
  92.         }
  93.  
  94.         if(GamemodeAbstraction.isCreative(player.getGameMode())){
  95.             if(c.naturalSettings.emptyInventories){
  96.                 if(block.getState() instanceof Chest){
  97.                     Chest state = (Chest) block.getState();
  98.                     state.getBlockInventory().clear();
  99.                 }else if(block.getState() instanceof Jukebox){
  100.                     Jukebox state = (Jukebox) block.getState();
  101.                     state.setPlaying(null);
  102.                 }else if(block.getState() instanceof Furnace){
  103.                     Furnace state = (Furnace) block.getState();
  104.                     state.getInventory().clear();
  105.                 }else if(block.getState() instanceof BrewingStand){
  106.                     BrewingStand state = (BrewingStand) block.getState();
  107.                     state.getInventory().clear();
  108.                 }
  109.                 // TODO: Verify that this is it
  110.             }
  111.         }
  112.  
  113.         if(c.naturalSettings.removeAttached){
  114.             for(Entity e : block.getChunk().getEntities()){
  115.                 if(e instanceof ItemFrame){
  116.                     double d2 = e.getLocation().distanceSquared(block.getLocation());
  117.                     if(d2 < 1.65 && d2 > 1.6 || d2 > 0.5 && d2 < 0.51){
  118.                         plugin.getHookManager().sendEntityBreak(player.getName(), e.getLocation(), Material.ITEM_FRAME, BlockLogger.DEFAULT_DATA);
  119.                         e.remove();
  120.                     }
  121.                 }
  122.             }
  123.             for(BlockFace face : ASUtils.TRUE_BLOCK_FACES){
  124.                 Block rel = block.getRelative(face);
  125.                 if(MaterialAPI.isDroppedOnBreak(rel, block, true)){
  126.                     rel.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
  127.                     plugin.getHookManager().sendBlockBreak(player.getName(), rel.getLocation(), rel.getType(), rel.getData());
  128.                     breakBlock(rel, player.getGameMode(), c, true);
  129.                 }
  130.             }
  131.  
  132.             // Check for falling sand/gravel exploit
  133.             boolean moreBlocks = true;
  134.             Block active = block;
  135.             if(MaterialAPI.isAffectedByGravity(active.getType()) || MaterialAPI.isAffectedByGravity(active.getRelative(BlockFace.UP).getType())){
  136.                 do{
  137.                     Block below = active.getRelative(BlockFace.DOWN);
  138.                     active = below;
  139.                     if(below.getType() == Material.AIR){
  140.                         continue;
  141.                     }
  142.                     if(MaterialAPI.canBreakFallingBlock(below.getType())){
  143.                         // Remove all sand/gravel above this block
  144.                         boolean checkMoreBlocks = true;
  145.                         Block above = block.getRelative(BlockFace.UP);
  146.                         do{
  147.                             for(BlockFace face : ASUtils.TRUE_BLOCK_FACES){
  148.                                 Block side = above.getRelative(face);
  149.                                 if(MaterialAPI.isDroppedOnBreak(side, above, true)){
  150.                                     side.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
  151.                                     plugin.getHookManager().sendBlockBreak(player.getName(), side.getLocation(), side.getType(), side.getData());
  152.                                     breakBlock(side, player.getGameMode(), c, true);
  153.                                 }
  154.                             }
  155.                             if(MaterialAPI.isAffectedByGravity(above.getType())){
  156.                                 above.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
  157.                                 plugin.getHookManager().sendBlockBreak(player.getName(), above.getLocation(), above.getType(), above.getData());
  158.                                 breakBlock(above, player.getGameMode(), c, true);
  159.                                 above = above.getRelative(BlockFace.UP);
  160.                             }else{
  161.                                 checkMoreBlocks = false;
  162.                             }
  163.                         }while (checkMoreBlocks);
  164.                         moreBlocks = false;
  165.                     }else{
  166.                         moreBlocks = false;
  167.                     }
  168.                 }while (moreBlocks);
  169.             }
  170.  
  171.             // Cacti/Sugar Cane check
  172.             active = block;
  173.             if(block.getType() == Material.CACTUS || block.getType() == Material.SUGAR_CANE_BLOCK){
  174.                 moreBlocks = true;
  175.                 List<Location> breakBlocks = new ArrayList<Location>();
  176.                 do{
  177.                     Block above = active.getRelative(BlockFace.UP);
  178.                     if(above.getType() == Material.CACTUS || above.getType() == Material.SUGAR_CANE_BLOCK){
  179.                         breakBlocks.add(above.getLocation());
  180.                     }else{
  181.                         moreBlocks = false;
  182.                     }
  183.                     active = above;
  184.                 }while (moreBlocks);
  185.                 for(int i = breakBlocks.size() - 1; i > -1; i--){
  186.                     Location location = breakBlocks.get(i);
  187.                     Block toBreak = location.getBlock();
  188.                     toBreak.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
  189.                     plugin.getHookManager().sendBlockBreak(player.getName(), location, toBreak.getType(), toBreak.getData());
  190.                     breakBlock(toBreak, player.getGameMode(), c, true);
  191.                 }
  192.             }
  193.         }
  194.     }
Advertisement
Add Comment
Please, Sign In to add comment