RuneHive-Game
Loading...
Searching...
No Matches
Utility.java
Go to the documentation of this file.
1package com.runehive.util;
2
3import com.runehive.RuneHive;
4import com.runehive.game.world.Interactable;
5import com.runehive.game.world.entity.mob.Direction;
6import com.runehive.game.world.entity.mob.Mob;
7import com.runehive.game.world.entity.mob.player.Player;
8import com.runehive.game.world.entity.skill.Skill;
9import com.runehive.game.world.pathfinding.TraversalMap;
10import com.runehive.game.world.pathfinding.path.Path;
11import com.runehive.game.world.position.Position;
12import com.runehive.net.packet.out.SendMessage;
13
14import java.io.*;
15import java.lang.reflect.InvocationTargetException;
16import java.text.NumberFormat;
17import java.text.SimpleDateFormat;
18import java.util.*;
19import java.util.stream.Collectors;
20import java.util.stream.Stream;
21
22/**
23 * Handles miscellaneous methods.
24 *
25 * @author Daniel
26 */
27public class Utility {
28
29 /** Random instance, used to generate pseudo-random primitive types. */
30 private static final Random RANDOM = new Random(System.currentTimeMillis());
31
32 /** Array of all valid characters. */
33 private static final char[] VALID_CHARS = {'_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '/'};
34
35 /** Gets a percentage amount. */
36 public static double getPercentageAmount(int progress, int total) {
37 return 100D * progress / total;
38 }
39
40 /** Formats digits for integers. */
41 public static String formatDigits(final int amount) {
42 return NumberFormat.getInstance().format(amount);
43 }
44
45 /** Formats digits for longs. */
46 public static String formatDigits(final long amount) {
47 return NumberFormat.getInstance().format(amount);
48 }
49
50 /** Formats digits for doubles. */
51 public static String formatDigits(final double amount) {
52 return NumberFormat.getInstance().format(amount);
53 }
54
55 /** Formats a price for longs. */
56 public static String formatPrice(final long amount) {
57 if (amount >= 0 && amount < 1_000)
58 return "" + amount;
59 if (amount >= 1_000 && amount < 1_000_000) {
60 return (amount / 1_000) + "K";
61 }
62 if (amount >= 1_000_000 && amount < 1_000_000_000) {
63 return (amount / 1_000_000) + "M";
64 }
65 if (amount >= 1_000_000_000 && amount < Integer.MAX_VALUE) {
66 return (amount / 1_000_000_000) + "B";
67 }
68 return "<col=fc2a2a>Lots!";
69 }
70
71 public static long stringToLong(String string) {
72 long l = 0L;
73 for (int i = 0; i < string.length() && i < 12; i++) {
74 char c = string.charAt(i);
75 l *= 37L;
76 if (c >= 'A' && c <= 'Z')
77 l += (1 + c) - 65;
78 else if (c >= 'a' && c <= 'z')
79 l += (1 + c) - 97;
80 else if (c >= '0' && c <= '9')
81 l += (27 + c) - 48;
82 }
83 while (l % 37L == 0L && l != 0L)
84 l /= 37L;
85 return l;
86 }
87
88 /** Formats name of enum. */
89 public static String formatEnum(final String string) {
90 return capitalizeSentence(string.toLowerCase().replace("_", " "));
91 }
92
93 /** Formats the player name. */
94 public static String rank(final String string) {
95 return Stream.of(string.trim().split("\\s")).filter(word -> word.length() > 0).map(word -> word.substring(0, 1).toUpperCase() + word.substring(1)).collect(Collectors.joining(" "));
96 }
97
98 /** Capitalize each letter after . */
99 public static String capitalizeSentence(final String string) {
100 int pos = 0;
101 boolean capitalize = true;
102 StringBuilder sb = new StringBuilder(string);
103 while (pos < sb.length()) {
104 if (sb.charAt(pos) == '.') {
105 capitalize = true;
106 } else if (capitalize && !Character.isWhitespace(sb.charAt(pos))) {
107 sb.setCharAt(pos, Character.toUpperCase(sb.charAt(pos)));
108 capitalize = false;
109 }
110 pos++;
111 }
112 return sb.toString();
113 }
114
115 /** A or an */
116 public static String getAOrAn(String nextWord) {
117 String s = "a";
118 char c = nextWord.toUpperCase().charAt(0);
119 if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
120 s = "an";
121 }
122 return s;
123 }
124
125 /** Gets the date of server. */
126 public static String getDate() {
127 return new SimpleDateFormat("EE MMM dd yyyy").format(new Date());
128 }
129
130 /** Gets the date of server. */
131 public static String getSimpleDate() {
132 return new SimpleDateFormat("yyyy/MM/dd").format(new Date());
133 }
134
135 /** Converts an integer into words. */
136 public static String convertWord(int amount) {
137 return Words.getInstance(amount).getNumberInWords();
138 }
139
140 /** Gets the current server time and formats it */
141 public static String getTime() {
142 return new SimpleDateFormat("hh:mm aa").format(new Date());
143 }
144
145 /** Gets the time based off a long. */
146 public static String getTime(long period) {
147 return new SimpleDateFormat("m:ss").format(period);
148 }
149
150 public static String bigDaddyTime(long period) {
151 return new SimpleDateFormat("HH:mm:ss").format(period);
152 }
153
154 /** Gets the current uptime of server and formats it */
155 public static String getUptime() {
156 return getTime((int) (RuneHive.UPTIME.elapsedTime() / 600));
157 }
158
159 /** Gets a basic time based off seconds. */
160 public static String getTime(int ticks) {
161 long secs = ticks * 3 / 5;
162
163 if (secs < 60) {
164 return "0:" + (secs < 10 ? "0" : "") + secs;
165 }
166
167 long mins = secs / 60;
168 long remainderSecs = secs - (mins * 60);
169 if (mins < 60) {
170 return mins + ":" + (remainderSecs < 10 ? "0" : "") + remainderSecs + "";
171 }
172
173 long hours = mins / 60;
174 long remainderMins = mins - (hours * 60);
175 if (hours < 24) {
176 return hours + "h " + (remainderMins < 10 ? "0" : "") + remainderMins + "m " + (remainderSecs < 10 ? "0" : "") + remainderSecs + "s";
177 }
178
179 long days = hours / 24;
180 long remainderHrs = hours - (days * 24);
181 return days + "d " + (remainderHrs < 10 ? "0" : "") + remainderHrs + "h " + (remainderMins < 10 ? "0" : "") + remainderMins + "m";
182 }
183
184 /** Converts the first 12 characters in a string of text to a hash. */
185 public static long nameToLong(String text) {
186 long hash = 0L;
187 for (int index = 0; index < text.length() && index < 12; index++) {
188 char key = text.charAt(index);
189 hash *= 37L;
190 if (key >= 'A' && key <= 'Z')
191 hash += (1 + key) - 65;
192 else if (key >= 'a' && key <= 'z')
193 hash += (1 + key) - 97;
194 else if (key >= '0' && key <= '9')
195 hash += (27 + key) - 48;
196 }
197 while (hash % 37L == 0L && hash != 0L)
198 hash /= 37L;
199 return hash;
200 }
201
202 public static String longToString(long l) {
203 if (l <= 0L || l >= 0x5b5b57f8a98a5dd1L)
204 return null;
205 if (l % 37L == 0L)
206 return null;
207 int i = 0;
208 char ac[] = new char[12];
209 while (l != 0L) {
210 long l1 = l;
211 l /= 37L;
212 ac[11 - i++] = VALID_CHARS[(int) (l1 - l * 37L)];
213 }
214 return new String(ac, 12 - i, i);
215 }
216
217 public static long hash(String input) {
218 long hash = 0L;
219 if (input == null) {
220 input = "null";
221 }
222 for (int index = 0; index < input.length() && index < 12; index++) {
223 char key = input.charAt(index);
224 hash *= 37L;
225 if (key >= 'A' && key <= 'Z') {
226 hash += (1 + key) - 65;
227 } else if (key >= 'a' && key <= 'z') {
228 hash += (1 + key) - 97;
229 } else if (key >= '0' && key <= '9') {
230 hash += (27 + key) - 48;
231 }
232 }
233 while (hash % 37L == 0L && hash != 0L) {
234 hash /= 37L;
235 }
236 return hash;
237 }
238
239 public static int random(int bound) {
240 return random(0, bound, false);
241 }
242
243 public static int random(int lowerBound, int upperBound) {
244 return random(lowerBound, upperBound, false);
245 }
246
247 /** Picks a random element out of any array type. */
248 public static <T> T randomElement(Collection<T> collection) {
249 return new ArrayList<T>(collection).get((int) (RANDOM.nextDouble() * collection.size()));
250 }
251
252 /** Picks a random element out of any list type. */
253 public static <T> T randomElement(List<T> list) {
254 return list.get((int) (RANDOM.nextDouble() * list.size()));
255 }
256
257 public static boolean hasOneOutOf(double chance) {
258 if (chance < 1) {
259 chance = 0;
260 }
261 chance *= 100.0;
262 return random(1, (int) chance) <= 100;
263 }
264
265 /** Picks a random element out of any array type. */
266 public static <T> T randomElement(T[] array) {
267 return array[(int) (RANDOM.nextDouble() * array.length)];
268 }
269
270 /** Picks a random element out of any array type. */
271 public static int randomElement(int[] array) {
272 return array[(int) (RANDOM.nextDouble() * array.length)];
273 }
274
275 public static int random(int lowerBound, int upperBound, boolean inclusive) {
276 if (lowerBound >= upperBound) {
277 throw new IllegalArgumentException("The lower bound cannot be larger than or equal to the upper bound!");
278 }
279
280 return lowerBound + RANDOM.nextInt(upperBound - lowerBound) + (inclusive ? 1 : 0);
281 }
282
283 /** Gets all of the classes in a directory */
284 public static List<Object> getClassesInDirectory(String directory) {
285 List<Object> classes = new LinkedList<>();
286 File dir = new File(directory);
287 if (!dir.exists() || !dir.isDirectory()) {
288 return classes;
289 }
290 try {
291 File[] files = dir.listFiles();
292 if (files == null) {
293 return classes;
294 }
295 for (File f : files) {
296 if (f.isDirectory() || f.getName().contains("$")) {
297 continue;
298 }
299 String domainPath = Utility.class.getProtectionDomain().getCodeSource().getLocation().getPath().replace("/", "\\");
300 String filePath = "\\" + f.getPath();
301 String clazzName = filePath.replace(domainPath, "");
302 clazzName = clazzName.replace("\\", ".");
303 clazzName = clazzName.replace(".class", "");
304 Class<?> clazz = Class.forName(clazzName);
305 Object o = clazz.getDeclaredConstructor().newInstance();
306 classes.add(o);
307 }
308 } catch (ClassNotFoundException | IllegalAccessException | InstantiationException
309 | InvocationTargetException | NoSuchMethodException e) {
310 // e.printStackTrace();
311 }
312 return classes;
313 }
314
315 /** Gets all of the sub directories of a folder */
316 public static List<String> getSubDirectories(Class<?> clazz) {
317 String filePath = clazz.getResource("/" + clazz.getName().replace(".", "/") + ".class").getFile();
318 File file = new File(filePath);
319 File directory = file.getParentFile();
320 List<String> list = new ArrayList<>();
321
322 File[] files = directory.listFiles();
323
324 if (files == null) {
325 return Collections.emptyList();
326 }
327
328 for (File f : files) {
329 if (f.isDirectory()) {
330 list.add(f.getPath());
331 }
332 }
333
334 String[] directories = list.toArray(new String[0]);
335 return Arrays.asList(directories);
336 }
337
338
339 /*
340 * Check for a map region change, and if the map region has changed, set the
341 * appropriate flag so the new map region packet is sent.
342 */
343 public static boolean isRegionChange(Position position, Position region) {
344 int diffX = position.getX() - region.getChunkX() * 8;
345 int diffY = position.getY() - region.getChunkY() * 8;
346 boolean changed = false;
347
348 if (diffX < 16) {
349 changed = true;
350 } else if (diffX >= 88) {
351 changed = true;
352 }
353
354 if (diffY < 16) {
355 changed = true;
356 } else if (diffY >= 88) {
357 changed = true;
358 }
359
360 return changed;
361 }
362
363 public static int getDistance(Interactable source, Position target) {
364 Position sourceTopRight = source.getPosition().transform(source.width() - 1, source.length() - 1);
365
366 int dx, dy;
367
368 if (sourceTopRight.getX() < target.getX()) {
369 dx = target.getX() - sourceTopRight.getX();
370 } else if (source.getX() > target.getX()) {
371 dx = source.getX() - target.getX();
372 } else {
373 dx = 0;
374 }
375
376 if (sourceTopRight.getY() < target.getY()) {
377 dy = target.getY() - sourceTopRight.getY();
378 } else if (source.getY() > target.getY()) {
379 dy = source.getY() - target.getY();
380 } else {
381 dy = 0;
382 }
383
384 return dx + dy;
385 }
386
387 public static int getDistance(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength) {
388 if (source.getHeight() != target.getHeight()) {
389 return Integer.MAX_VALUE;
390 }
391
392 if (sourceWidth <= 0) sourceWidth = 1;
393 if (sourceLength <= 0) sourceLength = 1;
394 if (targetWidth <= 0) targetWidth = 1;
395 if (targetLength <= 0) targetLength = 1;
396
397 Position sourceTopRight = source.transform(sourceWidth - 1, sourceLength - 1, 0);
398 Position targetTopRight = target.transform(targetWidth - 1, targetLength - 1, 0);
399
400 int dx, dy;
401
402 if (sourceTopRight.getX() < target.getX()) {
403 dx = target.getX() - sourceTopRight.getX();
404 } else if (source.getX() > targetTopRight.getX()) {
405 dx = source.getX() - targetTopRight.getX();
406 } else {
407 dx = 0;
408 }
409
410 if (sourceTopRight.getY() < target.getY()) {
411 dy = target.getY() - sourceTopRight.getY();
412 } else if (source.getY() > targetTopRight.getY()) {
413 dy = source.getY() - targetTopRight.getY();
414 } else {
415 dy = 0;
416 }
417
418 return dx + dy;
419 }
420
421 public static Position getDelta(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength) {
422 if (source.getHeight() != target.getHeight()) {
423 return Position.create(Integer.MAX_VALUE, Integer.MAX_VALUE);
424 }
425
426 if (sourceWidth <= 0) sourceWidth = 1;
427 if (sourceLength <= 0) sourceLength = 1;
428 if (targetWidth <= 0) targetWidth = 1;
429 if (targetLength <= 0) targetLength = 1;
430
431 Position sourceTopRight = source.transform(sourceWidth - 1, sourceLength - 1, 0);
432 Position targetTopRight = target.transform(targetWidth - 1, targetLength - 1, 0);
433
434 int dx, dy;
435
436 if (sourceTopRight.getX() < target.getX()) {
437 dx = target.getX() - sourceTopRight.getX();
438 } else if (source.getX() > targetTopRight.getX()) {
439 dx = source.getX() - targetTopRight.getX();
440 } else {
441 dx = 0;
442 }
443
444 if (sourceTopRight.getY() < target.getY()) {
445 dy = target.getY() - sourceTopRight.getY();
446 } else if (source.getY() > targetTopRight.getY()) {
447 dy = source.getY() - targetTopRight.getY();
448 } else {
449 dy = 0;
450 }
451
452 return Position.create(dx, dy);
453 }
454
455 public static int getDistance(Interactable source, Interactable target) {
456 return getDistance(source.getPosition(), source.width(), source.length(), target.getPosition(), target.width(), target.length());
457 }
458
459 public static Position getDelta(Interactable source, Interactable target) {
460 return getDelta(source.getPosition(), source.width(), source.length(), target.getPosition(), target.width(), target.length());
461 }
462
463 public static Position getDelta(Position source, Position target) {
464 int dx = target.getX() - source.getX();
465 int dy = target.getY() - source.getY();
466 return Position.create(dx, dy);
467 }
468
469
470 public static boolean withinDistance(Interactable source, Interactable target, int radius) {
471 return within(source, target, radius);
472 }
473
474 public static boolean withinDistance(Interactable source, Position target, int radius) {
475 return within(source.getPosition(), source.width(), source.length(), target, 1, 1, radius);
476 }
477
479 Position found = null;
480 Position[] positions = getBoundaries(source);
481
482 for (Position next : positions) {
483 Direction direction = Direction.getDirection(source.getPosition(), next);
484
485 if (inside(next, 0, 0, source.getPosition(), source.width(), source.length())) {
486 continue;
487 }
488
489 if (TraversalMap.isTraversable(next, direction, false)) {
490 found = next;
491 break;
492 }
493 }
494
495 return found;
496 }
497
498 public static Position findBestInside(Interactable source, Interactable target) {
499 if (target.width() <= 1 || target.length() <= 1) {
500 return target.getPosition();
501 }
502
503 int dx, dy, dist = Integer.MAX_VALUE;
504 Position best = source.getPosition();
505
506 for (int x = 0; x < target.width(); x++) {
507 Position boundary = target.getPosition().transform(x, 0);
508 int distance = getDistance(source, boundary);
509
510 if (dist > distance) {
511 dist = distance;
512 best = boundary;
513 }
514
515 boundary = target.getPosition().transform(x, target.length() - 1);
516 distance = getDistance(source, boundary);
517
518 if (dist > distance) {
519 dist = distance;
520 best = boundary;
521 }
522 }
523
524 for (int y = 0; y < target.length(); y++) {
525 Position boundary = target.getPosition().transform(0, y);
526 int distance = getDistance(source, boundary);
527
528 if (dist > distance) {
529 dist = distance;
530 best = boundary;
531 }
532
533 boundary = target.getPosition().transform(target.width() - 1, y);
534 distance = getDistance(source, boundary);
535
536 if (dist > distance) {
537 dist = distance;
538 best = boundary;
539 }
540 }
541
542 if (best.equals(source.getPosition())) {
543 return source.getPosition();
544 }
545
546 Direction direction = Direction.getDirection(source.getPosition(), best);
547 Position sourceTopRight = source.getPosition().transform(source.width() - 1, source.length() - 1, 0);
548
549 if (source.getX() > best.getX()) {
550 dx = best.getX() - source.getX();
551 } else if (sourceTopRight.getX() < best.getX()) {
552 dx = best.getX() - sourceTopRight.getX();
553 } else {
554 dx = direction.getDirectionX();
555 }
556 if (source.getY() > best.getY()) {
557 dy = best.getY() - source.getY();
558 } else if (sourceTopRight.getY() < best.getY()) {
559 dy = best.getY() - sourceTopRight.getY();
560 } else {
561 dy = direction.getDirectionY();
562 }
563
564 return source.getPosition().transform(dx, dy);
565 }
566
567 public static void fixInsidePosition(Mob source, Interactable target) {
568 if (source.movement.needsPlacement()) return;
569
570 List<Position> boundaries = new LinkedList<>();
571 Map<Position, LinkedList<Position>> paths = new HashMap<>();
572 Position cur = source.getPosition();
573
574 for (Position position : getBoundaries(target)) {
575 Position delta = getDelta(target.getPosition(), position);
576 int dx = Integer.signum(delta.getX()), dy = Integer.signum(delta.getY());
577 Direction direction = Direction.getDirection(dx, dy);
578
579 if (direction == Direction.NONE)
580 continue;
581
582 while (inside(cur, source.width(), source.length(), target.getPosition(), target.width(), target.length())) {
583 if (!TraversalMap.isTraversable(cur, direction, source.width())) {
584 break;
585 }
586
587 cur = cur.transform(direction.getFaceLocation());
588 LinkedList<Position> list = paths.computeIfAbsent(position, pos -> {
589 boundaries.add(position);
590 return new LinkedList<>();
591 });
592
593 list.add(cur);
594 paths.put(position, list);
595 }
596 }
597
598 if (boundaries.isEmpty()) return;
599
600 source.setFixingInside(true);
601 Position random = boundaries.get(0);//RandomUtils.random(boundaries);
602 source.movement.addPath(new Path(paths.get(random)));
603 }
604
605 public static Position[] getBoundaries(Interactable interactable) {
606 int nextSlot = 0;
607 int width = interactable.width();
608 int length = interactable.length();
609 Position[] boundaries = new Position[2 * (width + length)];
610 for (int y = 0; y < length + 2; y++) {
611 for (int x = 0; x < width + 2; x++) {
612 int xx = x % (width + 1);
613 int yy = y % (length + 1);
614 if (xx == 0 && yy == 0 || xx != 0 && yy != 0) continue;
615 boundaries[nextSlot++] = interactable.getPosition().transform(x - 1, y - 1, 0);
616 }
617 }
618 return boundaries;
619 }
620
621 public static Position[] getInnerBoundaries(Position position, int width, int length) {
622 int nextSlot = 0;
623 Position[] boundaries = new Position[width * length];
624 for (int y = 0; y < length; y++) {
625 for (int x = 0; x < width; x++) {
626 boundaries[nextSlot++] = position.transform(x, y, 0);
627 }
628 }
629 return boundaries;
630 }
631
632 public static Position[] getInnerBoundaries(Interactable interactable) {
633 int nextSlot = 0;
634 int width = interactable.width();
635 int length = interactable.length();
636 Position[] boundaries = new Position[width * length];
637 for (int y = 0; y < length; y++) {
638 for (int x = 0; x < width; x++) {
639 boundaries[nextSlot++] = interactable.getPosition().transform(x, y, 0);
640 }
641 }
642 return boundaries;
643 }
644
645 public static String formatName(final String input) {
646 if(input == null) {
647 return null;
648 }
649 //Open chagpt
650 StringBuilder formattedName = new StringBuilder();
651
652 // Split the input string by spaces
653 String[] words = input.trim().split(" ");
654
655 // Capitalize the first letter of each word
656 for (String word : words) {
657 if (!word.isEmpty()) {
658 // Capitalize the first letter
659 char firstLetter = Character.toUpperCase(word.charAt(0));
660 formattedName.append(firstLetter);
661
662 // Append the remaining letters
663 if (word.length() > 1) {
664 formattedName.append(word.substring(1));
665 }
666
667 // Append a space
668 formattedName.append(" ");
669 }
670 }
671
672 // Remove the trailing space and return the formatted name
673 return formattedName.toString().trim();
674 }
675
676 public static boolean within(Position source, Position target, int distance) {
677 Interactable interactableSource = Interactable.create(source);
678 Interactable interactableTarget = Interactable.create(target);
679 return within(interactableSource, interactableTarget, distance);
680 }
681
682 public static String formatText(String s) {
683 for (int i = 0; i < s.length(); i++) {
684 if (i == 0) {
685 s = String.format("%s%s", Character.toUpperCase(s.charAt(0)),
686 s.substring(1));
687 }
688 if (!Character.isLetterOrDigit(s.charAt(i))) {
689 if (i + 1 < s.length()) {
690 s = String.format("%s%s%s", s.subSequence(0, i + 1),
691 Character.toUpperCase(s.charAt(i + 1)),
692 s.substring(i + 2));
693 }
694 }
695 }
696 return s.replace("_", " ");
697 }
698
699 public static boolean within(Interactable source, Interactable target, int distance) {
700 return within(source.getPosition(), source.width(), source.length(), target.getPosition(), target.width(), target.length(), distance);
701 }
702
703 public static boolean withinOctal(Interactable source, Interactable target, int distance) {
704 return withinOctal(source.getPosition(), source.width(), source.length(), target.getPosition(), target.width(), target.length(), distance);
705 }
706
707 public static boolean withinOctal(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength, int distance) {
708 if (target.getHeight() != source.getHeight()) {
709 return false;
710 }
711 Position sourceTopRight = source.transform(sourceWidth - 1, sourceLength - 1);
712 Position targetTopRight = target.transform(targetWidth - 1, targetLength - 1);
713 int dx, dy;
714 if (sourceTopRight.getX() < target.getX()) {
715 dx = Math.abs(target.getX() - sourceTopRight.getX());
716 } else if (source.getX() > targetTopRight.getX()) {
717 dx = Math.abs(targetTopRight.getX() - source.getX());
718 } else {
719 dx = 0;
720 }
721 if (sourceTopRight.getY() < target.getY()) {
722 dy = Math.abs(target.getY() - sourceTopRight.getY());
723 } else if (source.getY() > targetTopRight.getY()) {
724 dy = Math.abs(targetTopRight.getY() - source.getY());
725 } else {
726 dy = 0;
727 }
728 return dx <= distance && dy <= distance;
729 }
730
731 public static boolean within(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength, int distance) {
732 if (target.getHeight() != source.getHeight()) {
733 return false;
734 }
735 Position sourceTopRight = source.transform(sourceWidth - 1, sourceLength - 1);
736 Position targetTopRight = target.transform(targetWidth - 1, targetLength - 1);
737 int dx, dy;
738 if (sourceTopRight.getX() < target.getX()) {
739 dx = Math.abs(target.getX() - sourceTopRight.getX());
740 } else if (source.getX() > targetTopRight.getX()) {
741 dx = Math.abs(targetTopRight.getX() - source.getX());
742 } else {
743 dx = 0;
744 }
745 if (sourceTopRight.getY() < target.getY()) {
746 dy = Math.abs(target.getY() - sourceTopRight.getY());
747 } else if (source.getY() > targetTopRight.getY()) {
748 dy = Math.abs(targetTopRight.getY() - source.getY());
749 } else {
750 dy = 0;
751 }
752 return dx + dy <= distance;
753 }
754
755 public static boolean withinViewingDistance(Interactable source, Interactable target, int radius) {
756 if (source == null || target == null) {
757 return false;
758 }
759
760 if (target.getHeight() != source.getHeight()) {
761 return false;
762 }
763 Position sourceTopRight = source.getPosition().transform(source.width() - 1, source.length() - 1);
764 Position targetTopRight = target.getPosition().transform(target.width() - 1, target.length() - 1);
765 int dx, dy;
766 if (sourceTopRight.getX() < target.getX()) {
767 dx = Math.abs(target.getX() - sourceTopRight.getX());
768 } else if (source.getX() > targetTopRight.getX()) {
769 dx = Math.abs(targetTopRight.getX() - source.getX());
770 } else {
771 dx = 0;
772 }
773 if (sourceTopRight.getY() < target.getY()) {
774 dy = Math.abs(target.getY() - sourceTopRight.getY());
775 } else if (source.getY() > targetTopRight.getY()) {
776 dy = Math.abs(targetTopRight.getY() - source.getY());
777 } else {
778 dy = 0;
779 }
780 return dx <= radius && dy <= radius;
781 }
782
783 public static boolean inside(Interactable source, Interactable target) {
784 return inside(source.getPosition(), source.width(), source.length(), target.getPosition(), target.width(), target.length());
785 }
786
787 public static boolean inside(Interactable source, Position target) {
788 return inside(source.getPosition(), source.width(), source.length(), target, 1, 1);
789 }
790
791 public static boolean inside(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength) {
792 if (sourceWidth <= 0) sourceWidth = 1;
793 if (sourceLength <= 0) sourceLength = 1;
794 if (targetWidth <= 0) targetWidth = 1;
795 if (targetLength <= 0) targetLength = 1;
796 Position sourceTopRight = source.transform(sourceWidth - 1, sourceLength - 1, 0);
797 Position targetTopRight = target.transform(targetWidth - 1, targetLength - 1, 0);
798 if (source.equals(target) || sourceTopRight.equals(targetTopRight)) {
799 return true;
800 }
801 if (source.getX() > targetTopRight.getX() || sourceTopRight.getX() < target.getX()) {
802 return false;
803 }
804 return source.getY() <= targetTopRight.getY() && sourceTopRight.getY() >= target.getY();
805 }
806
807 public static boolean checkRequirements(Player player, int[] requirements, String action) {
808 boolean can = true;
809 for (int index = 0; index < requirements.length; index++) {
810 int level = player.skills.getMaxLevel(index);
811 int required = requirements[index];
812
813 if (level < required) {
814 player.send(new SendMessage("You need a level of " + required + " " + Skill.getName(index) + " to " + action));
815 can = false;
816 }
817 }
818 return can;
819 }
820
821 public static boolean isLarger(Interactable source, Interactable other) {
822 return source.width() * source.length() > other.width() * other.length();
823 }
824
825 public static int getStarters(String host) {
826 int amount = 0;
827 try {
828 File file = new File("./data/starters/" + host + ".txt");
829 if (!file.exists()) {
830 return 0;
831 }
832 BufferedReader in = new BufferedReader(new FileReader(file));
833
834 String whatever = in.readLine();
835
836 long max = Long.parseLong(whatever);
837
838 if (max > Integer.MAX_VALUE) {
839 amount = 2;
840 } else {
841 amount = (int) max;
842 }
843
844 in.close();
845 } catch (Exception e) {
846 e.printStackTrace();
847 }
848 return amount;
849 }
850
851 public static boolean setStarter(Player player) {
852 String host = player.lastHost;
853
854 int amount = getStarters(host);
855
856 if (amount >= 2) {
857 return false;
858 }
859
860 if (amount == 0) {
861 amount = 1;
862 } else if (amount == 1) {
863 amount = 2;
864 }
865
866 try {
867 File file = new File("./data/starters/" + host + ".txt");
868 BufferedWriter out = new BufferedWriter(new FileWriter(file, false));
869 out.write(String.valueOf(amount));
870 out.close();
871 } catch (Exception e) {
872 e.printStackTrace();
873 return false;
874 }
875 return true;
876 }
877
878 public static int[] shuffleArray(int[] array) {
879 int[] shuffledArray = new int[array.length];
880 System.arraycopy(array, 0, shuffledArray, 0, array.length);
881
882 for (int i = shuffledArray.length - 1; i > 0; i--) {
883 int index = RANDOM.nextInt(i + 1);
884 int a = shuffledArray[index];
885 shuffledArray[index] = shuffledArray[i];
886 shuffledArray[i] = a;
887 }
888 return shuffledArray;
889 }
890
891 public static int getCurrentDay() {
892 return Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
893 }
894
895 public static int min(int... values) {
896 int i = Integer.MAX_VALUE;
897 for(int i2 : values)
898 if(i2 < i)
899 i = i2;
900 return i;
901 }
902
903 public static final boolean goodDistance(int objectX, int objectY, int playerX, int playerY, int distance) {
904 int deltaX = objectX - playerX;
905 int deltaY = objectY - playerY;
906 int trueDistance = ((int) Math.sqrt(Math.pow(deltaX, 2) + Math.pow(deltaY, 2)));
907 return trueDistance <= distance;
908 }
909
910 public static boolean inRange(int absX, int absY, int size, int targetX, int targetY, int targetSize, int distance) {
911 if (absX < targetX) {
912 /*
913 * West of target
914 */
915 int closestX = absX + (size - 1);
916 int diffX = targetX - closestX;
917 if (diffX > distance)
918 return false;
919 } else if (absX > targetX) {
920 /*
921 * East of target
922 */
923 int closestTargetX = targetX + (targetSize - 1);
924 int diffX = absX - closestTargetX;
925 if (diffX > distance)
926 return false;
927 }
928 if (absY < targetY) {
929 /*
930 * South of target
931 */
932 int closestY = absY + (size - 1);
933 int diffY = targetY - closestY;
934 return diffY <= distance;
935 } else if (absY > targetY) {
936 /*
937 * North of target
938 */
939 int closestTargetY = targetY + (targetSize - 1);
940 int diffY = absY - closestTargetY;
941 return diffY <= distance;
942 }
943 return true;
944 }
945
946 public static boolean inRange(Interactable source, Interactable target, int distance) {
947 return inRange(source.getX(), source.getY(), source.width(), target.getX(), target.getY(), target.width(), distance);
948 }
949
950}
static final Stopwatch UPTIME
Definition RuneHive.java:60
Handles the mob class.
Definition Mob.java:66
void setFixingInside(boolean fixingInside)
Definition Mob.java:755
boolean addPath(Path path)
Finds a smart path to the target.
This class represents a character controlled by a player.
Definition Player.java:125
Represents a trainable and usable skill.
Definition Skill.java:18
static String getName(int skill)
Gets the name for a skill id.
Definition Skill.java:465
int getMaxLevel(int id)
Gets the highest possible level of a skill.
Contains traversal data for a set of regions.
static boolean isTraversable(Position from, Direction direction, int size)
Tests whether or not a specified position is traversable in the specified direction.
Represents a single path in the path finding system.
Definition Path.java:13
Represents a single tile on the game world.
Definition Position.java:14
int getHeight()
Gets the height coordinate, or height.
Definition Position.java:51
int getY()
Gets the absolute y coordinate.
Definition Position.java:46
int getX()
Gets the absolute x coordinate.
Definition Position.java:41
Position transform(int diffX, int diffY, int diffZ)
Creates a new location based on this location.
static Position create(int x, int y, int z)
Creates a location.
int getChunkX()
Gets the chunk x coordinate.
Definition Position.java:76
int getChunkY()
Gets the chunk y coordinate.
Definition Position.java:81
The OutgoingPacket that sends a message to a Players chatbox in the client.
long elapsedTime(TimeUnit unit)
Handles miscellaneous methods.
Definition Utility.java:27
static String formatName(final String input)
Definition Utility.java:645
static String getTime(long period)
Gets the time based off a long.
Definition Utility.java:146
static int random(int bound)
Definition Utility.java:239
static int randomElement(int[] array)
Picks a random element out of any array type.
Definition Utility.java:271
static long nameToLong(String text)
Converts the first 12 characters in a string of text to a hash.
Definition Utility.java:185
static int getStarters(String host)
Definition Utility.java:825
static boolean setStarter(Player player)
Definition Utility.java:851
static String formatText(String s)
Definition Utility.java:682
static boolean within(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength, int distance)
Definition Utility.java:731
static String longToString(long l)
Definition Utility.java:202
static boolean hasOneOutOf(double chance)
Definition Utility.java:257
static< T > T randomElement(T[] array)
Picks a random element out of any array type.
Definition Utility.java:266
static Position findAccessableTile(Interactable source)
Definition Utility.java:478
static String getTime(int ticks)
Gets a basic time based off seconds.
Definition Utility.java:160
static final boolean goodDistance(int objectX, int objectY, int playerX, int playerY, int distance)
Definition Utility.java:903
static boolean inRange(Interactable source, Interactable target, int distance)
Definition Utility.java:946
static String capitalizeSentence(final String string)
Capitalize each letter after .
Definition Utility.java:99
static long hash(String input)
Definition Utility.java:217
static String formatDigits(final long amount)
Formats digits for longs.
Definition Utility.java:46
static String getDate()
Gets the date of server.
Definition Utility.java:126
static Position getDelta(Interactable source, Interactable target)
Definition Utility.java:459
static boolean inside(Interactable source, Interactable target)
Definition Utility.java:783
static List< String > getSubDirectories(Class<?> clazz)
Gets all of the sub directories of a folder.
Definition Utility.java:316
static List< Object > getClassesInDirectory(String directory)
Gets all of the classes in a directory.
Definition Utility.java:284
static int getDistance(Interactable source, Interactable target)
Definition Utility.java:455
static String formatDigits(final int amount)
Formats digits for integers.
Definition Utility.java:41
static double getPercentageAmount(int progress, int total)
Gets a percentage amount.
Definition Utility.java:36
static String bigDaddyTime(long period)
Definition Utility.java:150
static boolean inRange(int absX, int absY, int size, int targetX, int targetY, int targetSize, int distance)
Definition Utility.java:910
static int getDistance(Interactable source, Position target)
Definition Utility.java:363
static String formatDigits(final double amount)
Formats digits for doubles.
Definition Utility.java:51
static Position findBestInside(Interactable source, Interactable target)
Definition Utility.java:498
static boolean inside(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength)
Definition Utility.java:791
static< T > T randomElement(Collection< T > collection)
Picks a random element out of any array type.
Definition Utility.java:248
static void fixInsidePosition(Mob source, Interactable target)
Definition Utility.java:567
static int getDistance(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength)
Definition Utility.java:387
static Position getDelta(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength)
Definition Utility.java:421
static boolean withinDistance(Interactable source, Interactable target, int radius)
Definition Utility.java:470
static Position[] getBoundaries(Interactable interactable)
Definition Utility.java:605
static final char[] VALID_CHARS
Array of all valid characters.
Definition Utility.java:33
static String rank(final String string)
Formats the player name.
Definition Utility.java:94
static boolean isRegionChange(Position position, Position region)
Definition Utility.java:343
static boolean withinOctal(Position source, int sourceWidth, int sourceLength, Position target, int targetWidth, int targetLength, int distance)
Definition Utility.java:707
static int random(int lowerBound, int upperBound)
Definition Utility.java:243
static boolean withinDistance(Interactable source, Position target, int radius)
Definition Utility.java:474
static Position[] getInnerBoundaries(Position position, int width, int length)
Definition Utility.java:621
static String getAOrAn(String nextWord)
A or an.
Definition Utility.java:116
static boolean withinViewingDistance(Interactable source, Interactable target, int radius)
Definition Utility.java:755
static int random(int lowerBound, int upperBound, boolean inclusive)
Definition Utility.java:275
static String getTime()
Gets the current server time and formats it.
Definition Utility.java:141
static Position[] getInnerBoundaries(Interactable interactable)
Definition Utility.java:632
static boolean isLarger(Interactable source, Interactable other)
Definition Utility.java:821
static String formatPrice(final long amount)
Formats a price for longs.
Definition Utility.java:56
static boolean checkRequirements(Player player, int[] requirements, String action)
Definition Utility.java:807
static boolean within(Interactable source, Interactable target, int distance)
Definition Utility.java:699
static String getUptime()
Gets the current uptime of server and formats it.
Definition Utility.java:155
static int min(int... values)
Definition Utility.java:895
static< T > T randomElement(List< T > list)
Picks a random element out of any list type.
Definition Utility.java:253
static String convertWord(int amount)
Converts an integer into words.
Definition Utility.java:136
static int getCurrentDay()
Definition Utility.java:891
static Position getDelta(Position source, Position target)
Definition Utility.java:463
static final Random RANDOM
Random instance, used to generate pseudo-random primitive types.
Definition Utility.java:30
static boolean inside(Interactable source, Position target)
Definition Utility.java:787
static int[] shuffleArray(int[] array)
Definition Utility.java:878
static String formatEnum(final String string)
Formats name of enum.
Definition Utility.java:89
static long stringToLong(String string)
Definition Utility.java:71
static boolean withinOctal(Interactable source, Interactable target, int distance)
Definition Utility.java:703
static boolean within(Position source, Position target, int distance)
Definition Utility.java:676
static String getSimpleDate()
Gets the date of server.
Definition Utility.java:131
This class specially written to convert the given number into words.
Definition Words.java:11
String getNumberInWords()
Definition Words.java:208
static Words getInstance(long num)
Definition Words.java:30
Represents the enumerated directions an entity can walk or face.
static Direction getDirection(int deltaX, int deltaY)
Gets the direction between two locations.
An object implementing Interactable has uses.
static Interactable create(Position position)
Creates a new instance of an Interactable.