cn.nukkit.level.generator.Generator Java Examples

The following examples show how to use cn.nukkit.level.generator.Generator. 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: BaseLevelProvider.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public BaseLevelProvider(Level level, String path) throws IOException {
    this.level = level;
    this.path = path;
    File file_path = new File(this.path);
    if (!file_path.exists()) {
        file_path.mkdirs();
    }
    CompoundTag levelData = NBTIO.readCompressed(new FileInputStream(new File(this.getPath() + "level.dat")), ByteOrder.BIG_ENDIAN);
    if (levelData.get("Data") instanceof CompoundTag) {
        this.levelData = levelData.getCompound("Data");
    } else {
        throw new LevelException("Invalid level.dat");
    }

    if (!this.levelData.contains("generatorName")) {
        this.levelData.putString("generatorName", Generator.getGenerator("DEFAULT").getSimpleName().toLowerCase());
    }

    if (!this.levelData.contains("generatorOptions")) {
        this.levelData.putString("generatorOptions", "");
    }

    this.spawn = new Vector3(this.levelData.getInt("SpawnX"), this.levelData.getInt("SpawnY"), this.levelData.getInt("SpawnZ"));
}
 
Example #2
Source File: BaseLevelProvider.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public BaseLevelProvider(Level level, String path) throws IOException {
    this.level = level;
    this.path = path;
    File file_path = new File(this.path);
    if (!file_path.exists()) {
        file_path.mkdirs();
    }
    CompoundTag levelData = NBTIO.readCompressed(new FileInputStream(new File(this.getPath() + "level.dat")), ByteOrder.BIG_ENDIAN);
    if (levelData.get("Data") instanceof CompoundTag) {
        this.levelData = levelData.getCompound("Data");
    } else {
        throw new LevelException("Invalid level.dat");
    }

    if (!this.levelData.contains("generatorName")) {
        this.levelData.putString("generatorName", Generator.getGenerator("DEFAULT").getSimpleName().toLowerCase());
    }

    if (!this.levelData.contains("generatorOptions")) {
        this.levelData.putString("generatorOptions", "");
    }

    this.spawn = new Vector3(this.levelData.getInt("SpawnX"), this.levelData.getInt("SpawnY"), this.levelData.getInt("SpawnZ"));
}
 
Example #3
Source File: BaseLevelProvider.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public BaseLevelProvider(Level level, String path) throws IOException {
    this.level = level;
    this.path = path;
    File file_path = new File(this.path);
    if (!file_path.exists()) {
        file_path.mkdirs();
    }
    CompoundTag levelData = NBTIO.readCompressed(new FileInputStream(new File(this.getPath() + "level.dat")), ByteOrder.BIG_ENDIAN);
    if (levelData.get("Data") instanceof CompoundTag) {
        this.levelData = levelData.getCompound("Data");
    } else {
        throw new LevelException("Invalid level.dat");
    }

    if (!this.levelData.contains("generatorName")) {
        this.levelData.putString("generatorName", Generator.getGenerator("DEFAULT").getSimpleName().toLowerCase());
    }

    if (!this.levelData.contains("generatorOptions")) {
        this.levelData.putString("generatorOptions", "");
    }

}
 
Example #4
Source File: Level.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Generator init() {
    try {
        Generator generator = generatorClass.getConstructor(Map.class).newInstance(provider.getGeneratorOptions());
        NukkitRandom rand = new NukkitRandom(getSeed());
        ChunkManager manager;
        if (Server.getInstance().isPrimaryThread()) {
            generator.init(Level.this, rand);
        }
        generator.init(new PopChunkManager(getSeed()), rand);
        return generator;
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #5
Source File: McRegion.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("Data")
            .putCompound("GameRules", new CompoundTag())

            .putLong("DayTime", 0)
            .putInt("GameType", 0)
            .putString("generatorName", Generator.getGeneratorName(generator))
            .putString("generatorOptions", options.getOrDefault("preset", ""))
            .putInt("generatorVersion", 1)
            .putBoolean("hardcore", false)
            .putBoolean("initialized", true)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putBoolean("raining", false)
            .putInt("rainTime", 0)
            .putLong("RandomSeed", seed)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putBoolean("thundering", false)
            .putInt("thunderTime", 0)
            .putInt("version", 19133)
            .putLong("Time", 0)
            .putLong("SizeOnDisk", 0);

    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
 
Example #6
Source File: GeneratorRegisterTask.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRun() {
    Block.init();
    Biome.init();
    SimpleChunkManager manager = new SimpleChunkManager(this.seed);
    try {
        Generator generator = this.generator.getConstructor(Map.class).newInstance(this.settings);
        generator.init(manager, new NukkitRandom(manager.getSeed()));
        GeneratorPool.put(this.levelId, generator);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #7
Source File: GenerationTask.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRun() {
    Generator generator = GeneratorPool.get(this.levelId);

    if (generator == null) {
        this.state = false;
        return;
    }

    SimpleChunkManager manager = (SimpleChunkManager) generator.getChunkManager();

    if (manager == null) {
        this.state = false;
        return;
    }

    synchronized (manager) {
        BaseFullChunk chunk = this.chunk.clone();

        if (chunk == null) {
            return;
        }

        manager.setChunk(chunk.getX(), chunk.getZ(), chunk);

        generator.generateChunk(chunk.getX(), chunk.getZ());

        chunk = manager.getChunk(chunk.getX(), chunk.getZ());
        chunk.setGenerated();
        this.chunk = chunk.clone();

        manager.setChunk(chunk.getX(), chunk.getZ(), null);
    }

}
 
Example #8
Source File: GenerationTask.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRun() {
    Generator generator = level.getGenerator();
    this.state = false;
    if (generator == null) {
        return;
    }

    SimpleChunkManager manager = (SimpleChunkManager) generator.getChunkManager();

    if (manager == null) {
        this.state = false;
        return;
    }

    manager.cleanChunks(level.getSeed());
    synchronized (manager) {
        try {
            BaseFullChunk chunk = this.chunk;

            if (chunk == null) {
                return;
            }

            synchronized (chunk) {
                if (!chunk.isGenerated()) {
                    manager.setChunk(chunk.getX(), chunk.getZ(), chunk);
                    generator.generateChunk(chunk.getX(), chunk.getZ());
                    chunk = manager.getChunk(chunk.getX(), chunk.getZ());
                    chunk.setGenerated();
                }
            }
            this.chunk = chunk;
            state = true;
        } finally {
            manager.cleanChunks(level.getSeed());
        }
    }

}
 
Example #9
Source File: Anvil.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("Data")
            .putCompound("GameRules", new CompoundTag())

            .putLong("DayTime", 0)
            .putInt("GameType", 0)
            .putString("generatorName", Generator.getGeneratorName(generator))
            .putString("generatorOptions", options.getOrDefault("preset", ""))
            .putInt("generatorVersion", 1)
            .putBoolean("hardcore", false)
            .putBoolean("initialized", true)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putBoolean("raining", false)
            .putInt("rainTime", 0)
            .putLong("RandomSeed", seed)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putBoolean("thundering", false)
            .putInt("thunderTime", 0)
            .putInt("version", VERSION)
            .putLong("Time", 0)
            .putLong("SizeOnDisk", 0);

    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
 
Example #10
Source File: Anvil.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("Data")
            .putCompound("GameRules", new CompoundTag())

            .putLong("DayTime", 0)
            .putInt("GameType", 0)
            .putString("generatorName", Generator.getGeneratorName(generator))
            .putString("generatorOptions", options.containsKey("preset") ? options.get("preset") : "")
            .putInt("generatorVersion", 1)
            .putBoolean("hardcore", false)
            .putBoolean("initialized", true)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putBoolean("raining", false)
            .putInt("rainTime", 0)
            .putLong("RandomSeed", seed)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putBoolean("thundering", false)
            .putInt("thunderTime", 0)
            .putInt("version", 19133)
            .putLong("Time", 0)
            .putLong("SizeOnDisk", 0);

    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
 
Example #11
Source File: McRegion.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("Data")
            .putCompound("GameRules", new CompoundTag())

            .putLong("DayTime", 0)
            .putInt("GameType", 0)
            .putString("generatorName", Generator.getGeneratorName(generator))
            .putString("generatorOptions", options.containsKey("preset") ? options.get("preset") : "")
            .putInt("generatorVersion", 1)
            .putBoolean("hardcore", false)
            .putBoolean("initialized", true)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putBoolean("raining", false)
            .putInt("rainTime", 0)
            .putLong("RandomSeed", seed)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putBoolean("thundering", false)
            .putInt("thunderTime", 0)
            .putInt("version", 19133)
            .putLong("Time", 0)
            .putLong("SizeOnDisk", 0);

    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
 
Example #12
Source File: McRegion.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("Data")
            .putCompound("GameRules", new CompoundTag())

            .putLong("DayTime", 0)
            .putInt("GameType", 0)
            .putString("generatorName", Generator.getGeneratorName(generator))
            .putString("generatorOptions", options.containsKey("preset") ? options.get("preset") : "")
            .putInt("generatorVersion", 1)
            .putBoolean("hardcore", false)
            .putBoolean("initialized", true)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putBoolean("raining", false)
            .putInt("rainTime", 0)
            .putLong("RandomSeed", seed)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putBoolean("thundering", false)
            .putInt("thunderTime", 0)
            .putInt("version", 19133)
            .putLong("Time", 0)
            .putLong("SizeOnDisk", 0);

    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
 
Example #13
Source File: Anvil.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/region").exists()) {
        new File(path + "/region").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("Data")
            .putCompound("GameRules", new CompoundTag())

            .putLong("DayTime", 0)
            .putInt("GameType", 0)
            .putString("generatorName", Generator.getGeneratorName(generator))
            .putString("generatorOptions", options.containsKey("preset") ? options.get("preset") : "")
            .putInt("generatorVersion", 1)
            .putBoolean("hardcore", false)
            .putBoolean("initialized", true)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putBoolean("raining", false)
            .putInt("rainTime", 0)
            .putLong("RandomSeed", seed)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putBoolean("thundering", false)
            .putInt("thunderTime", 0)
            .putInt("version", 19133)
            .putLong("Time", 0)
            .putLong("SizeOnDisk", 0);

    NBTIO.writeGZIPCompressed(new CompoundTag().putCompound("Data", levelData), new FileOutputStream(path + "level.dat"), ByteOrder.BIG_ENDIAN);
}
 
Example #14
Source File: GenerationTask.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRun() {
    Generator generator = GeneratorPool.get(this.levelId);

    if (generator == null) {
        this.state = false;
        return;
    }

    SimpleChunkManager manager = (SimpleChunkManager) generator.getChunkManager();

    if (manager == null) {
        this.state = false;
        return;
    }

    synchronized (manager) {
        BaseFullChunk chunk = this.chunk.clone();

        if (chunk == null) {
            return;
        }

        manager.setChunk(chunk.getX(), chunk.getZ(), chunk);

        generator.generateChunk(chunk.getX(), chunk.getZ());

        chunk = manager.getChunk(chunk.getX(), chunk.getZ());
        chunk.setGenerated();
        this.chunk = chunk.clone();

        manager.setChunk(chunk.getX(), chunk.getZ(), null);
    }

}
 
Example #15
Source File: GeneratorRegisterTask.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onRun() {
    Block.init();
    Biome.init();
    SimpleChunkManager manager = new SimpleChunkManager(this.seed);
    try {
        Generator generator = this.generator.getConstructor(Map.class).newInstance(this.settings);
        generator.init(manager, new NukkitRandom(manager.getSeed()));
        GeneratorPool.put(this.levelId, generator);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #16
Source File: GeneratorPool.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static Generator get(int levelId) {
    return generators.getOrDefault(levelId, null);
}
 
Example #17
Source File: GeneratorRegisterTask.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public GeneratorRegisterTask(Level level, Generator generator) {
    this.generator = generator.getClass();
    this.settings = generator.getSettings();
    this.seed = level.getSeed();
    this.levelId = level.getId();
}
 
Example #18
Source File: GeneratorPool.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void put(int levelId, Generator generator) {
    generators.put(levelId, generator);
}
 
Example #19
Source File: Server.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean generateLevel(String name, long seed, Class<? extends Generator> generator, Map<String, Object> options) {
    return generateLevel(name, seed, generator, options, null);
}
 
Example #20
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator, Map<String, String> options) throws IOException {
    if (!new File(path + "/db").exists()) {
        new File(path + "/db").mkdirs();
    }

    CompoundTag levelData = new CompoundTag("")
            .putLong("currentTick", 0)
            .putInt("DayCycleStopTime", -1)
            .putInt("GameType", 0)
            .putInt("Generator", Generator.getGeneratorType(generator))
            .putBoolean("hasBeenLoadedInCreative", false)
            .putLong("LastPlayed", System.currentTimeMillis() / 1000)
            .putString("LevelName", name)
            .putFloat("lightningLevel", 0)
            .putInt("lightningTime", new Random().nextInt())
            .putInt("limitedWorldOriginX", 128)
            .putInt("limitedWorldOriginY", 70)
            .putInt("limitedWorldOriginZ", 128)
            .putInt("Platform", 0)
            .putFloat("rainLevel", 0)
            .putInt("rainTime", new Random().nextInt())
            .putLong("RandomSeed", seed)
            .putByte("spawnMobs", 0)
            .putInt("SpawnX", 128)
            .putInt("SpawnY", 70)
            .putInt("SpawnZ", 128)
            .putInt("storageVersion", 4)
            .putLong("Time", 0)
            .putLong("worldStartCount", ((long) Integer.MAX_VALUE) & 0xffffffffL);

    byte[] data = NBTIO.write(levelData, ByteOrder.LITTLE_ENDIAN);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    outputStream.write(Binary.writeLInt(3));
    outputStream.write(Binary.writeLInt(data.length));
    outputStream.write(data);

    Utils.writeFile(path + "level.dat", new ByteArrayInputStream(outputStream.toByteArray()));

    DB db = Iq80DBFactory.factory.open(new File(path + "/db"), new Options().createIfMissing(true));
    db.close();
}
 
Example #21
Source File: LevelDB.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator) throws IOException {
    generate(path, name, seed, generator, new HashMap<>());
}
 
Example #22
Source File: Anvil.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator) throws IOException {
    generate(path, name, seed, generator, new HashMap<>());
}
 
Example #23
Source File: McRegion.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator) throws IOException {
    generate(path, name, seed, generator, new HashMap<>());
}
 
Example #24
Source File: Anvil.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public static void generate(String path, String name, long seed, Class<? extends Generator> generator) throws IOException {
    generate(path, name, seed, generator, new HashMap<>());
}
 
Example #25
Source File: Server.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean generateLevel(String name, long seed, Class<? extends Generator> generator) {
    return this.generateLevel(name, seed, generator, new HashMap<>());
}
 
Example #26
Source File: Server.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean generateLevel(String name, long seed, Class<? extends Generator> generator, Map<String, Object> options) {
    return generateLevel(name, seed, generator, options, null);
}
 
Example #27
Source File: Server.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public boolean generateLevel(String name, long seed, Class<? extends Generator> generator) {
    return this.generateLevel(name, seed, generator, new HashMap<>());
}
 
Example #28
Source File: Level.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public Generator getGenerator() {
    return generators.get();
}
 
Example #29
Source File: Level.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public void initLevel() {
    Generator generator = generators.get();
    this.dimension = generator.getDimension();
    this.gameRules = this.provider.getGamerules();
}
 
Example #30
Source File: GeneratorPool.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
public static void put(int levelId, Generator generator) {
    generators.put(levelId, generator);
}