Skip to content

Commit 052fdff

Browse files
committed
Update BetterFirework to use InventoryManager for inventory switching stuff
1 parent b9b22fb commit 052fdff

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/main/kotlin/com/lambda/module/modules/movement/BetterFirework.kt

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.module.modules.movement
1919

2020
import com.lambda.config.groups.HotbarSettings
21+
import com.lambda.config.groups.InventorySettings
2122
import com.lambda.config.settings.collections.SetSetting.Companion.immutableSet
2223
import com.lambda.config.settings.complex.Bind
2324
import com.lambda.context.SafeContext
@@ -28,6 +29,7 @@ import com.lambda.event.listener.SafeListener.Companion.listen
2829
import com.lambda.interaction.material.StackSelection.Companion.selectStack
2930
import com.lambda.interaction.request.hotbar.HotbarManager
3031
import com.lambda.interaction.request.hotbar.HotbarRequest
32+
import com.lambda.interaction.request.inventory.InventoryRequest.Companion.inventoryRequest
3133
import com.lambda.module.Module
3234
import com.lambda.module.tag.ModuleTag
3335
import com.lambda.threading.runSafe
@@ -37,7 +39,6 @@ import com.lambda.util.NamedEnum
3739
import com.lambda.util.player.SlotUtils.hotbar
3840
import com.lambda.util.player.SlotUtils.hotbarAndStorage
3941
import net.minecraft.client.network.ClientPlayerEntity
40-
import net.minecraft.client.option.KeyBinding
4142
import net.minecraft.entity.effect.StatusEffects
4243
import net.minecraft.item.Items
4344
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket
@@ -46,28 +47,32 @@ import net.minecraft.network.packet.c2s.play.PlayerInteractItemC2SPacket
4647
import net.minecraft.screen.slot.SlotActionType
4748
import net.minecraft.util.Hand
4849
import net.minecraft.util.hit.HitResult
49-
import org.lwjgl.glfw.GLFW
5050

5151
object BetterFirework : Module(
5252
name = "BetterFirework",
5353
description = "Automatic takeoff with fireworks",
5454
tag = ModuleTag.MOVEMENT,
5555
) {
5656
private var activateButton by setting("Activate Key", Bind(0, 0, Mouse.Middle.ordinal), "Button to activate Firework").group(Group.General)
57-
private var middleClickCancel by setting("Middle Click Cancel", false, description = "Cancel pick block action on middle mouse click") { activateButton.key != KeyCode.UNBOUND.code }.group(Group.General)
57+
private var middleClickCancel by setting("Middle Click Cancel", false, description = "Cancel pick block action on middle mouse click") { activateButton.key != KeyCode.Unbound.code }.group(Group.General)
5858
private var fireworkInteract by setting("Right Click Fly", true, "Automatically start flying when right clicking fireworks")
5959
private var fireworkInteractCancel by setting("Right Click Cancel", false, "Cancel block interactions while holding fireworks") { fireworkInteract }
6060

6161
private var clientSwing by setting("Swing", true, "Swing hand client side").group(Group.General)
62-
private var silentUse by setting("Silent", true, "Silent use fireworks from the inventory") { activateButton.key != KeyCode.UNBOUND.code }.group(Group.General)
62+
private var silentUse by setting("Silent", true, "Silent use fireworks from the inventory") { activateButton.key != KeyCode.Unbound.code }.group(Group.General)
6363

6464
override val hotbarConfig = HotbarSettings(this, Group.Hotbar).apply {
6565
::sequenceStageMask.edit { immutableSet(setOf(TickEvent.Pre)) }
6666
}
6767

68+
override val inventoryConfig = InventorySettings(this, Group.Inventory).apply {
69+
::tickStageMask.edit { immutableSet(setOf(TickEvent.Pre)) }
70+
}
71+
6872
private enum class Group(override val displayName: String) : NamedEnum {
6973
General("General"),
70-
Hotbar("Hotbar")
74+
Hotbar("Hotbar"),
75+
Inventory("Inventory")
7176
}
7277

7378
private var takeoffState = TakeoffState.None
@@ -198,39 +203,40 @@ object BetterFirework : Module(
198203
* Use a firework from the hotbar or inventory if possible.
199204
* Return true if a firework has been used
200205
*/
201-
fun SafeContext.startFirework(silent: Boolean): Boolean {
206+
fun SafeContext.startFirework(silent: Boolean) {
202207
val stack = selectStack(count = 1) { isItem(Items.FIREWORK_ROCKET) }
203208

204209
stack.bestItemMatch(player.hotbar)
205210
?.let {
206211
val request = HotbarManager.request(HotbarRequest(player.hotbar.indexOf(it), this@BetterFirework, keepTicks = 0))
207212
if (request.done) {
208-
player.networkHandler.sendPacket(PlayerInteractItemC2SPacket(Hand.MAIN_HAND, 0, player.yaw, player.pitch))
213+
interaction.interactItem(player, Hand.MAIN_HAND)
209214
sendSwing()
210215
}
211216

212-
return true
217+
return
213218
}
214219

215-
if (!silent) return false
220+
if (!silent) return
216221

217222
stack.bestItemMatch(player.hotbarAndStorage)
218223
?.let {
219-
val swap = player.hotbarAndStorage.indexOf(it)
220-
221-
interaction.clickSlot(player.playerScreenHandler.syncId, swap, 0, SlotActionType.SWAP, mc.player)
222-
223-
val request = HotbarManager.request(HotbarRequest(0, this@BetterFirework, keepTicks = 0))
224-
if (request.done) {
225-
player.networkHandler.sendPacket(PlayerInteractItemC2SPacket(Hand.MAIN_HAND, 0, player.yaw, player.pitch))
226-
sendSwing()
227-
}
228-
229-
interaction.clickSlot(player.playerScreenHandler.syncId, swap, 0, SlotActionType.SWAP, mc.player)
230-
return true
224+
val swapSlotId = player.hotbarAndStorage.indexOf(it)
225+
val hotbarSlotToSwapWith = player.hotbar.find { slot -> slot.isEmpty } ?.let { slot -> player.hotbar.indexOf(slot) } ?: 8
226+
227+
inventoryRequest {
228+
swap(swapSlotId, hotbarSlotToSwapWith)
229+
action {
230+
HotbarManager.request(HotbarRequest(hotbarSlotToSwapWith, this@BetterFirework, keepTicks = 0, nowOrNothing = true))
231+
.submit(queueIfClosed = false)
232+
.done.let {
233+
interaction.interactItem(player, Hand.MAIN_HAND)
234+
sendSwing()
235+
}
236+
}
237+
swap(swapSlotId, hotbarSlotToSwapWith)
238+
}.submit()
231239
}
232-
233-
return false
234240
}
235241

236242
enum class TakeoffState {

0 commit comments

Comments
 (0)