Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
[enhancement] Add store option to ChestStealer. (#1851)
Browse files Browse the repository at this point in the history
Co-authored-by: Dominika <[email protected]>
  • Loading branch information
scorbett123 and 5HT2 authored Jan 15, 2021
1 parent ee7da9c commit 5c69b69
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import net.minecraft.client.gui.GuiButton
class KamiGuiStealButton(x: Int, y: Int) :
GuiButton(696969, x, y, 50, 20, "Steal") {
override fun mouseReleased(mouseX: Int, mouseY: Int) {
if (ChestStealer.stealMode.value === ChestStealer.StealMode.MANUAL) {
if (ChestStealer.mode.value === ChestStealer.Mode.MANUAL) {
ChestStealer.stealing = false
playPressSound(Wrapper.minecraft.soundHandler)
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/me/zeroeightsix/kami/gui/mc/KamiGuiStoreButton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package me.zeroeightsix.kami.gui.mc

import me.zeroeightsix.kami.module.modules.player.ChestStealer
import me.zeroeightsix.kami.util.Wrapper
import net.minecraft.client.gui.GuiButton

class KamiGuiStoreButton(x: Int, y: Int) :
GuiButton(420420, x, y, 50, 20, "Store") {
override fun mouseReleased(mouseX: Int, mouseY: Int) {
if (ChestStealer.mode.value === ChestStealer.Mode.MANUAL) {
ChestStealer.storing = false
playPressSound(Wrapper.minecraft.soundHandler)
}
super.mouseReleased(mouseX, mouseY)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.zeroeightsix.kami.mixin.client.gui;

import me.zeroeightsix.kami.gui.mc.KamiGuiStealButton;
import me.zeroeightsix.kami.gui.mc.KamiGuiStoreButton;
import me.zeroeightsix.kami.module.modules.player.ChestStealer;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
Expand All @@ -21,19 +22,24 @@ public class MixinGuiContainer extends GuiScreen {
@Shadow protected int xSize;

private final GuiButton stealButton = new KamiGuiStealButton(this.guiLeft + this.xSize + 2, this.guiTop + 2);
private final GuiButton storeButton = new KamiGuiStoreButton(this.guiLeft + this.xSize + 2, this.guiTop + 4 + stealButton.height);

@Inject(method = "initGui", at = @At("HEAD"))
public void initGui(CallbackInfo ci) {
if (ChestStealer.INSTANCE.isValidGui()) {
this.buttonList.add(stealButton);
this.buttonList.add(storeButton);
ChestStealer.updateButton(stealButton, this.guiLeft, this.xSize, this.guiTop);
ChestStealer.updateButton(storeButton, this.guiLeft, this.xSize, this.guiTop);
}
}

@Override
protected void actionPerformed(GuiButton button) throws IOException {
if (button.id == 696969) {
ChestStealer.INSTANCE.setStealing(!ChestStealer.INSTANCE.getStealing());
} else if (button.id == 420420) {
ChestStealer.INSTANCE.setStoring(!ChestStealer.INSTANCE.getStoring());
} else {
super.actionPerformed(button);
}
Expand All @@ -42,6 +48,7 @@ protected void actionPerformed(GuiButton button) throws IOException {
@Inject(method = "updateScreen", at = @At("HEAD"))
public void updateScreen(CallbackInfo ci) {
ChestStealer.updateButton(stealButton, this.guiLeft, this.xSize, this.guiTop);
ChestStealer.updateButton(storeButton, this.guiLeft, this.xSize, this.guiTop);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,39 @@ import net.minecraftforge.fml.common.gameevent.TickEvent
internal object ChestStealer : Module(
name = "ChestStealer",
category = Category.PLAYER,
description = "Automatically steal items from containers"
description = "Automatically steal or store items from containers"
) {
val stealMode = setting("StealMode", StealMode.TOGGLE)
val mode = setting("Mode", Mode.TOGGLE)
private val movingMode = setting("MovingMode", MovingMode.QUICK_MOVE)
private val ignoreEjectItem = setting("IgnoresEjectItem", false)
private val delay = setting("Delay(ms)", 250, 0..1000, 25)
private val ignoreEjectItem = setting("IgnoresEjectItem", false, description = "Ignore AutoEject items in InventoryManager")
private val delay = setting("Delay", 250, 0..1000, 25, description = "Move stack delay in ms")

enum class StealMode {
enum class Mode {
ALWAYS, TOGGLE, MANUAL
}

private enum class MovingMode {
QUICK_MOVE, PICKUP, THROW
}

private enum class ContainerMode(val offset: Int) {
STEAL(36), STORE(0)
}

var stealing = false
var storing = false
val timer = TickTimer()

init {
safeListener<TickEvent.ClientTickEvent> {
stealing = if (isContainerOpen() && (stealing || stealMode.value == StealMode.ALWAYS)) {
steal(getStealingSlot())
stealing = if (isContainerOpen() && (stealing || mode.value == Mode.ALWAYS)) {
stealOrStore(getStealingSlot(), ContainerMode.STEAL)
} else {
false
}

storing = if (isContainerOpen() && (storing || mode.value == Mode.ALWAYS)) {
stealOrStore(getStoringSlot(), ContainerMode.STORE)
} else {
false
}
Expand All @@ -50,6 +61,10 @@ internal object ChestStealer : Module(
return getStealingSlot() != null
}

private fun SafeClientEvent.canStore(): Boolean {
return getStoringSlot() != null
}

private fun SafeClientEvent.isContainerOpen(): Boolean {
return player.openContainer != null
&& isValidGui()
Expand All @@ -69,27 +84,44 @@ internal object ChestStealer : Module(
fun updateButton(button: GuiButton, left: Int, size: Int, top: Int) {
runSafe {
if (isEnabled && isContainerOpen()) {
val str = if (stealing) {
"Stop"
} else {
"Steal"
}
if (button.id == 696969) {
val str = if (stealing) {
"Stop"
} else {
"Steal"
}

button.x = left + size + 2
button.y = top + 2
button.enabled = canSteal() and !storing
button.visible = true
button.displayString = str
} else if (button.id == 420420) {
val str = if (storing) {
"Stop"
} else {
"Store"
}

button.x = left + size + 2
button.y = top + 2
button.enabled = canSteal()
button.visible = true
button.displayString = str
button.x = left + size + 2
button.y = top + 24
button.enabled = canStore() and !stealing
button.visible = true
button.displayString = str
}
} else {
button.visible = false
}
}
}

private fun SafeClientEvent.steal(slot: Int?): Boolean {
private fun SafeClientEvent.stealOrStore(slot: Int?, containerMode: ContainerMode): Boolean {
if (slot == null) return false

val size = getContainerSlotSize()
val slotTo = player.openContainer.getSlots(size until size + 36).firstEmpty() ?: return false
val rangeStart = if (containerMode == ContainerMode.STEAL) size else 0
val slotTo = player.openContainer.getSlots(rangeStart until size + containerMode.offset).firstEmpty()
?: return false
val windowID = player.openContainer.windowId

if (timer.tick(delay.value.toLong())) {
Expand All @@ -99,17 +131,33 @@ internal object ChestStealer : Module(
MovingMode.THROW -> throwAllInSlot(windowID, slot)
}
}

return true
}

private fun SafeClientEvent.getStealingSlot(): Int? {
val container = player.openContainer.inventory

for (slot in 0 until getContainerSlotSize()) {
val item = container[slot].item
if (item == Items.AIR) continue
if (ignoreEjectItem.value && InventoryManager.ejectList.contains(item.registryName.toString())) continue
return slot
}

return null
}

private fun SafeClientEvent.getStoringSlot(): Int? {
val container = player.openContainer.inventory
val size = getContainerSlotSize()

for (slot in size until size + 36) {
val item = container[slot].item
if (item == Items.AIR) continue
return slot
}

return null
}

Expand Down

0 comments on commit 5c69b69

Please sign in to comment.