Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. package com.fearpvp.wind;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.HashSet;
  5. import java.util.List;
  6.  
  7. import org.bukkit.Location;
  8. import org.bukkit.Material;
  9. import org.bukkit.block.Block;
  10. import org.bukkit.entity.Entity;
  11. import org.bukkit.metadata.FixedMetadataValue;
  12. import org.bukkit.plugin.java.JavaPlugin;
  13. import org.bukkit.scheduler.BukkitRunnable;
  14. import org.bukkit.util.Vector;
  15.  
  16. public class TornadoResource
  17. {
  18. /**
  19. * Spawns a tornado at the given location l.
  20. *
  21. * @param plugin
  22. * - Plugin instance that spawns the tornado.
  23. * @param location
  24. * - Location to spawn the tornado.
  25. * @param material
  26. * - The base material for the tornado.
  27. * @param data
  28. * - Data for the block.
  29. * @param direction
  30. * - The direction the tornado should move in.
  31. * @param speed
  32. * - How fast it moves in the given direction. Warning! A number greater than 0.3 makes it look weird.
  33. * @param amount_of_blocks
  34. * - The max amount of blocks that can exist in the tornado.
  35. * @param time
  36. * - The amount of ticks the tornado should be alive.
  37. * @param spew
  38. * - Defines if the tornado should remove or throw out any block it picks up.
  39. * @param explode
  40. * - This defines if the tornado should "explode" when it dies. Warning! Right now it only creates a huge mess.
  41. */
  42. public static void spawnTornado(
  43. final JavaPlugin plugin,
  44. final Location location,
  45. final Material material,
  46. final byte data,
  47. final Vector direction,
  48. final double speed,
  49. final int amount_of_blocks,
  50. final long time,
  51. final boolean spew,
  52. final boolean explode
  53. ) {
  54.  
  55. class VortexBlock {
  56.  
  57. private Entity entity;
  58.  
  59. public boolean removable = true;
  60.  
  61. private float ticker_vertical = 0.0f;
  62. private float ticker_horisontal = (float) (Math.random() * 2 * Math.PI);
  63.  
  64. @SuppressWarnings("deprecation")
  65. public VortexBlock(Location l, Material m, byte d) {
  66.  
  67. if (l.getBlock().getType() != Material.AIR) {
  68.  
  69. Block b = l.getBlock();
  70. entity = l.getWorld().spawnFallingBlock(l, b.getType(), b.getData());
  71.  
  72. if (b.getType() != Material.WATER)
  73. b.setType(Material.AIR);
  74.  
  75. removable = !spew;
  76. }
  77. else {
  78. entity = l.getWorld().spawnFallingBlock(l, m, d);
  79. removable = !explode;
  80. }
  81.  
  82. addMetadata();
  83. }
  84.  
  85. public VortexBlock(Entity e) {
  86. entity = e;
  87. removable = false;
  88. addMetadata();
  89. }
  90.  
  91. private void addMetadata() {
  92. entity.setMetadata("vortex", new FixedMetadataValue(plugin, "protected"));
  93. }
  94.  
  95. public void remove() {
  96. if(removable) {
  97. entity.remove();
  98. }
  99. entity.removeMetadata("vortex", plugin);
  100. }
  101.  
  102. @SuppressWarnings("deprecation")
  103. public HashSet<VortexBlock> tick() {
  104.  
  105. double radius = Math.sin(verticalTicker()) * 2;
  106. float horisontal = horisontalTicker();
  107.  
  108. Vector v = new Vector(radius * Math.cos(horisontal), 0.5D, radius * Math.sin(horisontal));
  109.  
  110. HashSet<VortexBlock> new_blocks = new HashSet<VortexBlock>();
  111.  
  112. // Pick up blocks
  113. Block b = entity.getLocation().add(v.clone().normalize()).getBlock();
  114. if(b.getType() != Material.AIR) {
  115. new_blocks.add(new VortexBlock(b.getLocation(), b.getType(), b.getData()));
  116. }
  117.  
  118. // Pick up other entities
  119. List<Entity> entities = entity.getNearbyEntities(1.0D, 1.0D, 1.0D);
  120. for(Entity e : entities) {
  121. if(!e.hasMetadata("vortex")) {
  122. new_blocks.add(new VortexBlock(e));
  123. }
  124. }
  125.  
  126. setVelocity(v);
  127.  
  128. return new_blocks;
  129. }
  130.  
  131. private void setVelocity(Vector v) {
  132. entity.setVelocity(v);
  133. }
  134.  
  135. private float verticalTicker() {
  136. if (ticker_vertical < 1.0f) {
  137. ticker_vertical += 0.05f;
  138. }
  139. return ticker_vertical;
  140. }
  141.  
  142. private float horisontalTicker() {
  143. // ticker_horisontal = (float) ((ticker_horisontal + 0.8f) % 2*Math.PI);
  144. return (ticker_horisontal += 0.8f);
  145. }
  146. }
  147.  
  148. // Modify the direction vector using the speed argument.
  149. if (direction != null) {
  150. direction.normalize().multiply(speed);
  151. }
  152.  
  153. // This set will contain every block created to make sure the metadata for each and everyone is removed.
  154. final HashSet<VortexBlock> clear = new HashSet<VortexBlock>();
  155.  
  156. final int id = new BukkitRunnable() {
  157.  
  158. private ArrayDeque<VortexBlock> blocks = new ArrayDeque<VortexBlock>();
  159.  
  160. public void run() {
  161.  
  162. if (direction != null) {
  163. location.add(direction);
  164. }
  165.  
  166. // Spawns 10 blocks at the time.
  167. for (int i = 0; i < 10; i++) {
  168. checkListSize();
  169. VortexBlock vb = new VortexBlock(location, material, data);
  170. blocks.add(vb);
  171. clear.add(vb);
  172. }
  173.  
  174. // Make all blocks in the list spin, and pick up any blocks that get in the way.
  175. ArrayDeque<VortexBlock> que = new ArrayDeque<VortexBlock>();
  176.  
  177. for (VortexBlock vb : blocks) {
  178. HashSet<VortexBlock> new_blocks = vb.tick();
  179. for(VortexBlock temp : new_blocks) {
  180. que.add(temp);
  181. }
  182. }
  183.  
  184. // Add the new blocks
  185. for(VortexBlock vb : que) {
  186. checkListSize();
  187. blocks.add(vb);
  188. clear.add(vb);
  189. }
  190. }
  191.  
  192. // Removes the oldest block if the list goes over the limit.
  193. private void checkListSize() {
  194. while(blocks.size() >= amount_of_blocks) {
  195. VortexBlock vb = blocks.getFirst();
  196. vb.remove();
  197. blocks.remove(vb);
  198. clear.remove(vb);
  199. }
  200. }
  201. }.runTaskTimer(plugin, 5L, 5L).getTaskId();
  202.  
  203. // Stop the "tornado" after the given time.
  204. new BukkitRunnable() {
  205. public void run() {
  206. for(VortexBlock vb : clear) {
  207. vb.remove();
  208. }
  209. plugin.getServer().getScheduler().cancelTask(id);
  210. }
  211. }.runTaskLater(plugin, time);
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement