Java Code Examples for cn.nukkit.level.Level#getEntities()

The following examples show how to use cn.nukkit.level.Level#getEntities() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GarbageCollectorCommand.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    int chunksCollected = 0;
    int entitiesCollected = 0;
    int tilesCollected = 0;
    long memory = Runtime.getRuntime().freeMemory();

    for (Level level : sender.getServer().getLevels().values()) {
        int chunksCount = level.getChunks().size();
        int entitiesCount = level.getEntities().length;
        int tilesCount = level.getBlockEntities().size();
        level.doChunkGarbageCollection();
        level.unloadChunks(true);
        chunksCollected += chunksCount - level.getChunks().size();
        entitiesCollected += entitiesCount - level.getEntities().length;
        tilesCollected += tilesCount - level.getBlockEntities().size();
        level.clearCache(true);
    }

    System.gc();

    long freedMemory = Runtime.getRuntime().freeMemory() - memory;

    sender.sendMessage(TextFormat.GREEN + "---- " + TextFormat.WHITE + "Garbage collection result" + TextFormat.GREEN + " ----");
    sender.sendMessage(TextFormat.GOLD + "Chunks: " + TextFormat.RED + chunksCollected);
    sender.sendMessage(TextFormat.GOLD + "Entities: " + TextFormat.RED + entitiesCollected);
    sender.sendMessage(TextFormat.GOLD + "Block Entities: " + TextFormat.RED + tilesCollected);
    sender.sendMessage(TextFormat.GOLD + "Memory freed: " + TextFormat.RED + NukkitMath.round((freedMemory / 1024d / 1024d), 2) + " MB");
    return true;
}
 
Example 2
Source File: GarbageCollectorCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    int chunksCollected = 0;
    int entitiesCollected = 0;
    int tilesCollected = 0;
    long memory = Runtime.getRuntime().freeMemory();

    for (Level level : sender.getServer().getLevels().values()) {
        int chunksCount = level.getChunks().size();
        int entitiesCount = level.getEntities().length;
        int tilesCount = level.getBlockEntities().size();
        level.doChunkGarbageCollection();
        level.unloadChunks(true);
        chunksCollected += chunksCount - level.getChunks().size();
        entitiesCollected += entitiesCount - level.getEntities().length;
        tilesCollected += tilesCount - level.getBlockEntities().size();
    }

    ThreadCache.clean();
    System.gc();

    long freedMemory = Runtime.getRuntime().freeMemory() - memory;

    sender.sendMessage(TextFormat.GREEN + "---- " + TextFormat.WHITE + "Garbage collection result" + TextFormat.GREEN + " ----");
    sender.sendMessage(TextFormat.GOLD + "Chunks: " + TextFormat.RED + chunksCollected);
    sender.sendMessage(TextFormat.GOLD + "Entities: " + TextFormat.RED + entitiesCollected);
    sender.sendMessage(TextFormat.GOLD + "Block Entities: " + TextFormat.RED + tilesCollected);
    sender.sendMessage(TextFormat.GOLD + "Memory freed: " + TextFormat.RED + NukkitMath.round((freedMemory / 1024d / 1024d), 2) + " MB");
    return true;
}
 
Example 3
Source File: NukkitWorld.java    From FastAsyncWorldedit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<com.sk89q.worldedit.entity.Entity> getEntities(Region region) {
    Level world = getLevel();
    cn.nukkit.entity.Entity[] ents = world.getEntities();
    List<com.sk89q.worldedit.entity.Entity> entities = new ArrayList<com.sk89q.worldedit.entity.Entity>();
    for (cn.nukkit.entity.Entity ent : ents) {
        if (region.contains(NukkitUtil.toVector(ent.getLocation()))) {
            entities.add(new NukkitEntity(ent));
        }
    }
    return entities;
}
 
Example 4
Source File: GarbageCollectorCommand.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }

    int chunksCollected = 0;
    int entitiesCollected = 0;
    int tilesCollected = 0;
    long memory = Runtime.getRuntime().freeMemory();

    for (Level level : sender.getServer().getLevels().values()) {
        int chunksCount = level.getChunks().size();
        int entitiesCount = level.getEntities().length;
        int tilesCount = level.getBlockEntities().size();
        level.doChunkGarbageCollection();
        level.unloadChunks(true);
        chunksCollected += chunksCount - level.getChunks().size();
        entitiesCollected += entitiesCount - level.getEntities().length;
        tilesCollected += tilesCount - level.getBlockEntities().size();
    }

    ThreadCache.clean();
    System.gc();

    long freedMemory = Runtime.getRuntime().freeMemory() - memory;

    sender.sendMessage(TextFormat.GREEN + "---- " + TextFormat.WHITE + "Garbage collection result" + TextFormat.GREEN + " ----");
    sender.sendMessage(TextFormat.GOLD + "Chunks: " + TextFormat.RED + chunksCollected);
    sender.sendMessage(TextFormat.GOLD + "Entities: " + TextFormat.RED + entitiesCollected);
    sender.sendMessage(TextFormat.GOLD + "Block Entities: " + TextFormat.RED + tilesCollected);
    sender.sendMessage(TextFormat.GOLD + "Memory freed: " + TextFormat.RED + NukkitMath.round((freedMemory / 1024d / 1024d), 2) + " MB");
    return true;
}
 
Example 5
Source File: NukkitSensor.java    From Plan with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public int getEntityCount(Level world) {
    return world.getEntities().length;
}