Invisible entity for 1 tick

Discussion in 'Plugin Development' started by Elimnator, Dec 28, 2014.

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

    Elimnator

    I need to get all the entities in a certain radius of a location. But the only way to do that is to spawn an entity for 1 tick and use it to getNearbyEntities.

    I tried spawning a XP orb but player can see it for a tick. Is there a way to do this without the entity being seen?

    I saw in SethBlings videos he spawns a invisible motionless wither head, is there a way to do what with bukkit?
     
  2. Offline

    Skionz

    @Elimnator Iterate through the world's entities and check if the distance squared is <= value * value
     
  3. Offline

    Elimnator

    @Skionz
    I found this way to do it:
    Code:
                WorldServer sw = ((CraftWorld) l.getWorld()).getHandle();
                EntityWitherSkull skull = new EntityWitherSkull(sw);
                skull.setLocation(l.getX(), l.getY(), l.getZ(), 0, 0);
                sw.addEntity(skull);
    Do you think doing this every 20 ticks will cause more or less lagg then your way?
     
  4. Offline

    Skionz

    @Elimnator My way is probably better. You could also iterate through the entities in a specific chunk.
     
  5. Offline

    WarmakerT

    This solution isn't as resource-intensive as @Skionz:
    Code:
        public static List<Chunk> getNearbyChunks(Location l, int range) {
            List<Chunk> chunkList = new ArrayList<Chunk>();
            World world = l.getWorld();
            int chunks = range / 16 + 1;
    
            Chunk chunk;
            for (int x = l.getChunk().getX() - chunks; x < l.getChunk().getX() + chunks; x++) {
                for (int z = l.getChunk().getZ() - chunks; z < l.getChunk().getZ() + chunks; z++) {
                    chunk = world.getChunkAt(x, z);
                    if (chunk != null && chunk.isLoaded()) {
                       chunkList.add(chunk);
                    }
                }
            }
    
            return chunkList;
        }
    
        public static List<Entity> getEntitiesInNearbyChunks(Location l, int range, List<EntityType> entityTypes) {
            List<Entity> entities = new ArrayList<Entity>();
    
            for(Chunk chunk : getNearbyChunks(l, range)) {
                if (entityTypes == null && entityTypes.size() > 0) {
                    entities.addAll(Arrays.asList(chunk.getEntities()));
                } else {
                    for (Entity e : chunk.getEntities()) {
                        if (entityTypes.contains(e.getType())) {
                            entities.add(e);
                        }
                    }
                }
            }
    
            return entities;
        }
    
        public static List<Entity> getNearbyEntities(Location l, float range, List<EntityType> entityTypes) {
            List<Entity> entities = new ArrayList<Entity>();
            for (Entity e : getEntitiesInNearbyChunks(l, (int) range, entityTypes)) {
                if (e.getLocation().distance(l) <= range) {
                    entities.add(e);
                }
            }
    
            return entities;
        }
    Credit goes to worstboy32.
    If you only want getNearbyEntities then you'll have to change some things, since the last method is dependent on the second one and the second one is dependent on the first one.
     
  6. Offline

    Elimnator

    Thnaks @WarmakerT, I did think that looking through all the entities on in the whole world might cause a bit much lagg.
     
  7. Offline

    WarmakerT

    Don't forget to mark the question as Solved when you're ready.
     
  8. Why can't you just spawn the orb at a location below 0 or above like 300?
    Nobody will see it
     
  9. Offline

    Elimnator

  10. Must have derped this morning thinking getNearbyEntities wouldn't rely on Y axis :p
     
Thread Status:
Not open for further replies.

Share This Page