Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public abstract class GameRendererMixin {

@Inject(method = "renderLevel", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/GameRenderer;render3dHud(Lnet/minecraft/client/renderer/state/level/CameraRenderState;Lnet/minecraft/client/renderer/state/level/PlayerRenderState;Lnet/minecraft/client/renderer/state/OptionsRenderState;Z)V"))
private void onRenderLevel(CallbackInfo ci, @Local(name = "projectionMatrix") Matrix4f projectionMatrix, @Local(name = "cameraState") CameraRenderState cameraState, @Local(name = "worldPartialTicks") float worldPartialTicks, @Local(name = "bobStack") PoseStack bobStack) {
Modules.get().updateModuleSubscriptions();
if (!Utils.canUpdate()) return;

Profiler.get().push(MeteorClient.MOD_ID + "_render");
Expand Down Expand Up @@ -139,6 +140,7 @@ private void onRenderLevel(CallbackInfo ci, @Local(name = "projectionMatrix") Ma

@Inject(method = "renderLevel", at = @At("TAIL"))
private void onRenderLevelTail(CallbackInfo ci) {
Modules.get().updateModuleSubscriptions();
MeteorClient.EVENT_BUS.post(RenderAfterWorldEvent.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ private void onPreTick(CallbackInfo ci) {

startUseItemCalled = false;

Modules.get().updateModuleSubscriptions();

Profiler.get().push(MeteorClient.MOD_ID + "_pre_update");
MeteorClient.EVENT_BUS.post(TickEvent.Pre.get());
Profiler.get().pop();
Expand All @@ -127,6 +129,8 @@ private void onPreTick(CallbackInfo ci) {

@Inject(at = @At("TAIL"), method = "tick")
private void onTick(CallbackInfo ci) {
Modules.get().updateModuleSubscriptions();

Profiler.get().push(MeteorClient.MOD_ID + "_post_update");
MeteorClient.EVENT_BUS.post(TickEvent.Post.get());
Profiler.get().pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public abstract class Module implements ISerializable<Module>, Comparable<Module
public final Settings settings = new Settings();

private boolean active;
private boolean activated;
private boolean subscribed;
private boolean wantsSubscription;

public boolean serialize = true;
public boolean runInMainMenu = false;
Expand Down Expand Up @@ -94,19 +97,63 @@ public void toggle() {

settings.onActivated();

if (runInMainMenu || Utils.canUpdate()) {
if (autoSubscribe) MeteorClient.EVENT_BUS.subscribe(this);
onActivate();
}
if (runInMainMenu || Utils.canUpdate()) activate();
} else {
if (runInMainMenu || Utils.canUpdate()) {
if (autoSubscribe) MeteorClient.EVENT_BUS.unsubscribe(this);
onDeactivate();
}

active = false;
Modules.get().removeActive(this);

if (activated) deactivate();
}

Modules.get().markModuleSubscriptionsDirty();
}

void activate() {
if (activated) return;

activated = true;
if (autoSubscribe) wantsSubscription = true;
onActivate();
updateSubscription();
}

void deactivate() {
if (!activated) return;

activated = false;
onDeactivate();
if (autoSubscribe) wantsSubscription = false;
updateSubscription();
}

/** Requests event delivery without directly modifying the event bus. */
public final void subscribeToEvents() {
wantsSubscription = true;
updateSubscription();
}

/** Stops requesting event delivery without changing the module's active state. */
public final void unsubscribeFromEvents() {
wantsSubscription = false;
updateSubscription();
}

boolean wantsSubscription() {
return autoSubscribe ? active : wantsSubscription;
}

boolean isActivated() {
return activated;
}

void updateSubscription() {
boolean shouldSubscribe = wantsSubscription() && (runInMainMenu || (Modules.get().isInGame() && Utils.canUpdate()));
if (shouldSubscribe == subscribed) return;

if (shouldSubscribe) MeteorClient.EVENT_BUS.subscribe(this);
else MeteorClient.EVENT_BUS.unsubscribe(this);

subscribed = shouldSubscribe;
}

public void enable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class Modules extends System<Modules> {
private final Set<Module> active = new ReferenceOpenHashSet<>();
private @Nullable Module moduleToBind;
private boolean awaitingKeyRelease = false;
private boolean inGame, moduleSubscriptionsDirty = true, lastCanUpdate;

public Modules() {
super("modules");
Expand Down Expand Up @@ -317,24 +318,57 @@ private void onOpenScreen(OpenScreenEvent event) {
@EventHandler
private void onGameJoined(GameJoinedEvent event) {
synchronized (active) {
inGame = true;
moduleSubscriptionsDirty = true;

for (Module module : getAll()) {
if (module.isActive() && !module.runInMainMenu) {
MeteorClient.EVENT_BUS.subscribe(module);
module.onActivate();
}
if (module.isActive() && !module.runInMainMenu) module.activate();
}

updateModuleSubscriptions();
}
}

@EventHandler
private void onGameLeft(GameLeftEvent event) {
synchronized (active) {
inGame = false;
moduleSubscriptionsDirty = true;

for (Module module : getAll()) {
if (module.isActive() && !module.runInMainMenu) {
MeteorClient.EVENT_BUS.unsubscribe(module);
module.onDeactivate();
}
if (!module.runInMainMenu) module.deactivate();
}

updateModuleSubscriptions();
}
}

boolean isInGame() {
return inGame;
}

void markModuleSubscriptionsDirty() {
moduleSubscriptionsDirty = true;
}

/**
* Suspends event delivery while the player or world is unavailable without changing module
* activation state. Lifecycle callbacks are driven by explicit game join/leave events.
*/
public void updateModuleSubscriptions() {
synchronized (active) {
boolean canUpdate = inGame && Utils.canUpdate();
if (!moduleSubscriptionsDirty && canUpdate == lastCanUpdate) return;

for (Module module : getAll()) {
if (module.runInMainMenu) continue;

if (canUpdate && module.isActive() && !module.isActivated()) module.activate();
module.updateSubscription();
}

lastCanUpdate = canUpdate;
moduleSubscriptionsDirty = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package meteordevelopment.meteorclient.systems.modules.render;

import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.events.meteor.MouseScrollEvent;
import meteordevelopment.meteorclient.events.render.GetFovEvent;
import meteordevelopment.meteorclient.events.render.Render3DEvent;
Expand Down Expand Up @@ -87,7 +86,7 @@ public void onActivate() {
value = zoom.get();
time = 0.001;

MeteorClient.EVENT_BUS.subscribe(this);
subscribeToEvents();
enabled = true;
}

Expand All @@ -105,7 +104,7 @@ private void onTick(TickEvent.Post event) {
}

if (time == 0) {
MeteorClient.EVENT_BUS.unsubscribe(this);
unsubscribeFromEvents();
enabled = false;

onStop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,10 @@ public static FindItemResult find(Item... items) {
}

public static FindItemResult find(Predicate<ItemStack> isGood) {
if (mc.player == null) return new FindItemResult(0, 0);
return find(isGood, 0, mc.player.getInventory().getContainerSize());
}

public static FindItemResult find(Predicate<ItemStack> isGood, int start, int end) {
if (mc.player == null) return new FindItemResult(0, 0);

int slot = -1, count = 0;

Expand Down
Loading