Skip to content

Commit

Permalink
Features and fixes
Browse files Browse the repository at this point in the history
- [feat] Press "A" on a favorited item to remove it from your favorites
- [feat] Use "A" to quickly start/stop locating an NPC in the world
- [feat] Added config option to set the default journal mode (favorites, by item, by npc)
- [fix] Removed highlighting on party screen

Resolves #3, Fixes #12, Resolves #13
  • Loading branch information
blackjack26 committed Apr 19, 2024
1 parent a1327c7 commit 40ca1f5
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.bnjc.blockgamejournal.config.modules;

import dev.bnjc.blockgamejournal.journal.JournalMode;
import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry;
Expand All @@ -12,8 +13,13 @@ public class GeneralConfig implements ConfigData {
@ConfigEntry.Gui.Tooltip
public boolean highlightOutdatedRecipes;

@ConfigEntry.Gui.Tooltip
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
public JournalMode.Type defaultMode;

public GeneralConfig() {
highlightMissingRecipes = true;
highlightOutdatedRecipes = true;
defaultMode = JournalMode.Type.ITEM_SEARCH;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
import me.shedaniel.autoconfig.annotation.ConfigEntry;
import me.shedaniel.autoconfig.serializer.PartitioningSerializer;

@Getter
@Config(name = "blockgamejournal")
@Config.Gui.Background("minecraft:textures/block/polished_blackstone_bricks.png")
public class ModConfig extends PartitioningSerializer.GlobalData {
@Getter
@ConfigEntry.Gui.CollapsibleObject(startExpanded = true)
GeneralConfig generalConfig = new GeneralConfig();

@Getter
@ConfigEntry.Gui.CollapsibleObject(startExpanded = true)
DecompositionConfig decompositionConfig = new DecompositionConfig();

@Getter
@ConfigEntry.Gui.CollapsibleObject(startExpanded = true)
StorageConfig storageConfig = new StorageConfig();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@Config(name = "storage")
public class StorageConfig implements ConfigData {
@ConfigEntry.Gui.Tooltip
@ConfigEntry.Gui.EnumHandler(option = ConfigEntry.Gui.EnumHandler.EnumDisplayOption.BUTTON)
public Backend.Type backendType;

public StorageConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,13 @@ private void handlePostRender(float tickDelta, long limitTime, MatrixStack matri
RenderSystem.blendFuncSeparate(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA,
GlStateManager.SrcFactor.ONE, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA);

String worldName = "";
if (this.getMinecraftClient().world != null) {
worldName = this.getMinecraftClient().world.getRegistryKey().getValue().toString();
}

for (NPCEntry entry : knownNPCs.values()) {
if (entry.getPosition() == null || !entry.isLocating()) {
if (entry.getPosition() == null || !entry.isLocating() || !worldName.equals(entry.getWorld())) {
continue;
}

Expand All @@ -309,7 +314,7 @@ private void handlePostRender(float tickDelta, long limitTime, MatrixStack matri

double distance = Math.sqrt(entry.getDistanceSqToEntity(cameraEntity));

int maxDistance = 1000; // TODO: Configurable
int maxDistance = 10_000; // TODO: Configurable
if ((distance < maxDistance || maxDistance < 0 /* || entry == this.highlightedWaypoint */) && !this.getMinecraftClient().options.hudHidden) {
boolean isPointedAt = this.isPointedAt(entry, distance, cameraEntity, tickDelta);
String label = entry.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import dev.bnjc.blockgamejournal.util.ItemUtil;
import dev.bnjc.blockgamejournal.util.NbtUtil;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.network.ClientPlayerEntity;
Expand All @@ -30,7 +29,6 @@
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -69,8 +67,8 @@ public void init() {
private ActionResult handleOpenScreen(OpenScreenS2CPacket packet) {
String screenName = packet.getName().getString();

// Look for screen name "Some Name (#page#/#max#)"
Matcher matcher = Pattern.compile("^([\\w\\s]+)\\s\\(\\d+/\\d+\\)").matcher(screenName);
// Look for screen name "Some Name (#page#/#max#)" - exclude "Party" from the name
Matcher matcher = Pattern.compile("^((?!Party)[\\w\\s]+)\\s\\(\\d+/\\d+\\)").matcher(screenName);
PlayerEntity lastAttackedPlayer = this.gameFeature.getLastAttackedPlayer();
if (matcher.find() ||
(lastAttackedPlayer != null && screenName.equals(lastAttackedPlayer.getEntityName()))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class JournalScreen extends Screen {
private int top = 0;

@Getter
private JournalMode.Type currentMode = JournalMode.Type.ITEM_SEARCH;
private JournalMode.Type currentMode;

private final Screen parent;
private TextFieldWidget search;
Expand All @@ -67,6 +67,7 @@ public JournalScreen(@Nullable Screen parent) {
super(Text.translatable("blockgamejournal.recipe_journal"));

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

@Override
Expand Down Expand Up @@ -231,6 +232,19 @@ public void setSelectedNpc(String npc) {
this.search.setFocused(true);
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
boolean handled = super.keyPressed(keyCode, scanCode, modifiers);

handled |= this.itemList.keyPressed(keyCode, scanCode, modifiers);

return handled;
}

public void refreshItems() {
this.updateItems(this.search.getText());
}

private void updateItems(String filter) {
if (Journal.INSTANCE == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import dev.bnjc.blockgamejournal.journal.Journal;
import dev.bnjc.blockgamejournal.journal.JournalEntry;
import dev.bnjc.blockgamejournal.journal.JournalMode;
import dev.bnjc.blockgamejournal.journal.npc.NPCItemStack;
import dev.bnjc.blockgamejournal.util.GuiUtil;
import dev.bnjc.blockgamejournal.util.ItemUtil;
import lombok.Setter;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
Expand All @@ -17,7 +19,9 @@
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import org.jetbrains.annotations.Nullable;

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

Expand All @@ -32,6 +36,8 @@ public class ItemListWidget extends ClickableWidget {
private List<ItemStack> items = Collections.emptyList();
private int offset = 0;

private @Nullable ItemStack hoveredItem = null;

@Setter
private JournalMode.Type mode = JournalMode.Type.ITEM_SEARCH;

Expand Down Expand Up @@ -79,25 +85,64 @@ public void onClick(double mouseX, double mouseY) {

ItemStack item = items.get(index);

if (this.mode == JournalMode.Type.ITEM_SEARCH) {
// Open RecipeDisplay screen
MinecraftClient.getInstance().setScreen(new RecipeDisplay(item, this.parent));
if (item != null) {
if (this.mode == JournalMode.Type.ITEM_SEARCH) {
// Open RecipeDisplay screen
MinecraftClient.getInstance().setScreen(new RecipeDisplay(item, this.parent));
} else if (this.mode == JournalMode.Type.FAVORITES) {
RecipeDisplay recipeDisplay = new RecipeDisplay(item, this.parent);
recipeDisplay.filterEntries(JournalEntry::isFavorite);

MinecraftClient.getInstance().setScreen(recipeDisplay);
} else if (this.mode == JournalMode.Type.NPC_SEARCH) {
if (item.getItem() instanceof PlayerHeadItem) {
if (this.parent instanceof JournalScreen journalScreen && item.hasNbt()) {
journalScreen.setSelectedNpc(item.getNbt().getString(Journal.NPC_NAME_KEY));
}
} else {
MinecraftClient.getInstance().setScreen(new RecipeDisplay(item, this.parent));
}
}
}
else if (this.mode == JournalMode.Type.FAVORITES) {
RecipeDisplay recipeDisplay = new RecipeDisplay(item, this.parent);
recipeDisplay.filterEntries(JournalEntry::isFavorite);
}

MinecraftClient.getInstance().setScreen(recipeDisplay);
}
else if (this.mode == JournalMode.Type.NPC_SEARCH) {
if (item.getItem() instanceof PlayerHeadItem) {
if (this.parent instanceof JournalScreen journalScreen && item.hasNbt()) {
journalScreen.setSelectedNpc(item.getNbt().getString(Journal.NPC_NAME_KEY));
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
if (this.hoveredItem != null && Journal.INSTANCE != null) {
if (this.mode == JournalMode.Type.NPC_SEARCH && this.hoveredItem.getItem() instanceof PlayerHeadItem) {
String npcName = this.hoveredItem.getNbt().getString(Journal.NPC_NAME_KEY);
Journal.INSTANCE.getKnownNpc(npcName).ifPresent(npc -> {
if (keyCode == 65) {
// If NPC is locating and the A key is pressed, stop locating
if (npc.isLocating()) {
npc.setLocating(false);
NPCItemStack.updateStack(npcName, this.hoveredItem, npc);
}
// If NPC is not locating and the A key is pressed, start locating
else if (npc.getPosition() != null) {
npc.setLocating(true);
NPCItemStack.updateStack(npcName, this.hoveredItem, npc);
}
}
});
}
else if (this.mode == JournalMode.Type.FAVORITES) {
if (keyCode == 65) {
if (Journal.INSTANCE.hasJournalEntry(this.hoveredItem)) {
List<JournalEntry> entries = Journal.INSTANCE.getEntries().getOrDefault(ItemUtil.getKey(this.hoveredItem), new ArrayList<>());
for (JournalEntry entry : entries) {
entry.setFavorite(false);
}

if (this.parent instanceof JournalScreen journalScreen) {
journalScreen.refreshItems();
}
}
}
} else {
MinecraftClient.getInstance().setScreen(new RecipeDisplay(item, this.parent));
}
}

return super.keyPressed(keyCode, scanCode, modifiers);
}

@Override
Expand All @@ -120,6 +165,18 @@ private void renderItems(DrawContext context) {
int y = this.getY() + GRID_SLOT_SIZE * (i / this.gridWidth);
if (i < items.size()) {
ItemStack item = items.get(i);

if (this.mode == JournalMode.Type.NPC_SEARCH && Journal.INSTANCE != null) {
if (item.getItem() instanceof PlayerHeadItem) {
String npcName = item.getNbt().getString(Journal.NPC_NAME_KEY);
Journal.INSTANCE.getKnownNpc(npcName).ifPresent(npc -> {
if (npc.isLocating()) {
context.fill(x + 1, y + 1, x + GRID_SLOT_SIZE - 1, y + GRID_SLOT_SIZE - 1, 0x80_AA00AA);
}
});
}
}

context.drawItem(item, x + 1, y + 1);
}
}
Expand All @@ -128,30 +185,33 @@ private void renderItems(DrawContext context) {
private void renderTooltip(DrawContext context, int mouseX, int mouseY) {
List<ItemStack> items = this.getOffsetItems();
if (!this.isHovered()) {
this.hoveredItem = null;
return;
}

int x = (mouseX - this.getX()) / GRID_SLOT_SIZE;
int y = (mouseY - this.getY()) / GRID_SLOT_SIZE;
if (x < 0 || x > this.gridWidth || y < 0 || y > this.gridHeight) {
this.hoveredItem = null;
return;
}

int index = (y * this.gridWidth) + x;
if (index >= items.size()) {
this.hoveredItem = null;
return;
}

int slotX = this.getX() + x * GRID_SLOT_SIZE;
int slotY = this.getY() + y * GRID_SLOT_SIZE;
context.fill(slotX + 1, slotY + 1, slotX + GRID_SLOT_SIZE - 1, slotY + GRID_SLOT_SIZE - 1, 0x80_FFFFFF);

if (!this.hideTooltip) {
ItemStack stack = items.get(index);
this.hoveredItem = items.get(index);

if (!this.hideTooltip) {
context.getMatrices().push();
context.getMatrices().translate(0, 0, 150f);
context.drawItemTooltip(MinecraftClient.getInstance().textRenderer, stack, mouseX, mouseY);
context.drawItemTooltip(MinecraftClient.getInstance().textRenderer, this.hoveredItem, mouseX, mouseY);
context.getMatrices().pop();
}
}
Expand Down
41 changes: 9 additions & 32 deletions src/main/java/dev/bnjc/blockgamejournal/journal/Journal.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.bnjc.blockgamejournal.BlockgameJournal;
import dev.bnjc.blockgamejournal.journal.metadata.Metadata;
import dev.bnjc.blockgamejournal.journal.npc.NPCEntry;
import dev.bnjc.blockgamejournal.journal.npc.NPCItemStack;
import dev.bnjc.blockgamejournal.journal.npc.NPCNames;
import dev.bnjc.blockgamejournal.storage.Storage;
import dev.bnjc.blockgamejournal.storage.backend.FileBasedBackend;
Expand All @@ -27,10 +28,7 @@
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.Function;

public class Journal {
Expand Down Expand Up @@ -152,35 +150,14 @@ public void addEntry(ItemStack result, JournalEntry entry) {
return knownItems.get(key);
}

public @Nullable ItemStack getKnownNpcItem(String npcName) {
NPCEntry npcEntry = knownNPCs.get(npcName);
if (npcEntry == null) {
return null;
}

GameProfile gameProfile = npcEntry.getGameProfile();

ItemStack stack = new ItemStack(Items.PLAYER_HEAD);
stack.setSubNbt(PlayerHeadItem.SKULL_OWNER_KEY, NbtHelper.writeGameProfile(new NbtCompound(), gameProfile));

NPCNames.NPCName npcNameObj = NPCNames.get(npcName);
MutableText npcNameText = Text.literal(npcNameObj.name());
npcNameText.setStyle(npcNameText.getStyle().withItalic(false).withFormatting(Formatting.WHITE));
stack.setCustomName(npcNameText);

// Set "Lore" to the title of the NPC
if (npcNameObj.title() != null) {
MutableText loreText = Text.literal("« " + npcNameObj.title() + " »");
loreText.setStyle(loreText.getStyle().withItalic(false).withFormatting(Formatting.GRAY));

NbtList loreNbt = new NbtList();
loreNbt.add(NbtString.of(Text.Serializer.toJson(loreText)));
stack.getOrCreateSubNbt(ItemStack.DISPLAY_KEY).put(ItemStack.LORE_KEY, loreNbt);
}

stack.setSubNbt(NPC_NAME_KEY, NbtString.of(npcName));
public Optional<NPCEntry> getKnownNpc(String npcName) {
return Optional.ofNullable(knownNPCs.get(npcName));
}

return stack;
public @Nullable ItemStack getKnownNpcItem(String npcName) {
return NPCItemStack.from(npcName)
.map(NPCItemStack::getItemStack)
.orElse(null);
}

public boolean hasJournalEntry(ItemStack stack) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.bnjc.blockgamejournal.journal;

import net.minecraft.client.resource.language.I18n;
import net.minecraft.item.Item;
import net.minecraft.item.Items;

Expand All @@ -15,6 +16,11 @@ JournalMode.Type.NPC_SEARCH, new JournalMode(JournalMode.Type.NPC_SEARCH, Items.
public enum Type {
ITEM_SEARCH,
NPC_SEARCH,
FAVORITES,
FAVORITES;

@Override
public String toString() {
return I18n.translate("blockgamejournal.menu.mode." + this.name());
}
}
}
Loading

0 comments on commit 40ca1f5

Please sign in to comment.