Search in sources :

Example 1 with Level

use of cn.nukkit.level.Level in project Nukkit by Nukkit.

the class Server method checkTickUpdates.

private void checkTickUpdates(int currentTick, long tickTime) {
    for (Player p : new ArrayList<>(this.players.values())) {
        if (this.alwaysTickPlayers) {
            p.onUpdate(currentTick);
        }
    }
    // Do level ticks
    for (Level level : this.levelArray) {
        if (level.getTickRate() > this.baseTickRate && --level.tickRateCounter > 0) {
            continue;
        }
        try {
            long levelTime = System.currentTimeMillis();
            level.doTick(currentTick);
            int tickMs = (int) (System.currentTimeMillis() - levelTime);
            level.tickRateTime = tickMs;
            if (this.autoTickRate) {
                if (tickMs < 50 && level.getTickRate() > this.baseTickRate) {
                    int r;
                    level.setTickRate(r = level.getTickRate() - 1);
                    if (r > this.baseTickRate) {
                        level.tickRateCounter = level.getTickRate();
                    }
                    this.getLogger().debug("Raising level \"" + level.getName() + "\" tick rate to " + level.getTickRate() + " ticks");
                } else if (tickMs >= 50) {
                    if (level.getTickRate() == this.baseTickRate) {
                        level.setTickRate((int) Math.max(this.baseTickRate + 1, Math.min(this.autoTickRateLimit, Math.floor(tickMs / 50))));
                        this.getLogger().debug("Level \"" + level.getName() + "\" took " + NukkitMath.round(tickMs, 2) + "ms, setting tick rate to " + level.getTickRate() + " ticks");
                    } else if ((tickMs / level.getTickRate()) >= 50 && level.getTickRate() < this.autoTickRateLimit) {
                        level.setTickRate(level.getTickRate() + 1);
                        this.getLogger().debug("Level \"" + level.getName() + "\" took " + NukkitMath.round(tickMs, 2) + "ms, setting tick rate to " + level.getTickRate() + " ticks");
                    }
                    level.tickRateCounter = level.getTickRate();
                }
            }
        } catch (Exception e) {
            if (Nukkit.DEBUG > 1 && this.logger != null) {
                this.logger.logException(e);
            }
            this.logger.critical(this.getLanguage().translateString("nukkit.level.tickError", new String[] { level.getName(), e.toString() }));
            this.logger.logException(e);
        }
    }
}
Also used : Level(cn.nukkit.level.Level)

Example 2 with Level

use of cn.nukkit.level.Level in project Nukkit by Nukkit.

the class Server method generateLevel.

public boolean generateLevel(String name, long seed, Class<? extends Generator> generator, Map<String, Object> options, Class<? extends LevelProvider> provider) {
    if (Objects.equals(name.trim(), "") || this.isLevelGenerated(name)) {
        return false;
    }
    if (!options.containsKey("preset")) {
        options.put("preset", this.getPropertyString("generator-settings", ""));
    }
    if (generator == null) {
        generator = Generator.getGenerator(this.getLevelType());
    }
    if (provider == null) {
        if ((provider = LevelProviderManager.getProviderByName((String) this.getConfig("level-settings.default-format", "anvil"))) == null) {
            provider = LevelProviderManager.getProviderByName("anvil");
        }
    }
    String path;
    if (name.contains("/") || name.contains("\\")) {
        path = name;
    } else {
        path = this.getDataPath() + "worlds/" + name + "/";
    }
    Level level;
    try {
        provider.getMethod("generate", String.class, String.class, long.class, Class.class, Map.class).invoke(null, path, name, seed, generator, options);
        level = new Level(this, name, path, provider);
        this.levels.put(level.getId(), level);
        level.initLevel();
        level.setTickRate(this.baseTickRate);
    } catch (Exception e) {
        this.logger.error(this.getLanguage().translateString("nukkit.level.generationError", new String[] { name, e.getMessage() }));
        this.logger.logException(e);
        return false;
    }
    this.getPluginManager().callEvent(new LevelInitEvent(level));
    this.getPluginManager().callEvent(new LevelLoadEvent(level));
    /*this.getLogger().notice(this.getLanguage().translateString("nukkit.level.backgroundGeneration", name));

        int centerX = (int) level.getSpawnLocation().getX() >> 4;
        int centerZ = (int) level.getSpawnLocation().getZ() >> 4;

        TreeMap<String, Integer> order = new TreeMap<>();

        for (int X = -3; X <= 3; ++X) {
            for (int Z = -3; Z <= 3; ++Z) {
                int distance = X * X + Z * Z;
                int chunkX = X + centerX;
                int chunkZ = Z + centerZ;
                order.put(Level.chunkHash(chunkX, chunkZ), distance);
            }
        }

        List<Map.Entry<String, Integer>> sortList = new ArrayList<>(order.entrySet());

        Collections.sort(sortList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        });

        for (String index : order.keySet()) {
            Chunk.Entry entry = Level.getChunkXZ(index);
            level.populateChunk(entry.chunkX, entry.chunkZ, true);
        }*/
    return true;
}
Also used : LevelLoadEvent(cn.nukkit.event.level.LevelLoadEvent) Level(cn.nukkit.level.Level) LevelInitEvent(cn.nukkit.event.level.LevelInitEvent)

Example 3 with Level

use of cn.nukkit.level.Level in project Nukkit by Nukkit.

the class GarbageCollectorCommand method execute.

@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;
}
Also used : Level(cn.nukkit.level.Level)

Example 4 with Level

use of cn.nukkit.level.Level in project Nukkit by Nukkit.

the class WeatherCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length == 0 || args.length > 2) {
        sender.sendMessage(new TranslationContainer("commands.weather.usage", this.usageMessage));
        return false;
    }
    String weather = args[0];
    Level level;
    int seconds;
    if (args.length > 1) {
        try {
            seconds = Integer.parseInt(args[1]);
        } catch (Exception e) {
            sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
            return true;
        }
    } else {
        seconds = 600 * 20;
    }
    if (sender instanceof Player) {
        level = ((Player) sender).getLevel();
    } else {
        level = sender.getServer().getDefaultLevel();
    }
    switch(weather) {
        case "clear":
            level.setRaining(false);
            level.setThundering(false);
            level.setRainTime(seconds * 20);
            level.setThunderTime(seconds * 20);
            Command.broadcastCommandMessage(sender, new TranslationContainer("commands.weather.clear"));
            return true;
        case "rain":
            level.setRaining(true);
            level.setRainTime(seconds * 20);
            Command.broadcastCommandMessage(sender, new TranslationContainer("commands.weather.rain"));
            return true;
        case "thunder":
            level.setThundering(true);
            level.setRainTime(seconds * 20);
            level.setThunderTime(seconds * 20);
            Command.broadcastCommandMessage(sender, new TranslationContainer("commands.weather.thunder"));
            return true;
        default:
            sender.sendMessage(new TranslationContainer("commands.weather.usage", this.usageMessage));
            return false;
    }
}
Also used : Player(cn.nukkit.Player) TranslationContainer(cn.nukkit.lang.TranslationContainer) Level(cn.nukkit.level.Level)

Example 5 with Level

use of cn.nukkit.level.Level in project Nukkit by Nukkit.

the class ParticleCommand method execute.

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
    if (!this.testPermission(sender)) {
        return true;
    }
    if (args.length < 7) {
        sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
        return true;
    }
    Level level;
    if (sender instanceof Player) {
        level = ((Player) sender).getLevel();
    } else {
        level = sender.getServer().getDefaultLevel();
    }
    String name = args[0].toLowerCase();
    float[] floats = new float[6];
    for (int i = 0; i < floats.length; i++) {
        try {
            double d = Double.valueOf(args[i + 1]);
            floats[i] = (float) d;
        } catch (Exception e) {
            return false;
        }
    }
    Vector3 pos = new Vector3(floats[0], floats[1], floats[2]);
    float xd = floats[3];
    float yd = floats[4];
    float zd = floats[5];
    int count = 1;
    if (args.length > 7) {
        try {
            double c = Double.valueOf(args[7]);
            count = (int) c;
        } catch (Exception e) {
        // ignore
        }
    }
    count = Math.max(1, count);
    Integer data = null;
    if (args.length > 8) {
        try {
            double d = Double.valueOf(args[8]);
            data = (int) d;
        } catch (Exception e) {
        // ignore
        }
    }
    Particle particle = this.getParticle(name, pos, xd, yd, zd, data);
    if (particle == null) {
        sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.particle.notFound", name));
        return true;
    }
    sender.sendMessage(new TranslationContainer("commands.particle.success", new String[] { name, String.valueOf(count) }));
    Random random = new Random(System.currentTimeMillis());
    for (int i = 0; i < count; i++) {
        particle.setComponents(pos.x + (random.nextFloat() * 2 - 1) * xd, pos.y + (random.nextFloat() * 2 - 1) * yd, pos.z + (random.nextFloat() * 2 - 1) * zd);
        level.addParticle(particle);
    }
    return true;
}
Also used : Player(cn.nukkit.Player) Vector3(cn.nukkit.math.Vector3) Random(java.util.Random) TranslationContainer(cn.nukkit.lang.TranslationContainer) Level(cn.nukkit.level.Level)

Aggregations

Level (cn.nukkit.level.Level)24 Player (cn.nukkit.Player)6 TranslationContainer (cn.nukkit.lang.TranslationContainer)6 BlockEventPacket (cn.nukkit.network.protocol.BlockEventPacket)6 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)3 BlockEnderChest (cn.nukkit.block.BlockEnderChest)2 LevelLoadEvent (cn.nukkit.event.level.LevelLoadEvent)2 Vector3 (cn.nukkit.math.Vector3)2 DecimalFormat (java.text.DecimalFormat)2 Server (cn.nukkit.Server)1 LevelInitEvent (cn.nukkit.event.level.LevelInitEvent)1 QueryRegenerateEvent (cn.nukkit.event.server.QueryRegenerateEvent)1 Position (cn.nukkit.level.Position)1 SourceInterface (cn.nukkit.network.SourceInterface)1 ContainerClosePacket (cn.nukkit.network.protocol.ContainerClosePacket)1 ContainerOpenPacket (cn.nukkit.network.protocol.ContainerOpenPacket)1 BanEntry (cn.nukkit.permission.BanEntry)1 TextFormat (cn.nukkit.utils.TextFormat)1 Random (java.util.Random)1