Solved Monster Existence

Discussion in 'Plugin Development' started by kangkyuchang, Mar 29, 2020.

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

    kangkyuchang

    I wanna make sure there is a monster in the location that I randomly set. I'm currently using EntityDeathEvent, but I don't know what to do.
     
  2. Offline

    KarimAKL

    @kangkyuchang I'm not sure what exactly you mean but, i think this should work for getting the monsters near your location.

    Code:Java
    1. // A list of all the monsters found, if the list is empty, there's no monsters
    2. List<LivingEntity> monsters = new ArrayList<>();
    3.  
    4. // The location to check
    5. Location location = ...;
    6.  
    7. for (LivingEntity entity : location.getWorld().getLivingEntities()) {
    8. if (!isMonster(entity)) continue;
    9.  
    10. // Use this if you want to only check for that exact location
    11. if (location.equals(entity.getLocation())) monsters.add(entity);
    12.  
    13. // Use this if you want to check for monsters within a certain range of the location
    14. int range = 1; // 1 block
    15. if (location.distanceSquared(entity.getLocation()) <= range * range) monsters.add(entity);
    16. }
    17.  
    18. // Outside your method
    19.  
    20. // Check if the entity is a monster
    21. public boolean isMonster(Entity entity) {
    22. return true;
    23. }
     
  3. Offline

    kangkyuchang

    I'm sorry. I didn't explain it exactly. I'm going to teleport when there's no monster in my chosen area
     
  4. Offline

    KarimAKL

    @kangkyuchang In that case, i think the distanceSquared method should work for you.
     
  5. Offline

    NukerFall

    World#getNearbyEntities(...) gets entities nearby location in radius r
     
  6. Offline

    KarimAKL

    @NukerFall Wouldn't it be more effecient to only loop the living entities if you only want monsters? Also, i read somewhere that the distanceSquared(Location) method is more effective than the distance(Location) method, do you know if the getNearbyEntities() methods use that?
     
  7. Offline

    NukerFall

    never thought about that, cuz never used it.

    Code:
    ...
    main.getCommand("customteleport").setExecutor(this);
    ...
    public void onCommand(...) {
        setRandomLocationForTeleport();
        if (World.getNearbyEntities(location, r, r, r).size() == 0) {
            teleport((Player) sender);
        } else {
        sender.sendMessage("Monsters nearby!");
        }
        return true;
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 29, 2020
  8. Offline

    kangkyuchang

    Code:
    Location location = new Location(e.getEntity().getWorld(), i, 63, i2);
                            if(loc2 == i2)
                            {
                                for(LivingEntity entity : location.getWorld().getLivingEntities())
                                {
                                    if(monsters.contains(entity))
                                    {
                                        p.sendMessage("TEST");
                                        return;
                                    }
                                    if(!isMonster(entity)) continue;
                                    if(location.equals(entity.getLocation())) monsters.add(entity);
                                }
                            }
    I did this but it doesn't work.
     
  9. Offline

    NukerFall

    I sent you a code, try it. Maybe send all the class so we can look for all the fileds and methods you use.
     
Thread Status:
Not open for further replies.

Share This Page