Parts Of Code Working, Parts Not HELP!

Discussion in 'Plugin Development' started by mkezar, Feb 24, 2015.

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

    mkezar

    Hi. So im making a plugin that when you click a redstone block, it shoots you into the air and when you land, it will do damage. I have the shooting in the air part working (using fireballs). but when i come back down, it dosnt do the -1 hearts damage to the entitys below! here is my code
    Code:
    ackage plugins.mkezar;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Damageable;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class electro extends JavaPlugin implements Listener {
     
      @Override
      public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
      }
     
      @SuppressWarnings("deprecation")
      @EventHandler
      public void OnSwordClick(PlayerInteractEvent event) {
          Player player = event.getPlayer();
           
          if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
              if(player.getItemInHand().getType()== Material.REDSTONE_BLOCK){
                  if(player.getLevel() >= 45) {
                      Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                             public void run() {
                                 player.playSound(player.getLocation(),Sound.WITHER_SPAWN,1,1);
                                 player.setItemInHand(new ItemStack(Material.SULPHUR, 1));
                                 }
                        }, 2L);
                        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                             public void run() {
                                 ItemStack item = new ItemStack(Material.REDSTONE_BLOCK);
                                    player.getInventory().setItem(1, item);
                                 }
                        }, 300L);
                      Location loc = event.getPlayer().getLocation();
                      Block b = event.getPlayer().getTargetBlock(null, 200);
                      if (b != null) {
                          Location target = b.getLocation();
                          Location from = lookAt(loc, target);
                          from.getWorld().spawn(from, Fireball.class);
                          from.getWorld().spawn(from, Fireball.class);
                          from.getWorld().spawn(from, Fireball.class);
                      }
                      Block block = player.getLocation().add(0,0,0).getBlock();
                        if (!block.getType().equals(Material.AIR)) {
                        }else{
                            List <Entity> entities = player.getNearbyEntities(5,5,5);
                            for(Entity e : entities){
                                    Damageable d = (Damageable) e;
                                    d.setHealth(d.getHealth() - 5);
                            }
                        }
                  }
              }
          }
      }
      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;
      }
     
    }
    Where am i going wrong? Please Help :(

    plz? :(

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

    Gingerbreadman

    @mkezar can you explain what your trying to do again?
    and
    Code:
    d.setHealth(d.getHealth() - 5);
    should be
    Code:
    d.setHealth(d.getHealth() - 2);
    if you want one heart damage!
     
  3. Offline

    mkezar

    oh yeah :p i wanted 5 hearts damage :p so it would be - 10. so basically what i want, is when i get boosted in the air
    Code:
    from.getWorld().spawn(from, Fireball.class);
                          from.getWorld().spawn(from, Fireball.class);
                          from.getWorld().spawn(from, Fireball.class);
    when i came back down it would do damage
    Code:
    Block block = player.getLocation().add(0,0,0).getBlock();
                        if (!block.getType().equals(Material.AIR)) {
                        }else{
                            List <Entity> entities = player.getNearbyEntities(5,5,5);
                            for(Entity e : entities){
                                    Damageable d = (Damageable) e;
                                    d.setHealth(d.getHealth() - 10);
                            }
                        }
    but for some reason, when i land, it wont do the damage. none of it. so i want to figure out, what am i doing wrong that it is not doing damage when im landing?
     
  4. Offline

    Bluesocks

    @mkezar First of all, if you just want to give the player an upwards velocity, you'd be much better setting the players velocity to a positive Y. Also, you should probably be listening for when the player hits the ground and then run the code in the second snippet, therefore damage will be dealt when hitting the ground. Finally, I'm sure it would be better if you used d.damage(i), rather than setting the entityies' health x lower than it already is, so you don't ever run into setting the entities' health at lower than 0.
     
  5. Offline

    mkezar

    but if you set their Y, itll look like theyre being teleported to the sky, not shot upwards.
     
  6. Offline

    mythbusterma

    @mkezar

    He said set their velocity, not their position.
     
  7. Offline

    mkezar

    oh. lol :p
     
Thread Status:
Not open for further replies.

Share This Page