Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.anemix.rs2.model.players;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.Socket;
- import org.anemix.net.ActionSender;
- import org.anemix.net.packet.PacketHandler;
- import org.anemix.rs2.model.Entity;
- import org.anemix.rs2.model.PlayerUpdating;
- import org.anemix.util.Constants;
- import org.anemix.util.ISAACAlgorithm;
- import org.anemix.util.Stream;
- /**
- * Represents a player entity
- *
- * @author animeking1120
- *
- */
- public class Player extends Entity {
- public Socket socket;
- private Stream inStream, outStream;
- private InputStream inputStream;
- private OutputStream outputStream;
- private ISAACAlgorithm instreamDecryption, outStreamEncryption;
- private String username, password;
- public int packetType = -1, packetLength = 0;
- public byte[] buffer;
- private int timeoutCounter = 0;
- public int playerStandIndex = 0x328;
- public int playerTurnIndex = 0x337;
- public int playerWalkIndex = 0x333;
- public int playerTurn180Index = 0x334;
- public int playerTurn90CWIndex = 0x335;
- public int playerTurn90CCWIndex = 0x336;
- public int playerRunIndex = 0x338;
- private PlayerUpdating playerUpdating = new PlayerUpdating(this);
- private ActionSender actionSender = new ActionSender(this);
- public Player(Socket socket) {
- this.socket = socket;
- try {
- inputStream = socket.getInputStream();
- outputStream = socket.getOutputStream();
- } catch (IOException e) {
- e.printStackTrace();
- }
- inStream = new Stream(new byte[10000]);
- inStream.currentOffset = 0;
- outStream = new Stream(new byte[10000]);
- outStream.currentOffset = 0;
- buffer = new byte[10000];
- }
- public void destruct() {
- try {
- socket.close();
- inStream = null;
- inputStream.close();
- inputStream = null;
- outputStream.close();
- outputStream = null;
- outStream = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void flushOutStream() {
- if (getOutStream().currentOffset == 0 || this == null) {
- return;
- }
- try {
- outputStream.write(getOutStream().buffer, 0,
- getOutStream().currentOffset);
- outStream.currentOffset = 0;
- } catch (IOException e) {
- // e.printStackTrace();
- }
- }
- public void update() {
- getPlayerUpdating().updateThisPlayer(this, getOutStream());
- flushOutStream();
- }
- public void fillInStream(int forceRead) throws java.io.IOException {
- inStream.currentOffset = 0;
- inputStream.read(inStream.buffer, 0, forceRead);
- }
- public synchronized boolean processPackets() {
- try {
- if (timeoutCounter++ > 50) {
- return false;
- }
- if (inputStream == null)
- return false;
- int read = inputStream.available();
- if (read == 0) {
- return false;
- }
- if (packetType == -1) {
- packetType = inputStream.read() & 0xFF;
- if (instreamDecryption != null)
- packetType = (packetType - instreamDecryption.nextInt()) & 0xFF;
- packetLength = Constants.packetSizes[packetType];
- read--;
- }
- if (packetLength == -1) {
- if (read > 0) {
- packetLength = inputStream.read() & 0xff;
- read--;
- }
- }
- if (read < packetLength) {
- return false;
- }
- fillInStream(packetLength);
- timeoutCounter = 0;
- PacketHandler.parseIncomingPackets(this, packetType, packetLength);
- packetType = -1;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return true;
- }
- public Stream getInStream() {
- return inStream;
- }
- public Stream getOutStream() {
- return outStream;
- }
- public void setInStream(Stream inStream) {
- this.inStream = inStream;
- }
- public void setOutStream(Stream outStream) {
- this.outStream = outStream;
- }
- public InputStream getInputStream() {
- return inputStream;
- }
- public OutputStream getOutputStream() {
- return outputStream;
- }
- public ISAACAlgorithm getInstreamDecryption() {
- return instreamDecryption;
- }
- public void setInstreamDecryption(ISAACAlgorithm i) {
- this.instreamDecryption = i;
- }
- public ISAACAlgorithm getOutstreamEncryption() {
- return outStreamEncryption;
- }
- public void setOutstreamEncryption(ISAACAlgorithm i) {
- this.outStreamEncryption = i;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public PlayerUpdating getPlayerUpdating() {
- return playerUpdating;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public ActionSender getActionSender() {
- return actionSender;
- }
- }
Add Comment
Please, Sign In to add comment