Search in sources :

Example 11 with GlowChunk

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);
    }
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) ArrayList(java.util.ArrayList) PerlinOctaveGenerator(net.glowstone.util.noise.PerlinOctaveGenerator) BlockVector(org.bukkit.util.BlockVector) BlockVector(org.bukkit.util.BlockVector) Vector(org.bukkit.util.Vector) GlowChunk(net.glowstone.chunk.GlowChunk)

Example 12 with GlowChunk

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);
    }
}
Also used : NbtOutputStream(net.glowstone.util.nbt.NbtOutputStream) ArrayList(java.util.ArrayList) GlowChunk(net.glowstone.chunk.GlowChunk) Chunk(org.bukkit.Chunk) UnknownEntityTypeException(net.glowstone.io.entity.UnknownEntityTypeException) IOException(java.io.IOException) GlowEntity(net.glowstone.entity.GlowEntity) ChunkSection(net.glowstone.chunk.ChunkSection) CompoundTag(net.glowstone.util.nbt.CompoundTag) GlowChunkSnapshot(net.glowstone.chunk.GlowChunkSnapshot) BlockEntity(net.glowstone.block.entity.BlockEntity) Location(org.bukkit.Location)

Example 13 with GlowChunk

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;
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) BlockType(net.glowstone.block.blocktype.BlockType) BlockEntity(net.glowstone.block.entity.BlockEntity) GlowChunk(net.glowstone.chunk.GlowChunk)

Aggregations

GlowChunk (net.glowstone.chunk.GlowChunk)13 GlowBlock (net.glowstone.block.GlowBlock)5 ArrayList (java.util.ArrayList)4 Location (org.bukkit.Location)4 GlowWorld (net.glowstone.GlowWorld)3 ItemTable (net.glowstone.block.ItemTable)3 BlockChangeMessage (net.glowstone.net.message.play.game.BlockChangeMessage)3 Chunk (org.bukkit.Chunk)3 Material (org.bukkit.Material)3 World (org.bukkit.World)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 Collection (java.util.Collection)2 List (java.util.List)2 Getter (lombok.Getter)2 EventFactory (net.glowstone.EventFactory)2 BlockEntity (net.glowstone.block.entity.BlockEntity)2 Key (net.glowstone.chunk.GlowChunk.Key)2 GameRules (net.glowstone.constants.GameRules)2 GlowPlayer (net.glowstone.entity.GlowPlayer)2 NamespacedKey (org.bukkit.NamespacedKey)2