123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package me.hammerle.code;
- import java.io.File;
- import java.io.IOException;
- import java.nio.charset.MalformedInputException;
- import java.nio.file.Files;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Random;
- import java.util.stream.Collectors;
- import me.hammerle.exceptions.PreScriptException;
- public class ScriptUtils
- {
- private static final Random RANDOM = new Random();
-
- public static int randomInt(int min, int max)
- {
- return RANDOM.nextInt((max - min) + 1) + min;
- }
-
-
-
-
-
- public static int getInt(Object o)
- {
- return ((Number) o).intValue();
- }
-
- public static byte getByte(Object o)
- {
- return ((Number) o).byteValue();
- }
-
- public static double getDouble(Object o)
- {
- return ((Number) o).doubleValue();
- }
-
- public static float getFloat(Object o)
- {
- return ((Number) o).floatValue();
- }
-
-
-
-
-
- public static String connect(Object[] o, int skip)
- {
- return Arrays.stream(o, skip, o.length).map(ob -> String.valueOf(ob))
- .collect(Collectors.joining());
- }
-
- public static String connect(Collection<Object> c, int skip)
- {
- return c.stream().skip(skip).map(o -> o.toString()).collect(Collectors.joining());
- }
-
- public static String connect(Object[] c, String s, int skip)
- {
- return Arrays.stream(c, skip, c.length).map(o -> o.toString()).collect(Collectors.joining(s));
- }
-
- public static String connect(Collection<Object> c, String s, int skip)
- {
- return c.stream().skip(skip).map(o -> String.valueOf(o)).collect(Collectors.joining(s));
- }
-
-
-
-
-
- public static String readCode(String filename, String ending)
- {
- File script = new File("./" + filename + ending);
- if(script.exists())
- {
- try
- {
- return String.join("\n", Files.readAllLines(script.toPath()));
- }
- catch (MalformedInputException ex)
- {
- throw new PreScriptException(filename, "", "file contains an illegal character, change file encoding");
- }
- catch (IOException ex)
- {
- throw new PreScriptException(filename, "", "file '" + filename + "' cannot be read");
- }
- }
- throw new PreScriptException(filename, "", "file '" + filename + "' does not exist");
- }
-
- public static String readCode(String filename)
- {
- return readCode(filename, ".snuvi");
- }
- }
|