[LIB] Bouncy Blocks!

Discussion in 'Resources' started by TryB4, Mar 7, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    TryB4

    I was bored so I made this ;)

    BouncyBlocks:
    Code:java
    1. package com.tryb4.Factions.util;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Effect;
    7. import org.bukkit.Location;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.FallingBlock;
    10. import org.bukkit.event.Cancellable;
    11. import org.bukkit.event.Event;
    12. import org.bukkit.event.EventHandler;
    13. import org.bukkit.event.HandlerList;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.entity.EntityChangeBlockEvent;
    16. import org.bukkit.metadata.FixedMetadataValue;
    17. import org.bukkit.plugin.Plugin;
    18. import org.bukkit.util.Vector;
    19.  
    20. public class BouncyBlock implements Listener {
    21.  
    22. private Material mat;
    23. private int bounces, removeAfter;
    24. private FallingBlock fb;
    25. private Plugin p;
    26. private boolean dead = false;
    27. private boolean effect = false;
    28. private byte data = 0;
    29.  
    30. /**
    31. * @param m
    32. * - Block material
    33. * @param removeAfter
    34. * - How many times it can hit the ground before it will be
    35. * removed.
    36. */
    37. public BouncyBlock(Material m, int removeAfter, Plugin p) {
    38. this.mat = m;
    39. this.removeAfter = removeAfter;
    40. this.p = p;
    41. Bukkit.getPluginManager().registerEvents(this, p);
    42. }
    43.  
    44. public void spawn(Location l) {
    45. if (fb == null) {
    46. fb = l.getWorld().spawnFallingBlock(l, getMaterial(), getData());
    47. }
    48. fb.setMetadata("BB", new FixedMetadataValue(p, "A bouncy block"));
    49. fb.setDropItem(false);
    50. fb.setVelocity(getRandomVelocity());
    51. }
    52.  
    53. public void spawn(Location l, FallingBlock fb) {
    54. this.fb = fb;
    55. fb.setMetadata("BB", new FixedMetadataValue(p, "A bouncy block"));
    56. fb.setDropItem(false);
    57. fb.setVelocity(getRandomVelocity());
    58. }
    59.  
    60. public boolean doEffect() {
    61. return effect;
    62. }
    63.  
    64. public void setEffect(boolean effect) {
    65. this.effect = effect;
    66. }
    67.  
    68. public boolean isDead() {
    69. return dead;
    70. }
    71.  
    72. public void setDead(boolean dead) {
    73. if (dead) {
    74. fb.remove();
    75. }
    76. this.dead = dead;
    77. }
    78.  
    79. public int getBouncesToRemove() {
    80. return removeAfter;
    81. }
    82.  
    83. public int getBounces() {
    84. return bounces;
    85. }
    86.  
    87. public byte getData() {
    88. return data;
    89. }
    90.  
    91. public void setData(byte data) {
    92. this.data = data;
    93. }
    94.  
    95. public Material getMaterial() {
    96. return mat;
    97. }
    98.  
    99. public FallingBlock getFallingBlock() {
    100. return fb;
    101. }
    102.  
    103. public Plugin getPlugin() {
    104. return p;
    105. }
    106.  
    107. public void setBounces(int bounces) {
    108. this.bounces = bounces;
    109. }
    110.  
    111. public void setMaterial(Material mat) {
    112. this.mat = mat;
    113. }
    114.  
    115. private Vector getRandomVelocity() {
    116. Random random = new Random();
    117. final double power = 0.5D;
    118. double rix = random.nextBoolean() ? -power : power;
    119. double riz = random.nextBoolean() ? -power : power;
    120. double x = random.nextBoolean() ? (rix * (0.25D + (random.nextInt(3) / 5)))
    121. : 0.0D;
    122. double y = 0.6D + (random.nextInt(2) / 4.5D);
    123. double z = random.nextBoolean() ? (riz * (0.25D + (random.nextInt(3) / 5)))
    124. : 0.0D;
    125. Vector velocity = new Vector(x, y, z);
    126.  
    127. return velocity;
    128. }
    129.  
    130. @EventHandler
    131. public void entityChangeBlock(EntityChangeBlockEvent e) {
    132. if (e.getEntity() instanceof FallingBlock) {
    133. FallingBlock fb = (FallingBlock) e.getEntity();
    134. if (fb.hasMetadata("BB")) {
    135. if (fb.getUniqueId().compareTo(getFallingBlock().getUniqueId()) == 0) {
    136. BouncyBlockHitGroundEvent event = new BouncyBlockHitGroundEvent(
    137. this, fb, e.getBlock().getLocation());
    138. Bukkit.getPluginManager().callEvent(event);
    139.  
    140. if (event.isCancelled()) {
    141. setDead(true);
    142.  
    143. return;
    144. }
    145.  
    146. if (doEffect()) {
    147. fb.getWorld().playEffect(fb.getLocation(),
    148. Effect.STEP_SOUND, getMaterial());
    149. }
    150. if (getBounces() >= getBouncesToRemove()) {
    151. setDead(true);
    152. return;
    153. }
    154. setBounces(getBounces() + 1);
    155. e.setCancelled(true);
    156. fb.remove();
    157. fb = e.getEntity()
    158. .getLocation()
    159. .getWorld()
    160. .spawnFallingBlock(e.getEntity().getLocation(),
    161. getMaterial(), getData());
    162. spawn(fb.getLocation(), fb);
    163. }
    164. }
    165. }
    166. }
    167.  
    168. public static class BouncyBlockHitGroundEvent extends Event implements
    169. Cancellable {
    170. private static final HandlerList handlers = new HandlerList();
    171. private FallingBlock fb;
    172. private BouncyBlock bouncyBlock;
    173. private Location hit;
    174. private boolean cancelled = false;
    175.  
    176. public BouncyBlockHitGroundEvent(BouncyBlock bouncyBlock,
    177. FallingBlock fb, Location hit) {
    178. this.hit = hit;
    179. this.bouncyBlock = bouncyBlock;
    180. this.fb = fb;
    181. }
    182.  
    183. public FallingBlock getFallingBlock() {
    184. return fb;
    185. }
    186.  
    187. public BouncyBlock getBouncyBlock() {
    188. return bouncyBlock;
    189. }
    190.  
    191. public Location getHit() {
    192. return hit;
    193. }
    194.  
    195. public int getBouncesLeftUntilDeath() {
    196. return bouncyBlock.getBouncesToRemove() - bouncyBlock.getBounces();
    197. }
    198.  
    199. public HandlerList getHandlers() {
    200. return handlers;
    201. }
    202.  
    203. public static HandlerList getHandlerList() {
    204. return handlers;
    205. }
    206.  
    207. /**
    208. * Cancelling the event will just make the falling block act as a normal
    209. * one.
    210. */
    211. public void setCancelled(boolean cancelled) {
    212. this.cancelled = cancelled;
    213. }
    214.  
    215. public boolean isCancelled() {
    216. return cancelled;
    217. }
    218.  
    219. }
    220.  
    221. }
    222.  




    Example usage:

    Code:java
    1. BouncyBlock bb = new BouncyBlock(Material.EMERALD_BLOCK, 15, this);
    2. bb.spawn(e.getPlayer().getLocation());
    3. bb.setEffect(true);




    Video(using example):



    NOTE:
    Sometimes it disappears if it hits the corner of a block.


    Have fun.
     
  2. Offline

    Flybelette

    That's Weird x)
     
    TryB4 likes this.
  3. Offline

    chasechocolate

    Hahaha, that's awesome! Very creative sir.
     
    TryB4 likes this.
  4. Offline

    TryB4

  5. Offline

    Arcoz

    Awesome! :D
     
  6. Offline

    Favorlock

    Awesome!
     
  7. Offline

    DSH105

    That's awesome. Nice work ;D
     
  8. Offline

    DevRosemberg

    Ooooooooooh this is gona be fun. Good Job!
     
  9. Offline

    Creepapa

    Nice!
     
  10. Offline

    ItsLeoFTW

    wow.... this is awesome!
     
  11. Offline

    turt2live

    Looks awesome :D

    I've modified it a bit to have events and only one listener for all BouncyBlcoks. I've also reduced a bit of object creation (randoms, etc), added exceptions for null/bad arguments, and a bunch of documentation. I've done all this to include it in my "work in progress" common plugin API for plugins that I write.

    You can find my changes here: GitHub
     
    TryB4 and DSH105 like this.
  12. Offline

    TryB4

    turt2live
    That got more advanced than it had to be o_o
     
  13. Offline

    turt2live

    I got a wee bit bored ;) But yes... it's kinda overkill.
     
    TryB4 likes this.
  14. Offline

    ArthurMaker

    It will be awesome if this would do damage when it hits an entity! :D
     
  15. Offline

    Garris0n

    @tur2live now make it bounce towards a player like a slime. And make it do damage. Basically make a slime using only the Bukkit API.
     
  16. Offline

    turt2live

    You could do that easily, even without my changes.
     
  17. Offline

    Garris0n

    I know, I'm just too busy to do it so I recommended somebody else do it instead ;)
     
  18. Offline

    Dread9Nought

    I'll give you the first thought that I had while watching the video:

    "That's legit as shit."
     
    TryB4 likes this.
  19. Offline

    The_IcATaRiX

    Is it possible to get the location of that BB? If yes how? I would make a crazy kit with that :O
     
  20. Offline

    TryB4

    The_IcATaRiX
    <bouncyblock>.getFallingBlock().getLocation()
     
  21. Offline

    The_IcATaRiX

    TryB4
    Thank you very much!
     
  22. Offline

    ccrama

    This is too awesome not to use, TryB4, but don't know in what application i COULD use it. Did you have any reason to create it? (plugin in mind, some sort of application?)
     
  23. Offline

    Niknea

    TryB4 Where did the video go :(
     
  24. Offline

    turt2live

    Looks like something broke. Here's the link extracted from the page, it's a bit large. (click)
     
    Niknea likes this.
  25. Offline

    TryB4

    Added an event to see when the bouncy block hits the ground
     
  26. Offline

    LCastr0

    Musical Blocks (dansarcade.eu) uses your lib, I think :3
     
  27. Offline

    TryB4

    LCastr0
    If so I'm glad people are using it :)
     
  28. Offline

    Phasesaber

    Is there a way to despawn the blocks? And to have them bounce forever? (Thinking of adding them in a minigame I am making :p)
     
  29. Offline

    TryB4

    Phasesaber
    Just make the bounces until death amount really high?
     
  30. Offline

    Phasesaber

    Would it work if it is set to -1?
     
Thread Status:
Not open for further replies.

Share This Page