Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.35 KB | None | 0 0
  1. package com.avrgaming.civcraft.war;
  2.  
  3. import java.util.Date;
  4. import java.util.HashSet;
  5. import java.util.Random;
  6.  
  7. import org.bukkit.Color;
  8. import org.bukkit.FireworkEffect;
  9. import org.bukkit.Location;
  10. import org.bukkit.Material;
  11. import org.bukkit.FireworkEffect.Type;
  12. import org.bukkit.block.Block;
  13. import org.bukkit.entity.EntityType;
  14. import org.bukkit.event.EventHandler;
  15. import org.bukkit.event.EventPriority;
  16. import org.bukkit.event.Listener;
  17. import org.bukkit.event.block.BlockBreakEvent;
  18. import org.bukkit.event.block.BlockPlaceEvent;
  19. import org.bukkit.event.entity.EntityExplodeEvent;
  20. import org.bukkit.util.Vector;
  21.  
  22. import com.avrgaming.civcraft.camp.CampBlock;
  23. import com.avrgaming.civcraft.config.CivSettings;
  24. import com.avrgaming.civcraft.exception.CivException;
  25. import com.avrgaming.civcraft.exception.InvalidConfiguration;
  26. import com.avrgaming.civcraft.main.CivGlobal;
  27. import com.avrgaming.civcraft.main.CivMessage;
  28. import com.avrgaming.civcraft.object.CultureChunk;
  29. import com.avrgaming.civcraft.object.StructureBlock;
  30. //import com.avrgaming.civcraft.structure.Blacksmith;
  31. import com.avrgaming.civcraft.structure.Buildable;
  32. import com.avrgaming.civcraft.structure.TownHall;
  33. import com.avrgaming.civcraft.threading.TaskMaster;
  34. import com.avrgaming.civcraft.threading.tasks.FireWorkTask;
  35. import com.avrgaming.civcraft.util.BlockCoord;
  36. import com.avrgaming.civcraft.util.ChunkCoord;
  37. import com.avrgaming.civcraft.util.CivColor;
  38.  
  39. public class WarListener implements Listener {
  40.  
  41.     public static final String RESTORE_NAME = "special:TNT";
  42.     ChunkCoord coord = new ChunkCoord();
  43.    
  44.     private static final long COOLDOWN = 2;
  45.     public static int yield;
  46.     public static double playerDamage;
  47.     public static int structureDamage;
  48.     static {
  49.         try {
  50.             yield = CivSettings.getInteger(CivSettings.warConfig, "tnt.yield");
  51.             playerDamage = CivSettings.getDouble(CivSettings.warConfig, "tnt.player_damage");
  52.             structureDamage = CivSettings.getInteger(CivSettings.warConfig, "tnt.structure_damage");
  53.         } catch (InvalidConfiguration e) {
  54.             e.printStackTrace();
  55.         }
  56.     }
  57.    
  58.     @EventHandler(priority = EventPriority.HIGHEST)
  59.     public void onBlockBreak(BlockBreakEvent event) {
  60.         if (event.isCancelled()) {
  61.             return;
  62.         }
  63.        
  64.         if (!War.isWarTime()) {
  65.             return;
  66.         }
  67.        
  68.         coord.setFromLocation(event.getBlock().getLocation());
  69.         CultureChunk cc = CivGlobal.getCultureChunk(coord);
  70.        
  71.         if (cc == null) {
  72.             return;
  73.         }
  74.        
  75.         if (!cc.getCiv().getDiplomacyManager().isAtWar()) {
  76.             return;
  77.         }
  78.                
  79.         if (event.getBlock().getType().equals(Material.DIRT) ||
  80.             event.getBlock().getType().equals(Material.GRASS) ||
  81.             event.getBlock().getType().equals(Material.SAND) ||
  82.             event.getBlock().getType().equals(Material.GRAVEL) ||
  83.             event.getBlock().getType().equals(Material.TORCH) ||
  84.             event.getBlock().getType().equals(Material.REDSTONE_TORCH_OFF) ||
  85.             event.getBlock().getType().equals(Material.REDSTONE_TORCH_ON) ||
  86.             event.getBlock().getType().equals(Material.REDSTONE) ||
  87.             event.getBlock().getType().equals(Material.TNT) ||
  88.             event.getBlock().getType().equals(Material.LADDER) ||
  89.             event.getBlock().getType().equals(Material.VINE) ||
  90.             event.getBlock().getType().equals(Material.IRON_BLOCK) ||
  91.             event.getBlock().getType().equals(Material.GOLD_BLOCK) ||
  92.             event.getBlock().getType().equals(Material.DIAMOND_BLOCK) ||
  93.             event.getBlock().getType().equals(Material.EMERALD_BLOCK) ||
  94.             !event.getBlock().getType().isSolid()) {
  95.             return;
  96.         }
  97.        
  98.         CivMessage.sendError(event.getPlayer(), CivSettings.localize.localizedString("war_mustUseTNT"));
  99.         event.setCancelled(true);
  100.     }
  101.    
  102.     @SuppressWarnings("deprecation")
  103.     @EventHandler(priority = EventPriority.HIGH)
  104.     public void onBlockPlace(BlockPlaceEvent event) {
  105.         if (event.isCancelled()) {
  106.             return;
  107.         }
  108.        
  109.         if (!War.isWarTime()) {
  110.             return;
  111.         }
  112.        
  113.         coord.setFromLocation(event.getBlock().getLocation());
  114.         CultureChunk cc = CivGlobal.getCultureChunk(coord);
  115.        
  116.         if (cc == null) {
  117.             return;
  118.         }
  119.        
  120.         if (!cc.getCiv().getDiplomacyManager().isAtWar()) {
  121.             return;
  122.         }
  123.                 -
  124.         Object Material;
  125.         if (event.getBlock().getType().equals(Material.DIRT) ||
  126.             event.getBlock().getType().equals(Material.GRASS) ||
  127.             event.getBlock().getType().equals(Material.SAND) ||
  128.             event.getBlock().getType().equals(Material.GRAVEL) ||
  129.             event.getBlock().getType().equals(Material.TORCH) ||
  130.             event.getBlock().getType().equals(Material.REDSTONE_TORCH_OFF) ||
  131.             event.getBlock().getType().equals(Material.REDSTONE_TORCH_ON) ||
  132.             event.getBlock().getType().equals(Material.REDSTONE) ||
  133.             event.getBlock().getType().equals(Material.LADDER) ||
  134.             event.getBlock().getType().equals(Material.VINE) ||
  135.             event.getBlock().getType().equals(Material.TNT)) {
  136.            
  137.             if (event.getBlock().getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) {
  138.                 return;
  139.             }
  140.            
  141.             event.getBlock().getWorld().spawnFallingBlock(event.getBlock().getLocation(), event.getBlock().getType(), (byte) 0);
  142.             event.getBlock().setType(Material.AIR);
  143.            
  144.             return;
  145.         }
  146.        
  147.         if (event.getBlock().getType().equals(Material.IRON_BLOCK) ||
  148.                 event.getBlock().getType().equals(Material.GOLD_BLOCK) ||
  149.                 event.getBlock().getType().equals(Material.DIAMOND_BLOCK) ||
  150.                 event.getBlock().getType().equals(Material.EMERALD_BLOCK)) {
  151.                
  152.                 if (event.getBlock().getLocation().subtract(0, 1, 0).getBlock().getType() != Material.AIR) {
  153.                     return;
  154.                 }
  155.                
  156.                 return;
  157.             }
  158.        
  159.         CivMessage.sendError(event.getPlayer(), CivSettings.localize.localizedString("war_onlyBuildCertainBlocks"));
  160.         CivMessage.sendError(event.getPlayer(), CivSettings.localize.localizedString("war_canAlsoPlaceBridgeBlocks"));
  161.         event.setCancelled(true);
  162.     }
  163.    
  164.     private void explodeBlock(Block b, Date lastUse) {
  165.         Date now = new Date();
  166.         long diff = now.getTime() - lastUse.getTime();
  167.         diff /= 1000;
  168.         if (diff > WarListener.COOLDOWN) {
  169.             WarRegen.explodeThisBlock(b, WarListener.RESTORE_NAME);
  170.             launchExplodeFirework(b.getLocation());
  171.         }
  172.         lastUse = now;
  173.     }d
  174.    
  175.     private void launchExplodeFirework(Location loc) {
  176.         Random rand = new Random();
  177.         int rand1 = rand.nextInt(100);
  178.        
  179.         if (rand1 > 90)
  180.         {
  181.         FireworkEffect fe = FireworkEffect.builder().withColor(Color.ORANGE).withColor(Color.YELLOW).flicker(true).with(Type.BURST).build();       
  182.         TaskMaster.syncTask(new FireWorkTask(fe, loc.getWorld(), loc, 3), 0);
  183.         }
  184.     }
  185.    
  186.     @EventHandler(priority = EventPriority.HIGH)
  187.     public void onEntityExplode(EntityExplodeEvent event) {
  188.  
  189.         if (War.isWarTime()) {
  190.             event.setCancelled(false);
  191.         } else {
  192.             event.setCancelled(true);
  193.             return;
  194.         }
  195.        
  196.         if (event.isCancelled()) {
  197.             return;
  198.         }
  199.        
  200.         if (event.getEntity() == null) {
  201.             return;
  202.         }
  203.        
  204.         if (event.getEntityType().equals(EntityType.UNKNOWN)) {
  205.             return;
  206.         }
  207.  
  208.         if (event.getEntityType().equals(EntityType.PRIMED_TNT) ||
  209.                 event.getEntityType().equals(EntityType.MINECART_TNT)) {
  210.  
  211.             HashSet<Buildable> structuresHit = new HashSet<Buildable>();
  212.        
  213.             for (int y = -yield; y <= yield; y++) {
  214.                 for (int x = -yield; x <= yield; x++) {
  215.                     for (int z = -yield; z <= yield; z++) {
  216.                         Location loc = event.getLocation().clone().add(new Vector(x,y,z));
  217.                         Block b = loc.getBlock();
  218.                         if (loc.distance(event.getLocation()) < yield) {
  219.  
  220.                             BlockCoord bcoord = new BlockCoord();
  221.                             bcoord.setFromLocation(loc);
  222.                             StructureBlock sb = CivGlobal.getStructureBlock(bcoord);
  223.                             if (sb == null) {
  224.                             WarRegen.saveBlock(loc.getBlock(), Cannon.RESTORE_NAME, false);
  225.                             }
  226.                             if (sb.getTown() != null) {
  227.                             WarRegen.destroyThisBlock(loc.getBlock(), sb.getTown());
  228.                             } else {
  229.                             ItemManager.setTypeIdAndData(loc.getBlock(), CivData.AIR, 0, false);
  230.                             }
  231.                            
  232.                             StructureBlock sb = CivGlobal.getStructureBlock(bcoord);
  233.                             CampBlock cb = CivGlobal.getCampBlock(bcoord);
  234.                            
  235.                             if (sb == null && cb == null) {
  236.                                 explodeBlock(b);
  237.                                 continue;
  238.                             }
  239.                            
  240.                             if (sb != null) {
  241.                                
  242.                                 if (!sb.isDamageable()) {
  243.                                     continue;
  244.                                 }
  245.                                
  246.                                 if (sb.getOwner() instanceof TownHall) {
  247.                                     TownHall th = (TownHall)sb.getOwner();
  248.                                     if (th.getControlPoints().containsKey(bcoord)) {
  249.                                         continue;
  250.                                     }
  251.                                 }
  252.                                
  253.                                 if (!sb.getOwner().isDestroyed()) {
  254.                                     if (!structuresHit.contains(sb.getOwner())) {
  255.                                        
  256.                                         structuresHit.add(sb.getOwner());
  257.  
  258.                                         if (sb.getOwner() instanceof TownHall) {
  259.                                             TownHall th = (TownHall)sb.getOwner();
  260.  
  261.                                             if (th.getHitpoints() == 0) {
  262.                                                 explodeBlock(b);
  263.                                             } else {
  264.                                                 th.onTNTDamage(structureDamage);
  265.                                             }
  266.                                         } else {
  267.                                             sb.getOwner().onDamage(structureDamage, b.getWorld(), null, sb.getCoord(), sb);
  268.                                             CivMessage.sendCiv(sb.getCiv(), CivColor.Yellow+CivSettings.localize.localizedString("var_war_tntMsg",sb.getOwner().getDisplayName(),(
  269.                                                     sb.getOwner().getCenterLocation().getX()+","+
  270.                                                     sb.getOwner().getCenterLocation().getY()+","+
  271.                                                     sb.getOwner().getCenterLocation().getZ()+")"),
  272.                                                     (sb.getOwner().getHitpoints()+"/"+sb.getOwner().getMaxHitPoints())));
  273.                                         }
  274.                                     }
  275.                                 } else {
  276.                                     explodeBlock(b);
  277.                                 }
  278.                                 continue;
  279.                             }
  280.                         }
  281.                     }  
  282.                 }
  283.             }
  284.             event.setCancelled(true);
  285.         }
  286.  
  287.     }
  288.  
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement