Skip to content

Commit ef7ecfe

Browse files
committed
added support to prevent hotbar selection change when changing settings
1 parent 5a339ae commit ef7ecfe

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.elytra.mod.bandit.mixin;
2+
3+
import cn.elytra.mod.bandit.MixinBridger;
4+
import com.llamalad7.mixinextras.sugar.Local;
5+
import net.minecraft.client.Minecraft;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.injection.At;
8+
import org.spongepowered.asm.mixin.injection.Inject;
9+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
10+
11+
@Mixin(Minecraft.class)
12+
public class MinecraftMixin {
13+
14+
@Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/InventoryPlayer;changeCurrentItem(I)V"), cancellable = true)
15+
private void bandit$hookMouseScroll(CallbackInfo ci, @Local(ordinal = 1) int i) {
16+
var shouldCancel = MixinBridger.fireMouseScrollCancelable(i);
17+
if(shouldCancel) {
18+
ci.cancel();
19+
}
20+
}
21+
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.elytra.mod.bandit
2+
3+
object MixinBridger {
4+
5+
/**
6+
* Callbacks in this list will be fired when mouse scrolls.
7+
* Return `true` to cancel the scroll operation (the selected item in hotbar won't change), but
8+
* will not prevent continuing the callback.
9+
*/
10+
val onMouseScrollCancelable: MutableList<(scrollValue: Int) -> Boolean> = mutableListOf()
11+
12+
// internal usage only
13+
@JvmStatic
14+
fun fireMouseScrollCancelable(value: Int): Boolean {
15+
var canceled = false
16+
for(callback in onMouseScrollCancelable) {
17+
canceled = canceled or callback(value)
18+
}
19+
return canceled
20+
}
21+
22+
}

src/main/kotlin/cn/elytra/mod/bandit/client/VeinMiningHUD.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ object VeinMiningHUD {
144144
private fun drawExecutorSelectionMenu(heightBase: Int) = drawCircleList(executors, heightBase)
145145
private fun drawBlockFilterSelectionMenu(heightBase: Int) = drawCircleList(blockFilters, heightBase)
146146

147-
internal fun withActiveMenu(block: CircleList<*>.() -> Unit) {
147+
internal inline fun withActiveMenu(block: CircleList<*>.() -> Unit) {
148148
activeMenu?.let(block)
149149
}
150150

src/main/kotlin/cn/elytra/mod/bandit/client/VeinMiningHandlerClient.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.elytra.mod.bandit.client
22

33
import cn.elytra.mod.bandit.BanditMod
4+
import cn.elytra.mod.bandit.MixinBridger
45
import cn.elytra.mod.bandit.client.VeinMiningHandlerClient.keyPressed
56
import cn.elytra.mod.bandit.client.VeinMiningHandlerClient.onMouseInput
67
import cn.elytra.mod.bandit.client.VeinMiningHandlerClient.veinMiningBlockFilterId
@@ -10,13 +11,11 @@ import com.gtnewhorizon.gtnhlib.blockpos.BlockPos
1011
import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber
1112
import cpw.mods.fml.client.event.ConfigChangedEvent
1213
import cpw.mods.fml.common.eventhandler.SubscribeEvent
13-
import cpw.mods.fml.common.gameevent.InputEvent
1414
import cpw.mods.fml.common.gameevent.TickEvent
1515
import net.minecraft.client.Minecraft
1616
import net.minecraft.client.settings.KeyBinding
1717
import net.minecraftforge.client.event.RenderWorldLastEvent
1818
import org.lwjgl.input.Keyboard
19-
import org.lwjgl.input.Mouse
2019

2120
/**
2221
* The object that handles all the things about Vein Mining on the client side.
@@ -54,6 +53,11 @@ object VeinMiningHandlerClient {
5453
*/
5554
var maxSelectionRenderCount = 1024
5655

56+
init {
57+
// register the callback
58+
MixinBridger.onMouseScrollCancelable += this::onMouseInput
59+
}
60+
5761
@JvmStatic
5862
@SubscribeEvent
5963
fun onClientTick(e: TickEvent.ClientTickEvent) {
@@ -67,17 +71,17 @@ object VeinMiningHandlerClient {
6771
}
6872
}
6973

70-
@JvmStatic
71-
@SubscribeEvent
72-
fun onMouseInput(e: InputEvent.MouseInputEvent) {
73-
val d = Mouse.getEventDWheel()
74+
fun onMouseInput(d: Int): Boolean {
7475
VeinMiningHUD.withActiveMenu {
7576
if(d < 0) {
7677
this.move(1)
78+
return true
7779
} else if (d > 0) {
7880
this.move(-1)
81+
return true
7982
}
8083
}
84+
return false
8185
}
8286

8387
@JvmStatic

src/main/resources/mixins.bandit.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"target": "@env(DEFAULT)",
77
"compatibilityLevel": "JAVA_8",
88
"mixins": [
9+
"MinecraftMixin",
910
"MinecraftServerMixin"
1011
]
1112
}

0 commit comments

Comments
 (0)