Search in sources :

Example 1 with IllegalBlockModificationException

use of io.xol.chunkstories.api.exceptions.world.voxel.IllegalBlockModificationException in project chunkstories-core by Hugobros3.

the class BigVoxel method onPlace.

@Override
public void onPlace(FutureCell context, WorldModificationCause cause) throws IllegalBlockModificationException {
    // Be cool with the system doing it's thing
    if (cause == null)
        return;
    int x = context.getX();
    int y = context.getY();
    int z = context.getZ();
    // Check if there is space for it ...
    for (int a = x; a < x + xWidth; a++) {
        for (int b = y; b < y + yWidth; b++) {
            for (int c = z; c < z + zWidth; c++) {
                Chunk chunk = context.getWorld().getChunkWorldCoordinates(a, b, c);
                if (chunk == null)
                    throw new IllegalBlockModificationException(context, "All chunks upon wich this block places itself must be fully loaded !");
                CellData stuff = context.getWorld().peekSafely(a, b, c);
                if (stuff.getVoxel() == null || stuff.getVoxel().isAir() || !stuff.getVoxel().getDefinition().isSolid()) {
                    // These blocks are replaceable
                    continue;
                } else
                    throw new IllegalBlockModificationException(context, "Can't overwrite block at " + a + ": " + b + ": " + c);
            }
        }
    }
    // Actually build the thing then
    for (int a = 0; a < 0 + xWidth; a++) {
        for (int b = 0; b < 0 + yWidth; b++) {
            for (int c = 0; c < 0 + zWidth; c++) {
                int metadata = (byte) (((a & xMask) << xShift) | ((b & yMask) << yShift) | ((c & zMask) << zShift));
                context.getWorld().pokeSimple(a + x, b + y, c + z, this, -1, -1, metadata);
            }
        }
    }
}
Also used : IllegalBlockModificationException(io.xol.chunkstories.api.exceptions.world.voxel.IllegalBlockModificationException) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) CellData(io.xol.chunkstories.api.world.cell.CellData)

Example 2 with IllegalBlockModificationException

use of io.xol.chunkstories.api.exceptions.world.voxel.IllegalBlockModificationException in project chunkstories-core by Hugobros3.

the class VoxelDoor method onPlace.

@Override
public void onPlace(FutureCell cell, WorldModificationCause cause) throws IllegalBlockModificationException {
    // Ignore all that crap on a slave world
    if (!(cell.getWorld() instanceof WorldMaster))
        return;
    // We should only place the lower part, prevent entities from doing so !
    if (top && cause != null && cause instanceof Entity)
        throw new IllegalBlockModificationException(cell, "Entities can't place upper doors parts");
    // If the system adds the upper part, no modifications to be done on it
    if (top)
        return;
    World world = cell.getWorld();
    int x = cell.getX();
    int y = cell.getY();
    int z = cell.getZ();
    // Check top is free
    int topData = world.peekRaw(x, y + 1, z);
    if (VoxelFormat.id(topData) != 0)
        throw new IllegalBlockModificationException(cell, "Top part isn't free");
    // grab our attributes
    boolean isOpen = ((cell.getMetaData() >> 0) & 0x1) == 1;
    boolean hingeSide = ((cell.getMetaData() >> 1) & 0x1) == 1;
    int facingPassed = (cell.getMetaData() >> 2) & 0x3;
    // Default face is given by passed metadata
    VoxelSide doorSideFacing = VoxelSide.values()[facingPassed];
    // Determine side if placed by an entity and not internal code
    if (cause != null && cause instanceof Entity) {
        Location loc = ((Entity) cause).getLocation();
        double dx = loc.x() - (x + 0.5);
        double dz = loc.z() - (z + 0.5);
        if (Math.abs(dx) > Math.abs(dz)) {
            if (dx > 0)
                doorSideFacing = VoxelSide.RIGHT;
            else
                doorSideFacing = VoxelSide.LEFT;
        } else {
            if (dz > 0)
                doorSideFacing = VoxelSide.FRONT;
            else
                doorSideFacing = VoxelSide.BACK;
        }
        // If there is an adjacent one, set the hinge to right
        Voxel adjacent = null;
        switch(doorSideFacing) {
            case LEFT:
                adjacent = world.peekSimple(x, y, z - 1);
                break;
            case RIGHT:
                adjacent = world.peekSimple(x, y, z + 1);
                break;
            case FRONT:
                adjacent = world.peekSimple(x - 1, y, z);
                break;
            case BACK:
                adjacent = world.peekSimple(x + 1, y, z);
                break;
            default:
                break;
        }
        if (adjacent instanceof VoxelDoor) {
            hingeSide = true;
        }
        cell.setMetaData(computeMeta(isOpen, hingeSide, doorSideFacing));
    }
    // Place the upper part and we're good to go
    world.pokeSimple(x, y + 1, z, this.getUpperPart(), -1, -1, cell.getMetaData());
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) VoxelSide(io.xol.chunkstories.api.voxel.VoxelSide) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Voxel(io.xol.chunkstories.api.voxel.Voxel) IllegalBlockModificationException(io.xol.chunkstories.api.exceptions.world.voxel.IllegalBlockModificationException) World(io.xol.chunkstories.api.world.World) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Aggregations

IllegalBlockModificationException (io.xol.chunkstories.api.exceptions.world.voxel.IllegalBlockModificationException)2 Location (io.xol.chunkstories.api.Location)1 Entity (io.xol.chunkstories.api.entity.Entity)1 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)1 Voxel (io.xol.chunkstories.api.voxel.Voxel)1 VoxelSide (io.xol.chunkstories.api.voxel.VoxelSide)1 World (io.xol.chunkstories.api.world.World)1 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)1 CellData (io.xol.chunkstories.api.world.cell.CellData)1 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)1