diff --git a/src/main/kotlin/com/github/itzilly/sbt/features/gardener/CaneMacroer.kt b/src/main/kotlin/com/github/itzilly/sbt/features/gardener/CaneMacroer.kt index 53e0ad1..1b78d17 100644 --- a/src/main/kotlin/com/github/itzilly/sbt/features/gardener/CaneMacroer.kt +++ b/src/main/kotlin/com/github/itzilly/sbt/features/gardener/CaneMacroer.kt @@ -13,36 +13,71 @@ import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent object CaneMacroer { var isMacroToggled: Boolean = false val mc = Minecraft.getMinecraft() - var activeKeys: MutableList = mutableListOf() + val allowedScreens = listOf(GuiChat::class.java) @SubscribeEvent fun onTick(event: ClientTickEvent) { if (event.phase != TickEvent.Phase.END) return - if (!isMacroToggled) return - val canRunMacro = mc.currentScreen == null || mc.currentScreen is GuiChat - if (!canRunMacro) { - isMacroToggled = false - Chatterbox.say("Disabled macro!") + val currentScreen = mc.currentScreen + + if (currentScreen != null && allowedScreens.none { it.isInstance(currentScreen) }) { + Chatterbox.say("Macro stopped: Invalid screen") stop() + return + } + + val mop = mc.objectMouseOver + + if (mop == null || mop.typeOfHit?.name != "BLOCK") return + + val blockPos = mop.blockPos + val facing = mop.sideHit + + val player = mc.thePlayer + val blockCenter = blockPos.add(0.5, 0.5, 0.5) + val distanceSq = player.getDistanceSq(blockCenter.x.toDouble(), blockCenter.y.toDouble(), blockCenter.z.toDouble()) + + if (distanceSq > 25) { + Chatterbox.say("Macro stopped: Too far from block") + stop() + return + } + + if (isEntityBlockingView()) { + Chatterbox.say("Macro stopped: Entity in the way") + stop() + return } - val myKey = mc.gameSettings.keyBindForward as KeyBindingAccessor pressKey(mc.gameSettings.keyBindForward.keyCode) + mc.playerController.onPlayerDamageBlock(blockPos, facing) + player.swingItem() } fun stop() { KeyBinding.setKeyBindState(mc.gameSettings.keyBindForward.keyCode, false) KeyBinding.setKeyBindState(mc.gameSettings.keyBindAttack.keyCode, false) + isMacroToggled = false } -// private fun pressKey(key: KeyBindingAccessor) { -// key.setPressed(true) -// activeKeys.add(key) -// } - private fun pressKey(keyCode: Int) { KeyBinding.setKeyBindState(keyCode, true) } + + private fun isEntityBlockingView(): Boolean { + val lookVec = mc.thePlayer.lookVec + val eyePos = mc.thePlayer.getPositionEyes(1f) + + val entities = mc.theWorld.getEntitiesWithinAABBExcludingEntity( + mc.thePlayer, + mc.thePlayer.entityBoundingBox.expand(lookVec.xCoord * 5, lookVec.yCoord * 5, lookVec.zCoord * 5) + ) + + return entities.any { + it.canBeCollidedWith() && + it.entityBoundingBox.isVecInside(eyePos) + } + } } \ No newline at end of file