Guest User

Untitled

a guest
Jul 16th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.49 KB | None | 0 0
  1. package com.rs2hd.packetbuilder;
  2.  
  3. import com.rs2hd.Constants;
  4. import com.rs2hd.model.Appearance;
  5. import com.rs2hd.model.Equipment;
  6. import com.rs2hd.model.Item;
  7. import com.rs2hd.model.Player;
  8. import com.rs2hd.model.PlayerUpdateFlags;
  9. import com.rs2hd.model.World;
  10. import com.rs2hd.net.Packet.Size;
  11. import com.rs2hd.util.Misc;
  12.  
  13. /**
  14. * Player update block.
  15. * @author Graham
  16. *
  17. */
  18. public class PlayerUpdate implements PacketBuilder {
  19.  
  20. /**
  21. * Prevent instance creation.
  22. */
  23. private PlayerUpdate() {}
  24.  
  25. /**
  26. * Update the specified player.
  27. * @param p
  28. */
  29. public static void update(Player p) {
  30. if(p.getUpdateFlags().didMapRegionChange()) {
  31. p.getActionSender().sendMapRegion();
  32. }
  33. /*StaticPacketBuilder mainPacket = new StaticPacketBuilder().setId(24).setSize(Size.VAR_SHORT).initBitAccess();
  34. StaticPacketBuilder updateBlock = new StaticPacketBuilder().setBare(true);
  35. updateThisPlayerMovement(p, mainPacket);
  36. if(p.getUpdateFlags().isUpdateRequired()) {
  37. appendUpdateBlock(p, updateBlock, false);
  38. }
  39. mainPacket.addBits(8, p.getPlayerListSize());
  40. int size = p.getPlayerListSize();
  41. p.setPlayerListSize(0);
  42. boolean[] newPlayer = new boolean[Constants.PLAYER_CAP];
  43. for(int i = 0; i < size; i++) {
  44. if(p.getPlayerList()[i] == null || p.getPlayerList()[i].isDisconnected() || !p.getPlayerList()[i].getLocation().withinDistance(p.getLocation()) || p.getPlayerList()[i].getUpdateFlags().didTeleport() || p.getPlayerList()[i].isHidden()) {
  45. if(p.getPlayerList()[i] != null) {
  46. p.getPlayersInList()[p.getPlayerList()[i].getIndex()] = 0;
  47. }
  48. mainPacket.addBits(1, 1);
  49. mainPacket.addBits(2, 3);
  50. } else {
  51. updatePlayerMovement(p.getPlayerList()[i], mainPacket);
  52. p.getPlayerList()[p.getPlayerListSize()] = p.getPlayerList()[i];
  53. p.setPlayerListSize(p.getPlayerListSize()+1);
  54. }
  55. }
  56. for(Player p2 : World.getInstance().getPlayerList()) {
  57. if(p2 == null || p2.getIndex() == p.getIndex()) {
  58. continue;
  59. }
  60. if(p.getPlayersInList()[p2.getIndex()] == 1 || !p2.getLocation().withinDistance(p.getLocation()) || p2.isHidden()) {
  61. continue;
  62. }
  63. newPlayer[p.getPlayerListSize()] = true;
  64. addNewPlayer(p, p2, mainPacket);
  65. }
  66. for(int i = 0; i < p.getPlayerListSize(); i++) {
  67. if(newPlayer[i]) {
  68. appendUpdateBlock(p.getPlayerList()[i], updateBlock, true);
  69. } else {
  70. if(p.getPlayerList()[i].getUpdateFlags().isUpdateRequired()) {
  71. appendUpdateBlock(p.getPlayerList()[i], updateBlock, false);
  72. }
  73. }
  74. }
  75. if(updateBlock.getLength() > 0) {
  76. mainPacket.addBits(11, 2047);
  77. }
  78. mainPacket.finishBitAccess();
  79. if(updateBlock.getLength() > 0) {
  80. mainPacket.addBytes(updateBlock.toPacket().getData());
  81. }
  82. p.getSession().write(mainPacket.toPacket());*/
  83. }
  84.  
  85. private static void addNewPlayer(Player p, Player p2, StaticPacketBuilder updateBlock) {
  86. p.getPlayersInList()[p2.getIndex()] = 1;
  87. p.getPlayerList()[p.getPlayerListSize()] = p2;
  88. p.setPlayerListSize(p.getPlayerListSize()+1);
  89. updateBlock.addBits(11, p2.getIndex());
  90. int yPos = p2.getLocation().getY() - p.getLocation().getY();
  91. int xPos = p2.getLocation().getX() - p.getLocation().getX();
  92. if(xPos < 0) {
  93. xPos += 32;
  94. }
  95. if(yPos < 0) {
  96. yPos += 32;
  97. }
  98. updateBlock.addBits(5, yPos);
  99. updateBlock.addBits(5, xPos);
  100. updateBlock.addBits(1, 1);
  101. updateBlock.addBits(1, 1);
  102. updateBlock.addBits(3, 1);
  103. }
  104.  
  105. private static void updatePlayerMovement(Player player, StaticPacketBuilder mainPacket) {
  106. if(player.getSprites().getPrimarySprite() == -1) {
  107. if(player.getUpdateFlags().isUpdateRequired()) {
  108. mainPacket.addBits(1, 1);
  109. mainPacket.addBits(2, 0);
  110. } else {
  111. mainPacket.addBits(1, 0);
  112. }
  113. } else if (player.getSprites().getSecondarySprite() == -1) {
  114. mainPacket.addBits(1, 1);
  115. mainPacket.addBits(2, 1);
  116. mainPacket.addBits(3, player.getSprites().getPrimarySprite());
  117. mainPacket.addBits(1, player.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  118. } else {
  119. mainPacket.addBits(1, 1);
  120. mainPacket.addBits(2, 2);
  121. mainPacket.addBits(3, player.getSprites().getPrimarySprite());
  122. mainPacket.addBits(3, player.getSprites().getSecondarySprite());
  123. mainPacket.addBits(1, player.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  124. }
  125. }
  126.  
  127. private static void updateThisPlayerMovement(Player p, StaticPacketBuilder mainPacket) {
  128. if(p.getUpdateFlags().didTeleport()) {
  129. mainPacket.addBits(1, 1);
  130. mainPacket.addBits(2, 3);
  131. mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  132. mainPacket.addBits(2, p.getLocation().getZ());
  133. mainPacket.addBits(1, 1);
  134. mainPacket.addBits(7, p.getLocation().getLocalX(p.getUpdateFlags().getLastRegion()));
  135. mainPacket.addBits(7, p.getLocation().getLocalY(p.getUpdateFlags().getLastRegion()));
  136. } else {
  137. if(p.getSprites().getPrimarySprite() == -1) {
  138. mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  139. if(p.getUpdateFlags().isUpdateRequired()) {
  140. mainPacket.addBits(2, 0);
  141. }
  142. } else {
  143. if(p.getSprites().getSecondarySprite() != -1) {
  144. mainPacket.addBits(1, 1);
  145. mainPacket.addBits(2, 2);
  146. mainPacket.addBits(1, 1);
  147. mainPacket.addBits(3, p.getSprites().getSecondarySprite());
  148. mainPacket.addBits(3, p.getSprites().getPrimarySprite());
  149. mainPacket.addBits(1, p.getUpdateFlags().isUpdateRequired() ? 1 : 0);
  150. }
  151. }
  152. }
  153. }
  154.  
  155. private static void appendUpdateBlock(Player p, StaticPacketBuilder updateBlock, boolean forceAppearance) {
  156. int mask = 0x0;
  157.  
  158. PlayerUpdateFlags flags = p.getUpdateFlags();
  159. if(flags.isAppearanceUpdateRequired() || forceAppearance) {
  160. mask |= 0x1;
  161. }
  162. if(flags.isGraphicsUpdateRequired()) {
  163. mask |= 0x100;
  164. }
  165. if(flags.isAnimationUpdateRequired()) {
  166. mask |= 0x4;
  167. }
  168. if(flags.isHit2UpdateRequired()) {
  169. mask |= 0x400;
  170. }
  171. if(flags.isFaceToUpdateRequired()) {
  172. mask |= 0x10;
  173. }
  174. if(flags.isHitUpdateRequired()) {
  175. mask |= 0x20;
  176. }
  177. if(flags.isChatTextUpdateRequired()) {
  178. mask |= 0x80;
  179. }
  180.  
  181. if (mask >= 0x100) {
  182. mask |= 0x40;
  183. updateBlock.addByte((byte) (mask & 0xFF));
  184. updateBlock.addByte((byte) (mask >> 8));
  185. } else {
  186. updateBlock.addByte((byte) (mask & 0xFF));
  187. }
  188.  
  189. if(flags.isAppearanceUpdateRequired() || forceAppearance) {
  190. appendAppearanceUpdate(p, updateBlock);
  191. }
  192. if(flags.isGraphicsUpdateRequired()) {
  193. appendGraphicsUpdate(p, updateBlock);
  194. }
  195. if(flags.isAnimationUpdateRequired()) {
  196. appendAnimationUpdate(p, updateBlock);
  197. }
  198. if(flags.isHit2UpdateRequired()) {
  199. appendHit2Update(p, updateBlock);
  200. }
  201. if(flags.isFaceToUpdateRequired()) {
  202. appendFaceToUpdate(p, updateBlock);
  203. }
  204. if(flags.isHitUpdateRequired()) {
  205. appendHitUpdate(p, updateBlock);
  206. }
  207. if(flags.isChatTextUpdateRequired()) {
  208. appendChatTextUpdate(p, updateBlock);
  209. }
  210.  
  211. }
  212.  
  213. private static void appendFaceToUpdate(Player p, StaticPacketBuilder updateBlock) {
  214. updateBlock.addShortA(p.getUpdateFlags().getFaceTo());
  215. }
  216.  
  217. private static void appendHit2Update(Player p, StaticPacketBuilder updateBlock) {
  218. if(p.getHits().getHitDamage2() > 128) {
  219. updateBlock.addByte((byte)128);
  220. updateBlock.addShort(p.getHits().getHitDamage2());
  221. } else {
  222. updateBlock.addByte((byte)p.getHits().getHitDamage2());
  223. }
  224. //updateBlock.addByteS(p.getHits().getHitDamage2());
  225. updateBlock.addByteA(p.getHits().getHitType2());
  226. }
  227.  
  228. private static void appendHitUpdate(Player p, StaticPacketBuilder updateBlock) {
  229. if(p.getHits().getHitDamage1() > 128) {
  230. updateBlock.addByte((byte)128);
  231. updateBlock.addShort(p.getHits().getHitDamage1());
  232. } else {
  233. updateBlock.addByte((byte)p.getHits().getHitDamage1());
  234. }
  235. updateBlock.addByte((byte)p.getHits().getHitType1());
  236. int hpRatio = p.getSkills().getLevel(3) * 255 / p.getSkills().getLevelForXp(3);
  237. updateBlock.addByteA(hpRatio);
  238. }
  239.  
  240. private static void appendGraphicsUpdate(Player p, StaticPacketBuilder updateBlock) {
  241. updateBlock.addShortA(p.getLastGraphics().getId());
  242. updateBlock.addLEInt(p.getLastGraphics().getDelay());
  243. }
  244.  
  245. private static void appendAnimationUpdate(Player p, StaticPacketBuilder updateBlock) {
  246. updateBlock.addLEShortA(p.getLastAnimation().getId());
  247. updateBlock.addByteA(p.getLastAnimation().getDelay());
  248. }
  249.  
  250. private static void appendChatTextUpdate(Player p, StaticPacketBuilder updateBlock) {
  251. updateBlock.addShortA(p.getLastChatMessage().getEffects());
  252. updateBlock.addByteC(p.getRights());
  253. byte[] chatStr = new byte[256];
  254. chatStr[0] = (byte) p.getLastChatMessage().getChatText().length();
  255. int offset = 1 + Misc.encryptPlayerChat(chatStr, 0, 1, p.getLastChatMessage().getChatText().length(), p.getLastChatMessage().getChatText().getBytes());
  256. updateBlock.addByteC(offset);
  257. updateBlock.addBytesA(chatStr, 0, offset);
  258.  
  259. }
  260.  
  261. private static void appendAppearanceUpdate(Player p, StaticPacketBuilder updateBlock) {
  262. StaticPacketBuilder playerProps = new StaticPacketBuilder().setBare(true);
  263.  
  264. Appearance app = p.getAppearance();
  265. playerProps.addByte((byte) (app.getGender() & 0xFF));
  266. playerProps.addByte((byte) p.getHeadIcons().getPkIcon());
  267. playerProps.addByte((byte) p.getHeadIcons().getPrayerIcon());
  268. if(!app.isNpc()) {
  269. for(int i = 0; i < 4; i++) {
  270. if(p.getEquipment().get(i) != null) {
  271. playerProps.addShort(32768 + p.getEquipment().get(i).getDefinition().getEquipId());
  272. } else {
  273. playerProps.addByte((byte) 0);
  274. }
  275. }
  276. if(p.getEquipment().get(Equipment.SLOT_CHEST) != null) {
  277. playerProps.addShort(32768 + p.getEquipment().get(Equipment.SLOT_CHEST).getDefinition().getEquipId());
  278. } else {
  279. playerProps.addShort(0x100 + app.getLook(2));
  280. }
  281. if(p.getEquipment().get(Equipment.SLOT_SHIELD) != null) {
  282. playerProps.addShort(32768 + p.getEquipment().get(Equipment.SLOT_SHIELD).getDefinition().getEquipId());
  283. } else {
  284. playerProps.addByte((byte) 0);
  285. }
  286. Item chest = p.getEquipment().get(Equipment.SLOT_CHEST);
  287. if(chest != null) {
  288. if(!Equipment.isFullBody(chest.getDefinition())) {
  289. playerProps.addShort(0x100 + app.getLook(3));
  290. } else {
  291. playerProps.addByte((byte) 0);
  292. }
  293. } else {
  294. playerProps.addShort(0x100 + app.getLook(3));
  295. }
  296. if(p.getEquipment().get(Equipment.SLOT_LEGS) != null) {
  297. playerProps.addShort(32768 + p.getEquipment().get(Equipment.SLOT_LEGS).getDefinition().getEquipId());
  298. } else {
  299. playerProps.addShort(0x100 + app.getLook(5));
  300. }
  301. Item hat = p.getEquipment().get(Equipment.SLOT_HAT);
  302. if(hat != null) {
  303. if(!Equipment.isFullHat(hat.getDefinition()) && !Equipment.isFullMask(hat.getDefinition())) {
  304. playerProps.addShort(0x100 + app.getLook(0));
  305. } else {
  306. playerProps.addByte((byte) 0);
  307. }
  308. } else {
  309. playerProps.addShort(0x100 + app.getLook(0));
  310. }
  311. if(p.getEquipment().get(Equipment.SLOT_HANDS) != null) {
  312. playerProps.addShort(32768 + p.getEquipment().get(Equipment.SLOT_HANDS).getDefinition().getEquipId());
  313. } else {
  314. playerProps.addShort(0x100 + app.getLook(4));
  315. }
  316. if(p.getEquipment().get(Equipment.SLOT_FEET) != null) {
  317. playerProps.addShort(32768 + p.getEquipment().get(Equipment.SLOT_FEET).getDefinition().getEquipId());
  318. } else {
  319. playerProps.addShort(0x100 + app.getLook(6));
  320. }
  321. if(hat != null) {
  322. if(!Equipment.isFullMask(hat.getDefinition())) {
  323. playerProps.addShort(0x100 + app.getLook(1));
  324. } else {
  325. playerProps.addByte((byte) 0);
  326. }
  327. } else {
  328. playerProps.addShort(0x100 + app.getLook(1));
  329. }
  330. } else {
  331. playerProps.addShort(-1);
  332. playerProps.addShort(app.getNpcId());
  333. }
  334. for(int colour : app.getColours()) {
  335. playerProps.addByte((byte) colour);
  336. }
  337. playerProps.addShort(1426);
  338. playerProps.addLong(Misc.stringToLong(p.getPlayerDetails().getUsername()));
  339. playerProps.addByte((byte) p.getSkills().getCombatLevel()); //combat level
  340. playerProps.addByte((byte) p.getSkills().getCombatLevel());
  341. playerProps.addByte((byte) 0);
  342. playerProps.addByte((byte) 0);
  343. updateBlock.addByte((byte) (playerProps.getLength() & 0xFF));
  344. updateBlock.addBytes(playerProps.toPacket().getData(), 0, playerProps.getLength());
  345. }
  346.  
  347. }
Add Comment
Please, Sign In to add comment