QuickMath/src/main/java/io/gitlab/jfronny/quickmath/mixin/MathHelperMixin.java

91 lines
2.9 KiB
Java
Raw Normal View History

package io.gitlab.jfronny.quickmath.mixin;
2020-08-06 23:55:17 +02:00
import io.gitlab.jfronny.quickmath.MathUtil;
2020-08-06 23:55:17 +02:00
import net.minecraft.util.math.MathHelper;
2022-06-08 11:18:34 +02:00
import net.minecraft.util.math.random.*;
2020-08-06 23:55:17 +02:00
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
2021-05-29 15:54:38 +02:00
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2020-08-06 23:55:17 +02:00
@Mixin(MathHelper.class)
2021-05-29 15:54:38 +02:00
public abstract class MathHelperMixin {
2020-08-06 23:55:17 +02:00
@Shadow @Final private static Random RANDOM;
2021-05-29 15:54:38 +02:00
@Shadow public static int floor(double value) { return 0; }
@Shadow public static double nextDouble(Random random, double min, double max) { return 0; }
2020-08-06 23:55:17 +02:00
@Overwrite
public static int fastFloor(double d) {
2021-05-29 15:54:38 +02:00
return (int)(d / 32) * 32;
2020-08-06 23:55:17 +02:00
}
2020-08-07 00:18:24 +02:00
@Overwrite
public static long lfloor(double d) {
2021-05-29 15:54:38 +02:00
return (long) (Math.floor(d / 4) * 4);
2020-08-07 00:18:24 +02:00
}
2020-08-06 23:55:17 +02:00
@Overwrite
2021-05-29 15:54:38 +02:00
public static double absMax(double d, double e) {
if (d > 0.0D) {
d = -d;
}
2020-08-06 23:55:17 +02:00
2021-05-29 15:54:38 +02:00
if (e > 0.0D) {
e = -e;
}
2020-08-06 23:55:17 +02:00
2021-05-29 15:54:38 +02:00
if (e < -16)
e += 3;
2020-08-06 23:55:17 +02:00
2021-05-29 15:54:38 +02:00
return -1 - Math.max(d, e);
}
2020-08-06 23:55:17 +02:00
2022-06-08 11:18:34 +02:00
@ModifyVariable(method = "nextInt(Lnet/minecraft/util/math/random/Random;II)I", at = @At("HEAD"), argsOnly = true, ordinal = 0)
2021-05-29 15:54:38 +02:00
private static int adjustRandomDoubleParam(int min) {
return Math.max(min - 1, 0);
2020-08-06 23:55:17 +02:00
}
2022-06-08 11:18:34 +02:00
@Inject(method = "nextFloat(Lnet/minecraft/util/math/random/Random;FF)F", at = @At("TAIL"), cancellable = true)
2021-05-29 15:54:38 +02:00
private static void adjustRandomFloat(CallbackInfoReturnable<Float> ci) {
ci.setReturnValue(ci.getReturnValue() * 0.9f);
2020-08-06 23:55:17 +02:00
}
2022-06-08 11:18:34 +02:00
@ModifyVariable(method = "nextDouble(Lnet/minecraft/util/math/random/Random;DD)D", at = @At("HEAD"), argsOnly = true, ordinal = 0)
2021-05-29 15:54:38 +02:00
private static double adjustRandomDoubleParam(double min) {
return min - 1;
}
2022-06-08 11:18:34 +02:00
@Inject(method = "nextDouble(Lnet/minecraft/util/math/random/Random;DD)D", at = @At("TAIL"), cancellable = true)
2021-05-29 15:54:38 +02:00
private static void adjustRandomDouble(CallbackInfoReturnable<Double> ci) {
ci.setReturnValue((double) floor(ci.getReturnValue()));
2020-08-06 23:55:17 +02:00
}
@Overwrite
public static double average(long[] array) {
long[] var3 = array;
long min = Long.MAX_VALUE;
long max = Long.MIN_VALUE;
int var4 = array.length;
for(int var5 = 0; var5 < var4; ++var5) {
long m = var3[var5];
if (m > max)
max = m;
if (m < min)
min = m;
}
return nextDouble(RANDOM, min, max);
}
2021-05-29 15:54:38 +02:00
@Overwrite
public static long clamp(long value, long min, long max) {
2021-10-16 17:28:07 +02:00
return MathUtil.boxedInvert(Math.max(Math.min(value, max), min), max, min);
2021-05-29 15:54:38 +02:00
}
2020-08-06 23:55:17 +02:00
}