Skip to content

Commit

Permalink
improve auto_on
Browse files Browse the repository at this point in the history
should remove an edge case, where the overlay wouldn't turn off at all
anymore.
  • Loading branch information
andi-makes committed Aug 12, 2024
1 parent 5fecd28 commit b04c46a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 54 deletions.
33 changes: 4 additions & 29 deletions common/src/main/java/dev/schmarrn/lighty/ModeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import dev.schmarrn.lighty.api.LightyMode;
import dev.schmarrn.lighty.config.Config;
import dev.schmarrn.lighty.event.Compute;
import dev.schmarrn.lighty.overlaystate.SMACH;
import dev.schmarrn.lighty.overlaystate.State;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

Expand All @@ -26,9 +28,6 @@ public class ModeLoader {
@Nullable
private static LightyMode mode = null;

private static boolean enabled = false;
private static boolean autoEnabled = false;

private static final HashMap<ResourceLocation, LightyMode> MODES = new HashMap<>();

public static void loadMode(ResourceLocation id) {
Expand All @@ -44,40 +43,16 @@ public static void loadMode(ResourceLocation id) {
Compute.clear();
}

public static void disable() {
setEnabled(false);
}

public static void setAutoEnabled() {
autoEnabled = true;
}

public static void setAutoDisabled() {
autoEnabled = false;
}

public static void enable() {
setEnabled(true);
}

public static void toggle() {
enabled = !enabled;
SMACH.toggle();
}

public static void put(ResourceLocation id, LightyMode mode) {
MODES.put(id, mode);
}

public static boolean isEnabled() {
return enabled || autoEnabled;
}

public static boolean isManuallyEnabled() {
return enabled;
}

public static void setEnabled(boolean val) {
enabled = val;
return SMACH.getState() == State.AUTO || SMACH.getState() == State.ON;
}

public static LightyMode getCurrentMode() {
Expand Down
30 changes: 5 additions & 25 deletions common/src/main/java/dev/schmarrn/lighty/event/Compute.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@
import dev.schmarrn.lighty.ModeLoader;
import dev.schmarrn.lighty.api.LightyMode;
import dev.schmarrn.lighty.config.Config;
import dev.schmarrn.lighty.overlaystate.SMACH;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.ShaderInstance;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.util.Mth;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -121,29 +120,10 @@ private static BufferHolder buildChunk(LightyMode mode, SectionPos chunkPos, Tes
public static void computeCache(Minecraft client) {
if (client.player == null) return;

if (Config.SHOULD_AUTO_ON.getValue()) {
var activationItems = Config.AUTO_ON_ITEM_LIST.getValue();
boolean isAutoOn = false;
for (var rl : activationItems) {
Item activationItem = BuiltInRegistries.ITEM.get(rl);
Item mainHandItem = client.player.getMainHandItem().getItem();
Item offHandItem = client.player.getOffhandItem().getItem();

// if we hold the activation item in our hands, set auto enabled to true.
// if we don't, set it to false and if we aren't enabled, return early.
if (mainHandItem == activationItem || offHandItem == activationItem) {
ModeLoader.setAutoEnabled();
isAutoOn = true;
break;
}
}
if (!isAutoOn) {
ModeLoader.setAutoDisabled();
if (!ModeLoader.isManuallyEnabled()) {
return;
}
}
} else if (!ModeLoader.isEnabled()) {
// update state machine state that's based on items etc
SMACH.updateCompute(client);

if (!ModeLoader.isEnabled()) {
return;
}
LightyMode mode = ModeLoader.getCurrentMode();
Expand Down
68 changes: 68 additions & 0 deletions common/src/main/java/dev/schmarrn/lighty/overlaystate/SMACH.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dev.schmarrn.lighty.overlaystate;

import dev.schmarrn.lighty.config.Config;
import net.minecraft.client.Minecraft;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.Item;

public class SMACH {
private static State state = State.OFF;

public static void updateCompute(Minecraft client) {
if (!Config.SHOULD_AUTO_ON.getValue()) {
if (state == State.AUTO || state == State.OVERRIDE) {
// if auto_on is disabled, but we somehow got stuck inside of one of the auto_on states, reset to OFF
state = State.OFF;
}
return;
}
// check whether we are holding an item defined as a valid auto_on item
boolean holdsItem = false;
var activationItems = Config.AUTO_ON_ITEM_LIST.getValue();

for (var rl : activationItems) {
Item activationItem = BuiltInRegistries.ITEM.get(rl);
Item mainHandItem = client.player.getMainHandItem().getItem();
Item offHandItem = client.player.getOffhandItem().getItem();

// if we hold the activation item in our hands, set auto enabled to true.
// if we don't, set it to false and if we aren't enabled, return early.
if (mainHandItem == activationItem || offHandItem == activationItem) {
holdsItem = true;
break;
}
}

// update state:
switch (state) {
case OFF -> {
if (holdsItem) {
state = State.AUTO;
}
}
case ON -> {
// we stay in on, because auto_on should never override on state
}
case AUTO, OVERRIDE -> {
// in both cases AUTO and OVERRIDE, if we let go of the item, we reset to state OFF
if (!holdsItem) {
state = State.OFF;
}
}
}

}

public static void toggle() {
switch (state) {
case OFF -> state = State.ON;
case ON -> state = State.OFF;
case AUTO -> state = State.OVERRIDE;
case OVERRIDE -> state = State.AUTO;
}
}

public static State getState() {
return state;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.schmarrn.lighty.overlaystate;

public enum State {
OFF,
ON,
AUTO,
OVERRIDE
}

0 comments on commit b04c46a

Please sign in to comment.