Search in sources :

Example 1 with GlowStructure

use of net.glowstone.generator.structures.GlowStructure in project Glowstone by GlowstoneMC.

the class NbtStructureDataService method readStructuresData.

@Override
public Map<Integer, GlowStructure> readStructuresData() {
    Map<Integer, GlowStructure> structures = new HashMap<>();
    for (StructureStore<?> store : StructureStorage.getStructureStores()) {
        File structureFile = new File(structureDir, store.getId() + ".dat");
        if (structureFile.exists()) {
            try (NBTInputStream in = new NBTInputStream(new FileInputStream(structureFile))) {
                CompoundTag data = new CompoundTag();
                data = in.readCompound();
                if (data.isCompound("data")) {
                    data = data.getCompound("data");
                    if (data.isCompound("Features")) {
                        CompoundTag features = data.getCompound("Features");
                        features.getValue().keySet().stream().filter(features::isCompound).forEach(key -> {
                            GlowStructure structure = StructureStorage.loadStructure(world, features.getCompound(key));
                            structures.put(new Key(structure.getChunkX(), structure.getChunkZ()).hashCode(), structure);
                        });
                    }
                } else {
                    server.getLogger().log(Level.SEVERE, "No data tag in " + structureFile);
                }
            } catch (IOException e) {
                server.getLogger().log(Level.SEVERE, "Failed to read structure data from " + structureFile, e);
            }
        }
    }
    return structures;
}
Also used : GlowStructure(net.glowstone.generator.structures.GlowStructure) HashMap(java.util.HashMap) NBTInputStream(net.glowstone.util.nbt.NBTInputStream) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) CompoundTag(net.glowstone.util.nbt.CompoundTag) Key(net.glowstone.chunk.GlowChunk.Key)

Example 2 with GlowStructure

use of net.glowstone.generator.structures.GlowStructure in project Glowstone by GlowstoneMC.

the class StructurePopulator method populate.

@Override
public void populate(World world, Random random, Chunk source) {
    if (world.canGenerateStructures()) {
        int cx = source.getX();
        int cz = source.getZ();
        random.setSeed(world.getSeed());
        long randX = random.nextLong();
        long randZ = random.nextLong();
        boolean placed = false;
        for (int x = cx - 8; x <= cx + 8 && !placed; x++) {
            for (int z = cz - 8; z <= cz + 8 && !placed; z++) {
                if (!world.getChunkAt(x, z).isLoaded() && !world.getChunkAt(x, z).load(true)) {
                    continue;
                }
                random.setSeed(x * randX + z * randZ ^ world.getSeed());
                Map<Integer, GlowStructure> structures = ((GlowWorld) world).getStructures();
                int key = GlowChunk.Key.of(x, z).hashCode();
                if (structures.containsKey(key)) {
                    continue;
                }
                for (StructureStore<?> store : StructureStorage.getStructureStores()) {
                    GlowStructure structure = store.createNewStructure((GlowWorld) world, random, x, z);
                    if (structure.shouldGenerate(random)) {
                        structure.setDirty(true);
                        structures.put(key, structure);
                        GlowServer.logger.finer("structure in chunk " + x + "," + z);
                        placed = true;
                        break;
                    }
                }
            }
        }
        int x = cx << 4;
        int z = cz << 4;
        Iterator<Entry<Integer, GlowStructure>> it = ((GlowWorld) world).getStructures().entrySet().iterator();
        while (it.hasNext()) {
            GlowStructure structure = it.next().getValue();
            if (structure.getBoundingBox().intersectsWith(x, z, x + 15, z + 15)) {
                BlockStateDelegate delegate = new BlockStateDelegate();
                if (structure.generate(random, x, z, delegate)) {
                    // maybe later trigger a StructureGeneratedEvent event and cancel
                    delegate.updateBlockStates();
                } else {
                    delegate.rollbackBlockStates();
                    it.remove();
                }
            }
        }
    }
}
Also used : GlowStructure(net.glowstone.generator.structures.GlowStructure) Entry(java.util.Map.Entry) BlockStateDelegate(net.glowstone.util.BlockStateDelegate) GlowWorld(net.glowstone.GlowWorld)

Example 3 with GlowStructure

use of net.glowstone.generator.structures.GlowStructure in project Glowstone by GlowstoneMC.

the class NbtStructureDataService method writeStructuresData.

@Override
public void writeStructuresData(Map<Integer, GlowStructure> structures) {
    for (GlowStructure structure : structures.values()) {
        if (structure.isDirty()) {
            CompoundTag data;
            CompoundTag features;
            CompoundTag feature = new CompoundTag();
            CompoundTag inputRoot;
            StructureStore<GlowStructure> store = StructureStorage.saveStructure(structure, feature);
            File structureFile = new File(structureDir, store.getId() + ".dat");
            if (structureFile.exists()) {
                try (NbtInputStream in = new NbtInputStream(new FileInputStream(structureFile))) {
                    inputRoot = in.readCompound();
                    data = // NON-NLS
                    inputRoot.tryGetCompound("data").orElseGet(CompoundTag::new);
                } catch (IOException e) {
                    ConsoleMessages.Error.Structure.LOAD_FAILED.log(e, structureFile);
                    data = new CompoundTag();
                }
                features = // NON-NLS
                data.tryGetCompound("Features").orElseGet(CompoundTag::new);
            } else {
                data = new CompoundTag();
                features = new CompoundTag();
            }
            String key = "[" + structure.getChunkX() + "," + structure.getChunkZ() + "]";
            features.putCompound(key, feature);
            // NON-NLS
            data.putCompound("Features", features);
            CompoundTag root = new CompoundTag();
            // NON-NLS
            root.putCompound("data", data);
            try (NbtOutputStream nbtOut = new NbtOutputStream(new FileOutputStream(structureFile))) {
                nbtOut.writeTag(root);
            } catch (IOException e) {
                ConsoleMessages.Error.Structure.SAVE_FAILED.log(e, structureFile);
            }
            structure.setDirty(false);
        }
    }
}
Also used : GlowStructure(net.glowstone.generator.structures.GlowStructure) NbtOutputStream(net.glowstone.util.nbt.NbtOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) NbtInputStream(net.glowstone.util.nbt.NbtInputStream) CompoundTag(net.glowstone.util.nbt.CompoundTag) FileInputStream(java.io.FileInputStream)

Aggregations

GlowStructure (net.glowstone.generator.structures.GlowStructure)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 CompoundTag (net.glowstone.util.nbt.CompoundTag)2 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 GlowWorld (net.glowstone.GlowWorld)1 Key (net.glowstone.chunk.GlowChunk.Key)1 BlockStateDelegate (net.glowstone.util.BlockStateDelegate)1 NBTInputStream (net.glowstone.util.nbt.NBTInputStream)1 NbtInputStream (net.glowstone.util.nbt.NbtInputStream)1 NbtOutputStream (net.glowstone.util.nbt.NbtOutputStream)1