Search in sources :

Example 1 with NibbleArray

use of com.viaversion.viaversion.api.minecraft.chunks.NibbleArray in project ViaVersion by ViaVersion.

the class BlockConnectionStorage method store.

public void store(int x, int y, int z, int blockState) {
    short mapping = REVERSE_BLOCK_MAPPINGS[blockState];
    if (mapping == -1)
        return;
    blockState = mapping;
    long pair = getChunkSectionIndex(x, y, z);
    Pair<byte[], NibbleArray> map = getChunkSection(pair, (blockState & 0xF) != 0);
    int blockIndex = encodeBlockPos(x, y, z);
    map.key()[blockIndex] = (byte) (blockState >> 4);
    NibbleArray nibbleArray = map.value();
    if (nibbleArray != null) {
        nibbleArray.set(blockIndex, blockState);
    }
}
Also used : NibbleArray(com.viaversion.viaversion.api.minecraft.chunks.NibbleArray)

Example 2 with NibbleArray

use of com.viaversion.viaversion.api.minecraft.chunks.NibbleArray in project ViaVersion by ViaVersion.

the class WorldPackets method setNonFullLight.

private static void setNonFullLight(Chunk chunk, ChunkSection section, int ySection, int x, int y, int z) {
    int skyLight = 0;
    int blockLight = 0;
    for (BlockFace blockFace : BlockFace.values()) {
        NibbleArray skyLightArray = section.getLight().getSkyLightNibbleArray();
        NibbleArray blockLightArray = section.getLight().getBlockLightNibbleArray();
        int neighbourX = x + blockFace.modX();
        int neighbourY = y + blockFace.modY();
        int neighbourZ = z + blockFace.modZ();
        if (blockFace.modX() != 0) {
            // Another chunk, nothing we can do without an unnecessary amount of caching
            if (neighbourX == 16 || neighbourX == -1)
                continue;
        } else if (blockFace.modY() != 0) {
            if (neighbourY == 16 || neighbourY == -1) {
                if (neighbourY == 16) {
                    ySection += 1;
                    neighbourY = 0;
                } else {
                    ySection -= 1;
                    neighbourY = 15;
                }
                if (ySection == 16 || ySection == -1)
                    continue;
                ChunkSection newSection = chunk.getSections()[ySection];
                if (newSection == null)
                    continue;
                skyLightArray = newSection.getLight().getSkyLightNibbleArray();
                blockLightArray = newSection.getLight().getBlockLightNibbleArray();
            }
        } else if (blockFace.modZ() != 0) {
            // Another chunk, nothing we can do without an unnecessary amount of caching
            if (neighbourZ == 16 || neighbourZ == -1)
                continue;
        }
        if (blockLightArray != null && blockLight != 15) {
            int neighbourBlockLight = blockLightArray.get(neighbourX, neighbourY, neighbourZ);
            if (neighbourBlockLight == 15) {
                blockLight = 14;
            } else if (neighbourBlockLight > blockLight) {
                // lower light level by one
                blockLight = neighbourBlockLight - 1;
            }
        }
        if (skyLightArray != null && skyLight != 15) {
            int neighbourSkyLight = skyLightArray.get(neighbourX, neighbourY, neighbourZ);
            if (neighbourSkyLight == 15) {
                if (blockFace.modY() == 1) {
                    // Keep 15 if block is exposed to sky
                    skyLight = 15;
                    continue;
                }
                skyLight = 14;
            } else if (neighbourSkyLight > skyLight) {
                // lower light level by one
                skyLight = neighbourSkyLight - 1;
            }
        }
    }
    if (skyLight != 0) {
        if (!section.getLight().hasSkyLight()) {
            byte[] newSkyLight = new byte[2028];
            section.getLight().setSkyLight(newSkyLight);
        }
        section.getLight().getSkyLightNibbleArray().set(x, y, z, skyLight);
    }
    if (blockLight != 0) {
        section.getLight().getBlockLightNibbleArray().set(x, y, z, blockLight);
    }
}
Also used : NibbleArray(com.viaversion.viaversion.api.minecraft.chunks.NibbleArray) BlockFace(com.viaversion.viaversion.api.minecraft.BlockFace) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 3 with NibbleArray

use of com.viaversion.viaversion.api.minecraft.chunks.NibbleArray in project ViaVersion by ViaVersion.

the class BlockConnectionStorage method get.

public int get(int x, int y, int z) {
    long pair = getChunkSectionIndex(x, y, z);
    Pair<byte[], NibbleArray> map = blockStorage.get(pair);
    if (map == null)
        return 0;
    short blockPosition = encodeBlockPos(x, y, z);
    NibbleArray nibbleArray = map.value();
    return WorldPackets.toNewId(((map.key()[blockPosition] & 0xFF) << 4) | (nibbleArray == null ? 0 : nibbleArray.get(blockPosition)));
}
Also used : NibbleArray(com.viaversion.viaversion.api.minecraft.chunks.NibbleArray)

Example 4 with NibbleArray

use of com.viaversion.viaversion.api.minecraft.chunks.NibbleArray in project ViaVersion by ViaVersion.

the class BlockConnectionStorage method remove.

public void remove(int x, int y, int z) {
    long pair = getChunkSectionIndex(x, y, z);
    Pair<byte[], NibbleArray> map = blockStorage.get(pair);
    if (map == null)
        return;
    int blockIndex = encodeBlockPos(x, y, z);
    NibbleArray nibbleArray = map.value();
    if (nibbleArray != null) {
        nibbleArray.set(blockIndex, 0);
        boolean allZero = true;
        for (int i = 0; i < 4096; i++) {
            if (nibbleArray.get(i) != 0) {
                allZero = false;
                break;
            }
        }
        if (allZero)
            map.setValue(null);
    }
    map.key()[blockIndex] = 0;
    for (short entry : map.key()) {
        if (entry != 0)
            return;
    }
    blockStorage.remove(pair);
}
Also used : NibbleArray(com.viaversion.viaversion.api.minecraft.chunks.NibbleArray)

Example 5 with NibbleArray

use of com.viaversion.viaversion.api.minecraft.chunks.NibbleArray in project ViaVersion by ViaVersion.

the class BlockConnectionStorage method getChunkSection.

private Pair<byte[], NibbleArray> getChunkSection(long index, boolean requireNibbleArray) {
    Pair<byte[], NibbleArray> map = blockStorage.get(index);
    if (map == null) {
        map = new Pair<>(new byte[4096], null);
        blockStorage.put(index, map);
    }
    if (map.value() == null && requireNibbleArray) {
        map.setValue(new NibbleArray(4096));
    }
    return map;
}
Also used : NibbleArray(com.viaversion.viaversion.api.minecraft.chunks.NibbleArray)

Aggregations

NibbleArray (com.viaversion.viaversion.api.minecraft.chunks.NibbleArray)5 BlockFace (com.viaversion.viaversion.api.minecraft.BlockFace)1 ChunkSection (com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)1