Search in sources :

Example 1 with WorldClientNetworkedRemote

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

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

the class PlayerClientImplementation method setControlledEntity.

@Override
public boolean setControlledEntity(EntityControllable entity) {
    if (entity instanceof EntityControllable) {
        this.subscribe(entity);
        EntityControllable controllableEntity = (EntityControllable) entity;
        // If a world master, directly set the entity's controller
        if (world instanceof WorldMaster)
            controllableEntity.getControllerComponent().setController(this);
        // In remote networked worlds, we need to subscribe the server to our player actions to the controlled entity so he gets updates
        if (entity.getWorld() instanceof WorldClientNetworkedRemote) {
            // When changing controlled entity, first unsubscribe the remote server from the one we no longer own
            if (controlledEntity != null && controllableEntity != controlledEntity)
                ((WorldClientNetworkedRemote) controlledEntity.getWorld()).getRemoteServer().unsubscribe(controlledEntity);
            // Let know the server of new changes
            ((WorldClientNetworkedRemote) controllableEntity.getWorld()).getRemoteServer().subscribe(controllableEntity);
        }
        controlledEntity = controllableEntity;
    } else if (entity == null && getControlledEntity() != null) {
        // Directly unset it
        if (world instanceof WorldMaster)
            getControlledEntity().getControllerComponent().setController(null);
        // When loosing control over an entity, stop sending the server info about it
        if (controlledEntity != null)
            if (controlledEntity.getWorld() instanceof WorldClientNetworkedRemote)
                ((WorldClientNetworkedRemote) controlledEntity.getWorld()).getRemoteServer().unsubscribe(controlledEntity);
        controlledEntity = null;
    }
    return true;
}
Also used : EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldMaster(io.xol.chunkstories.api.world.WorldMaster) WorldClientNetworkedRemote(io.xol.chunkstories.api.world.WorldClientNetworkedRemote)

Example 3 with WorldClientNetworkedRemote

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

the class EntityComponentController method pull.

@Override
public void pull(StreamSource from, DataInputStream dis) throws IOException {
    long controllerUUID = 0;
    boolean isControllerNotNull = dis.readBoolean();
    if (isControllerNotNull)
        controllerUUID = dis.readLong();
    // This is strictly illegal, only remotely connected client worlds will accept such requests
    if (!(entity.getWorld() instanceof WorldClientNetworkedRemote)) {
        // Terminate connections immediately
        if (from instanceof Player)
            ((Player) from).disconnect("Illegal controller set attempt, terminating client connection for " + from);
        return;
    }
    LocalPlayer player = ((ClientInterface) entity.getWorld().getGameContext()).getPlayer();
    assert player != null;
    if (isControllerNotNull) {
        // long controllerUUID = dis.readLong();
        long clientUUID = player.getUUID();
        System.out.println("Entity " + entity + " is now in control of " + controllerUUID + " me=" + clientUUID);
        // This update tells us we are now in control of this entity
        if (clientUUID == controllerUUID) {
            EntityControllable controlledEntity = (EntityControllable) entity;
            // TODO sort out local hosted worlds properly ?
            // Client.getInstance().getServerConnection().subscribe(entity);
            controller = player;
            player.setControlledEntity(controlledEntity);
            System.out.println("The client is now in control of entity " + controlledEntity);
        } else {
            // If we receive a different UUID than ours in a EntityComponent change, it means that we don't control it anymore and someone else does.
            if (player.getControlledEntity() != null && player.getControlledEntity().equals(entity)) {
                player.setControlledEntity(null);
                // Client.getInstance().getServerConnection().unsubscribe(entity);
                controller = null;
                System.out.println("Lost control of entity " + entity + " to " + controllerUUID);
            }
        }
    } else {
        // If we receive a different UUID than ours in a EntityComponent change, it means that we don't control it anymore and someone else does.
        if (player.getControlledEntity() != null && player.getControlledEntity().equals(entity)) {
            player.setControlledEntity(null);
            // Client.getInstance().getServerConnection().unsubscribe(entity);
            controller = null;
            System.out.println("Lost control of entity " + entity);
        }
    }
}
Also used : Player(io.xol.chunkstories.api.player.Player) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) ClientInterface(io.xol.chunkstories.api.client.ClientInterface) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) WorldClientNetworkedRemote(io.xol.chunkstories.api.world.WorldClientNetworkedRemote)

Aggregations

WorldClientNetworkedRemote (io.xol.chunkstories.api.world.WorldClientNetworkedRemote)3 EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)2 Player (io.xol.chunkstories.api.player.Player)2 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)2 Location (io.xol.chunkstories.api.Location)1 ClientInterface (io.xol.chunkstories.api.client.ClientInterface)1 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)1 Entity (io.xol.chunkstories.api.entity.Entity)1 EventItemDroppedToWorld (io.xol.chunkstories.api.events.item.EventItemDroppedToWorld)1 PlayerMoveItemEvent (io.xol.chunkstories.api.events.player.PlayerMoveItemEvent)1 Inventory (io.xol.chunkstories.api.item.inventory.Inventory)1 PacketInventoryMoveItemPile (io.xol.chunkstories.api.net.packets.PacketInventoryMoveItemPile)1 World (io.xol.chunkstories.api.world.World)1