Search in sources :

Example 1 with Player

use of com.rs.entity.player.Player in project runesource by PureCS.

the class Server method accept.

/**
     * Accepts any incoming connections.
     *
     * @throws IOException
     */
private void accept() throws IOException {
    SocketChannel socket;
    /*
         * Here we use a for loop so that we can accept multiple clients per
		 * tick for lower latency. We limit the amount of clients that we
		 * accept per tick to better combat potential denial of service type
		 * attacks.
		 */
    for (int i = 0; i < 10; i++) {
        socket = serverChannel.accept();
        if (socket == null) {
            // No more connections to accept (as this one was invalid).
            break;
        }
        // Register the connection
        HostGateway.enter(socket.socket().getInetAddress().getHostAddress());
        // Set up the new connection.
        socket.configureBlocking(false);
        SelectionKey key = socket.register(selector, SelectionKey.OP_READ);
        Client client = new Player(key);
        System.out.println("Accepted " + client + ".");
        getClientMap().put(key, client);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) Player(com.rs.entity.player.Player) Client(com.rs.entity.player.Client)

Example 2 with Player

use of com.rs.entity.player.Player in project runesource by PureCS.

the class WorldHandler method trySendMessage.

public static void trySendMessage(String username, String message) {
    try {
        Player p = WorldHandler.getInstance().getPlayer(username);
        p.sendMessage(message);
    } catch (IndexOutOfBoundsException e) {
    }
}
Also used : Player(com.rs.entity.player.Player)

Example 3 with Player

use of com.rs.entity.player.Player in project runesource by PureCS.

the class MovementHandler method tick.

public void tick() {
    Point walkPoint;
    Point runPoint = null;
    // Handle the movement.
    walkPoint = waypoints.poll();
    // Handling run toggling
    if (entity instanceof Player) {
        Player player = (Player) entity;
        if (player.getAttributes().getSettings().isRunToggled() || isRunPath()) {
            if (player.getAttributes().hasRunEnergy()) {
                runPoint = waypoints.poll();
            } else {
                // Player is out of energy
                player.sendClientSetting(173, 0);
                player.getAttributes().getSettings().setRunToggled(false);
                setRunPath(false);
                runPoint = null;
            }
        }
    } else if (isRunPath()) {
        runPoint = waypoints.poll();
    }
    // Walking
    if (walkPoint != null && walkPoint.getDirection() != -1) {
        entity.getPosition().move(Position.DIRECTION_DELTA_X[walkPoint.getDirection()], Position.DIRECTION_DELTA_Y[walkPoint.getDirection()]);
        entity.setPrimaryDirection(walkPoint.getDirection());
    }
    // Running
    if (runPoint != null && runPoint.getDirection() != -1) {
        entity.getPosition().move(Position.DIRECTION_DELTA_X[runPoint.getDirection()], Position.DIRECTION_DELTA_Y[runPoint.getDirection()]);
        entity.setSecondaryDirection(runPoint.getDirection());
        // Reducing energy
        if (entity instanceof Player) {
            Player player = (Player) entity;
            player.getAttributes().decreaseRunEnergy(player.getRunEnergyDecrement());
            player.sendRunEnergy();
        }
    } else {
        // Restoring run energy
        if (entity instanceof Player) {
            Player player = (Player) entity;
            if (player.getAttributes().getRunEnergy() != 100) {
                player.getAttributes().increaseRunEnergy(player.getRunEnergyIncrement());
                player.sendRunEnergy();
            }
        }
    }
    // Check for region changes.
    int deltaX = entity.getPosition().getX() - entity.getCurrentRegion().getRegionX() * 8;
    int deltaY = entity.getPosition().getY() - entity.getCurrentRegion().getRegionY() * 8;
    if (deltaX < 16 || deltaX >= 88 || deltaY < 16 || deltaY > 88) {
        if (entity instanceof Player) {
            ((Player) entity).sendMapRegion();
        }
    }
}
Also used : Player(com.rs.entity.player.Player)

Example 4 with Player

use of com.rs.entity.player.Player in project runesource by PureCS.

the class WorldHandler method tick.

/**
 * Performs the processing of all world functions.
 */
public void tick() throws Exception {
    // Perform any logic processing for players.
    for (Player player : players) {
        if (player == null) {
            continue;
        }
        try {
            player.tick();
        } catch (Exception ex) {
            ex.printStackTrace();
            player.disconnect();
        }
    }
    // Perform any logic processing for NPCs.
    for (Npc npc : npcs) {
        if (npc == null) {
            continue;
        }
        try {
            npc.tick();
        } catch (Exception ex) {
            ex.printStackTrace();
            unregister(npc);
        }
    }
    // Process all plugins.
    PluginHandler.dispatchTick();
    // Process all tasks
    TaskHandler.tick();
    // Update all players.
    barrier.bulkRegister(playerAmount);
    for (Player player : players) {
        if (player == null) {
            continue;
        }
        threadPool.execute(() -> {
            synchronized (player) {
                try {
                    PlayerUpdating.update(player);
                    NpcUpdating.update(player);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    barrier.arriveAndDeregister();
                }
            }
        });
    }
    barrier.arriveAndAwaitAdvance();
    // Reset all players after tick.
    for (Player player : players) {
        if (player == null) {
            continue;
        }
        try {
            player.reset();
        } catch (Exception ex) {
            ex.printStackTrace();
            player.disconnect();
        }
    }
    // Reset all NPCs after tick.
    for (Npc npc : npcs) {
        if (npc == null) {
            continue;
        }
        try {
            npc.reset();
        } catch (Exception ex) {
            ex.printStackTrace();
            unregister(npc);
        }
    }
}
Also used : Npc(com.rs.entity.npc.Npc) Player(com.rs.entity.player.Player)

Example 5 with Player

use of com.rs.entity.player.Player in project runesource by PureCS.

the class NetworkService method accept.

/**
 * Accepts up to n incoming connections.
 */
private void accept(int n) throws IOException {
    SocketChannel socket;
    /*
         * Here we use a for loop so that we can accept multiple clients per
         * tick for lower latency. We limit the amount of clients that we
         * accept per tick to better combat potential denial of service type
         * attacks.
         */
    for (int i = 0; i < n; i++) {
        socket = serverChannel.accept();
        if (socket == null) {
            // No more connections to accept (as this one was invalid).
            break;
        }
        // Register the connection
        HostGateway.enter(socket.socket().getInetAddress().getHostAddress());
        // Set up the new connection.
        socket.configureBlocking(false);
        SelectionKey key = socket.register(selector, SelectionKey.OP_READ);
        Client client = new Player(key);
        System.out.println("Accepted " + client + ".");
        clientMap.put(key, client);
    }
}
Also used : ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketChannel(java.nio.channels.SocketChannel) SelectionKey(java.nio.channels.SelectionKey) Player(com.rs.entity.player.Player) Client(com.rs.entity.player.Client)

Aggregations

Player (com.rs.entity.player.Player)5 Client (com.rs.entity.player.Client)2 SelectionKey (java.nio.channels.SelectionKey)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)2 SocketChannel (java.nio.channels.SocketChannel)2 Npc (com.rs.entity.npc.Npc)1