Guest User

Untitled

a guest
Oct 7th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. package org.anemix.rs2.model.players;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.Socket;
  7.  
  8. import org.anemix.net.ActionSender;
  9. import org.anemix.net.packet.PacketHandler;
  10. import org.anemix.rs2.model.Entity;
  11. import org.anemix.rs2.model.PlayerUpdating;
  12. import org.anemix.util.Constants;
  13. import org.anemix.util.ISAACAlgorithm;
  14. import org.anemix.util.Stream;
  15.  
  16. /**
  17. * Represents a player entity
  18. *
  19. * @author animeking1120
  20. *
  21. */
  22. public class Player extends Entity {
  23.  
  24. public Socket socket;
  25.  
  26. private Stream inStream, outStream;
  27. private InputStream inputStream;
  28. private OutputStream outputStream;
  29.  
  30. private ISAACAlgorithm instreamDecryption, outStreamEncryption;
  31.  
  32. private String username, password;
  33.  
  34. public int packetType = -1, packetLength = 0;
  35.  
  36. public byte[] buffer;
  37.  
  38. private int timeoutCounter = 0;
  39.  
  40. public int playerStandIndex = 0x328;
  41. public int playerTurnIndex = 0x337;
  42. public int playerWalkIndex = 0x333;
  43. public int playerTurn180Index = 0x334;
  44. public int playerTurn90CWIndex = 0x335;
  45. public int playerTurn90CCWIndex = 0x336;
  46. public int playerRunIndex = 0x338;
  47.  
  48. private PlayerUpdating playerUpdating = new PlayerUpdating(this);
  49. private ActionSender actionSender = new ActionSender(this);
  50.  
  51. public Player(Socket socket) {
  52. this.socket = socket;
  53. try {
  54. inputStream = socket.getInputStream();
  55. outputStream = socket.getOutputStream();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. inStream = new Stream(new byte[10000]);
  60. inStream.currentOffset = 0;
  61. outStream = new Stream(new byte[10000]);
  62. outStream.currentOffset = 0;
  63. buffer = new byte[10000];
  64. }
  65.  
  66. public void destruct() {
  67. try {
  68. socket.close();
  69. inStream = null;
  70. inputStream.close();
  71. inputStream = null;
  72. outputStream.close();
  73. outputStream = null;
  74. outStream = null;
  75. } catch (IOException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79.  
  80. public void flushOutStream() {
  81. if (getOutStream().currentOffset == 0 || this == null) {
  82. return;
  83. }
  84. try {
  85. outputStream.write(getOutStream().buffer, 0,
  86. getOutStream().currentOffset);
  87. outStream.currentOffset = 0;
  88. } catch (IOException e) {
  89. // e.printStackTrace();
  90. }
  91. }
  92.  
  93. public void update() {
  94. getPlayerUpdating().updateThisPlayer(this, getOutStream());
  95. flushOutStream();
  96. }
  97.  
  98. public void fillInStream(int forceRead) throws java.io.IOException {
  99. inStream.currentOffset = 0;
  100. inputStream.read(inStream.buffer, 0, forceRead);
  101. }
  102.  
  103. public synchronized boolean processPackets() {
  104. try {
  105. if (timeoutCounter++ > 50) {
  106. return false;
  107. }
  108. if (inputStream == null)
  109. return false;
  110. int read = inputStream.available();
  111. if (read == 0) {
  112. return false;
  113. }
  114. if (packetType == -1) {
  115. packetType = inputStream.read() & 0xFF;
  116. if (instreamDecryption != null)
  117. packetType = (packetType - instreamDecryption.nextInt()) & 0xFF;
  118. packetLength = Constants.packetSizes[packetType];
  119. read--;
  120. }
  121. if (packetLength == -1) {
  122. if (read > 0) {
  123. packetLength = inputStream.read() & 0xff;
  124. read--;
  125. }
  126. }
  127. if (read < packetLength) {
  128. return false;
  129. }
  130. fillInStream(packetLength);
  131. timeoutCounter = 0;
  132. PacketHandler.parseIncomingPackets(this, packetType, packetLength);
  133. packetType = -1;
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. return true;
  138. }
  139.  
  140. public Stream getInStream() {
  141. return inStream;
  142. }
  143.  
  144. public Stream getOutStream() {
  145. return outStream;
  146. }
  147.  
  148. public void setInStream(Stream inStream) {
  149. this.inStream = inStream;
  150. }
  151.  
  152. public void setOutStream(Stream outStream) {
  153. this.outStream = outStream;
  154. }
  155.  
  156. public InputStream getInputStream() {
  157. return inputStream;
  158. }
  159.  
  160. public OutputStream getOutputStream() {
  161. return outputStream;
  162. }
  163.  
  164. public ISAACAlgorithm getInstreamDecryption() {
  165. return instreamDecryption;
  166. }
  167.  
  168. public void setInstreamDecryption(ISAACAlgorithm i) {
  169. this.instreamDecryption = i;
  170. }
  171.  
  172. public ISAACAlgorithm getOutstreamEncryption() {
  173. return outStreamEncryption;
  174. }
  175.  
  176. public void setOutstreamEncryption(ISAACAlgorithm i) {
  177. this.outStreamEncryption = i;
  178. }
  179.  
  180. public String getUsername() {
  181. return username;
  182. }
  183.  
  184. public void setUsername(String username) {
  185. this.username = username;
  186. }
  187.  
  188. public PlayerUpdating getPlayerUpdating() {
  189. return playerUpdating;
  190. }
  191.  
  192. public void setPassword(String password) {
  193. this.password = password;
  194. }
  195.  
  196. public ActionSender getActionSender() {
  197. return actionSender;
  198. }
  199.  
  200. }
Add Comment
Please, Sign In to add comment