Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/at/hannibal2/skyhanni/data/GlobalRender.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import at.hannibal2.skyhanni.utils.ChatUtils
@SkyHanniModule
object GlobalRender {

val renderDisabled get() = !enabled
private var enabled = true

@JvmStatic
val renderDisabled get() = !enabled

@HandleEvent
fun onCommandRegistration(event: CommandRegistrationEvent) {
event.registerBrigadier("shrendertoggle") {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/at/hannibal2/skyhanni/data/GuiData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import org.lwjgl.glfw.GLFW
@SkyHanniModule
object GuiData {

@JvmStatic
var preDrawEventCancelled = false

@HandleEvent(priority = HandleEvent.HIGH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,17 @@
import java.util.List;

@Mixin(AbstractContainerScreen.class)
public abstract class MixinHandledScreen {
public abstract class MixinAbstractContainerScreen {

@Inject(method = "render", at = @At(value = "HEAD"), cancellable = true)
private void renderHead(GuiGraphics context, int mouseX, int mouseY, float deltaTicks, CallbackInfo ci) {
if (GlobalRender.INSTANCE.getRenderDisabled()) return;
if (GlobalRender.getRenderDisabled()) return;
AbstractContainerScreen<?> gui = (AbstractContainerScreen<?>) (Object) this;
if (new GuiContainerEvent.PreDraw(context, gui, gui.getMenu(), mouseX, mouseY, deltaTicks).post()) {
GuiData.INSTANCE.setPreDrawEventCancelled(true);
GuiData.setPreDrawEventCancelled(true);
ci.cancel();
} else {
DelayedRun.INSTANCE.runNextTick(() -> {
GuiData.INSTANCE.setPreDrawEventCancelled(false);
return null;
});
DelayedRun.runNextTick(() -> GuiData.setPreDrawEventCancelled(false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import net.minecraft.client.input.MouseButtonInfo;

@Mixin(MouseHandler.class)
public class MixinMouse {
public class MixinMouseHandler {

@Shadow
private double accumulatedDX;
Expand All @@ -24,27 +24,24 @@ public class MixinMouse {

@Inject(method = "onMove", at = @At("RETURN"))
private void onMouseButton(long window, double x, double y, CallbackInfo ci) {
MouseCompat.INSTANCE.setDeltaMouseX(this.accumulatedDX);
MouseCompat.INSTANCE.setDeltaMouseY(this.accumulatedDY);
MouseCompat.setDeltaMouseX(this.accumulatedDX);
MouseCompat.setDeltaMouseY(this.accumulatedDY);
}

@Inject(method = "onScroll", at = @At("HEAD"))
private void onScroll(long window, double horizontal, double vertical, CallbackInfo ci) {
MouseCompat.INSTANCE.setScroll(vertical);
DelayedRun.INSTANCE.runNextTickOld(() -> {
MouseCompat.INSTANCE.setScroll(0);
return null;
});
MouseCompat.setScroll(vertical);
DelayedRun.runNextTickEnd(() -> MouseCompat.setScroll(0));
}

@Inject(method = "onButton", at = @At("HEAD"))
private void onMouseButton(long window, MouseButtonInfo input, int action, CallbackInfo ci) {
MouseCompat.INSTANCE.handleMouseButton(input, action);
MouseCompat.handleMouseButton(input, action);
}

@Inject(method = "handleAccumulatedMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;isWindowActive()Z"))
private void onMouseButtonHead(CallbackInfo ci, @Local(ordinal = 0) double timeDelta) {
MouseCompat.INSTANCE.setTimeDelta(timeDelta * 10000);
MouseCompat.setTimeDelta(timeDelta * 10000);
}

@ModifyVariable(method = "turnPlayer", at = @At("STORE"), ordinal = 1)
Expand Down
41 changes: 24 additions & 17 deletions src/main/java/at/hannibal2/skyhanni/utils/DelayedRun.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,51 @@ import kotlin.time.Duration
// TODO add names for runs
object DelayedRun {

private val tasks = mutableListOf<Pair<() -> Any, SimpleTimeMark>>()
private val futureTasks = ConcurrentLinkedQueue<Pair<() -> Any, SimpleTimeMark>>()
private val tasks = mutableListOf<Pair<() -> Any?, SimpleTimeMark>>()
private val futureTasks = ConcurrentLinkedQueue<Pair<() -> Any?, SimpleTimeMark>>()

fun runDelayed(duration: Duration, run: () -> Unit): SimpleTimeMark {
/**
* Runs [runnable] at the end of the next game tick after [duration] has passed,
* always on the main thread.
*/
fun runDelayed(duration: Duration, runnable: Runnable): SimpleTimeMark {
val time = SimpleTimeMark.now() + duration
futureTasks.add(run to time)
futureTasks.add(runnable::run to time)
return time
}

fun <T> runDelayedReturning(duration: Duration, run: () -> T): Pair<SimpleTimeMark, () -> T> {
fun <T> runDelayedReturning(duration: Duration, block: () -> T): Pair<SimpleTimeMark, () -> T> {
val time = SimpleTimeMark.now() + duration
val runnable = { run() }
@Suppress("UNCHECKED_CAST")
futureTasks.add((runnable as () -> Any) to time)
return time to runnable
futureTasks.add(block to time)
return time to block
}

/**
* Runs in the next game tick (up to 50ms delay), always on the main thread.
* Runs [runnable] at the start of the next game tick, always on the main thread.
*/
fun runNextTick(run: () -> Unit) = Minecraft.getInstance().schedule(run)
@JvmStatic
fun runNextTick(runnable: Runnable) = Minecraft.getInstance().schedule(runnable)

/**
* I'm not sure why, but this acts different to the above one
* Runs [runnable] at the end of the next game tick, always on the main thread.
*
* Prefer [runNextTick] unless you have a specific reason to use this method.
*/
fun runNextTickOld(run: () -> Unit) = futureTasks.add(run to SimpleTimeMark.farPast())
@JvmStatic
fun runNextTickEnd(runnable: Runnable) = futureTasks.add(runnable::run to SimpleTimeMark.farPast())

/**
* Runs now if we are on the main thread, otherwise queues it for the next tick.
* Runs [runnable] now if we are on the main thread,
* otherwise queues it for the start of the next game tick.
*/
fun runOrNextTick(run: () -> Unit) = Minecraft.getInstance().execute(run)
fun runOrNextTick(runnable: Runnable) = Minecraft.getInstance().execute(runnable)

fun checkRuns() {
tasks.removeIf { (runnable, time) ->
tasks.removeIf { (block, time) ->
val inPast = time.isInPast()
if (inPast) {
try {
runnable()
block()
} catch (e: Exception) {
ErrorManager.logErrorWithData(e, "DelayedRun task crashed while executing")
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/at/hannibal2/skyhanni/utils/compat/MouseCompat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import net.minecraft.client.input.MouseButtonInfo
object MouseCompat {
private const val NUMBER_OF_MOUSE_BUTTONS = 6

private val buttonStates = BooleanArray(NUMBER_OF_MOUSE_BUTTONS)

@JvmStatic
var deltaMouseY = 0.0
@JvmStatic
var deltaMouseX = 0.0
@JvmStatic
var scroll = 0.0
@JvmStatic
var timeDelta = 0.0
private val buttonStates = BooleanArray(NUMBER_OF_MOUSE_BUTTONS)

private val mouse by lazy {
Minecraft.getInstance().mouseHandler
Expand All @@ -32,7 +37,7 @@ object MouseCompat {

fun getScrollDelta(): Int {
val delta = scroll
DelayedRun.runNextTickOld { scroll = 0.0 }
DelayedRun.runNextTickEnd { scroll = 0.0 }
return delta.toInt() * 120
}

Expand All @@ -44,18 +49,14 @@ object MouseCompat {
return mouse.ypos().toInt()
}

// I have no clue what the difference between getx and geteventx is on 1.8.9
// on 1.8.9 they are pretty much the same (they are the exact same when the mouse is still)
fun getEventX(): Int = getX()
fun getEventY(): Int = getY()

fun getEventButtonState(): Boolean = buttonStates.any { it }
fun getEventNanoseconds(): Long = timeDelta.toLong()

fun getEventDY(): Int {
return deltaMouseY.toInt()
}

@JvmStatic
fun handleMouseButton(input: MouseButtonInfo, action: Int) {
val button: Int = input.button()
if (action == 1) {
Expand All @@ -64,7 +65,7 @@ object MouseCompat {
KeyPressEvent(button).post()
} else {
KeyPressEvent(button).post()
DelayedRun.runNextTickOld {
DelayedRun.runNextTickEnd {
setButtonState(button, false)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ class ScrollValue {
}

fun isPureScrollEvent(): Boolean {
val mouseX = MouseCompat.getEventX()
val mouseY = MouseCompat.getEventY()
val mouseX = MouseCompat.getX()
val mouseY = MouseCompat.getY()
val isScrollEvent = MouseCompat.getScrollDelta() != 0
val hasMouseMoved = mouseX != lastMouseX || mouseY != lastMouseY

Expand Down
Loading