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

ItemList Changes #1119

Merged
merged 6 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -57,16 +57,12 @@ public static void init() {
}

public static void tryDraw(ItemStack stack, DrawContext context, int x, int y) {
MinecraftClient client = MinecraftClient.getInstance();

if (client.player != null) {
SkyblockItemRarity itemRarity = getItemRarity(stack, client.player);

if (itemRarity != null) draw(context, x, y, itemRarity);
}
SkyblockItemRarity itemRarity = getItemRarity(stack);
if (itemRarity != null)
draw(context, x, y, itemRarity);
}

private static SkyblockItemRarity getItemRarity(ItemStack stack, ClientPlayerEntity player) {
private static SkyblockItemRarity getItemRarity(ItemStack stack) {
if (stack == null || stack.isEmpty()) return null;

String itemUuid = stack.getUuid();
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/de/hysky/skyblocker/skyblock/item/WikiLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.util.InputUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
import net.minecraft.util.Util;
Expand All @@ -33,12 +34,16 @@ public static void init() {
}

public static void openWiki(@NotNull Slot slot, @NotNull PlayerEntity player) {
ItemUtils.getItemIdOptional(slot.getStack())
.map(ItemRepository::getWikiLink)
.ifPresentOrElse(wikiLink -> CompletableFuture.runAsync(() -> Util.getOperatingSystem().open(wikiLink)).exceptionally(e -> {
LOGGER.error("[Skyblocker] Error while retrieving wiki article...", e);
player.sendMessage(Constants.PREFIX.get().append("Error while retrieving wiki article, see logs..."), false);
return null;
}), () -> player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.wikiLookup.noArticleFound")), false));
WikiLookup.openWiki(slot.getStack(), player);
}

public static void openWiki(ItemStack stack, PlayerEntity player) {
ItemUtils.getItemIdOptional(stack)
.map(ItemRepository::getWikiLink)
.ifPresentOrElse(wikiLink -> CompletableFuture.runAsync(() -> Util.getOperatingSystem().open(wikiLink)).exceptionally(e -> {
LOGGER.error("[Skyblocker] Error while retrieving wiki article...", e);
player.sendMessage(Constants.PREFIX.get().append("Error while retrieving wiki article, see logs..."), false);
return null;
}), () -> player.sendMessage(Constants.PREFIX.get().append(Text.translatable("skyblocker.wikiLookup.noArticleFound")), false));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.hysky.skyblocker.skyblock.itemlist.recipebook;

import de.hysky.skyblocker.utils.Identifiable;
import net.minecraft.util.Identifier;

import java.util.function.Predicate;

public enum FilterOption implements Identifiable {
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved

ALL(query -> true, Identifier.of("skyblocker", "textures/gui/filter/all.png")),
ENTITIES(query -> query.endsWith("(monster)") || query.endsWith("(miniboss)") || query.endsWith("(boss)")
|| query.endsWith("(animal)") || query.endsWith("(pest)") || query.endsWith("(sea creature)"),
Identifier.of("skyblocker", "textures/gui/filter/entities.png")),
NPCS(query -> query.endsWith("(npc)") || query.endsWith("(rift npc)"), Identifier.of("skyblocker", "textures/gui/filter/npcs.png")),
MAYORS(query -> query.endsWith("(mayor)") || query.endsWith("(retired mayor)"), Identifier.of("skyblocker", "textures/gui/filter/mayors.png")),

// Basically a negation on everything else.
ITEMS(query -> !ENTITIES.matches(query) && !NPCS.matches(query) && !MAYORS.matches(query),
Identifier.of("skyblocker", "textures/gui/filter/items.png"));
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved

final Predicate<String> matchingPredicate;
final Identifier texture;
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved

FilterOption(Predicate<String> matchingPredicate, Identifier texture) {
this.matchingPredicate = matchingPredicate;
this.texture = texture;
}

public boolean matches(String query) {
return matchingPredicate.test(query);
}

@Override
public Identifier identify() {
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
return texture;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ public interface RecipeAreaDisplay {

boolean mouseClicked(double mouseX, double mouseY, int button);

default boolean keyPressed(double mouseX, double mouseY, int keyCode, int scanCode, int modifiers) {
return false;
}

default void updateSearchResults(String query, FilterOption filterOption) {
updateSearchResults(query, filterOption, false);
}

/**
* If this tab does not use the search bar then no-op this.
*/
void updateSearchResults(String query);
void updateSearchResults(String query, FilterOption filterOption, boolean refresh);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@

import com.google.common.collect.Lists;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.skyblock.item.WikiLookup;
import de.hysky.skyblocker.skyblock.itemlist.ItemRepository;
import de.hysky.skyblocker.skyblock.itemlist.SkyblockCraftingRecipe;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.render.RenderHelper;
import de.hysky.skyblocker.utils.scheduler.MessageScheduler;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.recipebook.RecipeBookResults;
import net.minecraft.client.gui.widget.ToggleButtonWidget;
import net.minecraft.component.DataComponentTypes;
Expand Down Expand Up @@ -181,19 +185,23 @@ protected void closeRecipeView() {
* @implNote The {@code query} is always passed as lower case.
*/
@Override
public void updateSearchResults(String query) {
if (!query.equals(this.lastSearchQuery)) {
public void updateSearchResults(String query, FilterOption filterOption, boolean refresh) {
if (!query.equals(this.lastSearchQuery) || refresh) {
this.lastSearchQuery = query;
this.searchResults.clear();

//Search for stacks which contain the search term
for (ItemStack stack : ItemRepository.getItems()) {
String name = stack.getName().getString().toLowerCase(Locale.ENGLISH);
List<Text> lore = ItemUtils.getLore(stack);

//TODO turn lore lowercase
if (name.contains(query) || lore.stream().map(Text::getString).anyMatch(line -> line.contains(query))) {
this.searchResults.add(stack);
synchronized (this) {
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
for (ItemStack stack : ItemRepository.getItems()) {
String name = stack.getName().getString().toLowerCase(Locale.ENGLISH);
if (!filterOption.matches(name)) continue;
List<Text> lore = ItemUtils.getLore(stack);

if (name.contains(query) || lore.stream().map(Text::getString)
.map(string -> string.toLowerCase(Locale.ENGLISH))
.anyMatch(line -> line.contains(query))) {
this.searchResults.add(stack);
}
}
}

Expand Down Expand Up @@ -271,6 +279,15 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
return true;
}

if (this.recipeView && Screen.hasShiftDown()) {
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
// The crafting result button
var result = resultButtons.get(14);
var rawID = ItemUtils.getItemId(result.getDisplayStack());
if (result.isMouseOver(mouseX, mouseY)) {
MessageScheduler.INSTANCE.sendMessageAfterCooldown(String.format("/viewrecipe %s", rawID), true);
}
}

for (SkyblockRecipeResultButton resultButton : this.resultButtons) {
//If the result button was clicked then try and show a recipe if there is one
//for the item
Expand All @@ -297,4 +314,17 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {

return false;
}

@Override
public boolean keyPressed(double mouseX, double mouseY, int keyCode, int scanCode, int modifiers) {
if (SkyblockerConfigManager.get().general.wikiLookup.enableWikiLookup
&& WikiLookup.wikiLookup.matchesKey(keyCode, scanCode))
return this.resultButtons.stream()
.filter(button -> button.isMouseOver(mouseX, mouseY))
.findFirst().map(button -> {
WikiLookup.openWiki(button.getDisplayStack(), client.player);
return true;
}).orElse(false);
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public void initialize(MinecraftClient client, int parentLeft, int parentTop) {

@Override
public void draw(DrawContext context, int x, int y, int mouseX, int mouseY, float delta) {
assert recipeBook.searchField != null;
recipeBook.searchField.render(context, mouseX, mouseY, delta);
recipeBook.filterOption.render(context, mouseX, mouseY, delta);
results.draw(context, x, y, mouseX, mouseY, delta);
}

Expand All @@ -43,23 +45,28 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {

return true;
}

recipeBook.searchField.setFocused(false);
return recipeBook.filterOption.mouseClicked(mouseX, mouseY, button);
}
}

return false;
}

@Override
public void updateSearchResults(String query) {
results.updateSearchResults(query);
public boolean keyPressed(double mouseX, double mouseY, int keyCode, int scanCode, int modifiers) {
return this.results.keyPressed(mouseX, mouseY, keyCode, scanCode, modifiers);
}

@Override
public void updateSearchResults(String query, FilterOption filterOption, boolean refresh) {
results.updateSearchResults(query, filterOption, refresh);
}

@Override
public void initializeSearchResults(String query) {
if (ItemRepository.filesImported()) {
updateSearchResults(query);
updateSearchResults(query, FilterOption.ALL);
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think the asserts are necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They just fix all the warning in the IDE

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it makes sense to use asserts as "I know this won't be null, now stop giving me warnings."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it makes sense to use asserts as "I know this won't be null, now stop giving me warnings."

Or even with other boolean conditions, such as when the children method returns a list of three, you know for sure its size is going to be 3, so you can assert children().size() == 3 - It's just cleaner than @SurpressWarnings

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Locale;

import de.hysky.skyblocker.utils.render.gui.CyclingTextureWidget;
import net.minecraft.screen.ScreenHandler;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -34,8 +35,9 @@ public class SkyblockRecipeBookWidget extends RecipeBookWidget<NoopRecipeScreenH
private static final int IMAGE_HEIGHT = RecipeBookWidget.field_32409;
//Corresponds to field_32410 in RecipeBookWidget
private static final int OFFSET_X_POSITION = 86;
//81 is the search field's width, 4 is the space between it and the toggle crafting button, and 26 is the toggle crafting button's width
private static final int SEARCH_FIELD_WIDTH = 81 + 4 + 26;
// 81 is the search field's width, 4 is the space between it and the toggle crafting button, and 26 is the toggle crafting button's width, which we replace
// with the filtering button. (26 - 14) = 12 - 4 = 8
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
private static final int SEARCH_FIELD_WIDTH = 81 + 4 + 8;
/**
* The tabs in the Skyblock recipe book.
*/
Expand All @@ -46,6 +48,8 @@ public class SkyblockRecipeBookWidget extends RecipeBookWidget<NoopRecipeScreenH
private final List<Pair<RecipeTab, SkyblockRecipeTabButton>> tabButtons = Lists.newArrayList();
private Pair<RecipeTab, SkyblockRecipeTabButton> currentTab;

protected CyclingTextureWidget<FilterOption> filterOption;

public SkyblockRecipeBookWidget(ScreenHandler screenHandler) {
super(new NoopRecipeScreenHandler(screenHandler.syncId), List.of());
}
Expand All @@ -72,6 +76,16 @@ protected void reset() {
//This field's name is misleading, the rectangle is actually the area of the magnifying glass icon rather than the entire search field
this.searchFieldRect = ScreenRect.of(NavigationAxis.HORIZONTAL, left + 8, this.searchField.getY(), this.searchField.getX() - left, this.searchField.getHeight());

this.filterOption = new CyclingTextureWidget<>(this.searchField.getRight() + 4, this.searchField.getY(), 14, 14, FilterOption.class);
this.filterOption.setCycleListener(this::refilterSearchResults);
this.filterOption.setTextSupplier(option -> switch (option){
case ALL -> Text.translatable("skyblocker.config.general.itemList.filter.all");
case NPCS -> Text.translatable("skyblocker.config.general.itemList.filter.npcs");
case ENTITIES -> Text.translatable("skyblocker.config.general.itemList.filter.entities");
case MAYORS -> Text.translatable("skyblocker.config.general.itemList.filter.mayors");
case ITEMS -> Text.translatable("skyblocker.config.general.itemList.filter.items");
});
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved

//Setup Tabs
this.tabButtons.clear();

Expand Down Expand Up @@ -150,6 +164,21 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
return false;
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
var client = MinecraftClient.getInstance();
if (client.isWindowFocused()) {
var mouse = client.mouse;
var window = client.getWindow();
var mouseX = (mouse.getX() * ((double) window.getScaledWidth() / (double) window.getWidth()));
var mouseY = (mouse.getY() * ((double) window.getScaledHeight() / (double) window.getHeight()));
if (this.currentTab.left().keyPressed(mouseX, mouseY, keyCode, scanCode, modifiers)) {
return true;
}
}
return super.keyPressed(keyCode, scanCode, modifiers);
}

/**
* Same as the super classes implementation just that it checks for our custom tabs.
*/
Expand Down Expand Up @@ -179,13 +208,21 @@ protected void refreshTabButtons(boolean filteringCraftable) {
}
}

protected void refilterSearchResults(FilterOption filterOption) {
assert this.searchField != null;
String query = this.searchField.getText().toLowerCase(Locale.ENGLISH);
// Doesn't trigger the pirate speak check since the query wasn't changed.
this.currentTab.left().updateSearchResults(query, filterOption, true);
}

@Override
protected void refreshSearchResults() {
assert this.searchField != null;
String query = this.searchField.getText().toLowerCase(Locale.ENGLISH);

this.triggerPirateSpeakEasterEgg(query);
//Note: The rest of the query checks are implemented by the results class
this.currentTab.left().updateSearchResults(query);
this.currentTab.left().updateSearchResults(query, this.filterOption.getCurrent());
}

private RecipeBookWidgetAccessor accessor() {
Expand All @@ -194,7 +231,9 @@ private RecipeBookWidgetAccessor accessor() {

@Override
protected void refreshResults(boolean resetCurrentPage, boolean filteringCraftable) {
this.currentTab.left().updateSearchResults(this.searchField.getText());
assert this.searchField != null;
this.currentTab.left().updateSearchResults(this.searchField.getText().toLowerCase(Locale.ENGLISH),
this.filterOption.getCurrent());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class SkyblockRecipeResultButton extends ClickableWidget {
private static final int SIZE = 25;
private static final int ITEM_OFFSET = 4;

private ItemStack itemStack = null;
private ItemStack itemStack = ItemStack.EMPTY;

protected SkyblockRecipeResultButton() {
super(0, 0, SIZE, SIZE, ScreenTexts.EMPTY);
Expand All @@ -37,7 +37,7 @@ protected void setDisplayStack(ItemStack stack) {

protected void clearDisplayStack() {
this.visible = false;
this.itemStack = null;
this.itemStack = ItemStack.EMPTY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public ItemStack icon() {
}

@Override
public void updateSearchResults(String query) {}
public void updateSearchResults(String query, FilterOption filterOption, boolean refresh) {}

@Override
public void initializeSearchResults(String query) {}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/de/hysky/skyblocker/utils/Identifiable.java
kevinthegreat1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package de.hysky.skyblocker.utils;

import net.minecraft.util.Identifier;

@FunctionalInterface
public interface Identifiable {

Identifier identify();
}
Loading
Loading