How to detect an entity the player is looking at

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, Apr 11, 2016.

Thread Status:
Not open for further replies.
  1. Im new to coding kinda, but how do i detect if a player is looking at an entity? and how would i select that entity for a potion effect?
     
  2. Get all entitys in a radius from the player with:
    Code (Text):
    player.getNearbyEntities(10, 10, 10);
    The loop thru the returned list with entities and do whatever you want..
    Code (Text):
    for(Entity e : player.getNearbyEntities(10, 10, 10)){
        //Example
        if(e.getType() == EntityType.COW){
            LivingEntity cow = (LivingEntity) e;
            cow.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 10, 10));
        }
    }
     
    • Agree Agree x 2
    • Informative Informative x 1
  3. Ok this is very helpful, but how do i change it so that it selects the entity i am looking towards?
     
  4. I'm not entirely sure how to get the exact entity the player is looking at, by i think you can do something with getting the directions of the player and the entity and then compare the two directions or something..
     
  5. Ok i understand how to get the player direction (i think), but how would i test if an entity is in that direction?
     
  6. Aren't there like thousands of threads about this? xD
     
  7. Anyways here is what I use to get what a player is looking at, seems rather optimized.

    Code (Text):
      private boolean getLookingAt(Player player, Player player1)
      {
        Location eye = player.getEyeLocation();
        Vector toEntity = player1.getEyeLocation().toVector().subtract(eye.toVector());
        double dot = toEntity.normalize().dot(eye.getDirection());
       
        return dot > 0.99D;
      }
    I believe @DarkSeraphim helped me make this, so props to him if he did :3
     
    • Like Like x 2
    • Informative Informative x 1
  8. I guess that would work, just repalce the second parameter (player1) with a Entity and then loop thru the returned list and check if the entity is in sight of the player with the method.
     
    • Agree Agree x 1
  9. I had to use eye location so ya :p
     
  10. I have saw and tried my best to use them. But i also want to understand whats happening and every time i tried adding it to my code it gives me so many errors and when i get most of them sorted on link 3 (http://bukkit.org/threads/solved-get-the-entity-a-player-is-looking-at.40592/#post-793498)
    I cannot find out how to clear the error messages on the void

    here is that code:

    Code (Text):

    package com.thegamerplayz.mmoclasses.commands;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    public class MageLevitation implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command command,

                String label, String[] args) {

           

            if(label.equalsIgnoreCase("investigate")){

           

                if (!(sender instanceof Player)) {

                    sender.sendMessage("Silly Robot, This command is for players!");

                    return false;
                    }

            Player player = (Player) sender;
           
            if(player.hasPermission("MMOClasses.mage.levitation")){
           
                void getTarget() {
                       List<Entity> nearbyE = player.getNearbyEntities(10, 10, 10);
                       ArrayList<LivingEntity> livingE = new ArrayList<LivingEntity>();
                 
                       for (Entity e : nearbyE) {
                           if (e instanceof LivingEntity) {
                               livingE.add((LivingEntity) e);
                           }
                       }
                 
                       plugin.target = null;
                       BlockIterator bItr = new BlockIterator(plugin.player, plugin.range);
                       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 is close enough, set target and stop
                                                       plugin.target = e;
                                                       break;
                                               }
                                       }
                               }
                 
                           }
               
               
            }
        return false;
            }
            return true;
        }
    }
     
    Line 39 error:
    Multiple markers at this line
    - Syntax error on token "void", new expected
    - getTarget cannot be resolved to a type

    Line 41 error:
    Multiple markers at this line
    - Syntax error, insert ";" to complete BlockStatements
    - Syntax error, insert "}" to complete ClassBody
     
  11. Thxs camden for the code. Its much easier to read for me, but Grossen how would i do what you said? I am more of a person who learns by seeing and I don't have any other experience with this kind of thing to build off of
     
    • Friendly Friendly x 1
  12. Ok, but how do i change it so that it can affect other mobs and not just players.
     
  13. Not to be rude, but if you know the basics of Java (methods, objects etc..) should you be able to do that without any problem.
    I would recommend this for learning the basics of Java as it seems you have some basic problems here..
    https://www.codecademy.com/learn/learn-java
     
  14. I have used code academy, but how would Code academy teach me about minecraft plugins? I understand the basics of java and rarely run into problems with Java itself but where it merges with bukkit.
     
  15. You didn't know how to change a parameter in a method, isn't that basic Java?
     
  16. Maybe i should rephrase myself. I learned java the first time purely internet free mostly. I really liked programming, but due to poor choices of a step father on my mom, i only had a notebook to write on. So i learned how to do nothing but pure basic java. I barely even know what void is and i generally try to avoid them when i can. When i tried code academy before it didn't even mention void. and i can never find good tutorials for this kind of stuff as they just say when to use it and not what it does. If you know of a good tutorial for this it would be greatly appreciated.
     
  17. A "void" method is a method that doesn't return anything.
    Example:
    Code (Text):
    public void say(String msg){
        System.out.println(msg);
    }

    public int multiply(int i){
        return i*2;
    }
    Your question about how you can learn java, check this page out: https://www.spigotmc.org/wiki/how-to-learn-java-programming/
     
  18. OK i will look at that, but while i look at that...

    Code (Text):

    private boolean getLookingAt(Player player, Player player1)
                 {
                   Location eye = player.getEyeLocation();
                   Vector toEntity = player1.getEyeLocation().toVector().subtract(eye.toVector());
                   double dot = toEntity.normalize().dot(eye.getDirection());
                 
                   return dot > 0.99D;
                 }
     
    (thxs camden)

    going back to my previous question. How could this be changed to target all mobs and not just players? Or can it and im being very dumb?
     
Thread Status:
Not open for further replies.