Search in sources :

Example 6 with World

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

the class InventoryView method handleClick.

private boolean handleClick(MouseButton mouseButton) {
    // We assume a player has to be spawned in order to do items manipulation
    Player player = gameWindow.getClient().getPlayer();
    if (player == null) {
        this.gameWindow.setLayer(parentLayer);
        // this.mainScene.changeOverlay(parent);
        selectedItem = null;
        return true;
    }
    World world = player.getWorld();
    for (int i = 0; i < drawers.length; i++) {
        // Close button
        if (drawers[i].isOverCloseButton()) {
            this.gameWindow.setLayer(parentLayer);
            selectedItem = null;
        } else {
            int[] c = drawers[i].getSelectedSlot();
            if (c == null)
                continue;
            else {
                int x = c[0];
                int y = c[1];
                if (selectedItem == null) {
                    if (mouseButton.equals("mouse.left")) {
                        selectedItem = inventories[i].getItemPileAt(x, y);
                        selectedItemAmount = selectedItem == null ? 0 : selectedItem.getAmount();
                    } else if (mouseButton.equals("mouse.right")) {
                        selectedItem = inventories[i].getItemPileAt(x, y);
                        selectedItemAmount = selectedItem == null ? 0 : 1;
                    } else if (mouseButton.equals("mouse.middle")) {
                        selectedItem = inventories[i].getItemPileAt(x, y);
                        selectedItemAmount = selectedItem == null ? 0 : (selectedItem.getAmount() > 1 ? selectedItem.getAmount() / 2 : 1);
                    }
                // selectedItemInv = inventory;
                } else if (mouseButton.equals("mouse.right")) {
                    if (selectedItem.equals(inventories[i].getItemPileAt(x, y))) {
                        if (selectedItemAmount < inventories[i].getItemPileAt(x, y).getAmount())
                            selectedItemAmount++;
                    }
                } else if (mouseButton.equals("mouse.left")) {
                    // Ignore null-sum games
                    if (selectedItem.getInventory() == inventories[i] && x == selectedItem.getX() && y == selectedItem.getY()) {
                        selectedItem = null;
                        return true;
                    }
                    if (world instanceof WorldMaster) {
                        PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, selectedItem, selectedItem.getInventory(), inventories[i], selectedItem.getX(), selectedItem.getY(), x, y, selectedItemAmount);
                        player.getContext().getPluginManager().fireEvent(moveItemEvent);
                        // If move was successfull
                        if (!moveItemEvent.isCancelled())
                            selectedItem.moveItemPileTo(inventories[i], x, y, selectedItemAmount);
                        selectedItem = null;
                    } else if (world instanceof WorldClientNetworkedRemote) {
                        // When in a remote MP scenario, send a packet
                        PacketInventoryMoveItemPile packetMove = new PacketInventoryMoveItemPile(world, selectedItem, selectedItem.getInventory(), inventories[i], selectedItem.getX(), selectedItem.getY(), x, y, selectedItemAmount);
                        ((WorldClientNetworkedRemote) world).getRemoteServer().pushPacket(packetMove);
                        // And unsellect item
                        selectedItem = null;
                    }
                }
                return true;
            }
        }
    }
    // Clicked outside of any other inventory (drop!)
    if (selectedItem != null) {
        // SP scenario, replicated logic in PacketInventoryMoveItemPile
        if (world instanceof WorldMaster) {
            // For local item drops, we need to make sure we have a sutiable entity
            Entity playerEntity = player.getControlledEntity();
            if (playerEntity != null) {
                PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, selectedItem, selectedItem.getInventory(), null, selectedItem.getX(), selectedItem.getY(), 0, 0, selectedItemAmount);
                player.getContext().getPluginManager().fireEvent(moveItemEvent);
                if (!moveItemEvent.isCancelled()) {
                    // If we're pulling this out of an inventory ( and not /dev/null ), we need to
                    // remove it from that
                    Inventory sourceInventory = selectedItem.getInventory();
                    Location loc = playerEntity.getLocation();
                    EventItemDroppedToWorld dropItemEvent = new EventItemDroppedToWorld(loc, sourceInventory, selectedItem);
                    player.getContext().getPluginManager().fireEvent(dropItemEvent);
                    if (!dropItemEvent.isCancelled()) {
                        if (sourceInventory != null)
                            sourceInventory.setItemPileAt(selectedItem.getX(), selectedItem.getY(), null);
                        if (dropItemEvent.getItemEntity() != null)
                            loc.getWorld().addEntity(dropItemEvent.getItemEntity());
                    }
                }
            }
            selectedItem = null;
        } else // In MP scenario, move into /dev/null
        if (world instanceof WorldClientNetworkedRemote) {
            PacketInventoryMoveItemPile packetMove = new PacketInventoryMoveItemPile(world, selectedItem, selectedItem.getInventory(), null, selectedItem.getX(), selectedItem.getY(), 0, 0, selectedItemAmount);
            ((WorldClientNetworkedRemote) world).getRemoteServer().pushPacket(packetMove);
            selectedItem = null;
        }
    }
    return true;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Player(io.xol.chunkstories.api.player.Player) EventItemDroppedToWorld(io.xol.chunkstories.api.events.item.EventItemDroppedToWorld) PlayerMoveItemEvent(io.xol.chunkstories.api.events.player.PlayerMoveItemEvent) PacketInventoryMoveItemPile(io.xol.chunkstories.api.net.packets.PacketInventoryMoveItemPile) World(io.xol.chunkstories.api.world.World) EventItemDroppedToWorld(io.xol.chunkstories.api.events.item.EventItemDroppedToWorld) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) WorldClientNetworkedRemote(io.xol.chunkstories.api.world.WorldClientNetworkedRemote) Inventory(io.xol.chunkstories.api.item.inventory.Inventory) Location(io.xol.chunkstories.api.Location)

Example 7 with World

use of io.xol.chunkstories.api.world.World in project chunkstories-api by Hugobros3.

the class PacketEntity method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException, UnknownComponentException {
    long entityUUID = in.readLong();
    short entityTypeID = in.readShort();
    if (entityTypeID == -1)
        return;
    World world = processor.getWorld();
    if (world == null)
        return;
    Entity entity = world.getEntityByUUID(entityUUID);
    boolean addToWorld = false;
    // Create an entity if the servers tells you to do so
    if (entity == null) {
        if (world instanceof WorldMaster && sender instanceof RemotePlayer) {
            ((Player) sender).sendMessage("You are sending packets to the server about a removed entity. Ignoring those.");
            return;
        } else {
            entity = processor.getWorld().getContentTranslator().getEntityForId(entityTypeID).create(// This is technically wrong
            new Location(world, 0, 0, 0));
            entity.setUUID(entityUUID);
            addToWorld = true;
        }
    }
    int componentId = in.readInt();
    // Loop throught all components
    while (componentId != 0) {
        try {
            entity.getComponents().tryPullComponentInStream(componentId, sender, in);
        } catch (UnknownComponentException e) {
            processor.logger().warn(e.getMessage());
        }
        componentId = in.readInt();
    }
    // Add to world if it was missing and we didn't receive the despawn flag
    if (addToWorld && entity.exists()) {
        // Only the WorldMaster is allowed to spawn new entities in the world
        if (processor instanceof ClientPacketsProcessor)
            processor.getWorld().addEntity(entity);
    }
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) Player(io.xol.chunkstories.api.player.Player) RemotePlayer(io.xol.chunkstories.api.server.RemotePlayer) ClientPacketsProcessor(io.xol.chunkstories.api.client.net.ClientPacketsProcessor) RemotePlayer(io.xol.chunkstories.api.server.RemotePlayer) UnknownComponentException(io.xol.chunkstories.api.exceptions.UnknownComponentException) World(io.xol.chunkstories.api.world.World) PacketWorld(io.xol.chunkstories.api.net.PacketWorld) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) Location(io.xol.chunkstories.api.Location)

Example 8 with World

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

the class VoxelDoor method onRemove.

@Override
public void onRemove(ChunkCell context, WorldModificationCause cause) {
    // Don't interfere with system pokes, else we get stuck in a loop
    if (cause == null || !(cause instanceof Entity))
        return;
    World world = context.getWorld();
    int x = context.getX();
    int y = context.getY();
    int z = context.getZ();
    // Ignore all that crap on a slave world
    if (!(world instanceof WorldMaster))
        return;
    int otherPartOfTheDoorY = y;
    if (top)
        otherPartOfTheDoorY--;
    else
        otherPartOfTheDoorY++;
    Voxel restOfTheDoorVoxel = world.peekSimple(x, otherPartOfTheDoorY, z);
    // Remove the other part as well, if it still exists
    if (restOfTheDoorVoxel instanceof VoxelDoor)
        world.pokeSimple(x, otherPartOfTheDoorY, z, store().air(), -1, -1, 0);
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) ItemVoxel(io.xol.chunkstories.api.item.ItemVoxel) Voxel(io.xol.chunkstories.api.voxel.Voxel) World(io.xol.chunkstories.api.world.World) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Example 9 with World

use of io.xol.chunkstories.api.world.World 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

World (io.xol.chunkstories.api.world.World)9 Location (io.xol.chunkstories.api.Location)5 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)5 Entity (io.xol.chunkstories.api.entity.Entity)4 Player (io.xol.chunkstories.api.player.Player)4 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)3 Voxel (io.xol.chunkstories.api.voxel.Voxel)3 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)2 ItemVoxel (io.xol.chunkstories.api.item.ItemVoxel)2 ClientPluginManager (io.xol.chunkstories.api.plugin.ClientPluginManager)2 ServerConnection (io.xol.chunkstories.client.net.ServerConnection)2 PacketInput (io.xol.chunkstories.net.packets.PacketInput)2 WorldClientRemote (io.xol.chunkstories.world.WorldClientRemote)2 ClientInterface (io.xol.chunkstories.api.client.ClientInterface)1 ClientPacketsProcessor (io.xol.chunkstories.api.client.net.ClientPacketsProcessor)1 Controller (io.xol.chunkstories.api.entity.Controller)1 EntityWorldModifier (io.xol.chunkstories.api.entity.interfaces.EntityWorldModifier)1 ClientInputPressedEvent (io.xol.chunkstories.api.events.client.ClientInputPressedEvent)1 ClientInputReleasedEvent (io.xol.chunkstories.api.events.client.ClientInputReleasedEvent)1 EventItemDroppedToWorld (io.xol.chunkstories.api.events.item.EventItemDroppedToWorld)1