Skip to content

Commit

Permalink
NEXTGEN: ChestStealer fix (#1733)
Browse files Browse the repository at this point in the history
  • Loading branch information
be4dev authored Dec 24, 2023
1 parent 054f0e3 commit 89e3102
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 86 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ dependencies {
// Recommended mods (on IDE)
modRuntimeOnly "com.terraformersmc:modmenu:${project.mod_menu_version}"
modImplementation "maven.modrinth:sodium:${project.sodium_version}"
// modImplementation "maven.modrinth:viafabricplus:3.0.0"
// modImplementation "maven.modrinth:viafabricplus:3.0.2"

// fix nullable imports
implementation 'com.google.code.findbugs:jsr305:3.0.2'
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ org.gradle.jvmargs=-Xms1024m -Xmx4096m
# Fabric Properties
# Check these on https://fabricmc.net/versions.html
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.1
loader_version=0.15.1
yarn_mappings=1.20.4+build.3
loader_version=0.15.3

#Fabric api
fabric_version=0.91.2+1.20.4
fabric_version=0.91.3+1.20.4
# Loom
loom_version=1.4-SNAPSHOT
# Mod Properties
Expand All @@ -38,5 +38,5 @@ kotlin_version=1.8.21
# https://maven.fabricmc.net/net/fabricmc/fabric-language-kotlin/
fabric_kotlin_version=1.9.4+kotlin.1.8.21
# Recommended mods
mod_menu_version=9.0.0-pre.1
mod_menu_version=9.0.0
sodium_version=mc1.20.3-0.5.5
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ object ModuleAutoArmor : Module("AutoArmor", Category.COMBAT) {
private val hotbar by boolean("Hotbar", true)

var locked = false
var clickedInInventory = false
private var clickedInInventory = false

val repeatable = repeatable {
val player = mc.player ?: return@repeatable

// In case during swap delay something goes wrong, we check here
if (!canOperate(player, !locked || !clickedInInventory)) {
return@repeatable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ object ModuleFly : Module("Fly", Category.MOVEMENT) {

waitTicks(1)
strafeSince = startStrafe
} else if (packet is ExplosionS2CPacket) { // Check if velocity is affected by explosion
} else if (packet is ExplosionS2CPacket) { // Check if explosion affects velocity
packet.playerVelocityX = 0f
packet.playerVelocityY *= vertical
packet.playerVelocityZ = 0f
Expand All @@ -184,8 +184,7 @@ object ModuleFly : Module("Fly", Category.MOVEMENT) {
* @note NA
*/

private object VulcanGlide : Choice ("VulcanGlide") {

private object VulcanGlide : Choice("VulcanGlide") {

override val parent: ChoiceConfigurable
get() = modes
Expand All @@ -196,7 +195,6 @@ object ModuleFly : Module("Fly", Category.MOVEMENT) {
player.velocity.y = -0.155
}
} else player.velocity.y = -0.1

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ object ModuleChestStealer : Module("ChestStealer", Category.PLAYER) {

val startDelay by intRange("StartDelay", 1..2, 0..20)
val clickDelay by intRange("ClickDelay", 2..4, 0..20)
var closeDelay by intRange("CloseDelay", 1..5, 0..20)
var selectionMode by enumChoice("SelectionMode", SelectionMode.DISTANCE, SelectionMode.values())
val closeDelay by intRange("CloseDelay", 1..5, 0..20)
val quickSwaps by boolean("QuickSwaps", true)
val selectionMode by enumChoice("SelectionMode", SelectionMode.DISTANCE, SelectionMode.values())
val checkTitle by boolean("CheckTitle", true)

private var lastSlot = 0
Expand All @@ -67,19 +68,16 @@ object ModuleChestStealer : Module("ChestStealer", Category.PLAYER) {

if (startDelay > 0) {
waitConditional(startDelay - 1) { !screenIsChest() }

return@repeatable
}
}

val shouldSwap = !ModuleInventoryCleaner.enabled || !ModuleInventoryCleaner.notInContainers

// Quick swap items in hotbar (i.e. swords)
if (shouldSwap && performQuickSwaps(cleanupPlan, screen) != null) {
// Quick swap items in hotbar (i.e. swords), some servers hate them
if (quickSwaps && performQuickSwaps(cleanupPlan, screen) != null) {
return@repeatable
}

var stillRequiredSpace = getStillRequiredSpace(cleanupPlan, itemsToCollect.size)
val stillRequiredSpace = getStillRequiredSpace(cleanupPlan, itemsToCollect.size)

val sortedItemsToCollect = selectionMode.processor(itemsToCollect)

Expand All @@ -89,27 +87,18 @@ object ModuleChestStealer : Module("ChestStealer", Category.PLAYER) {
val hasFreeSpace = (0..35).any { player.inventory.getStack(it).isNothing() }

if (!hasFreeSpace && stillRequiredSpace > 0) {
val shouldReturn = makeSpace(cleanupPlan, 1, screen)

if (shouldReturn == true) {
if (delay > 0) {
waitConditional(delay - 1) { !screenIsChest() }
return@repeatable
}
// we exit out of the loop if inventory is full
val shouldPause = throwItem(cleanupPlan, screen) ?: break

continue
if (shouldPause && delay > 0) {
waitConditional(delay - 1) { !screenIsChest() }
return@repeatable
}

if (shouldReturn != null)
stillRequiredSpace -= 1
}

// now we have some free space so we perform item move
interaction.clickSlot(
screen.screenHandler.syncId,
slot.slotInContainer,
0,
SlotActionType.QUICK_MOVE,
player
screen.screenHandler.syncId, slot.slotInContainer, 0, SlotActionType.QUICK_MOVE, player
)

lastSlot = slot.slotInContainer
Expand All @@ -127,38 +116,26 @@ object ModuleChestStealer : Module("ChestStealer", Category.PLAYER) {
}
}

private fun makeSpace(
cleanupPlan: InventoryCleanupPlan,
requiredSpace: Int,
screen: GenericContainerScreen
/**
* @return if we should wait
*/
private fun throwItem(
cleanupPlan: InventoryCleanupPlan, screen: GenericContainerScreen
): Boolean? {
val itemsInInv = findNonEmptySlotsInInventory()
var stillRequiredSpace = requiredSpace
val itemsToThrowOut = ModuleInventoryCleaner.findItemsToThrowOut(cleanupPlan, itemsInInv)
.firstOrNull { it.getIdForServer(screen) != null } ?: return null

for (slot in itemsToThrowOut) {
if (stillRequiredSpace <= 0)
return false
interaction.clickSlot(
screen.screenHandler.syncId, itemsToThrowOut.getIdForServer(screen)!!, 1, SlotActionType.THROW, player
)

interaction.clickSlot(
screen.screenHandler.syncId,
slot.getIdForServer(screen) ?: continue,
1,
SlotActionType.THROW,
player
)

stillRequiredSpace--

return true
}

if (stillRequiredSpace > 0)
return null

return false
return true
}

/**
* @param slotsToCollect amount of items we need to take
*/
private fun getStillRequiredSpace(
cleanupPlan: InventoryCleanupPlan,
slotsToCollect: Int,
Expand Down Expand Up @@ -189,19 +166,18 @@ object ModuleChestStealer : Module("ChestStealer", Category.PLAYER) {
private fun isScreenTitleChest(screen: GenericContainerScreen): Boolean {
val titleString = screen.title.string

return titleString == Text.translatable("container.chest").string
|| titleString == Text.translatable("container.chestDouble").string
return titleString == Text.translatable("container.chest").string ||
titleString == Text.translatable("container.chestDouble").string
}


/**
* WARNING: Due to the remap the hotbar swaps are not valid anymore after this function.
*
* @return true if the chest stealer should wait for the next tick to continue.
* @return true if the chest stealer should wait for the next tick to continue. null if we didn't do anything
*/
private suspend fun Sequence<*>.performQuickSwaps(
cleanupPlan: InventoryCleanupPlan,
screen: GenericContainerScreen
cleanupPlan: InventoryCleanupPlan, screen: GenericContainerScreen
): Boolean? {
for (hotbarSwap in cleanupPlan.swaps) {
// We only care about swaps from the chest to the hotbar
Expand All @@ -223,8 +199,7 @@ object ModuleChestStealer : Module("ChestStealer", Category.PLAYER) {

cleanupPlan.remapSlots(
hashMapOf(
Pair(hotbarSwap.from, hotbarSwap.to),
Pair(hotbarSwap.to, hotbarSwap.from)
Pair(hotbarSwap.from, hotbarSwap.to), Pair(hotbarSwap.to, hotbarSwap.from)
)
)

Expand Down Expand Up @@ -256,32 +231,28 @@ object ModuleChestStealer : Module("ChestStealer", Category.PLAYER) {
}

private fun findItemsInContainer(screen: GenericContainerScreen) =
screen.screenHandler.slots
.filter { !it.stack.isNothing() && it.inventory === screen.screenHandler.inventory }
screen.screenHandler.slots.filter { !it.stack.isNothing() && it.inventory === screen.screenHandler.inventory }
.map { ContainerItemSlot(it.id) }

enum class SelectionMode(
override val choiceName: String,
val processor: (List<ContainerItemSlot>) -> List<ContainerItemSlot>
override val choiceName: String, val processor: (List<ContainerItemSlot>) -> List<ContainerItemSlot>
) : NamedChoice {
DISTANCE(
"Distance",
{
it.sortedBy { slot ->
val slotId = slot.slotInContainer
DISTANCE("Distance", {
it.sortedBy { slot ->
val slotId = slot.slotInContainer

val rowA = slotId / 9
val colA = slotId % 9
val rowA = slotId / 9
val colA = slotId % 9

val rowB = lastSlot / 9
val colB = lastSlot % 9
val rowB = lastSlot / 9
val colB = lastSlot % 9

(colA - colB) * (colA - colB) + (rowA - rowB) * (rowA - rowB)
}
(colA - colB) * (colA - colB) + (rowA - rowB) * (rowA - rowB)
}
}),
INDEX("Index", { list -> list.sortedBy { it.slotInContainer } }), RANDOM(
"Random", List<ContainerItemSlot>::shuffled
),
INDEX("Index", { list -> list.sortedBy { it.slotInContainer } }),
RANDOM("Random", List<ContainerItemSlot>::shuffled),
}

private fun screenIsChest(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ object ModuleInventoryCleaner : Module("InventoryCleaner", Category.PLAYER) {
)

val isGreedy by boolean("Greedy", true)
val notInContainers by boolean("NotInContainers", false)

val offHandItem by enumChoice("OffHandItem", ItemSortChoice.SHIELD, ItemSortChoice.values())
val slotItem1 by enumChoice("SlotItem-1", ItemSortChoice.SWORD, ItemSortChoice.values())
Expand Down

0 comments on commit 89e3102

Please sign in to comment.