Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. package timers;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.bukkit.Chunk;
  8. import org.bukkit.Location;
  9. import org.bukkit.Material;
  10. import org.bukkit.World;
  11. import org.bukkit.block.Block;
  12. import org.bukkit.block.BlockFace;
  13. import org.bukkit.entity.Entity;
  14. import org.bukkit.entity.LivingEntity;
  15. import org.bukkit.entity.Monster;
  16. import org.bukkit.entity.Player;
  17.  
  18.  
  19. import blocks.BlockStatus;
  20. import enums.BlockData;
  21. import main.SevenDays;
  22. import main.SevenDaysListener;
  23. import main.sdData;
  24. import main.sdPlayer;
  25.  
  26.  
  27. public class TrapTimer implements Runnable {
  28.  
  29.  
  30.  
  31.  
  32. private SevenDays plugin;
  33. private Long delay = 0l;
  34.  
  35. public TrapTimer(SevenDays sevenDays) {
  36. this.plugin = sevenDays;
  37. }
  38.  
  39.  
  40. @Override
  41. public void run() {
  42.  
  43. sdData.manageInjuries(); //This timer Also handles player injuries, like broken legs.
  44.  
  45. //We have ONE block we use only for barbed wire, this bit scans the entire world's loaded chunks for //barbed wire because I don't specifically save their locations, but it does them a few chunks at a time, //after a few passes its got all the loaded chunks scanned. You probably don't need this bit.
  46. if(System.currentTimeMillis() >= this.delay)
  47. {
  48. for(Chunk c:SevenDays.gameWorld.getLoadedChunks()) //iterate over all loaded chunks
  49. if(sdData.getCheckedChunks().contains(c))
  50. continue; //this chunk has already been cheked skip it.
  51. else if(sdData.getChunksToCheck().contains(c))
  52. continue; //this chunk hasn't been checked but is in the list.
  53. else
  54. sdData.getChunksToCheck().add(c); //a new chunk added to the list to check.
  55.  
  56.  
  57.  
  58. int count = 0; //number of chunks cheked
  59. int count2 = 0; //number of traps found
  60.  
  61.  
  62.  
  63. this.delay = System.currentTimeMillis() + 10000; //delay between scan passes, searching too many at once causes lag.
  64.  
  65. int passLimit = 450; //number of chunks to check before taking a break.
  66.  
  67. for(Chunk c:sdData.getChunksToCheck())
  68. {
  69.  
  70. if(count >= passLimit)
  71. break;
  72.  
  73. count2 = count2 + scanChunk(c);
  74. count++;
  75. sdData.getCheckedChunks().add(c);
  76.  
  77. }
  78. //if(count > 0)
  79. //System.out.println("Scanned " + count + " of " + sdData.getChunksToCheck().size() + " loaded chunks for traps, found: " + count2 + " total chunks scanned: " + sdData.getCheckedChunks().size());
  80.  
  81. sdData.getChunksToCheck().removeAll(sdData.getCheckedChunks());
  82. }
  83. //END CHUNK SCANNING
  84.  
  85.  
  86. //This bit of code checks the blocks where barbed wire *should be* like it was placed or picked up by
  87. //the scanner, it verifys that it is either a barbed wire block, or a barbed wire wrapped block.
  88. //if not it is removed from the list because a player/explosion etc broke it.
  89.  
  90. if(sdData.getBarbedWire().size() > 0) {
  91. List<Block> removed = new ArrayList<Block>();
  92.  
  93. //sData is just a class that holds all my lists/hash maps that need to be accessed from multiple classes
  94. // so they're in one place for easy access. You could use a hash map at the top of this class just as
  95. //easily.
  96.  
  97. for(Block trap:sdData.getBarbedWire()) {
  98.  
  99. if(trap.getType() != Material.WEB && (trap.getType() != Material.LOG_2
  100. && trap.getData()!= (byte) 0) && (trap.getType() != Material.LOG_2
  101. && trap.getData() != (byte) 1)) {
  102. removed.add(trap);
  103. continue;
  104. }
  105.  
  106. double range = 0.5;
  107. if(trap.getType() == Material.LOG_2)
  108. {
  109. range = 1.25;
  110. }
  111. //This next part is what calculates the damage to the living entities, when theyre within range of the
  112. //barbed wire, you could just as easily trigger an explosion here. I apply the damage to each of the
  113. //entities that are within the distance requirement, you could check for team mates here, create just
  114. //an explosion effect (one that does zero damage) and manually apply the damage to the entities you
  115. //choose to.
  116. for(Entity e:trap.getWorld().getNearbyEntities(trap.getLocation(), range,range,range ))
  117. {
  118. if(!(e instanceof LivingEntity)) //barbed wire does not damage non living things.
  119. continue;
  120.  
  121. LivingEntity le = (LivingEntity) e;
  122.  
  123. //the timer itself has a 20 tick delay per run, so any living entity in range takes 1.5 damage per second //when in contact with the barbed wire.
  124.  
  125. double damage = 1.5;
  126. le.damage(damage);
  127.  
  128. //barbed wire damages monsters more than other living entities. so we up the damage.
  129. if(le instanceof Monster) {
  130. damage = 30d;
  131. damageBlock(trap,damage,le);
  132. }
  133. }
  134.  
  135. if(!removed.isEmpty()) //if there were any invalid traps found, remove them from list.
  136. sdData.getBarbedWire().removeAll(removed);
  137. }
  138. }
  139. }
  140.  
  141. public static int scanChunk(Chunk chunk) {
  142. //this block of code handles scanning the chunks for specific block types
  143.  
  144. int count = 0;
  145. int bx = chunk.getX()<<4;
  146. int bz = chunk.getZ()<<4;
  147.  
  148. World world = chunk.getWorld();
  149. if(world.getName().equalsIgnoreCase("7days"))
  150. {
  151.  
  152. for(int xx = bx; xx < bx+16; xx++) {
  153. for(int zz = bz; zz < bz+16; zz++) {
  154. for(int yy = 0; yy < 128; yy++) {
  155. int typeId = world.getBlockTypeIdAt(xx, yy, zz);
  156. if(typeId == Material.WEB.getId() || typeId == Material.LOG_2.getId()) {
  157. if(!sdData.getBarbedWire().contains(world.getBlockAt(xx,yy,xx))) {
  158. sdData.getBarbedWire().add(world.getBlockAt(xx,yy,zz));
  159. count ++;
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. return count;
  167. }
  168.  
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement