Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @EventHandler (priority = EventPriority.LOW, ignoreCancelled = true)
- public void onBlockBreak(BlockBreakEvent event){
- Block block = event.getBlock();
- if(plugin.getHookManager().checkForSignProtection(block)){
- return; // Let them deal with it
- }
- Player player = event.getPlayer();
- Location blockLocation = block.getLocation();
- Location playerLocation = player.getLocation();
- GameMode blockGamemode = null, attachedGamemode = null;
- boolean isRegion = false, isAttached = false, isGamemode = false;
- boolean illegal = false;
- InteractionSettings interaction = null;
- Material attachedMaterial = Material.AIR;
- ASConfig c = configFor(blockLocation);
- if(c.blockBreak.contains(block.getType())){
- illegal = true;
- }
- if(!plugin.isBlocked(player, PermissionNodes.ALLOW_BLOCK_BREAK, PermissionNodes.DENY_BLOCK_BREAK, block.getType())){
- illegal = false;
- }
- if(!player.hasPermission(PermissionNodes.FREE_PLACE)){
- blockGamemode = plugin.getBlockManager().getType(block);
- if(blockGamemode != null){
- if(!GamemodeAbstraction.isMatch(blockGamemode, player.getGameMode())){
- isGamemode = true;
- interaction = configFor(player.getGameMode(), blockGamemode, blockLocation);
- }else if(!c.naturalSettings.allowMismatchedGM){
- for(BlockFace face : ASUtils.TRUE_BLOCK_FACES){
- Block rel = block.getRelative(face);
- if(MaterialAPI.isDroppedOnBreak(rel, block, true)){
- attachedGamemode = plugin.getBlockManager().getType(rel);
- if(attachedGamemode != null){
- if(!GamemodeAbstraction.isMatch(blockGamemode, attachedGamemode)){
- isAttached = true;
- interaction = configFor(player.getGameMode(), attachedGamemode, blockLocation);
- break;
- }
- }
- }
- }
- }
- }
- }
- Region playerRegion = plugin.getRegionManager().getRegion(player.getLocation());
- if(!player.hasPermission(PermissionNodes.REGION_BREAK)){
- Region blockRegion = plugin.getRegionManager().getRegion(block.getLocation());
- if(playerRegion != blockRegion){
- isRegion = true;
- illegal = true;
- }
- }
- if(!illegal && interaction != null){
- illegal = interaction.deny;
- }
- if(illegal){
- event.setCancelled(true);
- }else{
- plugin.getBlockManager().removeBlock(block);
- }
- Action action = Action.BLOCK_BREAK;
- String[] extras = null;
- if(isRegion){
- action = Action.REGION_BLOCK_BREAK;
- extras = new String[] {playerRegion.getName()};
- }else{
- if(isGamemode || isAttached){
- action = Action.GAMEMODE_BLOCK_BREAK;
- extras = new String[] {(isGamemode ? blockGamemode : attachedGamemode).name()};
- }
- }
- plugin.getMessages().notifyParties(player, action, illegal, isAttached ? attachedMaterial : block.getType(), extras);
- if(interaction != null && !interaction.deny){
- block.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
- plugin.getHookManager().sendBlockBreak(player.getName(), block.getLocation(), block.getType(), block.getData());
- plugin.getBlockManager().removeBlock(block);
- if(interaction.drop){
- block.breakNaturally();
- }else{
- block.setType(Material.AIR);
- }
- }
- if(event.isCancelled() || player.hasPermission(PermissionNodes.BREAK_ANYTHING)){
- return;
- }
- if(GamemodeAbstraction.isCreative(player.getGameMode())){
- if(c.naturalSettings.emptyInventories){
- if(block.getState() instanceof Chest){
- Chest state = (Chest) block.getState();
- state.getBlockInventory().clear();
- }else if(block.getState() instanceof Jukebox){
- Jukebox state = (Jukebox) block.getState();
- state.setPlaying(null);
- }else if(block.getState() instanceof Furnace){
- Furnace state = (Furnace) block.getState();
- state.getInventory().clear();
- }else if(block.getState() instanceof BrewingStand){
- BrewingStand state = (BrewingStand) block.getState();
- state.getInventory().clear();
- }
- // TODO: Verify that this is it
- }
- }
- if(c.naturalSettings.removeAttached){
- for(Entity e : block.getChunk().getEntities()){
- if(e instanceof ItemFrame){
- double d2 = e.getLocation().distanceSquared(block.getLocation());
- if(d2 < 1.65 && d2 > 1.6 || d2 > 0.5 && d2 < 0.51){
- plugin.getHookManager().sendEntityBreak(player.getName(), e.getLocation(), Material.ITEM_FRAME, BlockLogger.DEFAULT_DATA);
- e.remove();
- }
- }
- }
- for(BlockFace face : ASUtils.TRUE_BLOCK_FACES){
- Block rel = block.getRelative(face);
- if(MaterialAPI.isDroppedOnBreak(rel, block, true)){
- rel.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
- plugin.getHookManager().sendBlockBreak(player.getName(), rel.getLocation(), rel.getType(), rel.getData());
- breakBlock(rel, player.getGameMode(), c, true);
- }
- }
- // Check for falling sand/gravel exploit
- boolean moreBlocks = true;
- Block active = block;
- if(MaterialAPI.isAffectedByGravity(active.getType()) || MaterialAPI.isAffectedByGravity(active.getRelative(BlockFace.UP).getType())){
- do{
- Block below = active.getRelative(BlockFace.DOWN);
- active = below;
- if(below.getType() == Material.AIR){
- continue;
- }
- if(MaterialAPI.canBreakFallingBlock(below.getType())){
- // Remove all sand/gravel above this block
- boolean checkMoreBlocks = true;
- Block above = block.getRelative(BlockFace.UP);
- do{
- for(BlockFace face : ASUtils.TRUE_BLOCK_FACES){
- Block side = above.getRelative(face);
- if(MaterialAPI.isDroppedOnBreak(side, above, true)){
- side.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
- plugin.getHookManager().sendBlockBreak(player.getName(), side.getLocation(), side.getType(), side.getData());
- breakBlock(side, player.getGameMode(), c, true);
- }
- }
- if(MaterialAPI.isAffectedByGravity(above.getType())){
- above.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
- plugin.getHookManager().sendBlockBreak(player.getName(), above.getLocation(), above.getType(), above.getData());
- breakBlock(above, player.getGameMode(), c, true);
- above = above.getRelative(BlockFace.UP);
- }else{
- checkMoreBlocks = false;
- }
- }while (checkMoreBlocks);
- moreBlocks = false;
- }else{
- moreBlocks = false;
- }
- }while (moreBlocks);
- }
- // Cacti/Sugar Cane check
- active = block;
- if(block.getType() == Material.CACTUS || block.getType() == Material.SUGAR_CANE_BLOCK){
- moreBlocks = true;
- List<Location> breakBlocks = new ArrayList<Location>();
- do{
- Block above = active.getRelative(BlockFace.UP);
- if(above.getType() == Material.CACTUS || above.getType() == Material.SUGAR_CANE_BLOCK){
- breakBlocks.add(above.getLocation());
- }else{
- moreBlocks = false;
- }
- active = above;
- }while (moreBlocks);
- for(int i = breakBlocks.size() - 1; i > -1; i--){
- Location location = breakBlocks.get(i);
- Block toBreak = location.getBlock();
- toBreak.setMetadata(LOGBLOCK_METADATA_KEY, EMPTY_METADATA);
- plugin.getHookManager().sendBlockBreak(player.getName(), location, toBreak.getType(), toBreak.getData());
- breakBlock(toBreak, player.getGameMode(), c, true);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment