mirror of https://github.com/kami-blue/client
[fix] InventoryManager item refilling
This commit is contained in:
parent
a37727cfec
commit
28fae304ba
|
@ -17,11 +17,11 @@ object SetBuildingBlockCommand : ClientCommand(
|
||||||
val heldItem = player.inventory.getCurrentItem()
|
val heldItem = player.inventory.getCurrentItem()
|
||||||
when {
|
when {
|
||||||
heldItem.isEmpty -> {
|
heldItem.isEmpty -> {
|
||||||
InventoryManager.buildingBlockID.value = 0
|
InventoryManager.buildingBlockID = 0
|
||||||
MessageSendHelper.sendChatMessage("Building block has been reset")
|
MessageSendHelper.sendChatMessage("Building block has been reset")
|
||||||
}
|
}
|
||||||
heldItem.item.block !is BlockAir -> {
|
heldItem.item.block !is BlockAir -> {
|
||||||
InventoryManager.buildingBlockID.value = heldItem.item.id
|
InventoryManager.buildingBlockID = heldItem.item.id
|
||||||
MessageSendHelper.sendChatMessage("Building block has been set to ${heldItem.displayName}")
|
MessageSendHelper.sendChatMessage("Building block has been set to ${heldItem.displayName}")
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
|
|
|
@ -29,16 +29,16 @@ object InventoryManager : Module(
|
||||||
"minecraft:cobblestone"
|
"minecraft:cobblestone"
|
||||||
)
|
)
|
||||||
|
|
||||||
private val autoRefill = setting("AutoRefill", true)
|
private val autoRefill by setting("AutoRefill", true)
|
||||||
private val buildingMode = setting("BuildingMode", false, { autoRefill.value })
|
private val buildingMode by setting("BuildingMode", false, { autoRefill })
|
||||||
val buildingBlockID = setting("BuildingBlockID", 0, 0..1000, 1, { false })
|
var buildingBlockID by setting("BuildingBlockID", 0, 0..1000, 1, { false })
|
||||||
private val refillThreshold = setting("RefillThreshold", 16, 1..63, 1, { autoRefill.value })
|
private val refillThreshold by setting("RefillThreshold", 16, 1..63, 1, { autoRefill })
|
||||||
private val itemSaver = setting("ItemSaver", false)
|
private val itemSaver by setting("ItemSaver", false)
|
||||||
private val duraThreshold = setting("DurabilityThreshold", 5, 1..50, 1, { itemSaver.value })
|
private val duraThreshold by setting("DurabilityThreshold", 5, 1..50, 1, { itemSaver })
|
||||||
val autoEject = setting("AutoEject", false)
|
private val autoEject by setting("AutoEject", false)
|
||||||
private val fullOnly = setting("OnlyAtFull", false, { autoEject.value })
|
private val fullOnly by setting("OnlyAtFull", false, { autoEject })
|
||||||
private val pauseMovement = setting("PauseMovement", true)
|
private val pauseMovement by setting("PauseMovement", true)
|
||||||
private val delay = setting("DelayTicks", 1, 0..20, 1)
|
private val delay by setting("DelayTicks", 1, 0..20, 1)
|
||||||
val ejectList = setting(CollectionSetting("EjectList", defaultEjectList))
|
val ejectList = setting(CollectionSetting("EjectList", defaultEjectList))
|
||||||
|
|
||||||
enum class State {
|
enum class State {
|
||||||
|
@ -54,33 +54,30 @@ object InventoryManager : Module(
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
onToggle {
|
onDisable {
|
||||||
paused = false
|
paused = false
|
||||||
BaritoneUtils.unpause()
|
BaritoneUtils.unpause()
|
||||||
}
|
}
|
||||||
|
|
||||||
safeListener<PlayerTravelEvent> {
|
safeListener<PlayerTravelEvent> {
|
||||||
if (player.isSpectator || !pauseMovement.value || !paused) return@safeListener
|
if (player.isSpectator || !pauseMovement || !paused) return@safeListener
|
||||||
player.setVelocity(0.0, mc.player.motionY, 0.0)
|
player.setVelocity(0.0, mc.player.motionY, 0.0)
|
||||||
it.cancel()
|
it.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
safeListener<TickEvent.ClientTickEvent> {
|
safeListener<TickEvent.ClientTickEvent> {
|
||||||
if (it.phase != TickEvent.Phase.START || player.isSpectator || mc.currentScreen is GuiContainer) return@safeListener
|
if (it.phase != TickEvent.Phase.START || player.isSpectator || mc.currentScreen is GuiContainer) return@safeListener
|
||||||
if (!timer.tick(delay.value.toLong())) return@safeListener
|
|
||||||
|
if (!timer.tick(delay.toLong())) return@safeListener
|
||||||
|
|
||||||
setState()
|
setState()
|
||||||
|
|
||||||
if (currentState == State.IDLE) removeHoldingItem()
|
|
||||||
|
|
||||||
when (currentState) {
|
when (currentState) {
|
||||||
State.SAVING_ITEM -> saveItem()
|
State.SAVING_ITEM -> saveItem()
|
||||||
State.REFILLING_BUILDING -> refillBuilding()
|
State.REFILLING_BUILDING -> refillBuilding()
|
||||||
State.REFILLING -> refill()
|
State.REFILLING -> refill()
|
||||||
State.EJECTING -> eject()
|
State.EJECTING -> eject()
|
||||||
else -> {
|
State.IDLE -> removeHoldingItem()
|
||||||
// this is fine, Java meme
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
playerController.updateController()
|
playerController.updateController()
|
||||||
|
@ -96,7 +93,7 @@ object InventoryManager : Module(
|
||||||
else -> State.IDLE
|
else -> State.IDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentState != State.IDLE && pauseMovement.value && !paused) {
|
if (currentState != State.IDLE && pauseMovement && !paused) {
|
||||||
BaritoneUtils.pause()
|
BaritoneUtils.pause()
|
||||||
paused = true
|
paused = true
|
||||||
} else if (currentState == State.IDLE && paused) {
|
} else if (currentState == State.IDLE && paused) {
|
||||||
|
@ -107,31 +104,28 @@ object InventoryManager : Module(
|
||||||
|
|
||||||
/* State checks */
|
/* State checks */
|
||||||
private fun SafeClientEvent.saveItemCheck(): Boolean {
|
private fun SafeClientEvent.saveItemCheck(): Boolean {
|
||||||
return itemSaver.value && checkDamage(player.heldItemMainhand)
|
return itemSaver && checkDamage(player.heldItemMainhand)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun SafeClientEvent.refillBuildingCheck(): Boolean {
|
private fun SafeClientEvent.refillBuildingCheck(): Boolean {
|
||||||
if (!autoRefill.value || !buildingMode.value || buildingBlockID.value == 0) return false
|
if (!autoRefill || !buildingMode || buildingBlockID == 0) return false
|
||||||
|
|
||||||
val totalCount = player.inventorySlots.countID(buildingBlockID.value)
|
val totalCount = player.inventorySlots.countID(buildingBlockID)
|
||||||
val hotbarCount = player.hotbarSlots.countID(buildingBlockID.value)
|
val hotbarCount = player.hotbarSlots.countID(buildingBlockID)
|
||||||
|
|
||||||
return totalCount > refillThreshold.value
|
return totalCount >= refillThreshold
|
||||||
&& (hotbarCount <= refillThreshold.value
|
&& (hotbarCount < refillThreshold
|
||||||
|| (getRefillableSlotBuilding() != null && currentState == State.REFILLING_BUILDING))
|
|| (getRefillableSlotBuilding() != null && currentState == State.REFILLING_BUILDING))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun SafeClientEvent.refillCheck(): Boolean {
|
private fun SafeClientEvent.refillCheck(): Boolean {
|
||||||
if (!autoRefill.value) return false
|
return autoRefill && getRefillableSlot() != null
|
||||||
|
|
||||||
return getRefillableSlot() != null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun SafeClientEvent.ejectCheck(): Boolean {
|
private fun SafeClientEvent.ejectCheck(): Boolean {
|
||||||
if (!autoEject.value || ejectList.isEmpty()) return false
|
return autoEject && ejectList.isNotEmpty()
|
||||||
|
&& (!fullOnly || player.inventorySlots.firstEmpty() == null)
|
||||||
return getEjectSlot() != null && (!fullOnly.value || player.inventorySlots.firstEmpty() == null)
|
&& getEjectSlot() != null
|
||||||
}
|
}
|
||||||
/* End of state checks */
|
/* End of state checks */
|
||||||
|
|
||||||
|
@ -144,7 +138,7 @@ object InventoryManager : Module(
|
||||||
val emptySlot = player.inventorySlots.firstEmpty()
|
val emptySlot = player.inventorySlots.firstEmpty()
|
||||||
|
|
||||||
when {
|
when {
|
||||||
autoRefill.value && undamagedItem != null -> {
|
autoRefill && undamagedItem != null -> {
|
||||||
moveToHotbar(undamagedItem.slotNumber, currentSlot)
|
moveToHotbar(undamagedItem.slotNumber, currentSlot)
|
||||||
}
|
}
|
||||||
emptySlot != null -> {
|
emptySlot != null -> {
|
||||||
|
@ -157,7 +151,7 @@ object InventoryManager : Module(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun SafeClientEvent.refillBuilding() {
|
private fun SafeClientEvent.refillBuilding() {
|
||||||
player.storageSlots.firstID(buildingBlockID.value)?.let {
|
player.storageSlots.firstID(buildingBlockID)?.let {
|
||||||
quickMoveSlot(it)
|
quickMoveSlot(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -188,44 +182,12 @@ object InventoryManager : Module(
|
||||||
|
|
||||||
private fun checkDamage(itemStack: ItemStack) =
|
private fun checkDamage(itemStack: ItemStack) =
|
||||||
itemStack.isItemStackDamageable
|
itemStack.isItemStackDamageable
|
||||||
&& itemStack.itemDamage > itemStack.maxDamage * (1.0f - duraThreshold.value / 100.0f)
|
&& itemStack.itemDamage > itemStack.maxDamage * (1.0f - duraThreshold / 100.0f)
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks damage of item in given slot
|
|
||||||
*
|
|
||||||
* @return True if durability is lower than the value of [duraThreshold],
|
|
||||||
* false if not lower than the value of [duraThreshold],
|
|
||||||
* null if item is not damageable
|
|
||||||
*/
|
|
||||||
private fun checkDamage(slot: Int): Boolean? {
|
|
||||||
return if (!mc.player.inventory.getStackInSlot(slot).isEmpty) {
|
|
||||||
val item = mc.player.inventory.getStackInSlot(slot)
|
|
||||||
if (item.isItemStackDamageable) {
|
|
||||||
item.itemDamage > item.maxDamage * (1.0f - duraThreshold.value.toFloat() / 100.0f)
|
|
||||||
} else null
|
|
||||||
} else null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Same as [checkDamage], but uses full inventory slot
|
|
||||||
*
|
|
||||||
* @return True if durability is lower than the value of [duraThreshold],
|
|
||||||
* false if not lower than the value of [duraThreshold],
|
|
||||||
* null if item is not damageable or slot is empty
|
|
||||||
*/
|
|
||||||
private fun checkDamageFullInv(slot: Int): Boolean? {
|
|
||||||
return if (!mc.player.inventoryContainer.inventory[slot].isEmpty) {
|
|
||||||
val item = mc.player.inventoryContainer.inventory[slot]
|
|
||||||
if (item.isItemStackDamageable) {
|
|
||||||
item.itemDamage > item.maxDamage * (1.0f - duraThreshold.value.toFloat() / 100.0f)
|
|
||||||
} else null
|
|
||||||
} else null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun SafeClientEvent.getRefillableSlotBuilding(): Slot? {
|
private fun SafeClientEvent.getRefillableSlotBuilding(): Slot? {
|
||||||
if (player.storageSlots.firstID(buildingBlockID.value) == null) return null
|
if (player.storageSlots.firstID(buildingBlockID) == null) return null
|
||||||
|
|
||||||
return player.hotbarSlots.firstID(buildingBlockID.value) {
|
return player.hotbarSlots.firstID(buildingBlockID) {
|
||||||
it.isStackable && it.count < it.maxStackSize
|
it.isStackable && it.count < it.maxStackSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -233,28 +195,24 @@ object InventoryManager : Module(
|
||||||
private fun SafeClientEvent.getRefillableSlot(): Slot? {
|
private fun SafeClientEvent.getRefillableSlot(): Slot? {
|
||||||
return player.hotbarSlots.firstByStack {
|
return player.hotbarSlots.firstByStack {
|
||||||
!it.isEmpty
|
!it.isEmpty
|
||||||
&& (!buildingMode.value || it.item.id != buildingBlockID.value)
|
&& (!buildingMode || it.item.id != buildingBlockID)
|
||||||
&& (!autoEject.value || !ejectList.contains(it.item.registryName.toString()))
|
&& (!autoEject || !ejectList.contains(it.item.registryName.toString()))
|
||||||
&& it.isStackable
|
&& it.isStackable
|
||||||
&& (it.maxStackSize / 64.0f * refillThreshold.value).ceilToInt() < refillThreshold.value
|
&& it.count < (it.maxStackSize / 64.0f * refillThreshold).ceilToInt()
|
||||||
&& getCompatibleStack(it) != null
|
&& getCompatibleStack(it) != null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun SafeClientEvent.getCompatibleStack(stack: ItemStack): Slot? {
|
private fun SafeClientEvent.getCompatibleStack(stack: ItemStack): Slot? {
|
||||||
return player.storageSlots.firstID(stack.item.id) {
|
return player.storageSlots.firstByStack {
|
||||||
isCompatibleStacks(stack, it)
|
stack.isItemEqual(it) && ItemStack.areItemStackTagsEqual(stack, it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isCompatibleStacks(stack1: ItemStack, stack2: ItemStack): Boolean {
|
|
||||||
return stack1.isItemEqual(stack2) && ItemStack.areItemStackTagsEqual(stack2, stack1)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun SafeClientEvent.getEjectSlot(): Slot? {
|
private fun SafeClientEvent.getEjectSlot(): Slot? {
|
||||||
return player.inventorySlots.firstByStack {
|
return player.inventorySlots.firstByStack {
|
||||||
!it.isEmpty
|
!it.isEmpty
|
||||||
&& (!buildingMode.value || it.item.id != buildingBlockID.value)
|
&& (!buildingMode || it.item.id != buildingBlockID)
|
||||||
&& ejectList.contains(it.item.registryName.toString())
|
&& ejectList.contains(it.item.registryName.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue