Get Mobs in front of a player, within the Line of Sight and within a certain distance

Discussion in 'Plugin Development' started by leland22, Mar 3, 2014.

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

    leland22

    I have no clue how to do what I posted in the title. I have spent a few hours working on it, I've tried player.getLocation().getDirection() and I still cant get it. If anyone can help that would be greatly appreciated.

    List of Requirements for my plugin:
    1. Gets the players Line of Sight
    2. If a mob is within the line of sight and within say 2 blocks execute code
    3.Get the name of the mob
    4. If there is more than one mob only select one.

    My Code so far:
    Code:java
    1.  
    2. // Gets the entity nearby the player and calls the remove method
    3. @EventHandler
    4. @SuppressWarnings("deprecation")
    5. public void onLeftClick(PlayerInteractEvent event) {
    6. Player p = event.getPlayer();
    7. if (event.getAction() == Action.RIGHT_CLICK_AIR
    8. || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    9. if (p.isSneaking()) {
    10. if (p.getItemInHand().getType() == Material.WEB){
    11. if (p.getItemInHand().hasItemMeta()) {
    12. if (p.getItemInHand().getItemMeta().hasDisplayName()) {
    13. if (p.getItemInHand().getItemMeta()
    14. .getDisplayName()
    15. .contains("An empty animal trap")) {
    16.  
    17. for (Entity e : p.getNearbyEntities(3.0, 2.0,
    18. 3.0)) {
    19. if(p.hasLineOfSight(e)){
    20. System.out.println("LoS!");
    21. }
    22. for (Block b : p.getLineOfSight(null, 2)) {
    23. if (b.getLocation().distance(
    24. e.getLocation()) < 2) {
    25. remove(e);
    26. } else {
    27. p.sendMessage(ChatColor.DARK_RED
    28. + "Test1");
    29. }
    30. }
    31. }
    32. }
    33. }
    34. }
    35. }
    36. }
    37. }
    38. }


    Arche
    Tagged you because you're knowledgeable, sorry if it annoys you
     
  2. Offline

    Norbu10


    Code:
    I dont know so much but it is e.remove(); Not remove(e);
     
  3. Offline

    Wolfey

    It's his own method, I believe.
     
    Norbu10 likes this.
  4. Offline

    Norbu10

    Ok sorry!
     
  5. Offline

    Barinade

    What seems to be wrong with what you have currently?
     
  6. Offline

    leland22

    Barinade Norbu10 It removes every entity the player can see within 2 blocks instead of just one and when it fails to find any entities the message I want the player to get never sends
     
  7. Offline

    Barinade

    add "break;" after you remove an entity (break stops the current loop)
     
  8. Offline

    leland22

    Barinade That stops the loop but I still get every entity around the player instead of just one that the player is looking at.
     
  9. Offline

    Barinade

    On a single left click?





    • if(p.hasLineOfSight(e)){
      System.out.println("LoS!");
      }

      Your should use e.remove(); in here, as the entity is within 3 blocks
      (Entity e : p.getNearbyEntities(3.0, 2.0,3.0))



      Edit: This fucking formatting is really starting to piss me off.
     
  10. Offline

    fearless1333


    You need to break out of the loop in which you're iterating through the nearby entities. If you add break in the inner loop it will not have any visible effect on your code (though it makes it slightly more optimized).

    Something like this would work

    Code:java
    1.  
    2. boolean entRemoved = false;
    3. for (Entity e : p.getNearbyEntities(3.0, 2.0, 3.0)) {
    4. if(p.hasLineOfSight(e)){
    5. System.out.println("LoS!");
    6. }
    7. for (Block b : p.getLineOfSight(null, 2)) {
    8. if (b.getLocation().distance(
    9. e.getLocation()) < 2) {
    10. remove(e);
    11. entRemoved = true;
    12. break;
    13. } else {
    14. p.sendMessage(ChatColor.DARK_RED
    15. + "Test1");
    16. }
    17. if (entRemoved) {
    18. break;
    19. }
    20. }
    21.  


    However, there's actually no need for two loops. This would work better.

    Code:java
    1.  
    2. for (Entity e : p.getNearbyEntities(3.0, 2.0, 3.0)) {
    3. if (p.hasLineOfSight(e)){
    4. System.out.println("LoS!");
    5. }
    6. else {
    7. continue;
    8. }
    9. if (p.getLocation().distance(e.getLocation()) <= 2.0) {
    10. remove(e);
    11. break;
    12. }
    13. }
    14.  


    EDIT: fixed code
     
Thread Status:
Not open for further replies.

Share This Page