diff options
author | Appability <appable@icloud.com> | 2022-11-12 02:48:54 -0800 |
---|---|---|
committer | Appability <appable@icloud.com> | 2022-11-12 02:48:54 -0800 |
commit | 2b21d3a18b412ec8c205beacf403f0147dc04618 (patch) | |
tree | 7aaeda1956b03ba5b0e53cdf575663e3d68a34d2 /src/main/kotlin/com/ambientaddons/features/misc/Salvage.kt | |
parent | 8ec0ce4c645b3fb94c25cde9922f853a992b3fa7 (diff) | |
download | AmbientAddons-2b21d3a18b412ec8c205beacf403f0147dc04618.tar.gz AmbientAddons-2b21d3a18b412ec8c205beacf403f0147dc04618.tar.bz2 AmbientAddons-2b21d3a18b412ec8c205beacf403f0147dc04618.zip |
add autosalvage
Diffstat (limited to 'src/main/kotlin/com/ambientaddons/features/misc/Salvage.kt')
-rw-r--r-- | src/main/kotlin/com/ambientaddons/features/misc/Salvage.kt | 120 |
1 files changed, 118 insertions, 2 deletions
diff --git a/src/main/kotlin/com/ambientaddons/features/misc/Salvage.kt b/src/main/kotlin/com/ambientaddons/features/misc/Salvage.kt index a716758..914870f 100644 --- a/src/main/kotlin/com/ambientaddons/features/misc/Salvage.kt +++ b/src/main/kotlin/com/ambientaddons/features/misc/Salvage.kt @@ -1,5 +1,121 @@ package com.ambientaddons.features.misc -class Salvage { - +import AmbientAddons.Companion.config +import AmbientAddons.Companion.mc +import com.ambientaddons.events.GuiContainerEvent +import com.ambientaddons.utils.Extensions.chest +import com.ambientaddons.utils.Extensions.itemQuality +import com.ambientaddons.utils.Extensions.items +import com.ambientaddons.utils.Extensions.skyblockID +import com.ambientaddons.utils.Extensions.stars +import com.ambientaddons.utils.SalvageStrategy +import com.ambientaddons.utils.render.OverlayUtils +import net.minecraft.client.gui.inventory.GuiContainer +import net.minecraft.inventory.Slot +import net.minecraft.item.ItemStack +import net.minecraftforge.client.event.GuiOpenEvent +import net.minecraftforge.client.event.GuiScreenEvent +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import java.awt.Color + +object Salvage { + private var status: SalvageStatus = SalvageStatus.Idle + private const val salvageSlot = 31 + private const val clickDelay = 300 + private const val topQualityDelay = 1000 + private var nextClickTime = System.currentTimeMillis() + + private val canClick: Boolean + get() = (System.currentTimeMillis() - nextClickTime) >= 0 + + @SubscribeEvent + fun onGuiDraw(event: GuiScreenEvent.DrawScreenEvent) { + val chest = event.gui?.chest ?: return + if (config.salvageMode < 3 || chest.lowerChestInventory.name != "Salvage Item") return + val color = chest.lowerChestInventory.items.last()?.itemDamage + if (status == SalvageStatus.Waiting && color == 5) { + mc.playerController.windowClick( + chest.windowId, salvageSlot, 0, 0, mc.thePlayer + ) + status = SalvageStatus.Clicked + } else if (status == SalvageStatus.Clicked && chest.lowerChestInventory.getStackInSlot(salvageSlot)?.itemDamage == 5) { + status = SalvageStatus.Confirming + nextClickTime = System.currentTimeMillis() + topQualityDelay + } else if ((status == SalvageStatus.Clicked || status == SalvageStatus.Confirmed) && color == 14) { + status = SalvageStatus.Idle + nextClickTime = System.currentTimeMillis() + clickDelay + } else if (status == SalvageStatus.Confirming && canClick) { + mc.playerController.windowClick( + chest.windowId, salvageSlot, 0, 0, mc.thePlayer + ) + status = SalvageStatus.Confirmed + nextClickTime = System.currentTimeMillis() + clickDelay + } else if (config.salvageMode == 4 && status == SalvageStatus.Idle && canClick) { + val salvageableItemIndex = chest.inventory.slice(54 until 90).indexOfFirst { + if (it == null) false else getSalvageStrategy(it) == SalvageStrategy.Always + } + if (salvageableItemIndex != -1) { + mc.playerController.windowClick( + chest.windowId, salvageableItemIndex + 54, 0, 1, mc.thePlayer + ) + status = SalvageStatus.Waiting + } + } + } + + @SubscribeEvent + fun onContainerOpen(event: GuiOpenEvent) { + if (event.gui?.chest == null) return + status = SalvageStatus.Idle + } + + @SubscribeEvent + fun onSlotClick(event: GuiContainerEvent.SlotClickEvent) { + if (config.salvageMode < 2 || event.slot == null) return + if (!isSlotInInventory(event.gui, event.slot)) return + if (status != SalvageStatus.Idle + || getSalvageStrategy(event.slot.stack ?: return) == SalvageStrategy.Block + || !canClick + ) { + event.isCanceled = true + mc.thePlayer.playSound("random.pop", 1f, 0f) + } else { + event.isCanceled = true + mc.playerController.windowClick( + event.container.windowId, event.slotId, 0, 1, mc.thePlayer + ) + status = SalvageStatus.Waiting + } + } + + + @SubscribeEvent + fun onDrawSlot(event: GuiContainerEvent.DrawSlotEvent) { + if (config.salvageMode == 0 || !isSlotInInventory(event.gui, event.slot)) return + val color = when (getSalvageStrategy(event.slot.stack ?: return)) { + SalvageStrategy.Always -> Color.GREEN + SalvageStrategy.Allow -> Color.YELLOW + else -> return + } + OverlayUtils.renderRect( + event.slot.xDisplayPosition.toDouble(), event.slot.yDisplayPosition.toDouble(), 16.0, 16.0, color + ) + } + + private enum class SalvageStatus { + Idle, Waiting, Clicked, Confirming, Confirmed + } + + private fun getSalvageStrategy(item: ItemStack): SalvageStrategy { + AmbientAddons.persistentData.salvageMap[item.skyblockID]?.let { return it } + return when { + item.stars != null -> SalvageStrategy.Block + item.itemQuality == 50 -> if (config.topQualityStrategy) SalvageStrategy.Always else SalvageStrategy.Allow + item.itemQuality != null -> SalvageStrategy.Always + else -> SalvageStrategy.Block + } + } + + private fun isSlotInInventory(gui: GuiContainer, slot: Slot): Boolean = + gui.chest?.lowerChestInventory?.let { it.name == "Salvage Item" && it != slot.inventory } ?: false }
\ No newline at end of file |