Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void onBlockBreak(BlockBreakEvent event) {
- Nation nation;
- Resident res = null;
- War war = null;
- TownyWorld townyWorld;
- HashMap<Town, Set<Location>> warBlocks;
- if(event.getPlayer()!=null){
- try {
- townyWorld = TownyUniverse.getDataSource().getWorld(event.getBlock().getLocation().getWorld().getName());
- if (!townyWorld.isUsingTowny())
- return;
- res = TownyUniverse.getDataSource().getResident(event.getPlayer().getName());
- if(res!=null){
- Town town = res.getTown();
- if(town!=null){
- nation = TownyUniverse.getDataSource().getTown(town.toString()).getNation();
- if(nation!=null){
- war = WarManager.getWarForNation(nation);
- if(war==null){
- //This nation is not in any wars so don't worry about it
- return;
- }else{
- try {
- Coord coord = Coord.parseCoord(event.getBlock().getLocation());
- TownBlock townBlock = townyWorld.getTownBlock(coord);
- Town defTown = townBlock.getTown();
- Nation defNation = defTown.getNation();
- if(defNation!=null && defNation!=nation){
- if(war.hasNation(defNation)){
- if (event.getBlock().getType() != Material.TNT && event.getBlock().getType() != Material.CHEST) {
- //The player is at war with the town being damaged
- //Now add the block being parsed to a regentask
- //then do event.setCancelled(false); to allow it to be broken during wars
- //then subtract points from defending nation
- try
- {
- war.chargeTownPoints(defNation, defTown, 0.01);
- Double lP = war.getTownPoints(defTown);
- for(Resident res2 : defTown.getResidents()){
- if(Bukkit.getServer().getOnlinePlayers().contains(Bukkit.getPlayer(res2.getName()))){
- Bukkit.getPlayer(res2.getName()).sendMessage(ChatColor.RED + "Someone is attacking! Def Points : " + (Integer.valueOf(lP.intValue())).toString());
- }
- }
- if (lP <= 10 && lP != -1 && WarManager.getWars().contains(war)) {
- for(Resident res2 : defTown.getResidents()){
- if(Bukkit.getServer().getOnlinePlayers().contains(Bukkit.getPlayer(res2.getName()))){
- Bukkit.getPlayer(res2.getName()).sendMessage(ChatColor.RED + "Be careful! Your town only has a " + (Integer.valueOf(lP.intValue())).toString() + " points left!");
- }
- }
- }
- ProtectionRegenTask task = new ProtectionRegenTask(this.mplugin.getInstance().getTowny(), event.getBlock(), true);
- TownyRegenAPI.addProtectionRegenTask(task);
- //Get the List of Blocks Damaged by war
- warBlocks = this.mplugin.getInstance().getWarBlocks();
- //if the defending town is already in it, add the new value to the set and replace it
- if(warBlocks.containsKey(defTown)){
- Set<Location> l = warBlocks.get(defTown);
- if(!l.contains(event.getBlock().getLocation())){
- l.add(event.getBlock().getLocation());
- }
- warBlocks.replace(defTown, l);
- event.getPlayer().sendMessage("Adding Block to already existing list");
- //if the defending town is not already in it, add a new entry to the set
- }else{
- Set<Location> l = new HashSet<Location>();
- l.add(event.getBlock().getLocation());
- warBlocks.put(defTown, l);
- event.getPlayer().sendMessage("Adding Block to new list");
- }
- BlockLocation bl = new BlockLocation(event.getBlock().getLocation());
- if(bl.equals(task.getBlockLocation())){
- event.getPlayer().sendMessage("These are the same");
- }else{
- System.out.println(bl);
- System.out.println(task.getBlockLocation());
- }
- this.mplugin.getInstance().setWarBlocks(warBlocks);
- this.mplugin.getInstance().saveBlockData();
- event.getPlayer().sendMessage("You deal 0.01 damage to " + defTown.getFormattedName());
- event.getPlayer().sendMessage(defTown.getFormattedName() + ": " + (Integer.valueOf(lP.intValue())).toString() + "/" + (Integer.valueOf(((Double)war.getTownMaxPoints(defTown)).intValue()).toString()));
- event.setCancelled(false);
- }catch (Exception ex)
- {
- event.getPlayer().getServer().getConsoleSender().sendMessage(ChatColor.RED + "An error occured, check the console!");
- ex.printStackTrace();
- }
- }
- }
- }
- } catch (TownyException x) {
- //Wilderness shit here so don't worry
- return;
- }
- }
- }
- }
- }
- } catch (NotRegisteredException e) {
- // failed to get world so abort
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement