Search in sources :

Example 1 with PlayerInputPressedEvent

use of io.xol.chunkstories.api.events.player.PlayerInputPressedEvent in project chunkstories by Hugobros3.

the class Lwjgl3ClientInputsManager method onInputPressed.

public boolean onInputPressed(Input input) {
    if (input.equals("fullscreen")) {
        gameWindow.toggleFullscreen();
        return true;
    }
    // System.out.println("Input pressed "+input.getName());
    // Try the client-side event press
    ClientInputPressedEvent event = new ClientInputPressedEvent(gameWindow.getClient(), input);
    ClientPluginManager cpm = gameWindow.getClient().getPluginManager();
    if (cpm != null) {
        cpm.fireEvent(event);
        if (event.isCancelled())
            return false;
    }
    // Try the GUI handling
    Layer layer = gameWindow.getLayer();
    if (layer.handleInput(input))
        return true;
    // System.out.println("wasn't handled");
    final LocalPlayer player = Client.getInstance().getPlayer();
    if (player == null)
        return false;
    final EntityControllable entityControlled = player.getControlledEntity();
    // There has to be a controlled entity for sending inputs to make sense.
    if (entityControlled == null)
        return false;
    // Send input to server
    World world = entityControlled.getWorld();
    if (world instanceof WorldClientRemote) {
        // MouseScroll inputs are strictly client-side
        if (!(input instanceof MouseScroll)) {
            ServerConnection connection = ((WorldClientRemote) entityControlled.getWorld()).getConnection();
            PacketInput packet = new PacketInput(world);
            packet.input = input;
            packet.isPressed = true;
            connection.pushPacket(packet);
        }
        return entityControlled.onControllerInput(input, Client.getInstance().getPlayer());
    } else {
        PlayerInputPressedEvent event2 = new PlayerInputPressedEvent(Client.getInstance().getPlayer(), input);
        cpm.fireEvent(event2);
        if (event2.isCancelled())
            return false;
    // entity.handleInteraction(input, entity.getControllerComponent().getController());
    }
    // Handle interaction locally
    return entityControlled.onControllerInput(input, Client.getInstance().getPlayer());
}
Also used : ClientPluginManager(io.xol.chunkstories.api.plugin.ClientPluginManager) PacketInput(io.xol.chunkstories.net.packets.PacketInput) LocalPlayer(io.xol.chunkstories.api.client.LocalPlayer) WorldClientRemote(io.xol.chunkstories.world.WorldClientRemote) ServerConnection(io.xol.chunkstories.client.net.ServerConnection) ClientInputPressedEvent(io.xol.chunkstories.api.events.client.ClientInputPressedEvent) World(io.xol.chunkstories.api.world.World) Layer(io.xol.chunkstories.api.gui.Layer) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) MouseScroll(io.xol.chunkstories.api.input.Mouse.MouseScroll) PlayerInputPressedEvent(io.xol.chunkstories.api.events.player.PlayerInputPressedEvent)

Example 2 with PlayerInputPressedEvent

use of io.xol.chunkstories.api.events.player.PlayerInputPressedEvent in project chunkstories by Hugobros3.

the class PacketInput method process.

public void process(PacketSender sender, DataInputStream in, PacketReceptionContext processor) throws IOException {
    long code = in.readLong();
    boolean pressed = in.readBoolean();
    if (processor instanceof ServerPlayerPacketsProcessor) {
        ServerPlayerPacketsProcessor sppc = (ServerPlayerPacketsProcessor) processor;
        // Look for the controller handling this buisness
        EntityControllable entity = (EntityControllable) sppc.getPlayer().getControlledEntity();
        if (entity != null) {
            // Get input of the client
            input = sppc.getPlayer().getInputsManager().getInputFromHash(code);
            if (input == null)
                throw new NullPointerException("Unknown input hash : " + code);
            // System.out.println(processor.getServerClient().getProfile() +
            // " "+input + " "+pressed);
            // Update it's state
            ((InputVirtual) input).setPressed(pressed);
            // Fire appropriate event
            if (pressed) {
                PlayerInputPressedEvent event = new PlayerInputPressedEvent(sppc.getPlayer(), input);
                entity.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
                if (!event.isCancelled())
                    entity.onControllerInput(input, entity.getControllerComponent().getController());
            } else {
                PlayerInputReleasedEvent event = new PlayerInputReleasedEvent(sppc.getPlayer(), input);
                entity.getWorld().getGameLogic().getPluginsManager().fireEvent(event);
            }
        // TODO why is this disabled and still there ?
        // If we pressed the input, apply game logic
        // if(pressed)
        // entity.handleInteraction(input,
        // entity.getControllerComponent().getController());
        }
    }
}
Also used : PlayerInputReleasedEvent(io.xol.chunkstories.api.events.player.PlayerInputReleasedEvent) ServerPlayerPacketsProcessor(io.xol.chunkstories.api.server.ServerPacketsProcessor.ServerPlayerPacketsProcessor) EntityControllable(io.xol.chunkstories.api.entity.interfaces.EntityControllable) PlayerInputPressedEvent(io.xol.chunkstories.api.events.player.PlayerInputPressedEvent) InputVirtual(io.xol.chunkstories.input.InputVirtual)

Aggregations

EntityControllable (io.xol.chunkstories.api.entity.interfaces.EntityControllable)2 PlayerInputPressedEvent (io.xol.chunkstories.api.events.player.PlayerInputPressedEvent)2 LocalPlayer (io.xol.chunkstories.api.client.LocalPlayer)1 ClientInputPressedEvent (io.xol.chunkstories.api.events.client.ClientInputPressedEvent)1 PlayerInputReleasedEvent (io.xol.chunkstories.api.events.player.PlayerInputReleasedEvent)1 Layer (io.xol.chunkstories.api.gui.Layer)1 MouseScroll (io.xol.chunkstories.api.input.Mouse.MouseScroll)1 ClientPluginManager (io.xol.chunkstories.api.plugin.ClientPluginManager)1 ServerPlayerPacketsProcessor (io.xol.chunkstories.api.server.ServerPacketsProcessor.ServerPlayerPacketsProcessor)1 World (io.xol.chunkstories.api.world.World)1 ServerConnection (io.xol.chunkstories.client.net.ServerConnection)1 InputVirtual (io.xol.chunkstories.input.InputVirtual)1 PacketInput (io.xol.chunkstories.net.packets.PacketInput)1 WorldClientRemote (io.xol.chunkstories.world.WorldClientRemote)1