Skip to content

Commit

Permalink
refactor: make stateless events singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
opZywl committed Dec 11, 2024
1 parent d26e20d commit cfe384d
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/ccbluex/liquidbounce/FDPClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ object FDPClient {
// Set is starting status
isStarting = false

callEvent(StartupEvent())
callEvent(StartupEvent)
LOGGER.info("Successfully started client")
}
}
Expand All @@ -240,7 +240,7 @@ object FDPClient {
*/
fun stopClient() {
// Call client shutdown
callEvent(ClientShutdownEvent())
callEvent(ClientShutdownEvent)

// Stop all CoroutineScopes
SharedScopes.stop()
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/net/ccbluex/liquidbounce/event/Events.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class ClickBlockEvent(val clickedBlock: BlockPos?, val enumFacing: EnumFacing?)
/**
* Called when client is shutting down
*/
class ClientShutdownEvent : Event()
object ClientShutdownEvent : Event()

/**
* Called when another entity moves
Expand Down Expand Up @@ -109,7 +109,7 @@ class MovementInputEvent(var originalInput: MovementInput) : Event()
/**
* Called in "onLivingUpdate" after when the player's sprint states are updated
*/
class PostSprintUpdateEvent : Event()
object PostSprintUpdateEvent : Event()

/**
* Called in "moveFlying"
Expand Down Expand Up @@ -163,7 +163,7 @@ class Render2DEvent(val partialTicks: Float) : Event()
/**
* Called when packets sent to client are processed
*/
class GameLoopEvent : Event()
object GameLoopEvent : Event()

/**
* Called when world is going to be rendered
Expand All @@ -184,7 +184,7 @@ class ScreenEvent(val guiScreen: GuiScreen?) : Event()
/**
* Called when the session changes
*/
class SessionEvent : Event()
object SessionUpdateEvent : Event()

/**
* Called when player is going to step
Expand All @@ -194,20 +194,20 @@ class StepEvent(var stepHeight: Float) : Event()
/**
* Called when player step is confirmed
*/
class StepConfirmEvent : Event()
object StepConfirmEvent : Event()

/**
* tick... tack... tick... tack
*/
class GameTickEvent : Event()
object GameTickEvent : Event()

class TickEndEvent : Event()
object TickEndEvent : Event()
/**
* tick tack for player
*/
class PlayerTickEvent(val state: EventState) : CancellableEvent()

class RotationUpdateEvent : Event()
object RotationUpdateEvent : Event()

class RotationSetEvent(var yawDiff: Float, var pitchDiff: Float) : CancellableEvent()
class CameraPositionEvent(
Expand All @@ -224,7 +224,7 @@ class ClientSlotChange(var supposedSlot: Int, var modifiedSlot: Int) : Event()
/**
* Called when minecraft player will be updated
*/
class UpdateEvent : Event()
object UpdateEvent : Event()

/**
* Called when the world changes
Expand All @@ -238,6 +238,6 @@ class ClickWindowEvent(val windowId: Int, val slotId: Int, val mouseButtonClicke
CancellableEvent()

/**
* Called when LiquidBounce finishes starting up
* Called when FDP finishes starting up
*/
class StartupEvent : Event()
object StartupEvent : Event()
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlinx.coroutines.launch
import net.ccbluex.liquidbounce.handler.irc.Client
import net.ccbluex.liquidbounce.handler.irc.packet.packets.*
import net.ccbluex.liquidbounce.event.EventTarget
import net.ccbluex.liquidbounce.event.SessionEvent
import net.ccbluex.liquidbounce.event.SessionUpdateEvent
import net.ccbluex.liquidbounce.event.UpdateEvent
import net.ccbluex.liquidbounce.features.module.Module
import net.ccbluex.liquidbounce.features.module.Category
Expand Down Expand Up @@ -165,7 +165,7 @@ object IRCModule : Module("IRC", Category.CLIENT, subjective = true, gameDetecti
}

@EventTarget
fun onSession(sessionEvent: SessionEvent) {
fun onSession(sessionEvent: SessionUpdateEvent) {
client.disconnect()
connect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ object KillAura : Module("KillAura", Category.COMBAT, Keyboard.KEY_G, hideModule
"OnLadder",
"InLiquid",
"InWeb"
), "Distance"
), "Armor"
)
private val targetMode by choices("TargetMode", arrayOf("Single", "Switch", "Multi"), "Switch")
private val limitedMultiTargets by int("LimitedMultiTargets", 0, 0..50) { targetMode == "Multi" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ object AutoAccount :
account.session.username, account.session.uuid,
account.session.token, account.session.type
)
callEvent(SessionEvent())
callEvent(SessionUpdateEvent)
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.google.gson.JsonParser
import kotlinx.coroutines.*
import net.ccbluex.liquidbounce.event.EventTarget
import net.ccbluex.liquidbounce.event.Listenable
import net.ccbluex.liquidbounce.event.SessionEvent
import net.ccbluex.liquidbounce.event.SessionUpdateEvent
import net.ccbluex.liquidbounce.utils.ClientUtils.LOGGER
import net.ccbluex.liquidbounce.utils.MinecraftInstance
import net.ccbluex.liquidbounce.utils.extensions.SharedScopes
Expand Down Expand Up @@ -214,7 +214,7 @@ object CapeService : Listenable, MinecraftInstance() {
* We want to immediately update the owner of the cape and refresh the cape carriers
*/
@EventTarget
fun handleNewSession(sessionEvent: SessionEvent) {
fun handleNewSession(sessionEvent: SessionUpdateEvent) {
// Check if donator cape is actually enabled and has a transfer code, also make sure the account used is premium.
val capeUser = clientCapeUser ?: return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private void init(CallbackInfo callbackInfo) {

@Inject(method = "runGameLoop", at = @At(value = "INVOKE", target = "Lnet/minecraft/profiler/Profiler;startSection(Ljava/lang/String;)V", ordinal = 1))
private void hook(CallbackInfo ci) {
EventManager.INSTANCE.callEvent(new GameLoopEvent());
EventManager.INSTANCE.callEvent(GameLoopEvent.INSTANCE);
}

@Inject(method = "startGame", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;checkGLError(Ljava/lang/String;)V", ordinal = 2, shift = At.Shift.AFTER))
Expand Down Expand Up @@ -181,12 +181,12 @@ private void injectGameRuntimeTicks(CallbackInfo ci) {

@Inject(method = "runTick", at = @At("TAIL"))
private void injectEndTickEvent(CallbackInfo ci) {
EventManager.INSTANCE.callEvent(new TickEndEvent());
EventManager.INSTANCE.callEvent(TickEndEvent.INSTANCE);
}

@Inject(method = "runTick", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;joinPlayerCounter:I", ordinal = 0))
private void onTick(final CallbackInfo callbackInfo) {
EventManager.INSTANCE.callEvent(new GameTickEvent());
EventManager.INSTANCE.callEvent(GameTickEvent.INSTANCE);
}

@Inject(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;dispatchKeypresses()V", shift = At.Shift.AFTER))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private void onUpdateWalkingPlayer(CallbackInfo ci) {

EventManager.INSTANCE.callEvent(new MotionEvent(posX, getEntityBoundingBox().minY, posZ, onGround, EventState.POST));

EventManager.INSTANCE.callEvent(new RotationUpdateEvent());
EventManager.INSTANCE.callEvent(RotationUpdateEvent.INSTANCE);

ci.cancel();
}
Expand Down Expand Up @@ -270,7 +270,7 @@ private void onPushOutOfBlocks(CallbackInfoReturnable<Boolean> callbackInfoRetur
*/
@Overwrite
public void onLivingUpdate() {
EventManager.INSTANCE.callEvent(new UpdateEvent());
EventManager.INSTANCE.callEvent(UpdateEvent.INSTANCE);

if (sprintingTicksLeft > 0) {
--sprintingTicksLeft;
Expand Down Expand Up @@ -404,7 +404,7 @@ public void onLivingUpdate() {
setSprinting(false);
}

EventManager.INSTANCE.callEvent(new PostSprintUpdateEvent());
EventManager.INSTANCE.callEvent(PostSprintUpdateEvent.INSTANCE);

sprint.correctSprintState(modifiedInput, isUsingItem);

Expand Down Expand Up @@ -661,7 +661,7 @@ public void moveEntity(double x, double y, double z) {
z = d8;
setEntityBoundingBox(axisalignedbb3);
} else {
EventManager.INSTANCE.callEvent(new StepConfirmEvent());
EventManager.INSTANCE.callEvent(StepConfirmEvent.INSTANCE);
}
}

Expand Down Expand Up @@ -778,7 +778,7 @@ private void preTickEvent(CallbackInfo ci) {
EventManager.INSTANCE.callEvent(tickEvent);

if (tickEvent.isCancelled()) {
EventManager.INSTANCE.callEvent(new RotationUpdateEvent());
EventManager.INSTANCE.callEvent(RotationUpdateEvent.INSTANCE);
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import me.liuli.elixir.account.MinecraftAccount;
import net.ccbluex.liquidbounce.event.EventManager;
import net.ccbluex.liquidbounce.event.SessionEvent;
import net.ccbluex.liquidbounce.event.SessionUpdateEvent;
import net.ccbluex.liquidbounce.handler.other.AutoReconnect;
import net.ccbluex.liquidbounce.handler.payload.ClientFixes;
import net.ccbluex.liquidbounce.file.FileManager;
Expand Down Expand Up @@ -75,7 +75,7 @@ private void actionPerformed(GuiButton button, CallbackInfo callbackInfo) throws

mc.displayGuiScreen(new GuiLoginProgress(minecraftAccount, () -> {
mc.addScheduledTask(() -> {
EventManager.INSTANCE.callEvent(new SessionEvent());
EventManager.INSTANCE.callEvent(SessionUpdateEvent.INSTANCE);
ServerUtils.INSTANCE.connectToLastServer();
});
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ScriptModule(name: String, category: Category, description: String, privat
fun onWorld(worldEvent: WorldEvent) = callEvent("world", worldEvent)

@EventTarget
fun onSession(sessionEvent: SessionEvent) = callEvent("session")
fun onSession(sessionEvent: SessionUpdateEvent) = callEvent("session")

@EventTarget
fun onClickBlock(clickBlockEvent: ClickBlockEvent) = callEvent("clickBlock", clickBlockEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import me.liuli.elixir.account.MinecraftAccount
import me.liuli.elixir.account.MojangAccount
import net.ccbluex.liquidbounce.FDPClient.CLIENT_CLOUD
import net.ccbluex.liquidbounce.event.EventManager.callEvent
import net.ccbluex.liquidbounce.event.SessionEvent
import net.ccbluex.liquidbounce.event.SessionUpdateEvent
import net.ccbluex.liquidbounce.features.module.modules.client.HUDModule.guiColor
import net.ccbluex.liquidbounce.file.FileManager.accountsConfig
import net.ccbluex.liquidbounce.file.FileManager.saveConfig
Expand Down Expand Up @@ -440,7 +440,7 @@ class GuiAltManager(private val prevGui: GuiScreen) : GuiScreen() {
minecraftAccount.session.token,
"microsoft"
)
callEvent(SessionEvent())
callEvent(SessionUpdateEvent)

success()
} catch (exception: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package net.ccbluex.liquidbounce.ui.client.altmanager.menus

import me.liuli.elixir.account.CrackedAccount
import net.ccbluex.liquidbounce.event.EventManager.callEvent
import net.ccbluex.liquidbounce.event.SessionEvent
import net.ccbluex.liquidbounce.event.SessionUpdateEvent
import net.ccbluex.liquidbounce.features.module.modules.client.HUDModule.guiColor
import net.ccbluex.liquidbounce.file.FileManager.accountsConfig
import net.ccbluex.liquidbounce.file.FileManager.saveConfig
Expand Down Expand Up @@ -171,7 +171,7 @@ class GuiLoginIntoAccount(private val prevGui: GuiAltManager, val directLogin: B
crackedAccount.session.username, crackedAccount.session.uuid,
crackedAccount.session.token, crackedAccount.session.type
)
callEvent(SessionEvent())
callEvent(SessionUpdateEvent)
status = "§aLogged into §f§l${mc.session.username}§a."
} else {
accountsConfig.addAccount(crackedAccount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package net.ccbluex.liquidbounce.utils.login

import com.google.gson.JsonParser
import net.ccbluex.liquidbounce.event.EventManager.callEvent
import net.ccbluex.liquidbounce.event.SessionEvent
import net.ccbluex.liquidbounce.event.SessionUpdateEvent
import net.ccbluex.liquidbounce.utils.MinecraftInstance
import net.minecraft.util.Session
import java.util.*
Expand Down Expand Up @@ -48,7 +48,7 @@ object LoginUtils : MinecraftInstance() {
return LoginResult.INVALID_ACCOUNT_DATA
}

callEvent(SessionEvent())
callEvent(SessionUpdateEvent)

return LoginResult.LOGGED
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package net.ccbluex.liquidbounce.utils.misc

import me.liuli.elixir.account.CrackedAccount
import net.ccbluex.liquidbounce.event.EventManager.callEvent
import net.ccbluex.liquidbounce.event.SessionEvent
import net.ccbluex.liquidbounce.event.SessionUpdateEvent
import net.ccbluex.liquidbounce.ui.client.gui.GuiClientConfiguration
import net.ccbluex.liquidbounce.utils.MinecraftInstance.Companion.mc
import net.minecraft.util.Session
Expand Down Expand Up @@ -51,7 +51,7 @@ object RandomUtils {
crackedAccount.session.token, crackedAccount.session.type
)

callEvent(SessionEvent())
callEvent(SessionUpdateEvent)
}

return crackedAccount
Expand Down

0 comments on commit cfe384d

Please sign in to comment.