Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. public void onBlockBreak(BlockBreakEvent event) {
  2. Nation nation;
  3. Resident res = null;
  4. War war = null;
  5. TownyWorld townyWorld;
  6. HashMap<Town, Set<Location>> warBlocks;
  7.  
  8. if(event.getPlayer()!=null){
  9. try {
  10. townyWorld = TownyUniverse.getDataSource().getWorld(event.getBlock().getLocation().getWorld().getName());
  11. if (!townyWorld.isUsingTowny())
  12. return;
  13. res = TownyUniverse.getDataSource().getResident(event.getPlayer().getName());
  14. if(res!=null){
  15. Town town = res.getTown();
  16. if(town!=null){
  17. nation = TownyUniverse.getDataSource().getTown(town.toString()).getNation();
  18. if(nation!=null){
  19. war = WarManager.getWarForNation(nation);
  20. if(war==null){
  21. //This nation is not in any wars so don't worry about it
  22. return;
  23. }else{
  24. try {
  25. Coord coord = Coord.parseCoord(event.getBlock().getLocation());
  26. TownBlock townBlock = townyWorld.getTownBlock(coord);
  27. Town defTown = townBlock.getTown();
  28. Nation defNation = defTown.getNation();
  29. if(defNation!=null && defNation!=nation){
  30. if(war.hasNation(defNation)){
  31. if (event.getBlock().getType() != Material.TNT && event.getBlock().getType() != Material.CHEST) {
  32. //The player is at war with the town being damaged
  33. //Now add the block being parsed to a regentask
  34. //then do event.setCancelled(false); to allow it to be broken during wars
  35. //then subtract points from defending nation
  36. try
  37. {
  38. war.chargeTownPoints(defNation, defTown, 0.01);
  39. Double lP = war.getTownPoints(defTown);
  40. for(Resident res2 : defTown.getResidents()){
  41. if(Bukkit.getServer().getOnlinePlayers().contains(Bukkit.getPlayer(res2.getName()))){
  42. Bukkit.getPlayer(res2.getName()).sendMessage(ChatColor.RED + "Someone is attacking! Def Points : " + (Integer.valueOf(lP.intValue())).toString());
  43. }
  44. }
  45. if (lP <= 10 && lP != -1 && WarManager.getWars().contains(war)) {
  46. for(Resident res2 : defTown.getResidents()){
  47. if(Bukkit.getServer().getOnlinePlayers().contains(Bukkit.getPlayer(res2.getName()))){
  48. Bukkit.getPlayer(res2.getName()).sendMessage(ChatColor.RED + "Be careful! Your town only has a " + (Integer.valueOf(lP.intValue())).toString() + " points left!");
  49. }
  50. }
  51. }
  52. ProtectionRegenTask task = new ProtectionRegenTask(this.mplugin.getInstance().getTowny(), event.getBlock(), true);
  53. TownyRegenAPI.addProtectionRegenTask(task);
  54.  
  55. //Get the List of Blocks Damaged by war
  56. warBlocks = this.mplugin.getInstance().getWarBlocks();
  57. //if the defending town is already in it, add the new value to the set and replace it
  58. if(warBlocks.containsKey(defTown)){
  59. Set<Location> l = warBlocks.get(defTown);
  60. if(!l.contains(event.getBlock().getLocation())){
  61. l.add(event.getBlock().getLocation());
  62. }
  63. warBlocks.replace(defTown, l);
  64. event.getPlayer().sendMessage("Adding Block to already existing list");
  65. //if the defending town is not already in it, add a new entry to the set
  66. }else{
  67. Set<Location> l = new HashSet<Location>();
  68. l.add(event.getBlock().getLocation());
  69. warBlocks.put(defTown, l);
  70. event.getPlayer().sendMessage("Adding Block to new list");
  71. }
  72. BlockLocation bl = new BlockLocation(event.getBlock().getLocation());
  73. if(bl.equals(task.getBlockLocation())){
  74. event.getPlayer().sendMessage("These are the same");
  75. }else{
  76. System.out.println(bl);
  77. System.out.println(task.getBlockLocation());
  78. }
  79. this.mplugin.getInstance().setWarBlocks(warBlocks);
  80. this.mplugin.getInstance().saveBlockData();
  81. event.getPlayer().sendMessage("You deal 0.01 damage to " + defTown.getFormattedName());
  82. event.getPlayer().sendMessage(defTown.getFormattedName() + ": " + (Integer.valueOf(lP.intValue())).toString() + "/" + (Integer.valueOf(((Double)war.getTownMaxPoints(defTown)).intValue()).toString()));
  83. event.setCancelled(false);
  84. }catch (Exception ex)
  85. {
  86. event.getPlayer().getServer().getConsoleSender().sendMessage(ChatColor.RED + "An error occured, check the console!");
  87. ex.printStackTrace();
  88. }
  89. }
  90. }
  91. }
  92. } catch (TownyException x) {
  93. //Wilderness shit here so don't worry
  94. return;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. } catch (NotRegisteredException e) {
  101. // failed to get world so abort
  102. return;
  103. }
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement