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