[Your Total Guide To] Exploding Arrows! [Your Total Guide To]

Discussion in 'Resources' started by JPG2000, Aug 15, 2013.

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

    JPG2000

    Im trying something new. Instead of teaching a concept, I will teach a cool way to use them.

    In this tutorial I will show you the proper way to create Explosive arrows that you can toggle with a command.

    Before we begin, get your plugin ready with a onEnable & onDisable. First, implement Listener and extend JavaPlugin and keep the rest blank.

    Lets create our Event, the onProjectileHitEvent.
    Code:java
    1. public void onHit(ProjectileHitEvent event) {
    2. }


    Now, lets check if the entity (the projectile fired) is an arrow, and if so lets create the explosion.
    Code:java
    1. public void onHit(ProjectileHitEvent event) {
    2. if (event.getEntity() instanceof Arrow) {
    3. event.getEntity().getWorld().createExplosion(event.getEntity().getLocation(), 4.0)
    4. }
    5. }

    Remember, this event gets fired when the arrow hits the ground. Therefor it will create an explosion when it hits a block.
    NOTE: If you wish to have every arrow explode, stop here. If not, continue.

    Now, lets add a toggle via a command(You can follow my other Simple Toggles tutorial). At the top, create a ArrayList named explode.
    Code:java
    1. ArrayList<String> explode = new ArrayList<String>();


    On the command, check if the player is in the ArrayList explode, if not then add. If so then remove. NOTE: We are storing the player's name, so use player.getName();

    Code:java
    1. if (alias.equalsIgnoreCase("arrow")) {
    2. Player player = (Player) sender;
    3. if (explode.contains(player.getName())) {
    4. explode.remove(player.getName());
    5. } else {
    6. explode.add(player.getName());
    7. }
    8. }


    So now, we have it so it toggles if the player is on the ArrayList. Now, we are to make a nother event to asign a metadata when ever a arrow is Fired.
    Code:java
    1. @EventHandler
    2. public void onProjectileShoot(ProjectileLaunchEvent event) {
    3. Projectile projectile = event.getEntity();
    4. LivingEntity shooter = projectile.getShooter();
    5. if (shooter instanceof Player && (projectile instanceof Arrow && explode.contains(((Player) shooter).getName())) {
    6. projectile.setMetadata("Explosive", new FixedMetadataValue(Main.getPlugin(), true));
    7. }
    8. }
    9.  

    That was alot of code, so let me explain. First, it assigns the projectile(so we can use it as a shortcut). Then we get the person firing it. Now we check if the player is in the ArrayList explode, if so assign the Arrow a metadata.

    Now, lets edit our onHit event, and check if the Arrow has the metadata. If so, lets create na explosion.
    Code:java
    1. public void onHit(ProjectileHitEvent event) {
    2. if (event.getEntity() instanceof Arrow) {
    3. if (event.getEnttity().hasMetadata("Explosive")) {
    4. event.getEnttity().getWorld().createExplosion(event.getEntity().getLocation(), 4.0)
    5. }
    6. }
    7. }

    This is the finished version of the code. NOTE: This is from a diffirent class, so some of the code will be diffirent (EG. The event's name are diffirent). But they have the main ideas.

    FinishedVersion: http://pastebin.com/m9KX8TD0

    Overall, that is how to create an exploding arrow that is toggleable. If you have anyquestions, feel free to ask.
    Thanks for reading! Tell me if you like tutorials on doing cool things like this, instead of plain Java ones.

    - Jake
     
    CubieX likes this.
  2. Offline

    ZeusAllMighty11 Retired Staff

    Missing the event handler. You may want to mention this is a listener class so it must implement Listener and it also must be registered in the main class that extends JavPlugin.

    You are also using Bukkit.getWorld() which I believe requires a parameter (name), so that wouldn't work. Instead, use event.getEntity().getWorld()

    The strength of the explosion should also be as a float so instead of 4, use 4.0
     
  3. Offline

    JPG2000

  4. Offline

    CubieX

    Code:
    event.getEnttity().getWorld().createExplosion(event.getEntity().getLocation(), 4.0)
    When I tried this, it seemed like the explosion did not hurt the player or other entities.
    It will destroy blocks, but player damage seems not to come from this "explosion power" value.
    Only the projectile itself (like an arrow) seems to apply the damage. (1-2 HP or so)

    Is there a way to apply player damage by using an explosion, without having to damage the hit player manually?

    BTW: The docs say, a power value of "4F" is equivalent to TNT, but when I used 4F (= 79 dec), the effect was more like a fusion bomb. oO
    The exploding pieces were thrown about 50 blocks away and damaged everything they hit.
    However, I only suffered 1,5 HP damage.

    [​IMG]
     
    Goblom likes this.
  5. Offline

    Loogeh

    CubieX That explosion seems too large as well.
     
  6. Offline

    Ultimate_n00b

    How I would do this, is check for the explosion event from the arrow. Get all the blocks and call the proper events on the player who launched the arrow. That way all protection plugins will work right.
     
  7. Offline

    mamifsidtect

    I know I'm just gonna get something for necro-ing this thread, but hey, a man needs to do what a man needs to do. This no longer works, and I was hoping to implement this into a plugin I am making right now. So any help would be great! bukkit-1.7.2-R0.2
     
  8. Offline

    Paxination

Thread Status:
Not open for further replies.

Share This Page