Heal Stick

Discussion in 'Plugin Development' started by xXCapzXx, Aug 8, 2015.

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

    xXCapzXx

    Hey guys, so I have made my heal stick really well with a friend. But right now, I am stuck on a problem, it is mean't to right click the person your looking at and heal them from 12 blocks.
    But if you right click from 100 blocks when your looking at them, it still works, so I want it only from 12 blocks, and if your out of 12 blocks, it will tell you.
    Code:
    @EventHandler
    
        public void click(PlayerInteractEvent e) {
    
            Player player = e.getPlayer();
    
            Location loc = player.getLocation();
    
            if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    
                if(e.getItem().getType() == Material.INK_SACK) {
    
                    Vector vec = player.getLocation().getDirection();
    
                    int timesToLoopUntilTheEnd = 15; //Can be changed
    
                    for(int i =0; i < timesToLoopUntilTheEnd; i++) {
    
                        for(Player t:Bukkit.getOnlinePlayers()) {
    
                            if(vec.toLocation(player.getWorld()).distanceSquared(t.getLocation()) <= 1) {
    
                            t.setHealth(8);
    
                            }
    
                            }
    
                        }
    
                  }
    
                }
    
               
    
     
  2. Invisible

    Reynergodoy

    again the same thread?
    and you're not healing, you're just setting the health to 8, if the has 10 of health you're lowering the hp '-'
     
  3. Offline

    xXCapzXx

    Oh yeah, it lowers, didn't add that xD.

    This new thread is for something different, and new code.
     
  4. Offline

    teej107

    umm...... what's with this for-loop?
     
  5. Offline

    xXCapzXx

    My friend did that, doesn't make much sense, I'll delete it.
     
  6. Offline

    teej107

    @xXCapzXx Now that we got that cleared away, it would be better to loop through the player's in the world rather than every player on the server. Now your logic error is in your if statement. Since you want to check to see if they are 12 blocks away, you need to check if their distance squared is 144 or less.
     
  7. Offline

    xXCapzXx

    And how would I do that?
     
  8. Offline

    teej107

    @xXCapzXx Do what exactly? Looping through the players or checking the distance? :p
     
  9. Offline

    xXCapzXx

    Since you want to check to see if they are 12 blocks away, you need to check if their distance squared is 144 or less.
     
  10. Offline

    teej107

    It's not that hard. Just one modification of your if statement.
     
  11. Offline

    xXCapzXx

    Like this?

    Code:
                       for(int i =0; i <= player.getLocation().distanceSquared(t.getLocation() ); i++) {
    
    But next to t.getLocation, it doesn't allow me to add <= 144
     
  12. Offline

    teej107

    @xXCapzXx No. That's a for-statement. I said an if-statement. You want to look at this piece of code here:
    Sometimes it helps to say out loud what the code statement means.
     
  13. Offline

    xXCapzXx

    I switched the code around. That code isn't good.

    How does this look?
    Code:
    @EventHandler
    
        public void click(PlayerInteractEvent e) {
    
    Boolean broken = false;
    
            Player player = e.getPlayer();
    
            Location loc = player.getEyeLocation();
    
            if(e.getAction() == Action.RIGHT_CLICK_AIR) {
    
                if(e.getItem().getType() == Material.INK_SACK) {
    
                    Vector vec = player.getLocation().getDirection();
    
                    int timesToLoopUntilTheEnd = 15; //Can be changed
    
                    for(int i =0; i < timesToLoopUntilTheEnd; i++) {
    
                    if(broken){
    
                    break;
    
                    }
    
               
    
                        for(Player t:Bukkit.getOnlinePlayers()) {
    
                            if(loc.add(vec).distanceSquared(t.getLocation()) <= 1.5*1.5){ 
    
                            if(t == player){
    
                            continue;
    
                            }
    
                                t.setHealth(20.0);
    
                                t.sendMessage(ChatColor.RED + "You were healed by " + ChatColor.BLUE + player.getName() + ChatColor.RED + "!");
    
                                player.sendMessage(ChatColor.RED + "You healed " + ChatColor.BLUE + t.getName() + ChatColor.RED + "!");
    
                                broken = true;
    
                                break;
    
                            }
    
                        }
    
                    }
    
                }
    
                 
    
              }
    
    }
    
    }
    
    
     
  14. Offline

    Zombie_Striker

    @xXCapzXx
    1. Are you sure this should only happen when the player is not looking at a block, or do you want it if the player right clicks anything?
    2. Are you sure that e.getItem isn't null? This already would ruin your event.
    3. Why are you not getting the players near the player by using player.getNearbyEntities(distance,distance,distance). Although you would have to check if the entities are players, it will return only the players near the player.
     
  15. Offline

    xXCapzXx

    I am not sure about anything, thats why I need your guys help.
     
  16. Offline

    Zombie_Striker

    @xXCapzXx
    Alright, then do the following:
    1. For you if statement that checks if the player is right clicking air, add an or statement( add || ) and check if the player is RIGHTCLICKBLOCK
    2. Add an if statement that checks i f .getItem != null ( != null means not equal null)
    3. for the for loop, instead of getting players, change the Player to Entitiy and change the Bukkit.getonlineplayers to event.getPlayer().getNearbyEntities(the distance, the distance, the distance), Then add an if statement that checks if the entitiy is a Player, and if so, heal them.
     
  17. Offline

    xXCapzXx

    I only did a few, but here is what I added
    Code:
    @EventHandler
    
        public void click(PlayerInteractEvent e) {
    
    Boolean broken = false;
    
            Player player = e.getPlayer();
    
            Location loc = player.getEyeLocation();
    
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    
                if(e.getItem().getType() == Material.INK_SACK) {
    
                    Vector vec = player.getLocation().getDirection();
    
                    int timesToLoopUntilTheEnd = 15; //Can be changed
    
                    for(int i =0; i < timesToLoopUntilTheEnd; i++) {
    
                    if(broken){
    
                    break;
    
                    }
    
               
    
                        for(Entity t: e.getPlayer().getNearbyEntities(12, 12, 12)) {
    
                            if(loc.add(vec).distanceSquared(t.getLocation()) <= 1.5*1.5){ 
    
                            if(t == player){
    
                            continue;
    
                            }
    
                            if(e.getItem() != null) {
    
                                ((Player) t).setHealth(20.0);
    
                                ((Player) t).sendMessage(ChatColor.RED + "You were healed by " + ChatColor.BLUE + player.getName() + ChatColor.RED + "!");
    
                                player.sendMessage(ChatColor.RED + "You healed " + ChatColor.BLUE + ((Player) t).getName() + ChatColor.RED + "!");
    
                                broken = true;
    
                                break;
    
                            }
    
                            }
    
                        }
    
                    }
    
     
  18. Offline

    Zombie_Striker

    @xXCapzXx
    Good, but:

    A) You don't need the if (loc.add) because you already know the entity is close to the player. and

    B) You're blindly casting t to be a player. You have nothing that is checking if 't' is a player.
     
  19. Offline

    xXCapzXx

    Here is my code
    Code:
    @EventHandler
    
        public void click(PlayerInteractEvent e) {
    
    Boolean broken = false;
    
            Player player = e.getPlayer();
    
            Location loc = player.getEyeLocation();
    
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    
                if(e.getItem().getType() == Material.INK_SACK) {
    
                    Vector vec = player.getLocation().getDirection();
    
                    int timesToLoopUntilTheEnd = 15; //Can be changed
    
                    for(int i =0; i < timesToLoopUntilTheEnd; i++) {
    
                    if(broken){
    
                    break;
    
                    }
    
               
    
                        for(Entity t: e.getPlayer().getNearbyEntities(12, 12, 12)) {
    
                            if(t == player){
    
                            continue;
    
                            }
    
                            if(e.getItem() != null && t == player) {
    
                                t.setHealth(20.0);
    
                                t.sendMessage(ChatColor.RED + "You were healed by " + ChatColor.BLUE + player.getName() + ChatColor.RED + "!");
    
                                player.sendMessage(ChatColor.RED + "You healed " + ChatColor.BLUE + t.getName() + ChatColor.RED + "!");
    
                                broken = true;
    
                                break;
    
                            }
    
                            }
    
                        }
    
                    }
    
                }
    
    I don't get what to do about on your B).
     
  20. Offline

    Zombie_Striker

    Code:
    for(Entity t: e.getPlayer().getNearbyEntities(12, 12, 12)) {
    
                            if(e.getItem() != null && t != player&&t instanceof Player) {
    
                                t.setHealth(20.0);
    
                                t.sendMessage(ChatColor.RED + "You were healed by " + ChatColor.BLUE + player.getName() + ChatColor.RED + "!");
    
                                player.sendMessage(ChatColor.RED + "You healed " + ChatColor.BLUE + t.getName() + ChatColor.RED + "!");
    
    //                    I don't understand why you have this boolean and the break. Is it that you only what to heal one player near you?
                                broken = true;
                                break;
    
                            }
    
                            }
     
  21. Offline

    xXCapzXx

    Thanks, I want it to heal the player you are LOOKING at within 12 blocks.

    Like dis?
    Code:
    @EventHandler
    
        public void click(PlayerInteractEvent e) {
    
    Boolean broken = false;
    
            Player player = e.getPlayer();
    
            Location loc = player.getEyeLocation();
    
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    
                if(e.getItem().getType() == Material.INK_SACK) {
    
                    Vector vec = player.getLocation().getDirection();
    
                    int timesToLoopUntilTheEnd = 15; //Can be changed
    
                    for(int i =0; i < timesToLoopUntilTheEnd; i++) {
    
                    if(broken){
    
                    break;
    
                    }
    
               
    
                            for(Entity t: e.getPlayer().getNearbyEntities(12, 12, 12)) {
    
                             
    
                                    if(e.getItem() != null && t != player&&t instanceof Player) {
    
             
    
                                        t.setHealth(20.0);
    
             
    
                                        t.sendMessage(ChatColor.RED + "You were healed by " + ChatColor.BLUE + player.getName() + ChatColor.RED + "!");
    
             
    
                                        player.sendMessage(ChatColor.RED + "You healed " + ChatColor.BLUE + t.getName() + ChatColor.RED + "!");
    
             
    
                             
    
                            }
    
                            }
    
                        }
    
     
  22. Offline

    SantaClawz69

    I'm sure you can figure it out from what you already have. All the resources are already within your code.
    @Zombie_Striker has been generous enough to give you all this code, the best way to learn is to try to do it yourself. Try to see if you can figure it out yourself with the Java Docs or Bukkit Wiki. @xXCapzXx
     
  23. Offline

    Chiller

    @xXCapzXx Here you go, this gets the entity a player is LOOKING at within a certain range.

    To use it: LivingEntity target = getTarget(event.getPlayer(), 12);
    if (target != null) { target.damage(1); }

    Code:
    public static LivingEntity getTarget(LivingEntity startEntity, int maxDistance)
    {
        List<Entity> nearbyE = startEntity.getNearbyEntities(maxDistance, maxDistance, maxDistance);
        List<LivingEntity> livingE = new ArrayList<LivingEntity>();
       
        for (Entity e : nearbyE)
        {
            if (e instanceof LivingEntity)
            {
                livingE.add((LivingEntity) e);
            }
        }
       
        BlockIterator bItr = new BlockIterator(startEntity, maxDistance);
       
        Block block;
        Location loc;
       
        int bx, by, bz;
        double ex, ey, ez;
       
        // Loop through player's line of sight
        while (bItr.hasNext())
        {
            block = bItr.next();
           
            bx = block.getX();
            by = block.getY();
            bz = block.getZ();
           
            // Check for entities near this block in the line of sight
            for (LivingEntity e : livingE)
            {
                loc = e.getLocation();
               
                ex = loc.getX();
                ey = loc.getY();
                ez = loc.getZ();
               
                if ((bx - .75 <= ex && ex <= bx + 1.75) && (bz - .75 <= ez && ez <= bz + 1.75) && (by - 1 <= ey && ey <= by + 2.5))
                {
                    // Entity looking at is close enough to be picked
                   
                    return e;
                }
            }
        }
       
        return null;
       
    }
    
     
  24. Offline

    xXCapzXx

    I still am not sure?
    Code:
    @EventHandler
    
        public void click(PlayerInteractEvent e) {
    
        Boolean broken = false;
    
    
    
            Player player = e.getPlayer();
    
    
    
            Location loc = player.getEyeLocation();
    
    
    
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    
    
    
                if(e.getItem().getType() == Material.INK_SACK) {
    
    
    
                    Vector vec = player.getLocation().getDirection();
    
    
    
                    int timesToLoopUntilTheEnd = 15; //Can be changed
    
    
    
                    for(int i =0; i < timesToLoopUntilTheEnd; i++) {
    
    
    
                    if(broken){
    
    
    
                    break;
    
    
    
                    }
    
    
    
               
    
    
    
                     
    
    
    
        for(Entity t: e.getPlayer().getNearbyEntities(12, 12, 12)) {
    
       
    
        if(e.getItem() != null && t != player&&t instanceof Player) {
    
         
    
                                t.setHealth(20.0);
    
    
    
                                t.sendMessage(ChatColor.RED + "You were healed by " + ChatColor.BLUE + player.getName() + ChatColor.RED + "!");
    
    
    
                                player.sendMessage(ChatColor.RED + "You healed " + ChatColor.BLUE + t.getName() + ChatColor.RED + "!");
    
    
    
                                broken = true;
    
    
    
                                break;
    
    
    
                            }
    
         
    
    
    
                            }
    
    
    
                        }
    
    
    
                    }
    
    
    
                }
    
          }
    
    }
    
     
  25. Offline

    Chiller

  26. Offline

    xXCapzXx

    Bump.
    There are errors.
     
  27. Offline

    Zombie_Striker

  28. Offline

    xXCapzXx

    Code:
    public static LivingEntity getTarget(LivingEntity startEntity, int maxDistance)
    
    {
    
        List<Entity> nearbyE = startEntity.getNearbyEntities(maxDistance, maxDistance, maxDistance);
    
        List<LivingEntity> livingE = new ArrayList<LivingEntity>();
    
       
    
        for (Entity e : nearbyE)
    
        {
    
            if (e instanceof LivingEntity)
    
            {
    
                livingE.add((LivingEntity) e);
    
            }
    
        }
    
       
    
        BlockIterator bItr = new BlockIterator(startEntity, maxDistance);
    
       
    
        Block block;
    
        Location loc;
    
       
    
        int bx, by, bz;
    
        double ex, ey, ez;
    
       
    
        // Loop through player's line of sight
    
        while (bItr.hasNext())
    
        {
    
            block = bItr.next();
    
           
    
            bx = block.getX();
    
            by = block.getY();
    
            bz = block.getZ();
    
           
    
            // Check for entities near this block in the line of sight
    
            for (LivingEntity e : livingE)
    
            {
    
                loc = e.getLocation();
    
               
    
                ex = loc.getX();
    
                ey = loc.getY();
    
                ez = loc.getZ();
    
               
    
                if ((bx - .75 <= ex && ex <= bx + 1.75) && (bz - .75 <= ez && ez <= bz + 1.75) && (by - 1 <= ey && ey <= by + 2.5))
    
                {
    
                    // Entity looking at is close enough to be picked
    
                   
    
                    return e;
    
                }
    
            }
    
        }
    
       
    
        returnnull;
    
       
    
    }
    
    
    
    @EventHandler
    
        public void click(PlayerInteractEvent e) {
    
        Boolean broken = false;
    
    
    
            Player player = e.getPlayer();
    
    
    
            Location loc = player.getEyeLocation();
    
    
    
            if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    
    
    
                if(e.getItem().getType() == Material.INK_SACK) {
    
    
    
                    Vector vec = player.getLocation().getDirection();
    
    
    
                    int timesToLoopUntilTheEnd = 15; //Can be changed
    
    
    
                    for(int i =0; i < timesToLoopUntilTheEnd; i++) {
    
    
    
                    if(broken){
    
    
    
                    break;
    
                   
    
                   
    
                   
    
                    }
    
    
    
               
    
    
    
                     
    
    
    
        for(Entity t: e.getPlayer().getNearbyEntities(12, 12, 12)) {
    
        getTarget(e.getPlayer(), 12);
    
        if (t != null) {
    
        }
    
       
    
        if(e.getItem() != null && t != player&&t instanceof Player) {
    
         
    
                                t.setHealth(20.0);
    
    
    
                                t.sendMessage(ChatColor.RED + "You were healed by " + ChatColor.BLUE + player.getName() + ChatColor.RED + "!");
    
    
    
                                player.sendMessage(ChatColor.RED + "You healed " + ChatColor.BLUE + t.getName() + ChatColor.RED + "!");
    
    
    
                            }
    
         
    
    
    
                            }
    
    
    
                        }
    
    
    
                    }
    
    
    
                }
    
    Keeps telling me to cast t, if the whole code is incorrect, can you edit it for me?
     
  29. Offline

    Chiller

    @xXCapzXx Thats not how we help, you have to help yourself, try googling what the error is about casting it. Also try understaning what the method that I gave you does before you ask for help again!
     
  30. Offline

    xXCapzXx

    Umm, help?
    Please, I really need it.
     
Thread Status:
Not open for further replies.

Share This Page