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;
}
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();
}
}
}
}
}
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);
}
}
}
Aggregations