Skip to content

Commit

Permalink
feat: add powder tracker hud
Browse files Browse the repository at this point in the history
  • Loading branch information
Morazzer committed Nov 16, 2024
1 parent f5f3712 commit 80c0760
Show file tree
Hide file tree
Showing 37 changed files with 880 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,34 @@ private void addMiningConfig(CookiesTranslationBuilder translationBuilder) {
translationBuilder.addConfig(CONFIG_MINING_SHAFT_BOX, "Enable box", "Renders a box under the corpse's location!");
translationBuilder.addConfig(CONFIG_MINING_SHAFT_BEAM, "Enable beacon beam", "Renders a beacon beam at the corpse's location!");
translationBuilder.addConfig(CONFIG_MINING_SHAFT_COLOR, "Highlight color", "Allows you to change the highlight color that is used for the helper.\n\n§eOnly works for new shafts!");

translationBuilder.addConfig(CONFIG_MINING_POWDER_TRACKER, "Powder Tracker", "Settings related to powder tracker hud");
translationBuilder.addConfig(CONFIG_MINING_POWDER_TRACKER_ENABLED, "Enable", "Enables the powder tracker!");
translationBuilder.addConfig(CONFIG_MINING_POWDER_TRACKER_SHOW_MS, "Show ms", "Adds milliseconds to the timer");
translationBuilder.addConfig(
CONFIG_MINING_POWDER_TRACKER_PAUSE,
"Pause Timer",
"Pauses the timer after a set period of time");
translationBuilder.addConfig(
CONFIG_MINING_POWDER_TRACKER_PAUSE_VALUE,
"Pause Delay",
"The delay to pause after!");
translationBuilder.addConfig(
CONFIG_MINING_POWDER_TRACKER_EXTRA_DATA, "Show extra data", """
Shows extra data for specific powder types.
Gemstone: chests/min
Glacite: shafts/min""");
translationBuilder.addConfig(
CONFIG_MINING_POWDER_TRACKER_SHAFT_TRACKING, "Shaft tracking", """
Changes how to track mineshafts.
Enter -> Whenever you enter a mineshafts (counts all)
Find -> Counts mineshafts found, even not entered ones""");
translationBuilder.addConfig(
CONFIG_MINING_POWDER_TRACKER_TIMEOUT,
"Timeout time",
"Changes the time the tracker can be inactive for, before resetting!");
}

private void addMiscConfig(CookiesTranslationBuilder translationBuilder) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21.3 2024-11-14T20:23:33.392554 cookies-mod/Language (en_us)
6b9d87aa8fb48ba3b4695f5acf5f091d2e8d2309 assets/cookies-mod/lang/en_us.json
// 1.21.3 2024-11-16T10:39:51.468308 cookies-mod/Language (en_us)
e62a557eb365acc40bf06ab25327f14f87a7fd60 assets/cookies-mod/lang/en_us.json
16 changes: 16 additions & 0 deletions src/main/generated/assets/cookies-mod/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@
"cookies.config.mining.modify_commissions.name": "Modify commission items",
"cookies.config.mining.modify_commissions.tooltip": "Visually changes the commission item to represent the stages a commission can be in.",
"cookies.config.mining.name": "Mining",
"cookies.config.mining.power_tracker.enabled.name": "Enable",
"cookies.config.mining.power_tracker.enabled.tooltip": "Enables the powder tracker!",
"cookies.config.mining.power_tracker.extra_data.name": "Show extra data",
"cookies.config.mining.power_tracker.extra_data.tooltip": "Shows extra data for specific powder types.\n\nGemstone: chests/min\nGlacite: shafts/min",
"cookies.config.mining.power_tracker.name": "Powder Tracker",
"cookies.config.mining.power_tracker.pause.name": "Pause Timer",
"cookies.config.mining.power_tracker.pause.tooltip": "Pauses the timer after a set period of time",
"cookies.config.mining.power_tracker.pause_value.name": "Pause Delay",
"cookies.config.mining.power_tracker.pause_value.tooltip": "The delay to pause after!",
"cookies.config.mining.power_tracker.shaft_tracking.name": "Shaft tracking",
"cookies.config.mining.power_tracker.shaft_tracking.tooltip": "Changes how to track mineshafts.\n\nEnter -> Whenever you enter a mineshafts (counts all)\nFind -> Counts mineshafts found, even not entered ones",
"cookies.config.mining.power_tracker.show_ms.name": "Show ms",
"cookies.config.mining.power_tracker.show_ms.tooltip": "Adds milliseconds to the timer",
"cookies.config.mining.power_tracker.timeout.name": "Timeout time",
"cookies.config.mining.power_tracker.timeout.tooltip": "Changes the time the tracker can be inactive for, before resetting!",
"cookies.config.mining.power_tracker.tooltip": "Settings related to powder tracker hud",
"cookies.config.mining.puzzler_solver.name": "Puzzler solver",
"cookies.config.mining.puzzler_solver.tooltip": "Highlight the correct block for the puzzler.",
"cookies.config.mining.shaft.beam.name": "Enable beacon beam",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/codes/cookies/mod/config/CookiesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import codes.cookies.mod.config.categories.FarmingConfig;
import codes.cookies.mod.config.categories.HelpersConfig;
import codes.cookies.mod.config.categories.ItemSearchConfig;
import codes.cookies.mod.config.categories.MiningConfig;
import codes.cookies.mod.config.categories.mining.MiningConfig;
import codes.cookies.mod.config.categories.MiscConfig;
import codes.cookies.mod.config.system.Config;
import codes.cookies.mod.config.system.SearchCategory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package codes.cookies.mod.config.categories;
package codes.cookies.mod.config.categories.mining;

import codes.cookies.mod.config.ConfigManager;
import codes.cookies.mod.config.categories.mining.powder.PowderTrackerHudFoldable;
import codes.cookies.mod.config.system.Category;
import codes.cookies.mod.config.system.Foldable;
import codes.cookies.mod.config.system.Parent;
import codes.cookies.mod.config.system.Row;
import codes.cookies.mod.config.system.options.BooleanOption;
import codes.cookies.mod.config.system.options.ColorOption;
import codes.cookies.mod.config.system.options.TextDisplayOption;
import codes.cookies.mod.features.mining.hollows.MinesOfDivanHelper;

import java.awt.*;
import java.util.function.Predicate;

import codes.cookies.mod.utils.cookies.Constants;

import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;

Expand All @@ -32,6 +28,7 @@ public class MiningConfig extends Category {
.withCallback(MinesOfDivanHelper::reset);

public ShaftConfig shaftConfig = new ShaftConfig();
public PowderTrackerHudFoldable powderTrackerHud = new PowderTrackerHudFoldable();

@Parent
public TextDisplayOption hotmParentDisplay = new TextDisplayOption(CONFIG_MINING_CATEGORIES_HOTM);
Expand All @@ -52,22 +49,6 @@ public static MiningConfig getInstance() {
return ConfigManager.getConfig().miningConfig;
}

public static class ShaftConfig extends Foldable {

public BooleanOption enable = new BooleanOption(CONFIG_MINING_SHAFT_ENABLE, true);
public BooleanOption text = new BooleanOption(CONFIG_MINING_SHAFT_TEXT, true).onlyIf(enable);
public BooleanOption box = new BooleanOption(CONFIG_MINING_SHAFT_BOX, true).onlyIf(enable);
public BooleanOption beam = new BooleanOption(CONFIG_MINING_SHAFT_BEAM, true).onlyIf(enable);
public ColorOption color = new ColorOption(CONFIG_MINING_SHAFT_COLOR, new Color(Constants.MAIN_COLOR))
.withAlpha().onlyIf(enable);

@Override
public String getName() {
return CONFIG_MINING_SHAFT;
}
}


@Override
public Row getRow() {
return Row.TOP;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package codes.cookies.mod.config.categories.mining;

import codes.cookies.mod.config.system.Foldable;
import codes.cookies.mod.config.system.options.BooleanOption;
import codes.cookies.mod.config.system.options.ColorOption;
import codes.cookies.mod.translations.TranslationKeys;
import codes.cookies.mod.utils.cookies.Constants;

import java.awt.*;

public class ShaftConfig extends Foldable {

public BooleanOption enable = new BooleanOption(TranslationKeys.CONFIG_MINING_SHAFT_ENABLE, true);
public BooleanOption text = new BooleanOption(TranslationKeys.CONFIG_MINING_SHAFT_TEXT, true).onlyIf(enable);
public BooleanOption box = new BooleanOption(TranslationKeys.CONFIG_MINING_SHAFT_BOX, true).onlyIf(enable);
public BooleanOption beam = new BooleanOption(TranslationKeys.CONFIG_MINING_SHAFT_BEAM, true).onlyIf(enable);
public ColorOption color = new ColorOption(
TranslationKeys.CONFIG_MINING_SHAFT_COLOR,
new Color(Constants.MAIN_COLOR))
.withAlpha().onlyIf(enable);

@Override
public String getName() {
return TranslationKeys.CONFIG_MINING_SHAFT;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package codes.cookies.mod.config.categories.mining.powder;

public enum PauseTime {

TEN_SEC(10), THIRTY_SEC(30), ONE_MIN(60), TWO_MIN(120), FIVE_MIN(300), TEN_MIN(600);

private final int timeInSec;

PauseTime(int timeInSec) {
this.timeInSec = timeInSec;
}

public long getTimeInMilliseconds() {
return timeInSec * 1000L;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package codes.cookies.mod.config.categories.mining.powder;

import codes.cookies.mod.config.categories.mining.MiningConfig;
import codes.cookies.mod.config.system.Foldable;
import codes.cookies.mod.config.system.HudSetting;
import codes.cookies.mod.config.system.options.BooleanOption;
import codes.cookies.mod.config.system.options.EnumCycleOption;
import codes.cookies.mod.features.mining.PowderHud;

public class PowderTrackerHudFoldable extends Foldable {

public static PowderTrackerHudFoldable getConfig() {
return MiningConfig.getInstance().powderTrackerHud;
}

@HudSetting(PowderHud.class)
public BooleanOption enableHud = new BooleanOption(CONFIG_MINING_POWDER_TRACKER_ENABLED, true);
public BooleanOption showMs = new BooleanOption(CONFIG_MINING_POWDER_TRACKER_SHOW_MS, false);
@HudSetting(PowderHud.class)
public BooleanOption pauseTimer = new BooleanOption(CONFIG_MINING_POWDER_TRACKER_PAUSE, true);
@HudSetting(PowderHud.class)
public EnumCycleOption<PauseTime> pauseAfter = new EnumCycleOption<>(CONFIG_MINING_POWDER_TRACKER_PAUSE_VALUE, PauseTime.TEN_SEC);
@HudSetting(PowderHud.class)
public BooleanOption showExtraData = new BooleanOption(CONFIG_MINING_POWDER_TRACKER_EXTRA_DATA, true);
@HudSetting(PowderHud.class)
public EnumCycleOption<ShaftTrackingType> trackingType = new EnumCycleOption<>(CONFIG_MINING_POWDER_TRACKER_SHAFT_TRACKING, ShaftTrackingType.ENTER);
@HudSetting(PowderHud.class)
public EnumCycleOption<TimeoutTime> timeoutTime = new EnumCycleOption<>(CONFIG_MINING_POWDER_TRACKER_TIMEOUT, TimeoutTime.TEN_MIN);

{
// registering here because order of registration equals call order
pauseAfter.onlyIf(pauseTimer).onlyIf(enableHud);
trackingType.onlyIf(showExtraData).onlyIf(enableHud);

pauseTimer.onlyIf(enableHud);
timeoutTime.onlyIf(enableHud);
showExtraData.onlyIf(enableHud);
}
public long getTimeoutTime() {
return Math.max(pauseAfter.getValue().getTimeInMilliseconds(), timeoutTime.getValue().getTimeInMilliseconds());
}

public long getPauseTime() {
return pauseAfter.getValue().getTimeInMilliseconds();
}

@Override
public String getName() {
return CONFIG_MINING_POWDER_TRACKER;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package codes.cookies.mod.config.categories.mining.powder;

public enum ShaftTrackingType {

ENTER, FIND

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package codes.cookies.mod.config.categories.mining.powder;

public enum TimeoutTime {

ONE_MIN(1), TWO_MINE(2), FIVE_MIN(5), TEN_MIN(10), TWENTY_MIN(20);

private final int timeInMinutes;

TimeoutTime(int timeInMinutes) {
this.timeInMinutes = timeInMinutes;
}

public long getTimeInMilliseconds() {
return timeInMinutes * 60 * 1000L;
}

}
3 changes: 2 additions & 1 deletion src/main/java/codes/cookies/mod/config/system/Foldable.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package codes.cookies.mod.config.system;

import codes.cookies.mod.translations.TranslationKeys;
import com.google.gson.JsonElement;

/**
* A foldable that will be displayed as such in the config.
*/
public abstract class Foldable implements SaveLoadHelper {
public abstract class Foldable implements SaveLoadHelper, TranslationKeys {

/**
* Gets the display name of the foldable.
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/codes/cookies/mod/config/system/Option.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ public boolean canBeSerialized() {
* @return The option.
*/
public O onlyIf(BooleanOption booleanOption) {
this.active = booleanOption.active;
booleanOption.withCallback((oldValue, newValue) -> this.active = newValue);
setActive(booleanOption.active);
booleanOption.withCallback((oldValue, newValue) -> setActive(newValue));
return this.asOption();
}

Expand Down Expand Up @@ -194,11 +194,15 @@ public final O withCallback(@NotNull Runnable runnable) {
* @return The option.
*/
public O onlyIfNot(BooleanOption booleanOption) {
this.active = !booleanOption.active;
booleanOption.withCallback((oldValue, newValue) -> this.active = !newValue);
setActive(!booleanOption.active);
booleanOption.withCallback((oldValue, newValue) -> setActive(!newValue));
return this.asOption();
}

protected void setActive(boolean active) {
this.active = active;
}

protected boolean expectPrimitiveNumber(@NotNull JsonElement jsonElement, Logger log) {
if (!jsonElement.isJsonPrimitive()) {
log.warn("Error while loading config value, expected number got %s".formatted(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public BooleanOption(@NotNull @TranslationKey String translationKey, Boolean val
super(translationKey, value);
}

@Override
public void setActive(boolean active) {
super.active = active;
updateCallbacks(this.value);
}

@Override
public void read(@NotNull JsonElement jsonElement) {
if (jsonElement instanceof JsonObject jsonObject) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.minecraft.text.Text;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

Expand Down
67 changes: 67 additions & 0 deletions src/main/java/codes/cookies/mod/data/mining/PowderData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package codes.cookies.mod.data.mining;

import java.util.HashMap;
import java.util.Map;

import codes.cookies.mod.events.mining.PowderUpdateEvent;
import codes.cookies.mod.utils.json.CodecJsonSerializable;
import com.mojang.serialization.Codec;
import org.slf4j.Logger;

public class PowderData implements CodecJsonSerializable<Map<PowderType, Integer>> {

public static final Codec<Map<PowderType, Integer>> CODEC = Codec.unboundedMap(PowderType.CODEC, Codec.INT);
private final Map<PowderType, Integer> trackedPowder = new HashMap<>();

public void update(PowderType powderType, int amount) {
if (trackedPowder.containsKey(powderType)) {
final int old = this.remove(powderType);
int delta = amount - old;
trackedPowder.put(powderType, amount);
this.broadcastUpdate(powderType, amount, delta);
return;
}

this.trackedPowder.put(powderType, amount);
this.broadcastUpdate(powderType, amount, 0);
}

private void broadcastUpdate(PowderType powderType, int amount, int delta) {
if (delta == 0) {
return;
}
PowderUpdateEvent.EVENT.invoker().update(powderType, amount, delta);
}

private int remove(PowderType powderType) {
final Integer remove = trackedPowder.remove(powderType);
if (remove == null) {
return 0;
}
return remove;
}

@Override
public Codec<Map<PowderType, Integer>> getCodec() {
return CODEC;
}

@Override
public void load(Map<PowderType, Integer> value) {
trackedPowder.putAll(value);
}

@Override
public Map<PowderType, Integer> getValue() {
return trackedPowder;
}

@Override
public Logger getLogger() {
return logger;
}

public void reset() {
this.trackedPowder.clear();
}
}
Loading

0 comments on commit 80c0760

Please sign in to comment.