Search in sources :

Example 16 with EntityControllable

use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories by Hugobros3.

the class Ingame method render.

@Override
public void render(RenderingInterface renderer) {
    // Update client entity
    if ((playerEntity == null || playerEntity != getPlayer().getControlledEntity()) && getPlayer().getControlledEntity() != null) {
        playerEntity = getPlayer().getControlledEntity();
        if (playerEntity instanceof EntityWithSelectedItem)
            inventoryBarDrawer = ((EntityWithSelectedItem) playerEntity).getInventory() == null ? null : new InventoryGridRenderer((EntityWithSelectedItem) playerEntity);
        else
            inventoryBarDrawer = null;
    }
    if (playerEntity != null && ((EntityLiving) playerEntity).isDead() && !(gameWindow.getLayer() instanceof DeathScreen))
        gameWindow.setLayer(new DeathScreen(gameWindow, this));
    // Update the player
    if (playerEntity instanceof EntityControllable)
        ((EntityControllable) playerEntity).onEachFrame(getPlayer());
    Location selectedBlock = null;
    if (playerEntity instanceof EntityControllable)
        selectedBlock = ((EntityControllable) playerEntity).getBlockLookingAt(true);
    world.getPluginManager().fireEvent(new CameraSetupEvent(renderer.getCamera()));
    // Main render call
    world.getWorldRenderer().renderWorld(renderer);
    // Debug draws
    if (client.getConfiguration().getBooleanOption("client.debug.physicsVisualization") && playerEntity != null) {
        wireframeDebugger.render(renderer);
    }
    if (!guiHidden && selectedBlock != null && playerEntity instanceof EntityCreative && ((EntityCreative) playerEntity).getCreativeModeComponent().get())
        selectionRenderer.drawSelectionBox(renderer, selectedBlock);
    // Fades in & out the overlay
    if (!isCovered()) {
        if (pauseOverlayFade > 0.0)
            pauseOverlayFade -= 0.1;
    } else {
        float maxFade = 1.0f;
        if (gameWindow.getLayer() instanceof ChatPanelOverlay)
            maxFade = 0.25f;
        if (pauseOverlayFade < maxFade)
            pauseOverlayFade += 0.1;
    }
    // Blit the final 3d image
    world.getWorldRenderer().blitFinalImage(renderer, guiHidden);
    // Draw the GUI
    if (!guiHidden) {
        chatManager.render(renderer);
        // Draw inventory
        if (playerEntity != null && inventoryBarDrawer != null)
            inventoryBarDrawer.drawPlayerInventorySummary(renderer, renderer.getWindow().getWidth() / 2 - 7, 64 + 64);
        // Draw debug info
        if (client.getConfiguration().getBooleanOption("client.debug.showDebugInfo"))
            debugInfoRenderer.drawF3debugMenu(renderer);
        renderer.getGuiRenderer().drawBoxWindowsSpaceWithSize(getGameWindow().getWidth() / 2 - 8, getGameWindow().getHeight() / 2 - 8, 16, 16, 0, 1, 1, 0, renderer.textures().getTexture("./textures/gui/cursor.png"), false, true, null);
    }
    // Lack of overlay should infer autofocus
    if (!isCovered())
        focus(true);
    // Check connection didn't died and change scene if it has
    if (world instanceof WorldClientRemote) {
        if (!((WorldClientRemote) world).getConnection().isOpen())
            gameWindow.getClient().exitToMainMenu("Connection terminated : " + "(TODO: not this way)");
    }
    // Auto-switch to pause if it detects the game isn't in focus anymore
    if (!gameWindow.hasFocus() && !isCovered()) {
        focus(false);
        gameWindow.setLayer(new PauseMenu(gameWindow, gameWindow.getLayer()));
    }
}
Also used : EntityLiving(io.xol.chunkstories.api.entity.EntityLiving) CameraSetupEvent(io.xol.chunkstories.api.events.rendering.CameraSetupEvent) InventoryGridRenderer(io.xol.chunkstories.gui.InventoryGridRenderer) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote) EntityWithSelectedItem(io.xol.chunkstories.api.entity.interfaces.EntityWithSelectedItem) EntityCreative(io.xol.chunkstories.api.entity.interfaces.EntityCreative) ChatPanelOverlay(io.xol.chunkstories.gui.layer.ingame.ChatManager.ChatPanelOverlay) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) Location(io.xol.chunkstories.api.Location)

Example 17 with EntityControllable

use of io.xol.chunkstories.api.entity.interfaces.EntityControllable 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 18 with EntityControllable

use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories-api by Hugobros3.

the class PacketInventoryMoveItemPile method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    if (!(processor instanceof ServerPlayerPacketsProcessor)) {
        processor.logger().warn("Received a " + this.getClass().getSimpleName() + " but this GameContext isn't providen with a packet processor made to deal with it");
        return;
    }
    ServerPlayerPacketsProcessor sppc = (ServerPlayerPacketsProcessor) processor;
    Player player = sppc.getPlayer();
    EntityControllable playerEntity = player.getControlledEntity();
    oldX = in.readInt();
    oldY = in.readInt();
    newX = in.readInt();
    newY = in.readInt();
    amount = in.readInt();
    from = InventoryTranslator.obtainInventoryHandle(in, processor);
    to = InventoryTranslator.obtainInventoryHandle(in, processor);
    // If this pile is spawned from the void
    if (// || from == InventoryTranslator.INVENTORY_CREATIVE_TRASH)
    from == null) {
        try {
            itemPile = ItemPile.obtainItemPileFromStream(player.getWorld().getContentTranslator(), in);
        } catch (NullItemException e) {
            // This ... isn't supposed to happen
            processor.logger().info("User " + sender + " is trying to spawn a null ItemPile for some reason.");
        } catch (UndefinedItemTypeException e) {
            // This is slightly more problematic
            processor.logger().warn(e.getMessage());
        // e.printStackTrace(processor.getLogger().getPrintWriter());
        }
    } else {
        itemPile = from.getItemPileAt(oldX, oldY);
    }
    // Check access
    if (to != null && playerEntity != null) {
        if (!to.isAccessibleTo(playerEntity)) {
            player.sendMessage("You don't have access to this.");
            return;
        }
    }
    // Check using event
    PlayerMoveItemEvent moveItemEvent = new PlayerMoveItemEvent(player, itemPile, from, to, oldX, oldY, newX, newY, amount);
    player.getContext().getPluginManager().fireEvent(moveItemEvent);
    if (!moveItemEvent.isCancelled()) {
        // Restrict item spawning
        if (// || from instanceof InventoryLocalCreativeMenu)
        from == null) {
            if (player.hasPermission("items.spawn") || (player.getControlledEntity() != null && player.getControlledEntity() instanceof EntityCreative && ((EntityCreative) player.getControlledEntity()).getCreativeModeComponent().get())) {
            // Let it happen when in creative mode or owns items.spawn perm
            } else {
                player.sendMessage("#C00000You are neither in creative mode nor have the items.spawn permission.");
                return;
            }
        }
        // If target inventory is null, this means the item was dropped
        if (to == null) {
            if (playerEntity == null) {
                System.out.println("Dropping items isn't possible if the player doesn't control any entity.");
                return;
            }
            // If we're pulling this out of an inventory ( and not /dev/null ), we need to remove it from that
            Inventory sourceInventory = itemPile.getInventory();
            Location loc = playerEntity.getLocation();
            EventItemDroppedToWorld dropItemEvent = new EventItemDroppedToWorld(loc, sourceInventory, itemPile);
            player.getContext().getPluginManager().fireEvent(dropItemEvent);
            if (!dropItemEvent.isCancelled()) {
                if (sourceInventory != null)
                    sourceInventory.setItemPileAt(itemPile.getX(), itemPile.getY(), null);
                if (dropItemEvent.getItemEntity() != null)
                    loc.getWorld().addEntity(dropItemEvent.getItemEntity());
            }
            return;
        }
        itemPile.moveItemPileTo(to, newX, newY, amount);
    }
}
Also used : UndefinedItemTypeException(io.xol.chunkstories.api.exceptions.UndefinedItemTypeException) Player(io.xol.chunkstories.api.player.Player) EventItemDroppedToWorld(io.xol.chunkstories.api.events.item.EventItemDroppedToWorld) PlayerMoveItemEvent(io.xol.chunkstories.api.events.player.PlayerMoveItemEvent) EntityCreative(io.xol.chunkstories.api.entity.interfaces.EntityCreative) ServerPlayerPacketsProcessor(io.xol.chunkstories.api.server.ServerPacketsProcessor.ServerPlayerPacketsProcessor) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) NullItemException(io.xol.chunkstories.api.exceptions.NullItemException) Inventory(io.xol.chunkstories.api.item.inventory.Inventory) Location(io.xol.chunkstories.api.Location)

Example 19 with EntityControllable

use of io.xol.chunkstories.api.entity.interfaces.EntityControllable in project chunkstories-api by Hugobros3.

the class EntityComponent method pushComponentEveryoneButController.

/**
 * Push the component to everyone but the controller, if such one exists
 */
public void pushComponentEveryoneButController() {
    // System.out.println("pushing2all");
    Iterator<Subscriber> iterator = entity.getAllSubscribers();
    Controller controller = null;
    if (entity instanceof EntityControllable) {
        controller = ((EntityControllable) entity).getControllerComponent().getController();
    }
    while (iterator.hasNext()) {
        Subscriber subscriber = iterator.next();
        // Don't push the update to the controller.
        if (controller != null && subscriber.equals(controller))
            continue;
        this.pushComponent(subscriber);
    }
}
Also used : Controller(io.xol.chunkstories.api.entity.Controller) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable)

Example 20 with EntityControllable

use of io.xol.chunkstories.api.entity.interfaces.EntityControllable 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

EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)27 Controller (io.xol.chunkstories.api.entity.Controller)10 Location (io.xol.chunkstories.api.Location)9 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)9 Player (io.xol.chunkstories.api.player.Player)7 Vector3d (org.joml.Vector3d)6 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)5 Entity (io.xol.chunkstories.api.entity.Entity)5 EntityCreative (io.xol.chunkstories.api.entity.interfaces.EntityCreative)4 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)3 World (io.xol.chunkstories.api.world.World)3 WorldClientRemote (io.xol.chunkstories.world.WorldClientRemote)3 ClientInterface (io.xol.chunkstories.api.client.ClientInterface)2 EntityWorldModifier (io.xol.chunkstories.api.entity.interfaces.EntityWorldModifier)2 PlayerInputPressedEvent (io.xol.chunkstories.api.events.player.PlayerInputPressedEvent)2 PlayerInputReleasedEvent (io.xol.chunkstories.api.events.player.PlayerInputReleasedEvent)2 PlayerVoxelModificationEvent (io.xol.chunkstories.api.events.player.voxel.PlayerVoxelModificationEvent)2 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)2 ItemPile (io.xol.chunkstories.api.item.inventory.ItemPile)2 ClientPluginManager (io.xol.chunkstories.api.plugin.ClientPluginManager)2