Advertisement
Guest User

Utils.java

a guest
Apr 24th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.54 KB | None | 0 0
  1. package com.rs.utils;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.math.BigInteger;
  8. import java.net.URL;
  9. import java.nio.channels.FileChannel;
  10. import java.security.MessageDigest;
  11. import java.text.DecimalFormat;
  12. import java.util.ArrayList;
  13. import java.util.Enumeration;
  14. import java.util.List;
  15. import java.util.Random;
  16.  
  17. import com.rs.Settings;
  18. import com.rs.cache.Cache;
  19. import com.rs.game.WorldTile;
  20. import com.rs.game.player.Player;
  21. import com.rs.game.player.Skills;
  22. import java.lang.management.ManagementFactory;
  23. import java.util.concurrent.TimeUnit;
  24.  
  25. public final class Utils {
  26.  
  27. public static String formatString(String name) {
  28. if (name == null)
  29. return "";
  30. name = name.replaceAll("_", " ");
  31. name = name.toLowerCase();
  32. StringBuilder newName = new StringBuilder();
  33. boolean wasSpace = true;
  34. for (int i = 0; i < name.length(); i++) {
  35. if (wasSpace) {
  36. newName.append(("" + name.charAt(i)).toUpperCase());
  37. wasSpace = false;
  38. } else {
  39. newName.append(name.charAt(i));
  40. }
  41. if (name.charAt(i) == ' ') {
  42. wasSpace = true;
  43. }
  44. }
  45. return newName.toString();
  46. }
  47.  
  48. public static String[] INVALID_CHARS = {
  49. "~", "`", "#", "$", "%", "^", "&", "*", "(", ")", "-",
  50. "_", "+", "=", "|", "[", "]", "{", "}", ">", "<", ".",
  51. ",", "?", "/"
  52. };
  53.  
  54.  
  55. public static String[] INVALID_TITLES = {
  56. "0wner", "owner", "own3r", "0wn3r"
  57. };
  58.  
  59. public static boolean titleHasInvalidChars(String title) {
  60. for (String n : INVALID_CHARS) {
  61. if (title.contains(n))
  62. return true;
  63. }
  64. return false;
  65. }
  66. public static boolean isValidTitle(String title) {
  67. for (String t : INVALID_TITLES) {
  68. if (title.toLowerCase().trim().contains(t))
  69. return false;
  70. }
  71. return true;
  72. }
  73.  
  74. public static boolean isValidHex(String hex) {
  75. return hex.matches("[0-9A-F]+");
  76. }
  77.  
  78. public static String formatNumber(int amount) {
  79. return new DecimalFormat("#,###,##0").format(amount).toString();
  80. }
  81.  
  82. public static boolean startsWithVowel(String name) {
  83. String[] VOWELS = { "a", "e", "i", "o", "u" };
  84. for (String n : VOWELS) {
  85. if (name.toLowerCase().startsWith(n)) {
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91.  
  92. public static long serverUptime() {
  93. return ManagementFactory.getRuntimeMXBean().getUptime();
  94. }
  95.  
  96. private static final String FORMAT = "%02d:%02d:%02d";
  97.  
  98. public static String formatMs(long ms) {
  99. return String.format(FORMAT,
  100. TimeUnit.MILLISECONDS.toHours(ms),
  101. TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
  102. TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
  103. }
  104.  
  105. private static final Object ALGORITHM_LOCK = new Object();
  106. private static final Random RANDOM = new Random();
  107.  
  108. private static long timeCorrection;
  109. private static long lastTimeUpdate;
  110.  
  111. public static synchronized long currentTimeMillis() {
  112. long l = System.currentTimeMillis();
  113. if (l < lastTimeUpdate)
  114. timeCorrection += lastTimeUpdate - l;
  115. lastTimeUpdate = l;
  116. return l + timeCorrection;
  117. }
  118.  
  119. public static byte[] cryptRSA(byte[] data, BigInteger exponent, BigInteger modulus) {
  120. return new BigInteger(data).modPow(exponent, modulus).toByteArray();
  121. }
  122.  
  123. public static final byte[] encryptUsingMD5(byte[] buffer) {
  124. // prevents concurrency problems with the algorithm
  125. synchronized (ALGORITHM_LOCK) {
  126. try {
  127. MessageDigest algorithm = MessageDigest.getInstance("MD5");
  128. algorithm.update(buffer);
  129. byte[] digest = algorithm.digest();
  130. algorithm.reset();
  131. return digest;
  132. } catch (Throwable e) {
  133. Logger.handle(e);
  134. }
  135. return null;
  136. }
  137. }
  138.  
  139. public static boolean inCircle(WorldTile location, WorldTile center, int radius) {
  140. return getDistance(center, location) < radius;
  141. }
  142.  
  143. public static void copyFile(File sourceFile, File destFile)
  144. throws IOException {
  145. if (!destFile.exists()) {
  146. destFile.createNewFile();
  147. }
  148.  
  149. FileChannel source = null;
  150. FileChannel destination = null;
  151. try {
  152. source = new FileInputStream(sourceFile).getChannel();
  153. destination = new FileOutputStream(destFile).getChannel();
  154. destination.transferFrom(source, 0, source.size());
  155. } finally {
  156. if (source != null) {
  157. source.close();
  158. }
  159. if (destination != null) {
  160. destination.close();
  161. }
  162. }
  163. }
  164.  
  165. @SuppressWarnings({ "rawtypes" })
  166. public static Class[] getClasses(String packageName)
  167. throws ClassNotFoundException, IOException {
  168. ClassLoader classLoader = Thread.currentThread()
  169. .getContextClassLoader();
  170. assert classLoader != null;
  171. String path = packageName.replace('.', '/');
  172. Enumeration<URL> resources = classLoader.getResources(path);
  173. List<File> dirs = new ArrayList<File>();
  174. while (resources.hasMoreElements()) {
  175. URL resource = resources.nextElement();
  176. dirs.add(new File(resource.getFile().replaceAll("%20", " ")));
  177. }
  178. ArrayList<Class> classes = new ArrayList<Class>();
  179. for (File directory : dirs) {
  180. classes.addAll(findClasses(directory, packageName));
  181. }
  182. return classes.toArray(new Class[classes.size()]);
  183. }
  184.  
  185. @SuppressWarnings("rawtypes")
  186. private static List<Class> findClasses(File directory, String packageName) {
  187. List<Class> classes = new ArrayList<Class>();
  188. if (!directory.exists()) {
  189. return classes;
  190. }
  191. File[] files = directory.listFiles();
  192. for (File file : files) {
  193. if (file.isDirectory()) {
  194. assert !file.getName().contains(".");
  195. classes.addAll(findClasses(file, packageName + "." + file.getName()));
  196. } else if (file.getName().endsWith(".class")) {
  197. try {
  198. classes.add(Class.forName(packageName + '.' + file.getName().substring(0, file.getName().length() - 6)));
  199. } catch (Throwable e) {
  200.  
  201. }
  202. }
  203. }
  204. return classes;
  205. }
  206.  
  207. public static final int getDistance(WorldTile t1, WorldTile t2) {
  208. return getDistance(t1.getX(), t1.getY(), t2.getX(), t2.getY());
  209. }
  210.  
  211. public static final int getDistance(int coordX1, int coordY1, int coordX2,
  212. int coordY2) {
  213. int deltaX = coordX2 - coordX1;
  214. int deltaY = coordY2 - coordY1;
  215. return ((int) Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));
  216. }
  217.  
  218. public static final byte[] DIRECTION_DELTA_X = new byte[] { -1, 0, 1, -1,
  219. 1, -1, 0, 1 };
  220. public static final byte[] DIRECTION_DELTA_Y = new byte[] { 1, 1, 1, 0, 0,
  221. -1, -1, -1 };
  222.  
  223. public static int getNpcMoveDirection(int dd) {
  224. if (dd < 0)
  225. return -1;
  226. return getNpcMoveDirection(DIRECTION_DELTA_X[dd], DIRECTION_DELTA_Y[dd]);
  227. }
  228.  
  229. public static int getNpcMoveDirection(int dx, int dy) {
  230. if (dx == 0 && dy > 0)
  231. return 0;
  232. if (dx > 0 && dy > 0)
  233. return 1;
  234. if (dx > 0 && dy == 0)
  235. return 2;
  236. if (dx > 0 && dy < 0)
  237. return 3;
  238. if (dx == 0 && dy < 0)
  239. return 4;
  240. if (dx < 0 && dy < 0)
  241. return 5;
  242. if (dx < 0 && dy == 0)
  243. return 6;
  244. if (dx < 0 && dy > 0)
  245. return 7;
  246. return -1;
  247. }
  248.  
  249. public static final int[][] getCoordOffsetsNear(int size) {
  250. int[] xs = new int[4 + (4 * size)];
  251. int[] xy = new int[xs.length];
  252. xs[0] = -size;
  253. xy[0] = 1;
  254. xs[1] = 1;
  255. xy[1] = 1;
  256. xs[2] = -size;
  257. xy[2] = -size;
  258. xs[3] = 1;
  259. xy[2] = -size;
  260. for (int fakeSize = size; fakeSize > 0; fakeSize--) {
  261. xs[(4 + ((size - fakeSize) * 4))] = -fakeSize + 1;
  262. xy[(4 + ((size - fakeSize) * 4))] = 1;
  263. xs[(4 + ((size - fakeSize) * 4)) + 1] = -size;
  264. xy[(4 + ((size - fakeSize) * 4)) + 1] = -fakeSize + 1;
  265. xs[(4 + ((size - fakeSize) * 4)) + 2] = 1;
  266. xy[(4 + ((size - fakeSize) * 4)) + 2] = -fakeSize + 1;
  267. xs[(4 + ((size - fakeSize) * 4)) + 3] = -fakeSize + 1;
  268. xy[(4 + ((size - fakeSize) * 4)) + 3] = -size;
  269. }
  270. return new int[][] { xs, xy };
  271. }
  272.  
  273. public static final int getFaceDirection(int xOffset, int yOffset) {
  274. return ((int) (Math.atan2(-xOffset, -yOffset) * 2607.5945876176133)) & 0x3fff;
  275. }
  276.  
  277. public static final int getMoveDirection(int xOffset, int yOffset) {
  278. if (xOffset < 0) {
  279. if (yOffset < 0)
  280. return 5;
  281. else if (yOffset > 0)
  282. return 0;
  283. else
  284. return 3;
  285. } else if (xOffset > 0) {
  286. if (yOffset < 0)
  287. return 7;
  288. else if (yOffset > 0)
  289. return 2;
  290. else
  291. return 4;
  292. } else {
  293. if (yOffset < 0)
  294. return 6;
  295. else if (yOffset > 0)
  296. return 1;
  297. else
  298. return -1;
  299. }
  300. }
  301.  
  302. public static final int getGraphicDefinitionsSize() {
  303. int lastArchiveId = Cache.STORE.getIndexes()[21].getLastArchiveId();
  304. return lastArchiveId * 256 + Cache.STORE.getIndexes()[21].getValidFilesCount(lastArchiveId);
  305. }
  306.  
  307. public static final int getAnimationDefinitionsSize() {
  308. int lastArchiveId = Cache.STORE.getIndexes()[20].getLastArchiveId();
  309. return lastArchiveId * 128 + Cache.STORE.getIndexes()[20].getValidFilesCount(lastArchiveId);
  310. }
  311.  
  312. public static final int getConfigDefinitionsSize() {
  313. int lastArchiveId = Cache.STORE.getIndexes()[22].getLastArchiveId();
  314. return lastArchiveId * 256 + Cache.STORE.getIndexes()[22].getValidFilesCount(lastArchiveId);
  315. }
  316.  
  317. public static final int getObjectDefinitionsSize() {
  318. int lastArchiveId = Cache.STORE.getIndexes()[16].getLastArchiveId();
  319. return lastArchiveId * 256 + Cache.STORE.getIndexes()[16].getValidFilesCount(lastArchiveId);
  320. }
  321.  
  322. public static final int getNPCDefinitionsSize() {
  323. int lastArchiveId = Cache.STORE.getIndexes()[18].getLastArchiveId();
  324. return lastArchiveId * 128 + Cache.STORE.getIndexes()[18].getValidFilesCount(lastArchiveId);
  325. }
  326.  
  327.  
  328. public static final int getItemDefinitionsSize() {
  329. Object Cache;
  330. int lastArchiveId = Cache.STORE.getIndexes()[19].getLastArchiveId();
  331. return (lastArchiveId * 256 + Cache.STORE.getIndexes()[19].getValidFilesCount(lastArchiveId)) - 22314;
  332. }
  333.  
  334. public static boolean itemExists(int id) {
  335. if(id >= getItemDefinitionsSize()) //setted because of custom items
  336. return false;
  337. return Cache.STORE.getIndexes()[19].fileExists(id >>> 8, 0xff & id);
  338. }
  339.  
  340. public static final int getInterfaceDefinitionsSize() {
  341. return Cache.STORE.getIndexes()[3].getLastArchiveId() + 1;
  342. }
  343.  
  344. public static final int getInterfaceDefinitionsComponentsSize(int interfaceId) {
  345. return Cache.STORE.getIndexes()[3].getLastFileId(interfaceId) + 1;
  346. }
  347.  
  348. public static String formatPlayerNameForProtocol(String name) {
  349. if (name == null)
  350. return "";
  351. name = name.replaceAll(" ", "_");
  352. name = name.toLowerCase();
  353. return name;
  354. }
  355.  
  356. public static String formatPlayerNameForDisplay(String name) {
  357. if (name == null)
  358. return "";
  359. name = name.replaceAll("_", " ");
  360. name = name.toLowerCase();
  361. StringBuilder newName = new StringBuilder();
  362. boolean wasSpace = true;
  363. for (int i = 0; i < name.length(); i++) {
  364. if (wasSpace) {
  365. newName.append(("" + name.charAt(i)).toUpperCase());
  366. wasSpace = false;
  367. } else {
  368. newName.append(name.charAt(i));
  369. }
  370. if (name.charAt(i) == ' ') {
  371. wasSpace = true;
  372. }
  373. }
  374. return newName.toString();
  375. }
  376.  
  377. public static final int getRandom(int maxValue) {
  378. return (int) (Math.random() * (maxValue + 1));
  379. }
  380. public static final int random(int min, int max) {
  381. final int n = Math.abs(max - min);
  382. return Math.min(min, max) + (n == 0 ? 0 : random(n));
  383. }
  384. public static final double random(double min, double max) {
  385. final double n = Math.abs(max - min);
  386. return Math.min(min, max) + (n == 0 ? 0 : random((int) n));
  387. }
  388. public static final int next(int max, int min) {
  389. return min + (int)(Math.random() * ((max - min) + 1));
  390. }
  391.  
  392. public static final double getRandomDouble(double maxValue) {
  393. return (Math.random() * (maxValue + 1));
  394.  
  395. }
  396.  
  397. public static final int random(int maxValue) {
  398. if(maxValue <= 0)
  399. return 0;
  400. return RANDOM.nextInt(maxValue);
  401. }
  402.  
  403. public static final String longToString(long l) {
  404. if (l <= 0L || l >= 0x5b5b57f8a98a5dd1L)
  405. return null;
  406. if (l % 37L == 0L)
  407. return null;
  408. int i = 0;
  409. char ac[] = new char[12];
  410. while (l != 0L) {
  411. long l1 = l;
  412. l /= 37L;
  413. ac[11 - i++] = VALID_CHARS[(int) (l1 - l * 37L)];
  414. }
  415. return new String(ac, 12 - i, i);
  416. }
  417.  
  418. public static final char[] VALID_CHARS = { '_', 'a', 'b', 'c', 'd', 'e',
  419. 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
  420. 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
  421. '5', '6', '7', '8', '9' };
  422.  
  423. public static boolean invalidAccountName(String name) {
  424. return name.length() < 2 || name.length() > 12 || name.startsWith("_") || name.endsWith("_") || name.contains("__") || containsInvalidCharacter(name);
  425. }
  426.  
  427. public static boolean invalidAuthId(String auth) {
  428. return auth.length() != 10 || auth.contains("_") || containsInvalidCharacter(auth);
  429. }
  430.  
  431. public static boolean containsInvalidCharacter(char c) {
  432. for(char vc : VALID_CHARS) {
  433. if(vc == c)
  434. return false;
  435. }
  436. return true;
  437. }
  438.  
  439. public static boolean containsInvalidCharacter(String name) {
  440. for(char c : name.toCharArray()) {
  441. if(containsInvalidCharacter(c))
  442. return true;
  443. }
  444. return false;
  445. }
  446.  
  447. public static final long stringToLong(String s) {
  448. long l = 0L;
  449. for (int i = 0; i < s.length() && i < 12; i++) {
  450. char c = s.charAt(i);
  451. l *= 37L;
  452. if (c >= 'A' && c <= 'Z')
  453. l += (1 + c) - 65;
  454. else if (c >= 'a' && c <= 'z')
  455. l += (1 + c) - 97;
  456. else if (c >= '0' && c <= '9')
  457. l += (27 + c) - 48;
  458. }
  459. while (l % 37L == 0L && l != 0L) {
  460. l /= 37L;
  461. }
  462. return l;
  463. }
  464.  
  465. public static final int packGJString2(int position, byte[] buffer,
  466. String String) {
  467. int length = String.length();
  468. int offset = position;
  469. for (int index = 0; length > index; index++) {
  470. int character = String.charAt(index);
  471. if (character > 127) {
  472. if (character > 2047) {
  473. buffer[offset++] = (byte) ((character | 919275) >> 12);
  474. buffer[offset++] = (byte) (128 | ((character >> 6) & 63));
  475. buffer[offset++] = (byte) (128 | (character & 63));
  476. } else {
  477. buffer[offset++] = (byte) ((character | 12309) >> 6);
  478. buffer[offset++] = (byte) (128 | (character & 63));
  479. }
  480. } else
  481. buffer[offset++] = (byte) character;
  482. }
  483. return offset - position;
  484. }
  485.  
  486. public static final int calculateGJString2Length(String String) {
  487. int length = String.length();
  488. int gjStringLength = 0;
  489. for (int index = 0; length > index; index++) {
  490. char c = String.charAt(index);
  491. if (c > '\u007f') {
  492. if (c <= '\u07ff')
  493. gjStringLength += 2;
  494. else
  495. gjStringLength += 3;
  496. } else
  497. gjStringLength++;
  498. }
  499. return gjStringLength;
  500. }
  501.  
  502. public static final int getNameHash(String name) {
  503. name = name.toLowerCase();
  504. int hash = 0;
  505. for (int index = 0; index < name.length(); index++)
  506. hash = method1258(name.charAt(index)) + ((hash << 5) - hash);
  507. return hash;
  508. }
  509.  
  510. public static final byte method1258(char c) {
  511. byte charByte;
  512. if (c > 0 && c < '\200' || c >= '\240' && c <= '\377') {
  513. charByte = (byte) c;
  514. } else if (c != '\u20AC') {
  515. if (c != '\u201A') {
  516. if (c != '\u0192') {
  517. if (c == '\u201E') {
  518. charByte = -124;
  519. } else if (c != '\u2026') {
  520. if (c != '\u2020') {
  521. if (c == '\u2021') {
  522. charByte = -121;
  523. } else if (c == '\u02C6') {
  524. charByte = -120;
  525. } else if (c == '\u2030') {
  526. charByte = -119;
  527. } else if (c == '\u0160') {
  528. charByte = -118;
  529. } else if (c == '\u2039') {
  530. charByte = -117;
  531. } else if (c == '\u0152') {
  532. charByte = -116;
  533. } else if (c != '\u017D') {
  534. if (c == '\u2018') {
  535. charByte = -111;
  536. } else if (c != '\u2019') {
  537. if (c != '\u201C') {
  538. if (c == '\u201D') {
  539. charByte = -108;
  540. } else if (c != '\u2022') {
  541. if (c == '\u2013') {
  542. charByte = -106;
  543. } else if (c == '\u2014') {
  544. charByte = -105;
  545. } else if (c == '\u02DC') {
  546. charByte = -104;
  547. } else if (c == '\u2122') {
  548. charByte = -103;
  549. } else if (c != '\u0161') {
  550. if (c == '\u203A') {
  551. charByte = -101;
  552. } else if (c != '\u0153') {
  553. if (c == '\u017E') {
  554. charByte = -98;
  555. } else if (c != '\u0178') {
  556. charByte = 63;
  557. } else {
  558. charByte = -97;
  559. }
  560. } else {
  561. charByte = -100;
  562. }
  563. } else {
  564. charByte = -102;
  565. }
  566. } else {
  567. charByte = -107;
  568. }
  569. } else {
  570. charByte = -109;
  571. }
  572. } else {
  573. charByte = -110;
  574. }
  575. } else {
  576. charByte = -114;
  577. }
  578. } else {
  579. charByte = -122;
  580. }
  581. } else {
  582. charByte = -123;
  583. }
  584. } else {
  585. charByte = -125;
  586. }
  587. } else {
  588. charByte = -126;
  589. }
  590. } else {
  591. charByte = -128;
  592. }
  593. return charByte;
  594. }
  595.  
  596. public static char[] aCharArray6385 = { '\u20ac', '\0', '\u201a', '\u0192',
  597. '\u201e', '\u2026', '\u2020', '\u2021', '\u02c6', '\u2030',
  598. '\u0160', '\u2039', '\u0152', '\0', '\u017d', '\0', '\0', '\u2018',
  599. '\u2019', '\u201c', '\u201d', '\u2022', '\u2013', '\u2014',
  600. '\u02dc', '\u2122', '\u0161', '\u203a', '\u0153', '\0', '\u017e',
  601. '\u0178' };
  602.  
  603. public static final String getUnformatedMessage(int messageDataLength,
  604. int messageDataOffset, byte[] messageData) {
  605. char[] cs = new char[messageDataLength];
  606. int i = 0;
  607. for (int i_6_ = 0; i_6_ < messageDataLength; i_6_++) {
  608. int i_7_ = 0xff & messageData[i_6_ + messageDataOffset];
  609. if ((i_7_ ^ 0xffffffff) != -1) {
  610. if ((i_7_ ^ 0xffffffff) <= -129 && (i_7_ ^ 0xffffffff) > -161) {
  611. int i_8_ = aCharArray6385[i_7_ - 128];
  612. if (i_8_ == 0)
  613. i_8_ = 63;
  614. i_7_ = i_8_;
  615. }
  616. cs[i++] = (char) i_7_;
  617. }
  618. }
  619. return new String(cs, 0, i);
  620. }
  621.  
  622. public static final byte[] getFormatedMessage(String message) {
  623. int i_0_ = message.length();
  624. byte[] is = new byte[i_0_];
  625. for (int i_1_ = 0; (i_1_ ^ 0xffffffff) > (i_0_ ^ 0xffffffff); i_1_++) {
  626. int i_2_ = message.charAt(i_1_);
  627. if (((i_2_ ^ 0xffffffff) >= -1 || i_2_ >= 128)
  628. && (i_2_ < 160 || i_2_ > 255)) {
  629. if ((i_2_ ^ 0xffffffff) != -8365) {
  630. if ((i_2_ ^ 0xffffffff) == -8219)
  631. is[i_1_] = (byte) -126;
  632. else if ((i_2_ ^ 0xffffffff) == -403)
  633. is[i_1_] = (byte) -125;
  634. else if (i_2_ == 8222)
  635. is[i_1_] = (byte) -124;
  636. else if (i_2_ != 8230) {
  637. if ((i_2_ ^ 0xffffffff) != -8225) {
  638. if ((i_2_ ^ 0xffffffff) != -8226) {
  639. if ((i_2_ ^ 0xffffffff) == -711)
  640. is[i_1_] = (byte) -120;
  641. else if (i_2_ == 8240)
  642. is[i_1_] = (byte) -119;
  643. else if ((i_2_ ^ 0xffffffff) == -353)
  644. is[i_1_] = (byte) -118;
  645. else if ((i_2_ ^ 0xffffffff) != -8250) {
  646. if (i_2_ == 338)
  647. is[i_1_] = (byte) -116;
  648. else if (i_2_ == 381)
  649. is[i_1_] = (byte) -114;
  650. else if ((i_2_ ^ 0xffffffff) == -8217)
  651. is[i_1_] = (byte) -111;
  652. else if (i_2_ == 8217)
  653. is[i_1_] = (byte) -110;
  654. else if (i_2_ != 8220) {
  655. if (i_2_ == 8221)
  656. is[i_1_] = (byte) -108;
  657. else if ((i_2_ ^ 0xffffffff) == -8227)
  658. is[i_1_] = (byte) -107;
  659. else if ((i_2_ ^ 0xffffffff) != -8212) {
  660. if (i_2_ == 8212)
  661. is[i_1_] = (byte) -105;
  662. else if ((i_2_ ^ 0xffffffff) != -733) {
  663. if (i_2_ != 8482) {
  664. if (i_2_ == 353)
  665. is[i_1_] = (byte) -102;
  666. else if (i_2_ != 8250) {
  667. if ((i_2_ ^ 0xffffffff) == -340)
  668. is[i_1_] = (byte) -100;
  669. else if (i_2_ != 382) {
  670. if (i_2_ == 376)
  671. is[i_1_] = (byte) -97;
  672. else is[i_1_] = (byte) 63;
  673. } else is[i_1_] = (byte) -98;
  674. } else is[i_1_] = (byte) -101;
  675. } else is[i_1_] = (byte) -103;
  676. } else is[i_1_] = (byte) -104;
  677. } else is[i_1_] = (byte) -106;
  678. } else is[i_1_] = (byte) -109;
  679. } else is[i_1_] = (byte) -117;
  680. } else is[i_1_] = (byte) -121;
  681. } else is[i_1_] = (byte) -122;
  682. } else is[i_1_] = (byte) -123;
  683. } else is[i_1_] = (byte) -128;
  684. } else is[i_1_] = (byte) i_2_;
  685. }
  686. return is;
  687. }
  688.  
  689. public static char method2782(byte value) {
  690. int byteChar = 0xff & value;
  691. if (byteChar == 0)
  692. throw new IllegalArgumentException("Non cp1252 character 0x" + Integer.toString(byteChar, 16) + " provided");
  693. if ((byteChar ^ 0xffffffff) <= -129 && byteChar < 160) {
  694. int i_4_ = aCharArray6385[-128 + byteChar];
  695. if ((i_4_ ^ 0xffffffff) == -1)
  696. i_4_ = 63;
  697. byteChar = i_4_;
  698. }
  699. return (char) byteChar;
  700. }
  701.  
  702. public static int getHashMapSize(int size) {
  703. size--;
  704. size |= size >>> -1810941663;
  705. size |= size >>> 2010624802;
  706. size |= size >>> 10996420;
  707. size |= size >>> 491045480;
  708. size |= size >>> 1388313616;
  709. return 1 + size;
  710. }
  711.  
  712. /**
  713. * Walk dirs 0 - South-West 1 - South 2 - South-East 3 - West 4 - East 5 -
  714. * North-West 6 - North 7 - North-East
  715. */
  716. public static int getPlayerWalkingDirection(int dx, int dy) {
  717. if (dx == -1 && dy == -1) {
  718. return 0;
  719. }
  720. if (dx == 0 && dy == -1) {
  721. return 1;
  722. }
  723. if (dx == 1 && dy == -1) {
  724. return 2;
  725. }
  726. if (dx == -1 && dy == 0) {
  727. return 3;
  728. }
  729. if (dx == 1 && dy == 0) {
  730. return 4;
  731. }
  732. if (dx == -1 && dy == 1) {
  733. return 5;
  734. }
  735. if (dx == 0 && dy == 1) {
  736. return 6;
  737. }
  738. if (dx == 1 && dy == 1) {
  739. return 7;
  740. }
  741. return -1;
  742. }
  743.  
  744. public static int getPlayerRunningDirection(int dx, int dy) {
  745. if (dx == -2 && dy == -2)
  746. return 0;
  747. if (dx == -1 && dy == -2)
  748. return 1;
  749. if (dx == 0 && dy == -2)
  750. return 2;
  751. if (dx == 1 && dy == -2)
  752. return 3;
  753. if (dx == 2 && dy == -2)
  754. return 4;
  755. if (dx == -2 && dy == -1)
  756. return 5;
  757. if (dx == 2 && dy == -1)
  758. return 6;
  759. if (dx == -2 && dy == 0)
  760. return 7;
  761. if (dx == 2 && dy == 0)
  762. return 8;
  763. if (dx == -2 && dy == 1)
  764. return 9;
  765. if (dx == 2 && dy == 1)
  766. return 10;
  767. if (dx == -2 && dy == 2)
  768. return 11;
  769. if (dx == -1 && dy == 2)
  770. return 12;
  771. if (dx == 0 && dy == 2)
  772. return 13;
  773. if (dx == 1 && dy == 2)
  774. return 14;
  775. if (dx == 2 && dy == 2)
  776. return 15;
  777. return -1;
  778. }
  779.  
  780. public static byte[] completeQuickMessage(Player player, int fileId,
  781. byte[] data) {
  782. if (fileId == 1)
  783. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.AGILITY) };
  784. else if (fileId == 8)
  785. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.ATTACK) };
  786. else if (fileId == 13)
  787. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.CONSTRUCTION) };
  788. else if (fileId == 16)
  789. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.COOKING) };
  790. else if (fileId == 23)
  791. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.CRAFTING) };
  792. else if (fileId == 30)
  793. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.DEFENCE) };
  794. else if (fileId == 34)
  795. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.FARMING) };
  796. else if (fileId == 41)
  797. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.FIREMAKING) };
  798. else if (fileId == 47)
  799. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.FISHING) };
  800. else if (fileId == 55)
  801. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.FLETCHING) };
  802. else if (fileId == 62)
  803. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.HERBLORE) };
  804. else if (fileId == 70)
  805. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.HITPOINTS) };
  806. else if (fileId == 74)
  807. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.HUNTER) };
  808. else if (fileId == 135)
  809. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.MAGIC) };
  810. else if (fileId == 127)
  811. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.MINING) };
  812. else if (fileId == 120)
  813. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.PRAYER) };
  814. else if (fileId == 116)
  815. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.RANGE) };
  816. else if (fileId == 111)
  817. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.RUNECRAFTING) };
  818. else if (fileId == 103)
  819. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.SLAYER) };
  820. else if (fileId == 96)
  821. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.SMITHING) };
  822. else if (fileId == 92)
  823. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.STRENGTH) };
  824. else if (fileId == 85)
  825. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.SUMMONING) };
  826. else if (fileId == 79)
  827. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.THIEVING) };
  828. else if (fileId == 142)
  829. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.WOODCUTTING) };
  830. else if (fileId == 990)
  831. data = new byte[] { (byte) player.getSkills().getLevelForXp(Skills.DUNGEONEERING) };
  832. else if (fileId == 965) {
  833. int value = player.getHitpoints();
  834. data = new byte[] { (byte) (value >> 24), (byte) (value >> 16),
  835. (byte) (value >> 8), (byte) value };
  836. } else if (fileId == 1108) {
  837. int value = player.getDominionTower().getKilledBossesCount();
  838. data = new byte[] { (byte) (value >> 24), (byte) (value >> 16),
  839. (byte) (value >> 8), (byte) value };
  840. } else if (fileId == 1109) {
  841. long value = player.getDominionTower().getTotalScore();
  842. data = new byte[] { (byte) (value >> 24), (byte) (value >> 16),
  843. (byte) (value >> 8), (byte) value };
  844. } else if (fileId == 1110) {
  845. int value = player.getDominionTower().getMaxFloorClimber();
  846. data = new byte[] { (byte) (value >> 24), (byte) (value >> 16),
  847. (byte) (value >> 8), (byte) value };
  848. } else if (fileId == 1111) {
  849. int value = player.getDominionTower().getMaxFloorEndurance();
  850. data = new byte[] { (byte) (value >> 24), (byte) (value >> 16),
  851. (byte) (value >> 8), (byte) value };
  852. }else if (fileId == 1134) {
  853. int value = player.getCrucibleHighScore();
  854. data = new byte[] { (byte) (value >> 24), (byte) (value >> 16),
  855. (byte) (value >> 8), (byte) value };
  856. }
  857.  
  858. else if (Settings.DEBUG)
  859. Logger.log("Utils", "qc: " + fileId + ", " + (data == null ? 0 : data.length));
  860. return data;
  861. }
  862.  
  863. public static String fixChatMessage(String message) {
  864. StringBuilder newText = new StringBuilder();
  865. boolean wasSpace = true;
  866. boolean exception = false;
  867. for (int i = 0; i < message.length(); i++) {
  868. if(!exception) {
  869. if (wasSpace) {
  870. newText.append(("" + message.charAt(i)).toUpperCase());
  871. if (!String.valueOf(message.charAt(i)).equals(" "))
  872. wasSpace = false;
  873. } else {
  874. newText.append(("" + message.charAt(i)).toLowerCase());
  875. }
  876. } else {
  877. newText.append(("" + message.charAt(i)));
  878. }
  879. if (String.valueOf(message.charAt(i)).contains(":"))
  880. exception = true;
  881. else
  882. if (String.valueOf(message.charAt(i)).contains(".")
  883. || String.valueOf(message.charAt(i)).contains("!")
  884. || String.valueOf(message.charAt(i)).contains("?"))
  885. wasSpace = true;
  886. }
  887. return newText.toString();
  888. }
  889.  
  890. private Utils() {
  891.  
  892. }
  893.  
  894. public static String getCompleted(String[] cmd, int index) {
  895. StringBuilder sb = new StringBuilder();
  896. for (int i = index; i < cmd.length; i++) {
  897. if (i == cmd.length - 1 || cmd[i + 1].startsWith("+")) {
  898. return sb.append(cmd[i]).toString();
  899. }
  900. sb.append(cmd[i]).append(" ");
  901. }
  902. return "null";
  903. }
  904.  
  905. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement