Solved Shoot 3 tnt [Need Help]

Discussion in 'Plugin Development' started by Josh014, Jan 21, 2014.

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

    Josh014

    Hey,
    So what I have now is I shoot 3 tnt in the direction I'm looking but not in the way I want them to spread out... I tried to edit the coordinates but it looked really weird and not what I expected to be like.

    I want the tnt be looked like this when I shoot them:

    Imgur

    Code:
    Code:java
    1. @EventHandler
    2. public void PlayerAbilities(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4.  
    5. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    6.  
    7. if (player.getInventory().getItemInHand().getType() == Material.TNT) {
    8.  
    9. TNTPrimed tnt1 = (TNTPrimed) player.getWorld().spawn(player.getEyeLocation().add(1, 0, 0), TNTPrimed.class);
    10. TNTPrimed tnt2 = (TNTPrimed) player.getWorld().spawn(player.getEyeLocation().add(0, 1, 0), TNTPrimed.class);
    11. TNTPrimed tnt3 = (TNTPrimed) player.getWorld().spawn(player.getEyeLocation().add(0, 0, 1), TNTPrimed.class);
    12.  
    13. tnt1.setVelocity(player.getEyeLocation().getDirection().multiply(2));
    14. tnt2.setVelocity(player.getEyeLocation().getDirection().multiply(2));
    15. tnt3.setVelocity(player.getEyeLocation().getDirection().multiply(2));
    16.  
    17. tnt1.setFuseTicks(40);
    18. tnt2.setFuseTicks(40);
    19. tnt3.setFuseTicks(40);
    20.  
    21. tnt1.setYield(2);
    22. tnt2.setYield(2);
    23. tnt3.setYield(2);
    24. }
    25. }
    26. }


    I know I did the x, y, z coordinate and that means it's the x, y, z coordinates of the world and not where the player is looking at.

    How to fix this?

    Thank you,

    I forgot to mention; Is it possible to explode a tnt right on hit with the ground/wall?

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

    Josh014

  3. Offline

    Josh014

  4. Offline

    Josh014

    Bump... Really no one?
     
  5. Offline

    Josh014

    Bump.... Come on....
     
  6. Offline

    Skatedog27

    OK, ok, enough bumping. You can infact make the tnt explode on impact with this event:
    Code:java
    1. public void onEntityHit(EntityHitEvent e) {
    2. if (e.getEntity instanceof TNTPrimed) {
    3. e.getEntity.getWorld().createExplosion(e.getEntity.getLocation(), 1.0f, false);
    4. }
    5. }
     
  7. Offline

    Josh014

    Skatedog27
    This will only create an explosion there where the tnt lands and it doesn't remove it...? Or am I wrong?
     
  8. Offline

    GrameTV

    These are the vectors I used for sth nearly the same like this:
    Code:java
    1.  
    2. //You can lower the spread factor by lowering the + 0.3 double
    3. Vector vec1 = new Vector(p.getLocation().getDirection().getX(), p.getLocation().getDirection().getY() + 0.3, p.getLocation().getDirection().getZ());
    4. Vector vec2 = new Vector(p.getLocation().getDirection().getX() + 0.3, p.getLocation().getDirection().getY() + 0.3, p.getLocation().getDirection().getZ());
    5. Vector vec3 = new Vector(p.getLocation().getDirection().getX(), p.getLocation().getDirection().getY() + 0.3, p.getLocation().getDirection().getZ() + 0.3);
    6.  


    If you create falling tnt blocks, you could listen for EntityChangeBlockEvent and spawn primedtnt at the hitlocation with fuse 0.

    ex:
    Code:java
    1. public void onTnTCollision(EntityChangeBlockEvent e) {
    2.  
    3. if(!(e.getEntity() instanceof FallingBlock)) return;
    4.  
    5. FallingBlock fb = (FallingBlock) e.getEntity();
    6.  
    7. e.setCancelled(true);
    8.  
    9. TNTPrimed tnt = fb.getWorld().spawn(fb.getLocation(), TNTPrimed.class);
    10. tnt.setFuseTicks(0);
    11. }


    Or, if you want to directly spawn primedtnt, you have to, like Skatedog27 said, listen for the EntityHitEvent:

    Code:java
    1. public void onEntityHit(EntityHitEvent e) {
    2. if (e.getEntity instanceof TNTPrimed) {
    3.  
    4. TNTPrimed tnt = (TNTPrimed) e.getEntity();
    5. //Force the tnt to explode
    6. tnt1.setFuseTicks(0);
    7.  
    8. }
    9. }


    or you could also do:

    Code:java
    1. public void onEntityHit(EntityHitEvent e) {
    2. if (e.getEntity instanceof TNTPrimed) {
    3.  
    4. TNTPrimed tnt = (TNTPrimed) e.getEntity();
    5. tnt.getWorld().createExplosion(tnt.getLocation(), 1.0f, false);
    6. tnt.remove();
    7.  
    8. }
    9. }


    I didn't test the code, but it should work
     
  9. Offline

    Josh014

    GrameTV, Nice thanks man gonna check it out when I'm back home :D.

    GrameTV. Alright I'm home and I did some tests with the spread but how can I do it that it shoots the same if a player looks in any direction?

    GrameTV and I just saw that "EntityHitEvent" doesn't exist.

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

    GrameTV

    It shoots the same, it just follows the direction. I assumed that Skatedog27 said the truth, but i didn't check for its existance. But anyways, you should do it with Falling Blocks I think.
     
  11. Offline

    Skatedog27

    Josh014 that will should remove the tnt, after all, it is blowing up. You can also change the amount of ticks until the tnt explodes.. however.. I do not know how to do that off of the top of my head
     
  12. Offline

    Josh014

    Skatedog27
    The "EntityHitEvent" doesn't exist...
     
  13. Offline

    Skatedog27

    Are you sure?? Hmmmm.... does your code look like this?
    Code:java
    1. @EventHandler
    2. public void onEntityHit(EntityHitEvent event) {
    3. //stuff
    4. }
     
  14. Offline

    Josh014

    Skatedog27,
    Yes there is no such as "EntityHitEvent", there is a "ProjectileHitEvent". I don't know if you meant that one?
     
  15. Offline

    Skatedog27

    hmmm... thats
    thats odd. Primed TNT isnt a projectile. It is an entity. Tomorrow I will code a plugin for what you are trying to do, and paste my code here, so you can see how I did it. Until then, im getting to bed. So tired, otherwise I would do it right now
     
  16. Offline

    Josh014

  17. Offline

    Skatedog27

    This works just fine, apparently, the ProjectileHitEvent can call for entities as well as projectiles.
    Code:java
    1. @EventHandler
    2. public void onProjectileHit(ProjectileHitEvent e) {
    3. if (e.getEntity() instanceof TNTPrimed && e.getEntity().getShooter() instanceof Player) {
    4. e.getEntity().getWorld().createExplosion(e.getEntity().getLocation(), 1.0f, false);
    5. }
    6. return;
    7. }
     
  18. Offline

    Josh014

    Skatedog27
    Thanks man! It worked fine for me :).
     
Thread Status:
Not open for further replies.

Share This Page