use of net.glowstone.chunk.GlowChunk in project Glowstone by GlowstoneMC.
the class SurfaceCaveDecorator method decorate.
@Override
public void decorate(World world, Random random, Chunk c) {
if (random.nextInt(8) != 0) {
return;
}
GlowChunk chunk = (GlowChunk) c;
final int startCx = random.nextInt(16);
final int startCz = random.nextInt(16);
final int startY = chunk.getHeight(startCx, startCz);
final GlowBlock startBlock = chunk.getBlock(startCx, startY, startCz);
if (startY > 128) {
return;
}
PerlinOctaveGenerator octaves = new PerlinOctaveGenerator(random, 3, 4, 2, 4);
int cx = c.getX() << 4;
int cz = c.getZ() << 4;
double[] noise = octaves.getFractalBrownianMotion(cx, cz, 0, 0.5D, 0.2D);
double[] angles = new double[noise.length];
for (int i = 0; i < noise.length; i++) {
angles[i] = 360.0 * noise[i];
}
int sectionCount = angles.length / 2;
List<BlockVector> nodes = new ArrayList<>();
BlockVector currentNode = new BlockVector(startBlock.getX(), startBlock.getY(), startBlock.getZ());
nodes.add(currentNode.clone());
int length = 5;
for (int i = 0; i < sectionCount; i++) {
double yaw = angles[i + sectionCount];
int deltaY = -Math.abs(NumberConversions.floor(noise[i] * length));
int deltaX = NumberConversions.floor((double) length * Math.cos(Math.toRadians(yaw)));
int deltaZ = NumberConversions.floor((double) length * Math.sin(Math.toRadians(yaw)));
currentNode.add(new Vector(deltaX, deltaY, deltaZ));
nodes.add(new BlockVector(currentNode.getBlockX(), currentNode.getBlockY(), currentNode.getBlockZ()));
}
for (BlockVector node : nodes) {
if (node.getBlockY() < 4) {
continue;
}
GlowBlock block = (GlowBlock) world.getBlockAt(node.getBlockX(), node.getBlockY(), node.getBlockZ());
caveAroundRay(block, random);
}
}
use of net.glowstone.chunk.GlowChunk in project Glowstone by GlowstoneMC.
the class AnvilChunkIoService method write.
@Override
public void write(GlowChunk chunk) throws IOException {
int x = chunk.getX();
int z = chunk.getZ();
RegionFile region = cache.getRegionFile(x, z);
int regionX = x & REGION_SIZE - 1;
int regionZ = z & REGION_SIZE - 1;
CompoundTag levelTags = new CompoundTag();
// core properties
// NON-NLS
levelTags.putInt("xPos", chunk.getX());
// NON-NLS
levelTags.putInt("zPos", chunk.getZ());
// NON-NLS
levelTags.putLong("LastUpdate", 0);
// NON-NLS
levelTags.putLong("InhabitedTime", chunk.getInhabitedTime());
// NON-NLS
levelTags.putBool("TerrainPopulated", chunk.isPopulated());
// chunk sections
List<CompoundTag> sectionTags = new ArrayList<>();
GlowChunkSnapshot snapshot = chunk.getChunkSnapshot(true, true, false);
ChunkSection[] sections = snapshot.getRawSections();
for (byte i = 0; i < sections.length; ++i) {
ChunkSection sec = sections[i];
if (sec == null) {
continue;
}
CompoundTag sectionTag = new CompoundTag();
// NON-NLS
sectionTag.putByte("Y", i);
sec.recount();
sec.writeToNbt(sectionTag);
sectionTags.add(sectionTag);
}
// NON-NLS
levelTags.putCompoundList("Sections", sectionTags);
// height map and biomes
// NON-NLS
levelTags.putIntArray("HeightMap", snapshot.getRawHeightmap());
// NON-NLS
levelTags.putByteArray("Biomes", snapshot.getRawBiomes());
// Save Slime Chunk
// NON-NLS
levelTags.putByte("isSlimeChunk", snapshot.isSlimeChunk() ? 1 : 0);
// entities
List<CompoundTag> entities = new ArrayList<>();
for (GlowEntity entity : chunk.getRawEntities()) {
if (!entity.shouldSave()) {
continue;
}
// passengers will be saved as part of the vehicle
if (entity.isInsideVehicle()) {
continue;
}
try {
CompoundTag tag = new CompoundTag();
EntityStorage.save(entity, tag);
entities.add(tag);
} catch (Exception e) {
ConsoleMessages.Warn.Entity.SAVE_FAILED.log(e, entity, chunk);
}
}
levelTags.putCompoundList("Entities", entities);
// block entities
List<CompoundTag> blockEntities = new ArrayList<>();
for (BlockEntity entity : chunk.getRawBlockEntities()) {
try {
CompoundTag tag = new CompoundTag();
entity.saveNbt(tag);
blockEntities.add(tag);
} catch (Exception ex) {
ConsoleMessages.Error.BlockEntity.SAVE_FAILED.log(ex, entity.getBlock());
}
}
levelTags.putCompoundList("TileEntities", blockEntities);
List<CompoundTag> tileTicks = new ArrayList<>();
for (Location location : chunk.getWorld().getTickMap()) {
Chunk locationChunk = location.getChunk();
if (locationChunk.getX() == chunk.getX() && locationChunk.getZ() == chunk.getZ()) {
int tileX = location.getBlockX();
int tileY = location.getBlockY();
int tileZ = location.getBlockZ();
String type = ItemIds.getName(location.getBlock().getType());
CompoundTag tag = new CompoundTag();
tag.putInt("x", tileX);
tag.putInt("y", tileY);
tag.putInt("z", tileZ);
tag.putString("i", type);
tileTicks.add(tag);
}
}
levelTags.putCompoundList("TileTicks", tileTicks);
CompoundTag levelOut = new CompoundTag();
levelOut.putCompound("Level", levelTags);
try (NbtOutputStream nbt = new NbtOutputStream(region.getChunkDataOutputStream(regionX, regionZ), false)) {
nbt.writeTag(levelOut);
}
}
use of net.glowstone.chunk.GlowChunk in project Glowstone by GlowstoneMC.
the class InMemoryBlockStorage method createMockBlock.
private GlowBlock createMockBlock(Location location, Material type, byte data) {
GlowBlock mockBlock = Mockito.mock(GlowBlock.class);
when(mockBlock.getLocation()).thenReturn(location);
when(mockBlock.getType()).thenReturn(type);
when(mockBlock.getType().getId()).thenReturn(type.getId());
when(mockBlock.getData()).thenReturn(data);
BlockEntity entity = null;
BlockType blockType = ItemTable.instance().getBlock(type);
if (blockType != null) {
GlowChunk mockChunk = Mockito.mock(GlowChunk.class);
when(mockChunk.getBlock(anyInt(), anyInt(), anyInt())).thenReturn(mockBlock);
entity = blockType.createBlockEntity(mockChunk, 0, 0, 0);
}
when(mockBlock.getBlockEntity()).thenReturn(entity);
return mockBlock;
}
Aggregations