Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
- [fix] Fixed wrapping on NPC professions
- [feat] Added slot position sorting toggle
  • Loading branch information
blackjack26 committed May 12, 2024
1 parent 4469386 commit ac28010
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 55 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cloth_config_version=12.0.119
mod_menu_version=8.0.1

# Project Metadata
mod_version=0.2.1-alpha
mod_version=0.2.2-alpha
mod_name=Blockgame Journal
default_release_type=release
github_owner=blackjack26
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.bnjc.blockgamejournal.config.modules;

import dev.bnjc.blockgamejournal.gui.widget.ItemListWidget;
import dev.bnjc.blockgamejournal.journal.JournalMode;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
Expand All @@ -17,9 +18,14 @@ public class GeneralConfig implements ConfigData {
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
public JournalMode.Type defaultMode;

@ConfigEntry.Gui.Tooltip
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
public ItemListWidget.Sort defaultNpcSort;

public GeneralConfig() {
highlightMissingRecipes = true;
highlightOutdatedRecipes = true;
defaultMode = JournalMode.Type.ITEM_SEARCH;
defaultNpcSort = ItemListWidget.Sort.A_TO_Z;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
import dev.bnjc.blockgamejournal.gui.widget.*;
import dev.bnjc.blockgamejournal.journal.Journal;
import dev.bnjc.blockgamejournal.journal.JournalEntry;
import dev.bnjc.blockgamejournal.journal.JournalItemStack;
import dev.bnjc.blockgamejournal.journal.JournalMode;
import dev.bnjc.blockgamejournal.journal.npc.NPCEntity;
import dev.bnjc.blockgamejournal.util.GuiUtil;
import dev.bnjc.blockgamejournal.util.SearchUtil;
import lombok.Getter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ButtonTextures;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.widget.TexturedButtonWidget;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -50,26 +53,33 @@ public class JournalScreen extends Screen {
@Getter
private static @Nullable NPCEntity selectedNpc = null;

@Getter
private static ItemListWidget.Sort npcItemSort = ItemListWidget.Sort.A_TO_Z;

private int left = 0;
private int top = 0;

@Getter
private JournalMode.Type currentMode;

private final Screen parent;

private TextFieldWidget search;
private ItemListWidget itemList;
private VerticalScrollWidget scroll;

private ButtonWidget closeButton;
private ButtonWidget sortButton;
private NPCWidget npcWidget;

private List<ItemStack> items = Collections.emptyList();
private List<JournalItemStack> items = Collections.emptyList();

public JournalScreen(@Nullable Screen parent) {
super(Text.translatable("blockgamejournal.recipe_journal"));

this.parent = parent;
this.currentMode = BlockgameJournal.getConfig().getGeneralConfig().defaultMode;
npcItemSort = BlockgameJournal.getConfig().getGeneralConfig().defaultNpcSort;
}

@Override
Expand Down Expand Up @@ -139,6 +149,23 @@ protected void init() {
this.closeButton.visible = JournalScreen.selectedNpc != null;
this.addDrawableChild(this.closeButton);

// Sort button
this.sortButton = new TexturedButtonWidget(
this.left + MENU_WIDTH - 2 * (3 + BUTTON_SIZE),
this.top + 5,
12,
12,
new ButtonTextures(GuiUtil.sprite("widgets/sort/button"), GuiUtil.sprite("widgets/sort/button_highlighted")),
button -> {
JournalScreen.npcItemSort = JournalScreen.npcItemSort == ItemListWidget.Sort.A_TO_Z ? ItemListWidget.Sort.SLOT : ItemListWidget.Sort.A_TO_Z;
this.sortButton.setTooltip(Tooltip.of(Text.translatable("blockgamejournal.sort." + JournalScreen.npcItemSort.name())));
this.refreshItems();
}
);
this.sortButton.setTooltip(Tooltip.of(Text.translatable("blockgamejournal.sort." + JournalScreen.npcItemSort.name())));
this.sortButton.visible = JournalScreen.selectedNpc != null;
this.addDrawableChild(this.sortButton);

// NPC Widget
this.npcWidget = new NPCWidget(JournalScreen.selectedNpc, this.left + MENU_WIDTH + 4, this.top, 68, 74);
this.addDrawableChild(this.npcWidget);
Expand Down Expand Up @@ -230,6 +257,7 @@ public void setSelectedNpc(String npc) {

this.updateItems(null);
this.closeButton.visible = npc != null;
this.sortButton.visible = npc != null;
}

@Override
Expand Down Expand Up @@ -269,28 +297,48 @@ private void updateItems(String filter) {
// Filter journal entry items
this.items = Journal.INSTANCE.getEntries().keySet()
.stream()
.map(Journal.INSTANCE::getKnownItem)
.map(JournalItemStack::fromKnownItem)
.filter(Objects::nonNull)
.sorted(Comparator.comparing(a -> a.getName().getString().toLowerCase(Locale.ROOT)))
.sorted(Comparator.comparing(a -> a.getStack().getName().getString().toLowerCase(Locale.ROOT)))
.toList();
}
else if (this.currentMode == JournalMode.Type.NPC_SEARCH) {
// If filter matches "npc:Some Name", filter by recipes that have that NPC
if (JournalScreen.selectedNpc != null) {
this.items = Journal.INSTANCE.getEntries().entrySet().stream()
.filter(entry -> entry.getValue().stream().anyMatch(e -> e.getNpcName().toLowerCase(Locale.ROOT).equals(JournalScreen.selectedNpc.getNpcWorldName().toLowerCase(Locale.ROOT))))
.map(entry -> Journal.INSTANCE.getKnownItem(entry.getKey()))
.filter(Objects::nonNull)
.sorted(Comparator.comparing(a -> a.getName().getString().toLowerCase(Locale.ROOT)))
.filter(entry -> entry.getValue().stream().anyMatch(this::isNpcNameMatching))
.map(entry -> {
if (JournalScreen.npcItemSort == ItemListWidget.Sort.A_TO_Z) {
JournalItemStack stack = JournalItemStack.fromKnownItem(entry.getKey());
if (stack != null) {
return List.of(stack);
}

return List.<JournalItemStack>of();
}

return entry.getValue().stream()
.filter(this::isNpcNameMatching)
.map((e) -> new JournalItemStack(e.getItem(), e.getSlot()))
.toList();
})
.flatMap(List::stream)
.sorted(Comparator.comparing(a -> a.getStack().getName().getString().toLowerCase(Locale.ROOT)))
.toList();
}
// Otherwise, show all known NPCs
else {
this.items = Journal.INSTANCE.getKnownNPCs().keySet()
.stream()
.map(Journal.INSTANCE::getKnownNpcItem)
.map((key) -> {
ItemStack s = Journal.INSTANCE.getKnownNpcItem(key);
if (s != null) {
return new JournalItemStack(s);
}
return null;
})
.filter(Objects::nonNull)
.sorted(Comparator.comparing(a -> a.getName().getString().toLowerCase(Locale.ROOT)))
.sorted(Comparator.comparing(a -> a.getStack().getName().getString().toLowerCase(Locale.ROOT)))
.toList();
}
}
Expand All @@ -299,9 +347,9 @@ else if (this.currentMode == JournalMode.Type.FAVORITES) {
this.items = Journal.INSTANCE.getEntries().entrySet()
.stream()
.filter(entry -> entry.getValue().stream().anyMatch(JournalEntry::isFavorite))
.map(entry -> Journal.INSTANCE.getKnownItem(entry.getKey()))
.map(entry -> JournalItemStack.fromKnownItem(entry.getKey()))
.filter(Objects::nonNull)
.sorted(Comparator.comparing(a -> a.getName().getString().toLowerCase(Locale.ROOT)))
.sorted(Comparator.comparing(a -> a.getStack().getName().getString().toLowerCase(Locale.ROOT)))
.toList();
}
else {
Expand All @@ -313,16 +361,23 @@ else if (this.currentMode == JournalMode.Type.FAVORITES) {
private void filter(@Nullable String filter) {
JournalScreen.lastSearch = filter;

List<ItemStack> filtered;
List<JournalItemStack> filtered;
if (filter == null || filter.isEmpty()) {
filtered = this.items;
} else {
filtered = this.items.stream()
.filter(item -> SearchUtil.defaultPredicate(item, filter))
.filter(item -> SearchUtil.defaultPredicate(item.getStack(), filter))
.toList();
}

this.itemList.setItems(filtered);
this.scroll.setDisabled(filtered.size() <= GRID_ROWS * GRID_COLUMNS);
}

private boolean isNpcNameMatching(JournalEntry entry) {
if (JournalScreen.selectedNpc == null) {
return false;
}
return entry.getNpcName().toLowerCase(Locale.ROOT).equals(selectedNpc.getNpcWorldName().toLowerCase(Locale.ROOT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.bnjc.blockgamejournal.journal.Journal;
import dev.bnjc.blockgamejournal.journal.JournalEntry;
import dev.bnjc.blockgamejournal.journal.JournalMode;
import dev.bnjc.blockgamejournal.journal.npc.NPCEntity;
import dev.bnjc.blockgamejournal.util.GuiUtil;
import dev.bnjc.blockgamejournal.util.ItemUtil;
import lombok.Getter;
Expand Down Expand Up @@ -49,6 +50,7 @@ public class RecipeScreen extends Screen {
private TexturedButtonWidget decomposeButton;
private TexturedButtonWidget favoriteButton;
private TexturedButtonWidget unfavoriteButton;
private NPCWidget npcWidget;

public RecipeScreen(String key, Screen parent) {
super(Text.empty());
Expand Down Expand Up @@ -231,9 +233,10 @@ protected void init() {
this.addDrawableChild(this.unfavoriteButton);

// NPC Widget
this.npcWidget = new NPCWidget(null, this.left + MENU_WIDTH + 4, this.top, 68, 74);
this.addDrawableChild(this.npcWidget);
if (this.parent instanceof JournalScreen journalScreen && journalScreen.getCurrentMode() == JournalMode.Type.NPC_SEARCH) {
NPCWidget npcWidget = new NPCWidget(JournalScreen.getSelectedNpc(), this.left + MENU_WIDTH + 4, this.top, 68, 74);
this.addDrawableChild(npcWidget);
this.npcWidget.setEntity(JournalScreen.getSelectedNpc());
}

this.goToPage(this.page);
Expand Down Expand Up @@ -267,7 +270,15 @@ private void goToPage(int page) {

this.updateButtons();

this.recipeWidget.setEntry(this.entries.get(this.page));
JournalEntry entry = this.entries.get(this.page);
this.recipeWidget.setEntry(entry);

if (entry != null) {
NPCEntity entity = new NPCEntity(MinecraftClient.getInstance().world, entry.getNpcName());
this.npcWidget.setEntity(entity);
} else {
this.npcWidget.setEntity(null);
}
}

private void updateButtons() {
Expand Down
Loading

0 comments on commit ac28010

Please sign in to comment.