1
0

ScriptUtils.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package me.hammerle.code;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.charset.MalformedInputException;
  5. import java.nio.file.Files;
  6. import java.util.Arrays;
  7. import java.util.Collection;
  8. import java.util.Random;
  9. import java.util.stream.Collectors;
  10. import me.hammerle.exceptions.PreScriptException;
  11. public class ScriptUtils
  12. {
  13. private static final Random RANDOM = new Random();
  14. public static int randomInt(int min, int max)
  15. {
  16. return RANDOM.nextInt((max - min) + 1) + min;
  17. }
  18. // -----------------------------------------------------------------------------------
  19. // number getter
  20. // -----------------------------------------------------------------------------------
  21. public static int getInt(Object o)
  22. {
  23. return ((Number) o).intValue();
  24. }
  25. public static byte getByte(Object o)
  26. {
  27. return ((Number) o).byteValue();
  28. }
  29. public static double getDouble(Object o)
  30. {
  31. return ((Number) o).doubleValue();
  32. }
  33. public static float getFloat(Object o)
  34. {
  35. return ((Number) o).floatValue();
  36. }
  37. // -----------------------------------------------------------------------------------
  38. // connectors
  39. // -----------------------------------------------------------------------------------
  40. public static String connect(Object[] o, int skip)
  41. {
  42. return Arrays.stream(o, skip, o.length).map(ob -> String.valueOf(ob))
  43. .collect(Collectors.joining());
  44. }
  45. public static String connect(Collection<Object> c, int skip)
  46. {
  47. return c.stream().skip(skip).map(o -> o.toString()).collect(Collectors.joining());
  48. }
  49. public static String connect(Object[] c, String s, int skip)
  50. {
  51. return Arrays.stream(c, skip, c.length).map(o -> o.toString()).collect(Collectors.joining(s));
  52. }
  53. public static String connect(Collection<Object> c, String s, int skip)
  54. {
  55. return c.stream().skip(skip).map(o -> String.valueOf(o)).collect(Collectors.joining(s));
  56. }
  57. // -----------------------------------------------------------------------------------
  58. // file stuff
  59. // -----------------------------------------------------------------------------------
  60. public static String readCode(String filename, String ending)
  61. {
  62. File script = new File("./" + filename + ending);
  63. if(script.exists())
  64. {
  65. try
  66. {
  67. return String.join("\n", Files.readAllLines(script.toPath()));
  68. }
  69. catch (MalformedInputException ex)
  70. {
  71. throw new PreScriptException(filename, "", "file contains an illegal character, change file encoding");
  72. }
  73. catch (IOException ex)
  74. {
  75. throw new PreScriptException(filename, "", "file '" + filename + "' cannot be read");
  76. }
  77. }
  78. throw new PreScriptException(filename, "", "file '" + filename + "' does not exist");
  79. }
  80. public static String readCode(String filename)
  81. {
  82. return readCode(filename, ".snuvi");
  83. }
  84. }