|
| 1 | +package sunsetsatellite.catalyst.multipart.api.impl.dragonfly; |
| 2 | + |
| 3 | + |
| 4 | +import com.google.gson.JsonArray; |
| 5 | +import net.fabricmc.loader.api.FabricLoader; |
| 6 | +import net.minecraft.client.Minecraft; |
| 7 | +import net.minecraft.core.data.DataLoader; |
| 8 | +import net.minecraft.core.util.helper.Axis; |
| 9 | +import sunsetsatellite.catalyst.multipart.api.impl.dragonfly.vector.Vector3f; |
| 10 | + |
| 11 | +import java.io.InputStream; |
| 12 | +import java.lang.reflect.Field; |
| 13 | +import java.util.Objects; |
| 14 | +import java.util.Random; |
| 15 | + |
| 16 | +public class Utilities { |
| 17 | + public static final float COMPARE_CONST = 0.001f; |
| 18 | + public static String writeFields(Class<?> clazz){ |
| 19 | + Field[] fields = clazz.getFields(); |
| 20 | + StringBuilder builder = new StringBuilder(); |
| 21 | + for (int i = 0; i < fields.length; i++) { |
| 22 | + Field field = fields[i]; |
| 23 | + builder.append(field.getName()).append(": ").append(field.getClass().cast(field)); |
| 24 | + if (i < fields.length-1){ |
| 25 | + builder.append("\n"); |
| 26 | + } |
| 27 | + } |
| 28 | + return builder.toString(); |
| 29 | + } |
| 30 | + public static String tabBlock(String string, int tabAmount){ |
| 31 | + StringBuilder builder = new StringBuilder(); |
| 32 | + for (String line: string.split("\n")) { |
| 33 | + builder.append(tabString(tabAmount)).append(line).append("\n"); |
| 34 | + } |
| 35 | + return builder.toString(); |
| 36 | + } |
| 37 | + public static String tabString(int tabAmount){ |
| 38 | + StringBuilder builder = new StringBuilder(); |
| 39 | + for (int i = 0; i < tabAmount; i++) { |
| 40 | + builder.append("\t"); |
| 41 | + } |
| 42 | + return builder.toString(); |
| 43 | + } |
| 44 | + public static boolean equalFloats(float a, float b){ |
| 45 | + return Math.abs(Float.compare(a, b)) < COMPARE_CONST; |
| 46 | + } |
| 47 | + public static Vector3f rotatePoint(Vector3f point, Vector3f origin, Axis axis, float angle){ |
| 48 | + switch (axis){ |
| 49 | + case X: |
| 50 | + return point.rotateAroundX(origin, -angle); |
| 51 | + case Y: |
| 52 | + return point.rotateAroundY(origin, angle); |
| 53 | + case Z: |
| 54 | + return point.rotateAroundZ(origin, -angle); |
| 55 | + } |
| 56 | + throw new RuntimeException("Axis " + axis + " Is not 'X', 'Y', or 'Z'!"); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Tries to load resource from multiple classes, if they all fail it throws an exception |
| 61 | + */ |
| 62 | + public static InputStream getResourceAsStream(String path){ |
| 63 | + try { |
| 64 | + Class.forName("net.minecraft.client.Minecraft"); |
| 65 | + try { |
| 66 | + return Objects.requireNonNull(Minecraft.getMinecraft(Utilities.class).texturePackList.getResourceAsStream(path)); |
| 67 | + } catch (Exception ignored){} |
| 68 | + } catch (Exception ignored) {} |
| 69 | + try { |
| 70 | + return Objects.requireNonNull(DataLoader.class.getResourceAsStream(path)); |
| 71 | + } catch (Exception ignored){} |
| 72 | + try { |
| 73 | + return Objects.requireNonNull(Utilities.class.getResourceAsStream(path)); |
| 74 | + } catch (Exception ignored){} |
| 75 | + try { |
| 76 | + return Objects.requireNonNull(FabricLoader.getInstance().getClass().getResourceAsStream(path)); |
| 77 | + } catch (Exception ignored){} |
| 78 | + try { |
| 79 | + return Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResourceAsStream(path)); |
| 80 | + } catch (Exception ignored){} |
| 81 | + throw new RuntimeException("Resource at '" + path + "' returned null! Does this file exist?"); |
| 82 | + } |
| 83 | + public static float[] floatArrFromJsonArr(JsonArray arr){ |
| 84 | + float[] result = new float[arr.size()]; |
| 85 | + for (int i = 0; i < arr.size(); i++) { |
| 86 | + result[i] = arr.get(i).getAsFloat(); |
| 87 | + } |
| 88 | + return result; |
| 89 | + } |
| 90 | + public static double[] doubleArrFromJsonArr(JsonArray arr){ |
| 91 | + double[] result = new double[arr.size()]; |
| 92 | + for (int i = 0; i < arr.size(); i++) { |
| 93 | + result[i] = arr.get(i).getAsDouble(); |
| 94 | + } |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + public static boolean equalFloat(double a, double b) { |
| 99 | + return Math.abs(a - b) < 1e-9; |
| 100 | + } |
| 101 | + public static Random getRandomFromPos(int x, int y, int z){ |
| 102 | + Random rand = new Random(0); |
| 103 | + long l1 = rand.nextLong() / 2L * 2L + 1L; |
| 104 | + long l2 = rand.nextLong() / 2L * 2L + 1L; |
| 105 | + long l3 = rand.nextLong() / 2L * 2L + 1L; |
| 106 | + rand.setSeed((long)x * l1 + (long)z * l2 + y * l3); |
| 107 | + return rand; |
| 108 | + } |
| 109 | +} |
0 commit comments