TNT Explosion Launches Player

Discussion in 'Plugin Development' started by MyNameIsHariK, Oct 22, 2013.

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

    MyNameIsHariK

    Hey Bukkit Community,

    I need your help to find a code to launch a player when they are hit by with 1 block of tnt. Basically I have coded a plugin where you right click with a stick it launches a tnt towards where the player is facing and I want it when it hits(explosion interaction)the player they launch up in the air.

    Thanks for reading,
    Hari

    Anyone help please?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  2. Offline

    WavyWonderz

    You could create an HashMap of tnt and players, when a player fires the tnt, add the tnt entity to the list with the player that fired it. Then when a tnt explodes, check to see if the tnt is part of the list. If it is part of it, get the player and add a bunch of Y-velocity to him
     
  3. Offline

    MyNameIsHariK

    So how would I do that?
     
  4. Offline

    NathanWolf

    Start with the simplest part of what you want to do, and work from there.

    I'd suggest starting with an EntityDamageEvent handler, check for event.getCause() == DamageCause.BLOCK_EXPLOSION (er, I think? Maybe ENTITY_EXPLOSION?)

    Then use event.getEntity().(new Vector(0, 5, 0));

    This should make it so players (or any entity) fly up anytime they're exploded by anything.. then it's up to you to make some more complex checking as others have suggested, to make the effects more specific. You might also be able to modify the tnt entity itself (maybe change its display name) to something you can check for, rather than having to track them all yourself.
     
    MyNameIsHariK likes this.
  5. Offline

    MyNameIsHariK

  6. Offline

    NathanWolf

    Sure, it's basically just putting together the pieces from my last post:

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent event)
    3. {
    4. if (event.getCause() == DamageCause.BLOCK_EXPLOSION || event.getCause() == DamageCause.ENTITY_EXPLOSION) {
    5. event.getEntity().setVelocity(new Vector(0, 3, 0));
    6. }
    7. }


    I tested that out, it works- and is pretty fun. Exploded entities go flying way up in the air.
     
    MyNameIsHariK likes this.
  7. Offline

    MyNameIsHariK

    NathanWolf Thanks!

    NathanWolf Does the player have to get damaged(hurt(lose hearts) to launch?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  8. Offline

    Garris0n

    Yes.
     
  9. Offline

    MyNameIsHariK

    Garris0n Is there a way to make it so the player does not get damaged?
     
  10. Offline

    NathanWolf

    Cancel the event (event.setCancelled) to avoid the player taking damage.
     
  11. Offline

    MyNameIsHariK

    NathanWolf how would I do that :p Im a noob
     
  12. Offline

    NathanWolf

    Just call event.setCancelled(true) :

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent event)
    3. {
    4. if (event.getCause() == DamageCause.BLOCK_EXPLOSION || event.getCause() == DamageCause.ENTITY_EXPLOSION) {
    5. event.getEntity().setVelocity(new Vector(0, 3, 0));
    6. event.setCancelled(true);
    7. }
    8. }


    EDIT: I also just noticed I was checking for BLOCK_EXPLOSION twice, I meant to check for ENTITY_EXPLOSION as well. I'm not actually sure which one you want, but you can experiment and see once it's working (experimentation is the key to learning! ;)

    Being a newbie is great, we're always happy to see new people eager to learn! Definitely spend some time reading the tutorials on the wiki, and take the time to set up a good IDE if you haven't already. Something like Eclipse can really speed along the learning process, and really helps find all the annoying little errors that it will take a while to learn to spot on your own.
     
  13. Offline

    MyNameIsHariK

    @N
    NathanWolf
    Code:java
    1. Fireball fb = e.getPlayer().launchProjectile(Fireball.class);

    How would I make that so it shoots TNT?
     
  14. Offline

    NathanWolf

    Well you can't unfortunately launch anything that's not actually a Projectile, and TNT is not.

    Fortunately for you I have some code that does pretty much this though:

    GrenadeSpell.java
     
  15. Offline

    WavyWonderz


    Instead of setting their velocity to a vector, it's always good to add it. That way if they are moving in a certain direction, they won't stop and go straight up, they'd go straight up, and also whatever velocity they had before. (If the player is standing still in both cases, the result will be the same)

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageEvent event){
    3. if (event.getCause() == DamageCause.BLOCK_EXPLOSION || event.getCause() == DamageCause.ENTITY_EXPLOSION) {
    4. event.getEntity().addVelocity(new Vector(0, 3, 0));
    5. event.setCancelled(true);
    6. }
    7. }
     
    NathanWolf likes this.
Thread Status:
Not open for further replies.

Share This Page