Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add powder mining tracker #1065

Merged
merged 18 commits into from
Jan 1, 2025
Merged
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 @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for making chat events as opposed to using Fabric's ones?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fabric's injection is like this:

if (ClientReceiveMessageEvents.ALLOW_GAME.invoker().allowReceiveGameMessage(message.get(), overlay)) {
	message.set(ClientReceiveMessageEvents.MODIFY_GAME.invoker().modifyReceivedGameMessage(message.get(), overlay));
	ClientReceiveMessageEvents.GAME.invoker().onReceiveGameMessage(message.get(), overlay);
} else {
	ClientReceiveMessageEvents.GAME_CANCELED.invoker().onReceiveGameMessageCanceled(message.get(), overlay);
	ci.cancel();
}

To listen to every incoming message, you'd have to register a listener to ALLOW_GAME that always returns true and never has anything to do with allowing messages or not. On top of that, if any listener before yours in ALLOW_GAME cancels it, your listener does not get called. So you have to register the same listener to GAME_CANCELED as well and complicate things even more. Oh and the 2 functional interfaces don't match due to their return values of boolean and void, so you need 3 whole methods (or lambdas) to achieve this.

This is easily solved by adding an event that passes the message to every listener without allowing any modifications, which is what the events I added do. They also don't interfere with fabric's events since they don't modify the message in any way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've talked to fabric maintainers about this. The most probably fix that we can put in fabric is just to make ALLOW_GAME not short curcuit. But they're skeptical of such use cases, and it might help if you bring this problem up with them.

Copy link
Collaborator Author

@Emirlol Emirlol Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see why they wouldn't want to make a breaking change like that, and using ALLOW_GAME to listen to all incoming messages is still going to be confusing due to the name and the unnecessary return value, so I'm not sure if that's the solution I'd want. Perhaps I could PR this event to fabric, but I can't think of any use cases outside of this either.

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);
AzureAaron marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
Loading