Skip to content

Commit

Permalink
refactor(Listenable): is running (#4685)
Browse files Browse the repository at this point in the history
  • Loading branch information
1zun4 authored Nov 25, 2024
1 parent e019e17 commit 07a1c90
Show file tree
Hide file tree
Showing 44 changed files with 79 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,12 @@ private void onFinishedLoading(CallbackInfo ci) {

@ModifyExpressionValue(method = "handleBlockBreaking", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z"))
private boolean injectMultiActionsBreakingWhileUsing(boolean original) {
return original && !(ModuleMultiActions.INSTANCE.handleEvents() && ModuleMultiActions.INSTANCE.getBreakingWhileUsing());
return original && !(ModuleMultiActions.INSTANCE.isRunning() && ModuleMultiActions.INSTANCE.getBreakingWhileUsing());
}

@ModifyExpressionValue(method = "doItemUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;isBreakingBlock()Z"))
private boolean injectMultiActionsPlacingWhileBreaking(boolean original) {
return original && !(ModuleMultiActions.INSTANCE.handleEvents() && ModuleMultiActions.INSTANCE.getPlacingWhileBreaking());
return original && !(ModuleMultiActions.INSTANCE.isRunning() && ModuleMultiActions.INSTANCE.getPlacingWhileBreaking());
}

@ModifyExpressionValue(method = "handleInputEvents", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z", ordinal = 0))
Expand All @@ -344,7 +344,7 @@ private boolean injectMultiActionsAttackingWhileUsingAndEnforcedBlockingState(bo
this.interactionManager.stopUsingItem(this.player);
}

if (!ModuleMultiActions.INSTANCE.handleEvents() || !ModuleMultiActions.INSTANCE.getAttackingWhileUsing()) {
if (!ModuleMultiActions.INSTANCE.isRunning() || !ModuleMultiActions.INSTANCE.getAttackingWhileUsing()) {
this.options.attackKey.timesPressed = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import net.ccbluex.liquidbounce.features.module.modules.exploit.disabler.disablers.DisablerVerusScaffoldG;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
import net.minecraft.util.hit.BlockHitResult;
import org.spongepowered.asm.mixin.Mixin;
import net.minecraft.network.packet.c2s.play.PlayerInteractBlockC2SPacket;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(PlayerInteractBlockC2SPacket.class)
public class MixinPlayerInteractBlockC2SPacket {
@Redirect(method = "write", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/PacketByteBuf;writeBlockHitResult(Lnet/minecraft/util/hit/BlockHitResult;)V"))
private void writeBlockHitResult(PacketByteBuf buf, BlockHitResult hitResult) {
if (DisablerVerusScaffoldG.INSTANCE.getEnabled() && DisablerVerusScaffoldG.INSTANCE.handleEvents()) {
if (DisablerVerusScaffoldG.INSTANCE.getEnabled() && DisablerVerusScaffoldG.INSTANCE.isRunning()) {
buf.writeBlockPos(hitResult.getBlockPos());
buf.writeVarInt(6 + hitResult.getSide().ordinal() * 7);
buf.writeFloat((float) hitResult.getPos().x - hitResult.getBlockPos().getX());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class MixinBlockEntityRenderDispatcher {
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/block/entity/BlockEntityRenderer;render(Lnet/minecraft/block/entity/BlockEntity;FLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;II)V")
)
private static void render(BlockEntityRenderer blockEntityRenderer, BlockEntity blockEntity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
if (ModuleStorageESP.INSTANCE.getEnabled() && ModuleStorageESP.INSTANCE.handleEvents()
if (ModuleStorageESP.INSTANCE.getEnabled() && ModuleStorageESP.INSTANCE.isRunning()
&& ModuleStorageESP.Glow.INSTANCE.isActive()) {
var outlineVertexConsumerProvider = MinecraftClient.getInstance().getBufferBuilders()
.getOutlineVertexConsumers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private boolean injectHasOutline(MinecraftClient instance, Entity entity) {
return true;
}

if (ModuleStorageESP.INSTANCE.getEnabled() && ModuleStorageESP.INSTANCE.handleEvents() &&
if (ModuleStorageESP.INSTANCE.getEnabled() && ModuleStorageESP.INSTANCE.isRunning() &&
ModuleStorageESP.Glow.INSTANCE.isActive() && ModuleStorageESP.categorize(entity) != null) {
return true;
}
Expand All @@ -229,7 +229,7 @@ private int injectTeamColor(Entity instance) {
return ModuleTNTTimer.INSTANCE.getTntColor(((TntEntity) instance).getFuse()).toARGB();
}

if (ModuleStorageESP.INSTANCE.getEnabled() && ModuleStorageESP.INSTANCE.handleEvents()
if (ModuleStorageESP.INSTANCE.getEnabled() && ModuleStorageESP.INSTANCE.isRunning()
&& ModuleStorageESP.Glow.INSTANCE.isActive()) {
var categorizedEntity = ModuleStorageESP.categorize(instance);
if (categorizedEntity != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ChoiceConfigurable<T : Choice>(
return
}

if (this.activeChoice.handleEvents()) {
if (this.activeChoice.isRunning()) {
this.activeChoice.disable()
}

Expand All @@ -81,7 +81,7 @@ class ChoiceConfigurable<T : Choice>(
this.activeChoice = it[0] as T
})

if (this.activeChoice.handleEvents()) {
if (this.activeChoice.isRunning()) {
this.activeChoice.enable()
}
}
Expand All @@ -91,15 +91,15 @@ class ChoiceConfigurable<T : Choice>(
return
}

if (this.activeChoice.handleEvents()) {
if (this.activeChoice.isRunning()) {
this.activeChoice.disable()
}

set(mutableListOf(defaultChoice), apply = {
this.activeChoice = it[0] as T
})

if (this.activeChoice.handleEvents()) {
if (this.activeChoice.isRunning()) {
this.activeChoice.enable()
}
}
Expand Down Expand Up @@ -130,7 +130,7 @@ abstract class Choice(name: String) : Configurable(name), Listenable, NamedChoic
* We check if the parent is active and if the mode is active, if so
* we handle the events.
*/
override fun handleEvents() = super.handleEvents() && isActive
override fun isRunning() = super.isRunning() && isActive

override fun parent() = this.parent.listenable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class ToggleableConfigurable(
* Because we pass the parent to the Listenable, we can simply
* call the super.handleEvents() and it will return false if the upper-listenable is disabled.
*/
override fun handleEvents() = super.handleEvents() && enabled
override fun isRunning() = super.isRunning() && enabled

override fun parent() = parent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ object EventManager {
for (eventHook in target) {
EventScheduler.process(event)

if (!eventHook.ignoresCondition && !eventHook.handlerClass.handleEvents()) {
if (!eventHook.ignoreRunning && !eventHook.handlerClass.isRunning()) {
continue
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/kotlin/net/ccbluex/liquidbounce/event/Listenable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ typealias Handler<T> = (T) -> Unit
class EventHook<T : Event>(
val handlerClass: Listenable,
val handler: Handler<T>,
val ignoresCondition: Boolean,
val ignoreRunning: Boolean,
val priority: Int = 0
)

interface Listenable {

/**
* Allows disabling event handling when condition is false.
* Returns whether the listenable is running or not, this is based on the parent listenable
* and if no parent is present, it will return the opposite of [isDestructed].
*
* When destructed, the listenable will not handle any events. This is likely to be overridden by
* the implementing class to provide a toggleable feature.
*
* This can be ignored by handlers when [ignoreRunning] is set to true on the [EventHook].
*/
fun handleEvents(): Boolean = parent()?.handleEvents() ?: !isDestructed
fun isRunning(): Boolean = parent()?.isRunning() ?: !isDestructed

/**
* Parent listenable
Expand Down Expand Up @@ -92,7 +98,7 @@ fun Listenable.repeatable(eventHandler: SuspendableHandler<DummyEvent>) {
// Ignore condition makes sense because we do not want our sequence to run after we do not handle events anymore
handler<GameTickEvent>(ignoreCondition = true) {
// Check if we should start or stop the sequence
if (this.handleEvents()) {
if (this.isRunning()) {
// Check if the sequence is already running
if (sequence == null) {
// If not, start it
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/net/ccbluex/liquidbounce/event/Sequence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object SequenceManager : Listenable {
val tickSequences = handler<GameTickEvent>(priority = 1000) {
for (sequence in sequences) {
// Prevent modules handling events when not supposed to
if (!sequence.owner.handleEvents()) {
if (!sequence.owner.isRunning()) {
sequence.cancel()
continue
}
Expand Down Expand Up @@ -82,7 +82,7 @@ open class Sequence<T : Event>(val owner: Listenable, val handler: SuspendableHa
}

internal open suspend fun coroutineRun() {
if (owner.handleEvents()) {
if (owner.isRunning()) {
runCatching {
handler(event)
}.onFailure {
Expand Down Expand Up @@ -185,7 +185,7 @@ class RepeatingSequence(owner: Listenable, handler: SuspendableHandler<DummyEven
override suspend fun coroutineRun() {
sync()

while (continueLoop && owner.handleEvents()) {
while (continueLoop && owner.isRunning()) {
super.coroutineRun()
sync()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ object CommandCenter : Listenable {
state = CenterHandlerState.INACTIVE
}

override fun handleEvents() = super.handleEvents() && inGame && state == CenterHandlerState.APPLY_ON_NEXT_EVENT
override fun isRunning() = super.isRunning() && inGame && state == CenterHandlerState.APPLY_ON_NEXT_EVENT

enum class CenterHandlerState {
INACTIVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ open class Module(
/**
* Events should be handled when module is enabled
*/
override fun handleEvents() = enabled && inGame && !isDestructed
override fun isRunning() = enabled && inGame && !isDestructed

/**
* Handles disconnect and if [disableOnQuit] is true disables module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ object ModuleAutoConfig : Module("AutoConfig", Category.CLIENT, state = true, al
/**
* Overwrites the condition requirement for being in game
*/
override fun handleEvents() = enabled
override fun isRunning() = enabled

}
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,6 @@ object ModuleLiquidChat : Module("LiquidChat", Category.CLIENT, hide = true, sta
}
}

override fun handleEvents() = enabled
override fun isRunning() = enabled

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ object ModuleRichPresence : Module("RichPresence", Category.CLIENT, state = true
.replace("%protocol%", protocolVersion.let { "${it.name} (${it.version})" })
.replace("%server%", hideSensitiveAddress(mc.currentServerEntry?.address ?: "none"))

override fun handleEvents() = true
override fun isRunning() = true

private inline fun IPCClient.sendRichPresence(builderAction: RichPresence.Builder.() -> Unit) =
sendRichPresence(RichPresence.Builder().apply(builderAction).build())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ object ModuleSuperKnockback : Module("SuperKnockback", Category.COMBAT, aliases
}
}

override fun handleEvents(): Boolean {
val handleEvents = super.handleEvents()
override fun isRunning(): Boolean {
val handleEvents = super.isRunning()

// Reset if the module is not handling events anymore
if (!handleEvents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ object ModuleSwordBlock : Module("SwordBlock", Category.COMBAT, aliases = arrayO
player: PlayerEntity = this.player,
offHandItem: Item = player.offHandStack.item,
mainHandItem: Item = player.mainHandStack.item,
) = (handleEvents() || AutoBlock.blockVisual) && offHandItem is ShieldItem
&& (mainHandItem is SwordItem || player === this.player && handleEvents() && alwaysHideShield)
) = (isRunning() || AutoBlock.blockVisual) && offHandItem is ShieldItem
&& (mainHandItem is SwordItem || player === this.player && isRunning() && alwaysHideShield)

@Suppress("UNUSED")
private val packetHandler = sequenceHandler<PacketEvent> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ object CrystalAuraSpeedDebugger : CrystalPostAttackTracker() {
attackedIds.clear()
}

override fun handleEvents() = ModuleCrystalAura.handleEvents() && ModuleDebug.enabled
override fun isRunning() = ModuleCrystalAura.isRunning() && ModuleDebug.enabled

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ abstract class CrystalPostAttackTracker : Listenable {
* @param id The id of the attacked entity.
*/
open fun attacked(id: Int) {
if (!handleEvents()) {
if (!isRunning()) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object SubmoduleSetDead : ToggleableConfigurable(ModuleCrystalAura, "SetDead", t
val entities: MutableMap<Int, EndCrystalEntity> = Int2ObjectMaps.synchronize(Int2ObjectOpenHashMap())

override fun attacked(id: Int) {
if (!handleEvents()) {
if (!isRunning()) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ object ModuleKillAura : Module("KillAura", Category.COMBAT) {
}
}
} else if (player.isUsingItem &&
!(ModuleMultiActions.handleEvents() && ModuleMultiActions.attackingWhileUsing)) {
!(ModuleMultiActions.isRunning() && ModuleMultiActions.attackingWhileUsing)) {
return // return if it's not allowed to attack while the player is using another item that's not a shield
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ object AutoBlock : ToggleableConfigurable(ModuleKillAura, "AutoBlocking", false)
* @see net.minecraft.client.render.item.HeldItemRenderer renderFirstPersonItem
*/
var blockVisual = false
get() = field && super.handleEvents()
get() = field && super.isRunning()

val shouldUnblockToHit
get() = unblockMode != UnblockMode.NONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ object ModuleVelocity : Module("Velocity", Category.COMBAT) {
}
}

override fun handleEvents() = super.handleEvents() && pause == 0
override fun isRunning() = super.isRunning() && pause == 0

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ internal object DisablerVerusExperimental : ToggleableConfigurable(ModuleDisable

// ping spoof, multiple checks
fun shouldBlink(packet: Packet<*>?): Boolean {
if (!this.handleEvents()) return false
if (!this.isRunning()) return false
if (waitTime > System.currentTimeMillis()) return false

if (packet == null && lastFlush + delayTime < System.currentTimeMillis()) {
Expand All @@ -220,7 +220,7 @@ internal object DisablerVerusExperimental : ToggleableConfigurable(ModuleDisable

// meant to trigger playerData.hasLag
fun shouldPrepareToFlush(packet: Packet<*>?): Boolean {
if (!this.handleEvents()) return false
if (!this.isRunning()) return false
if (waitTime > System.currentTimeMillis()) return false
if (packet !is PlayerMoveC2SPacket) return false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ object ModuleAntiStaff : Module("AntiStaff", Category.MISC) {

}

override fun handleEvents() = ModuleAntiStaff.enabled && enabled
override fun isRunning() = ModuleAntiStaff.enabled && enabled

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object ModuleAutoAccount : Module("AutoAccount", Category.MISC, aliases = arrayO

// We can receive chat messages before the world is initialized,
// so we have to handel events even before the that
override fun handleEvents() = enabled
override fun isRunning() = enabled

fun login() {
chat("login")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ object ModuleFreeze : Module("Freeze", Category.MOVEMENT) {
get() = modes

fun shouldLag(origin: TransferOrigin): LagResult? {
if (!enabled || !handleEvents()) {
if (!enabled || !isRunning()) {
return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object FlyNcpClip : Choice("NcpClip") {

var shouldLag = false
private set
get() = this.handleEvents() && blink && field
get() = this.isRunning() && blink && field

val repeatable = repeatable {
val startPos = startPosition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal object FlyVerusB3869Flat : Choice("VerusB3896Flat") {
get() = ModuleFly.modes

val requiresLag
get() = this.handleEvents()
get() = this.isRunning()

val packetHandler = handler<PacketEvent> { event ->
val packet = event.packet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ internal object NoSlowBlock : ToggleableConfigurable(ModuleNoSlow, "Blocking", t
)
}

override fun handleEvents(): Boolean {
if (!super.handleEvents() || !inGame) {
override fun isRunning(): Boolean {
if (!super.isRunning() || !inGame) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal object NoSlowBlockingBlink : Choice("Blink") {
get() = modes

fun shouldLag(packet: Packet<*>?): FakeLag.LagResult? {
if (!isActive || !handleEvents() || !player.isBlocking) {
if (!isActive || !isRunning() || !player.isBlocking) {
return null
}

Expand Down
Loading

0 comments on commit 07a1c90

Please sign in to comment.