<commit_before>package bukkit.tommytony.war;

import java.util.List;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockDamageLevel;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;

import com.tommytony.war.Monument;
import com.tommytony.war.Team;
import com.tommytony.war.Warzone;

/**
 * 
 * @author tommytony
 *
 */
public class WarBlockListener extends BlockListener {

	private War war;

	public WarBlockListener(War war) {
		this.war = war;
	}
	
	public void onBlockPlace(BlockPlaceEvent event) {
		Player player = event.getPlayer();
    	Block block = event.getBlock();
    	boolean captured = false;
    	if(player != null && block != null) {
			Team team = war.getPlayerTeam(player.getName()); 
			Warzone zone = war.warzone(player.getLocation());
			if(team != null && block != null && zone != null 
					&& zone.isMonumentCenterBlock(block)
					&& block.getType() == team.getMaterial()) {
				Monument monument = zone.getMonumentFromCenterBlock(block);
				if(monument != null && !monument.hasOwner()) {
					monument.capture(team);
					List<Team> teams = zone.getTeams();
					for(Team t : teams) {
						t.teamcast("Monument " + monument.getName() + " has been captured by team " + team.getName() + ".");
					}
					event.setCancelled(false);
					return; // important otherwise cancelled down a few line by isImportantblock
				} else {
					war.badMsg(player, "You can't capture a monument without a block of your team's material. Get one from your team spawn.");
					event.setCancelled(true);
					return;
				}
			}
			
			if(zone != null && zone.isImportantBlock(block)){
				war.badMsg(player, "Can't build here.");
				event.setCancelled(true);
				return;
			}
			// protect warzone lobbies
	    	for(Warzone wz: war.getWarzones()) {
	    		if(wz.getLobby() != null && wz.getLobby().getVolume().contains(block)) {
	    			war.badMsg(player, "Can't build here.");
		    		event.setCancelled(true);
		    		return;
	    		}
	    	}
	    	// protect the hub
	    	if(war.getWarHub() != null && war.getWarHub().getVolume().contains(block)) {
	    		war.badMsg(player, "Can't build here.");
	    		event.setCancelled(true);
	    		return;
	    	}
	    	
	    	// buildInZonesOnly
	    	if(zone == null && war.isBuildInZonesOnly() && !war.canBuildOutsideZone(player)) {
	    		war.badMsg(player, "You can only build inside warzones. Ask for the 'war.build' permission to build outside.");
	    		event.setCancelled(true);
	    		return;
	    	}
		}
    }
	
//	public void onBlockBreak(BlockBreakEvent event) {
//		Player player = event.getPlayer();
//    	Block block = event.getBlock();
//    	if(player != null && block != null) {
//    		handleBreakOrDamage(player, block, event);
//    	}
//    }
	
    public void onBlockDamage(BlockDamageEvent event) {
    	Player player = event.getPlayer();
    	Block block = event.getBlock();
    	if(player != null && block != null && event.getDamageLevel() == BlockDamageLevel.BROKEN) {
    		handleBreakOrDamage(player,block, event);
	    	
    	}
    }

	private void handleBreakOrDamage(Player player, Block block, Cancellable event) {
		Warzone warzone = war.warzone(player.getLocation());
    	Team team = war.getPlayerTeam(player.getName());
    	boolean isZoneMaker = war.isZoneMaker(player);
    	
    	if(warzone != null && team == null && !isZoneMaker) {
    		// can't actually destroy blocks in a warzone if not part of a team
    		war.badMsg(player, "Can't destroy part of a warzone if you're not in a team.");
    		event.setCancelled(true);
    		return;
    	} else if(team != null && block != null && warzone != null 
				&& warzone.isMonumentCenterBlock(block)){
			Monument monument = warzone.getMonumentFromCenterBlock(block);
			if(monument.hasOwner()) {

				List<Team> teams = warzone.getTeams();
				for(Team t : teams) {
					t.teamcast("Team " + monument.getOwnerTeam().getName() + " loses control of monument " + monument.getName());
				}
				monument.uncapture();
			}
			event.setCancelled(false);
			return;
		}else if(warzone != null && warzone.isImportantBlock(block)) {
    		if(team != null && team.getSpawnVolume().contains(block)) {
    			if(player.getInventory().contains(team.getMaterial())) {
    				war.badMsg(player, "You already have a " + team.getName() + " block.");
    				event.setCancelled(true);
    				return;
    			} else {
    				event.setCancelled(false);		// very important, otherwise could get cancelled but unbreakableZoneBlocks further down
    				return;
    			}
    			// let team members loot one block the spawn for monument captures
    		} else if (team != null && warzone.isEnemyTeamFlagBlock(team, block)) {
    			if(warzone.isFlagThief(player.getName())) {
    				// detect audacious thieves
    				war.badMsg(player, "You can only steal one flag at a time!");
    			} else {
	    			// player just broke the flag block of other team: cancel to avoid drop, give player the block, set block to air
	    			Team lostFlagTeam = warzone.getTeamForFlagBlock(block);
	    			player.getInventory().clear();
	    			player.getInventory().addItem(new ItemStack(lostFlagTeam.getMaterial(), 1));
	    			warzone.addFlagThief(lostFlagTeam, player.getName());
	    			block.setType(Material.AIR);
	    			
	    			for(Team t : warzone.getTeams()) {
						t.teamcast(player.getName() + " stole team " + lostFlagTeam.getName() + "'s flag.");
						if(t.getName().equals(lostFlagTeam.getName())){
							t.teamcast("Prevent " + player.getName() + " from reaching team " + team.getName() + "'s spawn or flag.");
						}
					}
	    			war.msg(player, "You have team " + lostFlagTeam.getName() + "'s flag. Reach your team spawn or flag to capture it!");
    			}
    			event.setCancelled(true);
    			return;
    		} else if (!warzone.isMonumentCenterBlock(block)){
	    		war.badMsg(player, "Can't destroy this.");
	    		event.setCancelled(true);
	    		return;
    		}
    	}
    	
    	// protect warzone lobbies
    	if(block != null) {
	    	for(Warzone zone: war.getWarzones()) {
	    		if(zone.getLobby() != null && 
	    				zone.getLobby().getVolume().contains(block)) {
	    			war.badMsg(player, "Can't destroy this.");
		    		event.setCancelled(true);
		    		return;
	    		}
	    	}
    	}
    	
    	// protect the hub
    	if(war.getWarHub() != null && war.getWarHub().getVolume().contains(block)) {
    		war.badMsg(player, "Can't destroy this.");
    		event.setCancelled(true);
    		return;
    	}
    	
    	// buildInZonesOnly
    	Warzone blockZone = war.warzone(new Location(block.getWorld(), block.getX(), block.getY(), block.getZ()));
    	if(blockZone == null 
    			&& war.isBuildInZonesOnly() 
    			&& !war.canBuildOutsideZone(player)) {
    		war.badMsg(player, "You can only build inside warzones. Ask for the 'war.build' permission to build outside.");
    		event.setCancelled(true);
    		return;
    	}
    	
    	// unbreakableZoneBlocks
    	if(blockZone != null && blockZone.isUnbreakableZoneBlocks() 
    			&& (!isZoneMaker
    					|| (isZoneMaker && team != null)) 
    					) {
    		// if the zone is unbreakable, no one but zone makers can break blocks (even then, zone makers in a team can't break blocks
    		war.badMsg(player, "The blocks in this zone are unbreakable!");
    		event.setCancelled(true);
    		return;
    	}
	}
}
<commit_msg>Made unbreakable zone setting also mean no-build.<commit_after>package bukkit.tommytony.war;

import java.util.List;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockDamageLevel;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.block.BlockDamageEvent;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;

import com.tommytony.war.Monument;
import com.tommytony.war.Team;
import com.tommytony.war.Warzone;

/**
 * 
 * @author tommytony
 *
 */
public class WarBlockListener extends BlockListener {

	private War war;

	public WarBlockListener(War war) {
		this.war = war;
	}
	
	public void onBlockPlace(BlockPlaceEvent event) {
		Player player = event.getPlayer();
    	Block block = event.getBlock();
    	boolean captured = false;
    	if(player != null && block != null) {
			Team team = war.getPlayerTeam(player.getName()); 
			Warzone zone = war.warzone(player.getLocation());
			if(team != null && block != null && zone != null 
					&& zone.isMonumentCenterBlock(block)
					&& block.getType() == team.getMaterial()) {
				Monument monument = zone.getMonumentFromCenterBlock(block);
				if(monument != null && !monument.hasOwner()) {
					monument.capture(team);
					List<Team> teams = zone.getTeams();
					for(Team t : teams) {
						t.teamcast("Monument " + monument.getName() + " has been captured by team " + team.getName() + ".");
					}
					event.setCancelled(false);
					return; // important otherwise cancelled down a few line by isImportantblock
				} else {
					war.badMsg(player, "You can't capture a monument without a block of your team's material. Get one from your team spawn.");
					event.setCancelled(true);
					return;
				}
			}
			
			if(zone != null && zone.isImportantBlock(block)){
				war.badMsg(player, "Can't build here.");
				event.setCancelled(true);
				return;
			}
			// protect warzone lobbies
	    	for(Warzone wz: war.getWarzones()) {
	    		if(wz.getLobby() != null && wz.getLobby().getVolume().contains(block)) {
	    			war.badMsg(player, "Can't build here.");
		    		event.setCancelled(true);
		    		return;
	    		}
	    	}
	    	// protect the hub
	    	if(war.getWarHub() != null && war.getWarHub().getVolume().contains(block)) {
	    		war.badMsg(player, "Can't build here.");
	    		event.setCancelled(true);
	    		return;
	    	}
	    	
	    	// buildInZonesOnly
	    	if(zone == null && war.isBuildInZonesOnly() && !war.canBuildOutsideZone(player)) {
	    		war.badMsg(player, "You can only build inside warzones. Ask for the 'war.build' permission to build outside.");
	    		event.setCancelled(true);
	    		return;
	    	}
	    	
	    	boolean isZoneMaker = war.isZoneMaker(player);
	    	// unbreakableZoneBlocks
	    	if(zone != null && zone.isUnbreakableZoneBlocks() 
	    			&& (!isZoneMaker
	    					|| (isZoneMaker && team != null)) 
	    					) {
	    		// if the zone is unbreakable, no one but zone makers can break blocks (even then, zone makers in a team can't break blocks
	    		war.badMsg(player, "The blocks in this zone are unbreakable - this also means you can't build!");
	    		event.setCancelled(true);
	    		return;
	    	}
		}
    }
	
//	public void onBlockBreak(BlockBreakEvent event) {
//		Player player = event.getPlayer();
//    	Block block = event.getBlock();
//    	if(player != null && block != null) {
//    		handleBreakOrDamage(player, block, event);
//    	}
//    }
	
    public void onBlockDamage(BlockDamageEvent event) {
    	Player player = event.getPlayer();
    	Block block = event.getBlock();
    	if(player != null && block != null && event.getDamageLevel() == BlockDamageLevel.BROKEN) {
    		handleBreakOrDamage(player,block, event);
	    	
    	}
    }

	private void handleBreakOrDamage(Player player, Block block, Cancellable event) {
		Warzone warzone = war.warzone(player.getLocation());
    	Team team = war.getPlayerTeam(player.getName());
    	boolean isZoneMaker = war.isZoneMaker(player);
    	
    	if(warzone != null && team == null && !isZoneMaker) {
    		// can't actually destroy blocks in a warzone if not part of a team
    		war.badMsg(player, "Can't destroy part of a warzone if you're not in a team.");
    		event.setCancelled(true);
    		return;
    	} else if(team != null && block != null && warzone != null 
				&& warzone.isMonumentCenterBlock(block)){
			Monument monument = warzone.getMonumentFromCenterBlock(block);
			if(monument.hasOwner()) {

				List<Team> teams = warzone.getTeams();
				for(Team t : teams) {
					t.teamcast("Team " + monument.getOwnerTeam().getName() + " loses control of monument " + monument.getName());
				}
				monument.uncapture();
			}
			event.setCancelled(false);
			return;
		}else if(warzone != null && warzone.isImportantBlock(block)) {
    		if(team != null && team.getSpawnVolume().contains(block)) {
    			if(player.getInventory().contains(team.getMaterial())) {
    				war.badMsg(player, "You already have a " + team.getName() + " block.");
    				event.setCancelled(true);
    				return;
    			} else {
    				event.setCancelled(false);		// very important, otherwise could get cancelled but unbreakableZoneBlocks further down
    				return;
    			}
    			// let team members loot one block the spawn for monument captures
    		} else if (team != null && warzone.isEnemyTeamFlagBlock(team, block)) {
    			if(warzone.isFlagThief(player.getName())) {
    				// detect audacious thieves
    				war.badMsg(player, "You can only steal one flag at a time!");
    			} else {
	    			// player just broke the flag block of other team: cancel to avoid drop, give player the block, set block to air
	    			Team lostFlagTeam = warzone.getTeamForFlagBlock(block);
	    			player.getInventory().clear();
	    			player.getInventory().addItem(new ItemStack(lostFlagTeam.getMaterial(), 1));
	    			warzone.addFlagThief(lostFlagTeam, player.getName());
	    			block.setType(Material.AIR);
	    			
	    			for(Team t : warzone.getTeams()) {
						t.teamcast(player.getName() + " stole team " + lostFlagTeam.getName() + "'s flag.");
						if(t.getName().equals(lostFlagTeam.getName())){
							t.teamcast("Prevent " + player.getName() + " from reaching team " + team.getName() + "'s spawn or flag.");
						}
					}
	    			war.msg(player, "You have team " + lostFlagTeam.getName() + "'s flag. Reach your team spawn or flag to capture it!");
    			}
    			event.setCancelled(true);
    			return;
    		} else if (!warzone.isMonumentCenterBlock(block)){
	    		war.badMsg(player, "Can't destroy this.");
	    		event.setCancelled(true);
	    		return;
    		}
    	}
    	
    	// protect warzone lobbies
    	if(block != null) {
	    	for(Warzone zone: war.getWarzones()) {
	    		if(zone.getLobby() != null && 
	    				zone.getLobby().getVolume().contains(block)) {
	    			war.badMsg(player, "Can't destroy this.");
		    		event.setCancelled(true);
		    		return;
	    		}
	    	}
    	}
    	
    	// protect the hub
    	if(war.getWarHub() != null && war.getWarHub().getVolume().contains(block)) {
    		war.badMsg(player, "Can't destroy this.");
    		event.setCancelled(true);
    		return;
    	}
    	
    	// buildInZonesOnly
    	Warzone blockZone = war.warzone(new Location(block.getWorld(), block.getX(), block.getY(), block.getZ()));
    	if(blockZone == null 
    			&& war.isBuildInZonesOnly() 
    			&& !war.canBuildOutsideZone(player)) {
    		war.badMsg(player, "You can only build inside warzones. Ask for the 'war.build' permission to build outside.");
    		event.setCancelled(true);
    		return;
    	}
    	
    	// unbreakableZoneBlocks
    	if(blockZone != null && blockZone.isUnbreakableZoneBlocks() 
    			&& (!isZoneMaker
    					|| (isZoneMaker && team != null)) 
    					) {
    		// if the zone is unbreakable, no one but zone makers can break blocks (even then, zone makers in a team can't break blocks
    		war.badMsg(player, "The blocks in this zone are unbreakable!");
    		event.setCancelled(true);
    		return;
    	}
	}
}

{'fraction_non_alphanumeric': 0.1331743441697, 'fraction_numerical': 0.000125517760763148, 'mean_word_length': 2.8490338164251208, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1216500', 'n_tokens_mistral': 5829, 'n_tokens_neox': 5228, 'n_words': 1146}