Summoning a fireball

Discussion in 'Plugin Development' started by Butkicker12, Oct 9, 2011.

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

    Butkicker12

    Im not even sure if this is the correct way to summon a fireball and launch it at p.getTargetBlock(null, 200);


    The current code I have (which is proboally incorrect) And untested because of errors:

    http://pastie.org/2667558
     
  2. Offline

    Adamadz

    I want to find out how to summon a Fireball too :p

    You could probs find out by looking at the source of this

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
    Butkicker12 likes this.
  3. Offline

    bergerkiller

    It's not all that hard. Few components:
    - Location to fire from:
    Code:
    Location loc = event.getPlayer().getLocation();
    - Location to aim at:
    Code:
    Block b = event.getPlayer().getTargetBlock(null, 200);
    if (b != null) {
        Location target = b.getLocation();
    
    }
    - LookAt/AimAt function:
    Code:
        public static Location lookAt(Location loc, Location lookat) {   
            //Clone the loc to prevent applied changes to the input loc
            loc = loc.clone();
            
            // Values of change in distance (make it relative)
            double dx = lookat.getX() - loc.getX();
            double dy = lookat.getY() - loc.getY();
            double dz = lookat.getZ() - loc.getZ();
            
            // Set yaw
            if (dx != 0) {
                // Set yaw start value based on dx
                if (dx < 0) {
                    loc.setYaw((float) (1.5 * Math.PI));        
                } else {
                    loc.setYaw((float) (0.5 * Math.PI));    
                }
                loc.setYaw((float) loc.getYaw() - (float) Math.atan(dz / dx));
            } else if (dz < 0) {
                loc.setYaw((float) Math.PI);
            }
            
            // Get the distance from dx/dz
            double dxz = Math.sqrt(Math.pow(dx, 2) + Math.pow(dz, 2));
            
            // Set pitch
            loc.setPitch((float) -Math.atan(dy / dxz));
            
            // Set values, convert to degrees (invert the yaw since Bukkit uses a different yaw dimension format)
            loc.setYaw(-loc.getYaw() * 180f / (float) Math.PI);
            loc.setPitch(loc.getPitch() * 180f / (float) Math.PI);
            
            return loc;
        }
    - Combine them:
    Code:
    Location from = lookAt(from, target);
    from.getWorld().spawn(from, FireBall.class);
     
  4. Offline

    Butkicker12

    Thanks, I think this will work. http://pastie.org/2667915
    One error though.
    Location from = lookAt(from, target);
    The local variable from may not have been initialized
     
  5. Offline

    bergerkiller

    (from, target) -> (loc, target) :)
     
  6. Offline

    Butkicker12

    Thanks

    It doesnt seem to want to go to the traget block, it spawns and explodes at the player.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  7. Offline

    Celeixen

    can you just spawn a fireball at the block the player is looking at, although it wont give the desired effect it would be better than nothing. (Try this just for testing purposes, just wanna make sure your getting the correct block).
     
  8. Offline

    dralletje

    I think you have to spawn the fireball on block away from the player, or above the player. So it will not touch the player
     
  9. Offline

    Butkicker12

    Yeah you are right, I think I got it last night. Im going to go test now.
     
Thread Status:
Not open for further replies.

Share This Page