Skip to content

Commit

Permalink
Add powder mining tracker (#1065)
Browse files Browse the repository at this point in the history
* Add chat events

* Add powder mining tracker

* Add ON_PRICE_UPDATE event and re-calculate profit after prices are updated

* Initial config

* Add config screens for filtering the shown items

* Fix Done button width being only 1 col wide when it's supposed to be 2

* Change to regex because index magic wasn't magic-ing

Regex my beloved

* Read and write from file and fix incorrect param used in recalculateAll

Also a small fix for the regex

* Change literal text to translatable

* Extract `ON_PRICE_UPDATE` event to a separate class under the `events` package

* Simplify switch to use pattern matching

* Extract render and chat message lambdas to methods

* Add PROFILE_INIT event

* Add support for multiple profiles

* Re-add ItemPrice#init with additional documentation

* Prevent rendering profit if there are no rewards

* Format item amounts when rendering

* Add more documentation
  • Loading branch information
Emirlol authored Jan 1, 2025
1 parent 8d879bd commit a09c6ee
Show file tree
Hide file tree
Showing 14 changed files with 734 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import de.hysky.skyblocker.config.ConfigUtils;
import de.hysky.skyblocker.config.SkyblockerConfig;
import de.hysky.skyblocker.config.configs.MiningConfig;
import de.hysky.skyblocker.config.screens.powdertracker.PowderFilterConfigScreen;
import de.hysky.skyblocker.skyblock.dwarven.CrystalsHudWidget;
import de.hysky.skyblocker.skyblock.dwarven.CarpetHighlighter;
import de.hysky.skyblocker.skyblock.dwarven.PowderMiningTracker;
import dev.isxander.yacl3.api.*;
import dev.isxander.yacl3.api.controller.ColorControllerBuilder;
import de.hysky.skyblocker.skyblock.tabhud.config.WidgetsConfigurationScreen;
import de.hysky.skyblocker.utils.Location;
import dev.isxander.yacl3.api.controller.FloatFieldControllerBuilder;
import dev.isxander.yacl3.api.controller.IntegerSliderControllerBuilder;
import it.unimi.dsi.fastutil.objects.ObjectImmutableList;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;

Expand Down Expand Up @@ -112,6 +115,12 @@ public static ConfigCategory create(SkyblockerConfig defaults, SkyblockerConfig
newValue -> config.mining.crystalHollows.chestHighlightColor = newValue)
.controller(v -> ColorControllerBuilder.create(v).allowAlpha(true))
.build())
.option(ButtonOption.createBuilder()
.name(Text.translatable("skyblocker.config.mining.crystalHollows.powderTrackerFilter"))
.description(OptionDescription.of(Text.translatable("skyblocker.config.mining.crystalHollows.powderTrackerFilter.@Tooltip")))
.text(Text.translatable("text.skyblocker.open"))
.action((screen, opt) -> MinecraftClient.getInstance().setScreen(new PowderFilterConfigScreen(screen, new ObjectImmutableList<>(PowderMiningTracker.getName2IdMap().keySet()))))
.build())
.build())

//Crystal Hollows Map
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/de/hysky/skyblocker/config/configs/MiningConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.minecraft.client.resource.language.I18n;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class MiningConfig {
@SerialEntry
Expand Down Expand Up @@ -83,6 +85,15 @@ public static class CrystalHollows {

@SerialEntry
public Color chestHighlightColor = new Color(0, 0, 255, 128);

@SerialEntry
public boolean enablePowderTracker = true;

@SerialEntry
public boolean countNaturalChestsInTracker = true;

@SerialEntry
public List<String> powderTrackerFilter = new ArrayList<>();
}

public static class CrystalsHud {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package de.hysky.skyblocker.config.screens.powdertracker;

import de.hysky.skyblocker.mixins.accessors.CheckboxWidgetAccessor;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.widget.CheckboxWidget;
import net.minecraft.client.gui.widget.ElementListWidget;
import net.minecraft.text.Text;

import java.util.List;

public class ItemTickList extends ElementListWidget<ItemTickList.ItemTickEntry> {
private final List<String> filters;
private final List<String> allItems;

public ItemTickList(MinecraftClient minecraftClient, int width, int height, int y, int entryHeight, List<String> filters, List<String> allItems) {
super(minecraftClient, width, height, y, entryHeight);
this.filters = filters;
this.allItems = allItems;
}

public void clearAndInit() {
clearEntries();
init();
}

public ItemTickList init() {
for (String item : allItems) {
ItemTickEntry entry = new ItemTickEntry(
CheckboxWidget.builder(Text.of(item), client.textRenderer)
.checked(!filters.contains(item))
.callback((checkbox1, checked) -> {
if (checked) filters.remove(item);
else filters.add(item);
})
.build()
);
addEntry(entry);
}
return this;
}

public static class ItemTickEntry extends ElementListWidget.Entry<ItemTickEntry> {
private final List<CheckboxWidget> children;

ItemTickEntry(CheckboxWidget checkboxWidget) {
children = List.of(checkboxWidget);
}

public void setChecked(boolean checked) {
for (CheckboxWidget child : children) {
((CheckboxWidgetAccessor) child).setChecked(checked);
}
}

@Override
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
for (CheckboxWidget child : children) {
child.setX(x);
child.setY(y);
child.setWidth(entryWidth);
child.setHeight(entryHeight);
child.render(context, mouseX, mouseY, tickDelta);
}
}

@Override
public List<? extends Selectable> selectableChildren() {
return children;
}

@Override
public List<? extends Element> children() {
return children;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package de.hysky.skyblocker.config.screens.powdertracker;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.dwarven.PowderMiningTracker;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.GridWidget;
import net.minecraft.client.gui.widget.SimplePositioningWidget;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class PowderFilterConfigScreen extends Screen {
@Nullable
private final Screen parent;
private final List<String> filters;
private final List<String> allItems;

public PowderFilterConfigScreen(@Nullable Screen parent, List<String> allItems) {
super(Text.of("Powder Mining Tracker Filter Config"));
this.parent = parent;
this.filters = new ArrayList<>(SkyblockerConfigManager.get().mining.crystalHollows.powderTrackerFilter); // Copy the list so we can undo changes when necessary
this.allItems = allItems;
}

@Override
protected void init() {
addDrawable((context, mouseX, mouseY, delta) -> {
assert client != null;
context.drawCenteredTextWithShadow(client.textRenderer, Text.translatable("skyblocker.config.mining.crystalHollows.powderTrackerFilter.screenTitle").formatted(Formatting.BOLD), width / 2, (32 - client.textRenderer.fontHeight) / 2, 0xFFFFFF);
});
ItemTickList itemTickList = addDrawableChild(new ItemTickList(MinecraftClient.getInstance(), width, height - 96, 32, 24, filters, allItems).init());
//Grid code gratuitously stolen from WaypointsScreen. Same goes for the y and heights above.
GridWidget gridWidget = new GridWidget();
gridWidget.getMainPositioner().marginX(5).marginY(2);
GridWidget.Adder adder = gridWidget.createAdder(2);

adder.add(ButtonWidget.builder(Text.translatable("text.skyblocker.reset"), button -> {
filters.clear();
itemTickList.clearAndInit();
}).build());
adder.add(ButtonWidget.builder(Text.translatable("text.skyblocker.undo"), button -> {
filters.clear();
filters.addAll(SkyblockerConfigManager.get().mining.crystalHollows.powderTrackerFilter);
itemTickList.clearAndInit();
}).build());
adder.add(ButtonWidget.builder(ScreenTexts.DONE, button -> {
saveFilters();
close();
})
.width((ButtonWidget.DEFAULT_WIDTH * 2) + 10)
.build(), 2);
gridWidget.refreshPositions();
SimplePositioningWidget.setPos(gridWidget, 0, this.height - 64, this.width, 64);
gridWidget.forEachChild(this::addDrawableChild);
}

public void saveFilters() {
SkyblockerConfigManager.get().mining.crystalHollows.powderTrackerFilter = filters;
SkyblockerConfigManager.save();
PowderMiningTracker.recalculateAll();
}

@Override
public void close() {
assert client != null;
client.setScreen(parent);
}
}
46 changes: 46 additions & 0 deletions src/main/java/de/hysky/skyblocker/events/ChatEvents.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package de.hysky.skyblocker.events;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Environment(EnvType.CLIENT)
public class ChatEvents {
/**
* This will be called when a game message is received, cancelled or not.
*
* @implNote Not fired when {@code overlay} is {@code true}. See {@link de.hysky.skyblocker.mixins.MessageHandlerMixin#skyblocker$monitorGameMessage(Text, boolean, CallbackInfo) the mixin} for more information.
*/
@SuppressWarnings("JavadocReference")
public static final Event<ChatTextEvent> RECEIVE_TEXT = EventFactory.createArrayBacked(ChatTextEvent.class, listeners -> message -> {
for (ChatTextEvent listener : listeners) {
listener.onMessage(message);
}
});

/**
* This will be called when a game message is received, cancelled or not.
* This method is called with the result of {@link Text#getString()} to avoid each listener having to call it.
*
* @implNote Not fired when {@code overlay} is {@code true}. See {@link de.hysky.skyblocker.mixins.MessageHandlerMixin#skyblocker$monitorGameMessage(Text, boolean, CallbackInfo) the mixin} for more information.
*/
@SuppressWarnings("JavadocReference")
public static final Event<ChatStringEvent> RECEIVE_STRING = EventFactory.createArrayBacked(ChatStringEvent.class, listeners -> message -> {
for (ChatStringEvent listener : listeners) {
listener.onMessage(message);
}
});

@FunctionalInterface
public interface ChatTextEvent {
void onMessage(Text message);
}

@FunctionalInterface
public interface ChatStringEvent {
void onMessage(String message);
}
}
21 changes: 21 additions & 0 deletions src/main/java/de/hysky/skyblocker/events/ItemPriceUpdateEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.hysky.skyblocker.events;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;

@FunctionalInterface
@Environment(EnvType.CLIENT)
public interface ItemPriceUpdateEvent {
void onPriceUpdate();

/**
* An event that is fired when all prices are updated.
*/
Event<ItemPriceUpdateEvent> ON_PRICE_UPDATE = EventFactory.createArrayBacked(ItemPriceUpdateEvent.class, listeners -> () -> {
for (ItemPriceUpdateEvent listener : listeners) {
listener.onPriceUpdate();
}
});
}
Loading

0 comments on commit a09c6ee

Please sign in to comment.