Search in sources :

Example 6 with WorldException

use of io.xol.chunkstories.api.exceptions.world.WorldException in project chunkstories by Hugobros3.

the class CubicChunk method pokeInternal.

/**
 * The 'core' of the core, this private function is responsible for placing and
 * keeping everyone up to snuff on block modifications. It all comes back to this really.
 */
private ActualChunkVoxelContext pokeInternal(final int worldX, final int worldY, final int worldZ, Voxel newVoxel, final int sunlight, final int blocklight, final int metadata, int raw_data, final boolean use_raw_data, final boolean update, final boolean return_context, final WorldModificationCause cause) {
    int x = sanitizeCoordinate(worldX);
    int y = sanitizeCoordinate(worldY);
    int z = sanitizeCoordinate(worldZ);
    ActualChunkVoxelContext cell_pre = peek(x, y, z);
    Voxel formerVoxel = cell_pre.getVoxel();
    assert formerVoxel != null;
    FreshFutureCell future = new FreshFutureCell(cell_pre);
    if (use_raw_data) {
        // We need this for voxel placement logic
        newVoxel = world.getContentTranslator().getVoxelForId(VoxelFormat.id(raw_data));
        // Build the future from parsing the raw data
        future.setVoxel(newVoxel);
        future.setSunlight(VoxelFormat.sunlight(raw_data));
        future.setBlocklight(VoxelFormat.blocklight(raw_data));
        future.setMetaData(VoxelFormat.meta(raw_data));
    } else {
        // Build the raw data from the set parameters by editing the in-place data
        // (because we allow only editing some aspects of the cell data)
        raw_data = cell_pre.getData();
        if (newVoxel != null) {
            raw_data = VoxelFormat.changeId(raw_data, world.getContentTranslator().getIdForVoxel(newVoxel));
            future.setVoxel(newVoxel);
        }
        if (sunlight >= 0) {
            raw_data = VoxelFormat.changeSunlight(raw_data, sunlight);
            future.setSunlight(sunlight);
        }
        if (blocklight >= 0) {
            raw_data = VoxelFormat.changeBlocklight(raw_data, blocklight);
            future.setBlocklight(blocklight);
        }
        if (metadata >= 0) {
            raw_data = VoxelFormat.changeMeta(raw_data, metadata);
            future.setMetaData(metadata);
        }
    }
    try {
        if (newVoxel == null || formerVoxel.equals(newVoxel)) {
            formerVoxel.onModification(cell_pre, future, cause);
        } else {
            formerVoxel.onRemove(cell_pre, cause);
            newVoxel.onPlace(future, cause);
        }
    } catch (WorldException e) {
        // Abort !
        if (return_context)
            return cell_pre;
        else
            return null;
    }
    // Allocate if it makes sense
    if (chunkVoxelData == null)
        chunkVoxelData = atomicalyCreateInternalData();
    chunkVoxelData[x * 32 * 32 + y * 32 + z] = raw_data;
    if (newVoxel != null && !formerVoxel.equals(newVoxel))
        newVoxel.whenPlaced(future);
    // Update lightning
    if (update)
        lightBaker.computeLightSpread(x, y, z, cell_pre.raw_data, raw_data);
    // Increment the modifications counter
    compr_uncomittedBlockModifications.incrementAndGet();
    // Don't spam the thread creation spawn
    occlusion.unbakedUpdates.incrementAndGet();
    // Update related summary
    if (update)
        world.getRegionsSummariesHolder().updateOnBlockPlaced(x, y, z, future);
    // Mark the nearby chunks to be re-rendered
    if (update) {
        int sx = chunkX;
        int ex = sx;
        int sy = chunkY;
        int ey = sy;
        int sz = chunkZ;
        int ez = sz;
        if (x == 0)
            sx--;
        else if (x == 31)
            ex++;
        if (y == 0)
            sy--;
        else if (y == 31)
            ey++;
        if (z == 0)
            sz--;
        else if (z == 31)
            ez++;
        for (int ix = sx; ix <= ex; ix++) for (int iy = sy; iy <= ey; iy++) for (int iz = sz; iz <= ez; iz++) {
            Chunk chunk = world.getChunk(ix, iy, iz);
            if (chunk != null && chunk instanceof ChunkRenderable)
                ((ChunkRenderable) chunk).meshUpdater().requestMeshUpdate();
        }
    }
    // If this is a 'master' world, notify remote users of the change !
    if (update && world instanceof WorldMaster && !(world instanceof WorldTool)) {
        PacketVoxelUpdate packet = new PacketVoxelUpdate(new ActualChunkVoxelContext(chunkX * 32 + x, chunkY * 32 + y, chunkZ * 32 + z, raw_data));
        Iterator<WorldUser> pi = this.chunkHolder.users.iterator();
        while (pi.hasNext()) {
            WorldUser user = pi.next();
            if (!(user instanceof RemotePlayer))
                continue;
            RemotePlayer player = (RemotePlayer) user;
            Entity clientEntity = player.getControlledEntity();
            if (clientEntity == null)
                // Ignore clients that aren't playing
                continue;
            player.pushPacket(packet);
        }
    }
    if (return_context)
        return new ActualChunkVoxelContext(chunkX * 32 + x, chunkY * 32 + y, chunkZ * 32 + z, raw_data);
    else
        return null;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) WorldUser(io.xol.chunkstories.api.world.WorldUser) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) ChunkRenderable(io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) PacketVoxelUpdate(io.xol.chunkstories.api.net.packets.PacketVoxelUpdate) RemotePlayer(io.xol.chunkstories.api.server.RemotePlayer) WorldTool(io.xol.chunkstories.world.WorldTool) Voxel(io.xol.chunkstories.api.voxel.Voxel) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 7 with WorldException

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

the class EntityPlayer method onControllerInput.

@Override
public boolean onControllerInput(Input input, Controller controller) {
    // We are moving inventory bringup here !
    if (input.getName().equals("inventory") && world instanceof WorldClient) {
        if (creativeMode.get()) {
            ((WorldClient) getWorld()).getClient().openInventories(this.inventoryComponent.getInventory(), new InventoryLocalCreativeMenu(world));
        } else {
            ((WorldClient) getWorld()).getClient().openInventories(this.inventoryComponent.getInventory(), this.armor.getInventory());
        }
        return true;
    }
    Location blockLocation = this.getBlockLookingAt(true);
    double maxLen = 1024;
    if (blockLocation != null) {
        Vector3d diff = new Vector3d(blockLocation).sub(this.getLocation());
        // Vector3d dir = diff.clone().normalize();
        maxLen = diff.length();
    }
    Vector3d initialPosition = new Vector3d(getLocation());
    initialPosition.add(new Vector3d(0, eyePosition, 0));
    Vector3dc direction = getDirectionLookingAt();
    Iterator<Entity> i = world.collisionsManager().rayTraceEntities(initialPosition, direction, maxLen);
    while (i.hasNext()) {
        Entity e = i.next();
        if (e.handleInteraction(this, input))
            return true;
    }
    ItemPile itemSelected = getSelectedItemComponent().getSelectedItem();
    if (itemSelected != null) {
        // See if the item handles the interaction
        if (itemSelected.getItem().onControllerInput(this, itemSelected, input, controller))
            return true;
    }
    if (getWorld() instanceof WorldMaster) {
        // Creative mode features building and picking.
        if (this.getCreativeModeComponent().get()) {
            if (input.getName().equals("mouse.left")) {
                if (blockLocation != null) {
                    // Player events mod
                    if (controller instanceof Player) {
                        Player player = (Player) controller;
                        WorldCell cell = world.peekSafely(blockLocation);
                        FutureCell future = new FutureCell(cell);
                        future.setVoxel(this.getDefinition().store().parent().voxels().air());
                        future.setBlocklight(0);
                        future.setSunlight(0);
                        future.setMetaData(0);
                        PlayerVoxelModificationEvent event = new PlayerVoxelModificationEvent(cell, future, EntityCreative.CREATIVE_MODE, player);
                        // Anyone has objections ?
                        world.getGameContext().getPluginManager().fireEvent(event);
                        if (event.isCancelled())
                            return true;
                        Vector3d rnd = new Vector3d();
                        for (int k = 0; k < 40; k++) {
                            rnd.set(blockLocation);
                            rnd.add(Math.random() * 0.98, Math.random() * 0.98, Math.random() * 0.98);
                            world.getParticlesManager().spawnParticleAtPosition("voxel_frag", rnd);
                        }
                        world.getSoundManager().playSoundEffect("sounds/gameplay/voxel_remove.ogg", Mode.NORMAL, blockLocation, 1.0f, 1.0f);
                        try {
                            world.poke(future, this);
                        // world.poke((int)blockLocation.x, (int)blockLocation.y, (int)blockLocation.z, null, 0, 0, 0, this);
                        } catch (WorldException e) {
                        // Discard but maybe play some effect ?
                        }
                        return true;
                    }
                }
            } else if (input.getName().equals("mouse.middle")) {
                if (blockLocation != null) {
                    CellData ctx = this.getWorld().peekSafely(blockLocation);
                    if (!ctx.getVoxel().isAir()) {
                        // Spawn new itemPile in his inventory
                        ItemVoxel item = (ItemVoxel) world.getGameContext().getContent().items().getItemDefinition("item_voxel").newItem();
                        item.voxel = ctx.getVoxel();
                        item.voxelMeta = ctx.getMetaData();
                        ItemPile itemVoxel = new ItemPile(item);
                        this.getInventory().setItemPileAt(getSelectedItemComponent().getSelectedSlot(), 0, itemVoxel);
                        return true;
                    }
                }
            }
        }
    }
    // Then we check if the world minds being interacted with
    return world.handleInteraction(this, blockLocation, input);
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Player(io.xol.chunkstories.api.player.Player) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) WorldCell(io.xol.chunkstories.api.world.World.WorldCell) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) WorldClient(io.xol.chunkstories.api.world.WorldClient) ItemPile(io.xol.chunkstories.api.item.inventory.ItemPile) CellData(io.xol.chunkstories.api.world.cell.CellData) Vector3dc(org.joml.Vector3dc) FutureCell(io.xol.chunkstories.api.world.cell.FutureCell) PlayerVoxelModificationEvent(io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent) Vector3d(org.joml.Vector3d) InventoryLocalCreativeMenu(io.xol.chunkstories.core.item.inventory.InventoryLocalCreativeMenu) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Aggregations

WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)7 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)4 FutureCell (io.xol.chunkstories.api.world.cell.FutureCell)4 Location (io.xol.chunkstories.api.Location)3 Entity (io.xol.chunkstories.api.entity.Entity)3 PlayerVoxelModificationEvent (io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent)3 Player (io.xol.chunkstories.api.player.Player)3 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)2 EntityWorldModifier (io.xol.chunkstories.api.entity.interfaces.EntityWorldModifier)2 ItemPile (io.xol.chunkstories.api.item.inventory.ItemPile)2 Voxel (io.xol.chunkstories.api.voxel.Voxel)2 WorldCell (io.xol.chunkstories.api.world.World.WorldCell)2 CellData (io.xol.chunkstories.api.world.cell.CellData)2 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)2 ChunkCell (io.xol.chunkstories.api.world.chunk.Chunk.ChunkCell)2 Vector3d (org.joml.Vector3d)2 ClientInterface (io.xol.chunkstories.api.client.ClientInterface)1 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)1 ClientPacketsProcessor (io.xol.chunkstories.api.client.net.ClientPacketsProcessor)1 Controller (io.xol.chunkstories.api.entity.Controller)1