From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: YatopiaMC Date: Fri, 23 Oct 2020 09:20:01 -0700 Subject: [PATCH] (Yatopia) Utilities Co-authored-by: Mykyta Komarnytskyy Co-authored-by: Ivan Pekov Original code by YatopiaMC, licensed under MIT You can find the original code on https://github.com/YatopiaMC/Yatopia diff --git a/build.gradle.kts b/build.gradle.kts index 9e9fd28a76ad6af45b9b45288a5060c6c2a1bcc4..74f8760264aa93af9d4d2895a96ed416a8ef411e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -49,6 +49,7 @@ dependencies { implementation("org.apache.logging.log4j:log4j-api:2.14.1") // Paper implementation("org.ow2.asm:asm:9.2") implementation("org.ow2.asm:asm-commons:9.2") // Paper - ASM event executor generation + implementation("org.apache.commons:commons-rng-core:1.4") implementation("com.googlecode.json-simple:json-simple:1.1.1") { // This includes junit transitively for whatever reason isTransitive = false diff --git a/src/main/java/xyz/arthurb/mirai/server/util/Constants.java b/src/main/java/xyz/arthurb/mirai/server/util/Constants.java new file mode 100644 index 0000000000000000000000000000000000000000..f39a4aadc3697d428aede01c36298a1e5b9df5b2 --- /dev/null +++ b/src/main/java/xyz/arthurb/mirai/server/util/Constants.java @@ -0,0 +1,6 @@ +package xyz.arthurb.mirai.server.util; + +public class Constants { + public static final int[] EMPTY_ARRAY = new int[0]; + public static final int[] ZERO_ARRAY = new int[]{0}; +} \ No newline at end of file diff --git a/src/main/java/xyz/arthurb/mirai/server/util/FastRandom.java b/src/main/java/xyz/arthurb/mirai/server/util/FastRandom.java new file mode 100644 index 0000000000000000000000000000000000000000..1bda325ec0eabaec992145e812785ddfd79d3e23 --- /dev/null +++ b/src/main/java/xyz/arthurb/mirai/server/util/FastRandom.java @@ -0,0 +1,64 @@ +package xyz.arthurb.mirai.server.util; + +import org.apache.commons.rng.core.source64.XoRoShiRo128PlusPlus; + +import java.util.Random; +import java.util.SplittableRandom; + +public class FastRandom extends Random { + + private XoRoShiRo128PlusPlus random; + + public FastRandom() { + super(); + SplittableRandom randomseed = new SplittableRandom(); + this.random = new XoRoShiRo128PlusPlus(randomseed.nextLong(), randomseed.nextLong()); + } + + public FastRandom(long seed) { + super(seed); + SplittableRandom randomseed = new SplittableRandom(seed); + this.random = new XoRoShiRo128PlusPlus(randomseed.nextLong(), randomseed.nextLong()); + } + + @Override + public boolean nextBoolean() { + return random.nextBoolean(); + } + + @Override + public int nextInt() { + return random.nextInt(); + } + + @Override + public float nextFloat() { + return (float) random.nextDouble(); + } + + @Override + public double nextDouble() { + return random.nextDouble(); + } + + @Override + public synchronized void setSeed(long seed) { + SplittableRandom randomseed = new SplittableRandom(seed); + this.random = new XoRoShiRo128PlusPlus(randomseed.nextLong(), randomseed.nextLong()); + } + + @Override + public void nextBytes(byte[] bytes) { + random.nextBytes(bytes); + } + + @Override + public int nextInt(int bound) { + return random.nextInt(bound); + } + + @Override + public long nextLong() { + return random.nextLong(); + } +} \ No newline at end of file diff --git a/src/main/java/xyz/arthurb/mirai/server/util/TimeUtils.java b/src/main/java/xyz/arthurb/mirai/server/util/TimeUtils.java new file mode 100644 index 0000000000000000000000000000000000000000..6aac96e4e4e312262faa281f23a52fd9f013c5b5 --- /dev/null +++ b/src/main/java/xyz/arthurb/mirai/server/util/TimeUtils.java @@ -0,0 +1,27 @@ +package xyz.arthurb.mirai.server.util; + +import java.util.concurrent.TimeUnit; + +public class TimeUtils { + + public static String getFriendlyName(TimeUnit unit) { + switch (unit) { + case NANOSECONDS: + return "ns"; + case MILLISECONDS: + return "ms"; + case MICROSECONDS: + return "micros"; + case SECONDS: + return "s"; + case MINUTES: + return "m"; + case DAYS: + return "d"; + case HOURS: + return "h"; + default: + throw new AssertionError(); + } + } +} \ No newline at end of file