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 Quest Bookmarking #127

Merged
merged 9 commits into from
May 26, 2024
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 @@ -51,13 +51,13 @@ public PanelButton(IGuiRect rect, int id, String txt)
this.setTextures(PresetTexture.BTN_NORMAL_0.getTexture(), PresetTexture.BTN_NORMAL_1.getTexture(), PresetTexture.BTN_NORMAL_2.getTexture());
this.setTextHighlight(PresetColor.BTN_DISABLED.getColor(), PresetColor.BTN_IDLE.getColor(), PresetColor.BTN_HOVER.getColor());
}

public PanelButton setClickAction(Consumer<PanelButton> action)
{
this.clickAction = action;
return this;
}

public PanelButton setTextHighlight(IGuiColor disabled, IGuiColor idle, IGuiColor hover)
{
this.colStates[0] = disabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import betterquesting.api.storage.BQ_Settings;
import betterquesting.api.utils.BigItemStack;
import betterquesting.api2.client.gui.misc.GuiRectangle;
import betterquesting.api2.client.gui.misc.IGuiRect;
import betterquesting.api2.client.gui.resources.colors.IGuiColor;
import betterquesting.api2.client.gui.resources.textures.GuiTextureColored;
import betterquesting.api2.client.gui.resources.textures.IGuiTexture;
import betterquesting.api2.client.gui.resources.textures.OreDictTexture;
import betterquesting.api2.client.gui.themes.presets.PresetColor;
import betterquesting.api2.client.gui.themes.presets.PresetIcon;
import betterquesting.api2.client.gui.themes.presets.PresetTexture;
import betterquesting.api2.storage.DBEntry;
import betterquesting.api2.utils.QuestTranslation;
import betterquesting.client.BookmarkHandler;
import betterquesting.questing.QuestDatabase;
import betterquesting.storage.QuestSettings;
import com.mojang.realmsclient.gui.ChatFormatting;
Expand All @@ -34,10 +37,13 @@

public class PanelButtonQuest extends PanelButtonStorage<Map.Entry<UUID, IQuest>>
{
private static final IGuiTexture PIN_TEX = PresetIcon.ICON_BOOKMARK.getTexture();
public final GuiRectangle rect;
public final EntityPlayer player;
public final IGuiTexture txFrame;

private boolean isBookmarked = false;

public PanelButtonQuest(GuiRectangle rect, int id, String txt, Map.Entry<UUID, IQuest> value)
{
super(rect, id, txt, value);
Expand Down Expand Up @@ -81,8 +87,22 @@ public PanelButtonQuest(GuiRectangle rect, int id, String txt, Map.Entry<UUID, I
setIcon(new OreDictTexture(1F, value == null ? new BigItemStack(Items.nether_star) : value.getValue().getProperty(NativeProps.ICON), false, true), 4);
//setTooltip(value == null ? Collections.emptyList() : value.getValue().getTooltip(player));
setActive(QuestingAPI.getAPI(ApiReference.SETTINGS).canUserEdit(player) || !lock || BQ_Settings.viewMode);
setBookmarked(BookmarkHandler.isBookmarked(value.getKey()));
}

@Override
public void drawPanel(int mx, int my, float partialTick) {
super.drawPanel(mx, my, partialTick);
if (isBookmarked){
IGuiRect bounds = this.getTransform();
PIN_TEX.drawTexture(bounds.getX(), bounds.getY(), bounds.getWidth()/4, bounds.getHeight()/4, 1, partialTick);
}
}

public void setBookmarked(boolean state) {
isBookmarked = state;
}

@Override
public List<String> getTooltip(int mx, int my)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package betterquesting.api2.client.gui.panels.lists;

import betterquesting.api.api.QuestingAPI;
import betterquesting.api.properties.NativeProps;
import betterquesting.api.questing.IQuest;
import betterquesting.api.questing.IQuestLine;
import betterquesting.api.questing.IQuestLineEntry;
import betterquesting.api2.cache.QuestCache;
import betterquesting.api2.client.gui.controls.PanelButtonCustom;
import betterquesting.api2.client.gui.controls.PanelButtonQuest;
import betterquesting.api2.client.gui.misc.GuiRectangle;
import betterquesting.api2.client.gui.misc.IGuiRect;
import betterquesting.api2.client.gui.panels.IGuiPanel;
import betterquesting.api2.client.gui.panels.content.PanelGeneric;
import betterquesting.api2.client.gui.panels.content.PanelTextBox;
import betterquesting.api2.client.gui.resources.textures.OreDictTexture;
import betterquesting.api2.utils.QuestTranslation;
import betterquesting.client.BookmarkHandler;
import betterquesting.misc.QuestSearchEntry;
import betterquesting.questing.QuestDatabase;
import betterquesting.questing.QuestLineDatabase;
import com.google.common.collect.Maps;
import net.minecraft.entity.player.EntityPlayer;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class CanvasQuestBookmarks extends CanvasScrolling {

private List<QuestSearchEntry> questList;
private Consumer<QuestSearchEntry> questOpenCallback;
private Consumer<QuestSearchEntry> questHighlightCallback;
private static List<QuestSearchEntry> bookmarkedQuests;
private final EntityPlayer player;
private final UUID questingUUID;

public CanvasQuestBookmarks(IGuiRect rect, EntityPlayer player) {
super(rect);
this.player = player;
questingUUID = QuestingAPI.getQuestingUUID(player);
}

@Override
public void initPanel()
{
super.initPanel();
int resultWidth = this.getTransform().getWidth();
if(BookmarkHandler.hasChanged()) {
bookmarkedQuests = getBookmarkedQuests();
}

for (int i = 0; i < bookmarkedQuests.size(); i++) {
QuestSearchEntry entry = bookmarkedQuests.get(i);
addResult(entry, i, resultWidth);
}
}

protected List<QuestSearchEntry> getBookmarkedQuests() {
if(questList == null) {
questList = collectQuests();
}
return questList.stream().filter(e -> BookmarkHandler.isBookmarked(e.getQuest().getKey()))
.sorted(Comparator.comparing(e -> BookmarkHandler.getIndexOf(e.getQuest().getKey()))).collect(Collectors.toList());
}

private List<QuestSearchEntry> collectQuests() {
return QuestLineDatabase.INSTANCE.entrySet().stream().flatMap(iQuestLineDBEntry ->
iQuestLineDBEntry.getValue().entrySet().stream().map(iQuestLineEntryDBEntry ->
createQuestSearchEntry(iQuestLineEntryDBEntry, iQuestLineDBEntry)
)).collect(Collectors.toList());
}

private QuestSearchEntry createQuestSearchEntry(Map.Entry<UUID, IQuestLineEntry> iQuestLineEntryDBEntry, Map.Entry<UUID, IQuestLine> iQuestLineDBEntry){
UUID questId = iQuestLineEntryDBEntry.getKey();
Map.Entry<UUID, IQuest> quest = Maps.immutableEntry(questId, QuestDatabase.INSTANCE.get(questId));
return new QuestSearchEntry(quest, iQuestLineDBEntry);
}

public void setQuestHighlightCallback(Consumer<QuestSearchEntry> questHighlightCallback) {
this.questHighlightCallback = questHighlightCallback;
}

public void setQuestOpenCallback(Consumer<QuestSearchEntry> questOpenCallback) {
this.questOpenCallback = questOpenCallback;
}

protected void addResult(QuestSearchEntry entry, int index, int cachedWidth) {
PanelButtonCustom buttonContainer = createContainerButton(entry, index, cachedWidth);

addTextBox(cachedWidth, buttonContainer, 56, 6, QuestTranslation.translateQuestLineName(entry.getQuestLineEntry()));
addTextBox(cachedWidth, buttonContainer, 36, 20, QuestTranslation.translateQuestName(entry.getQuest()));
}

private void addTextBox(int cachedWidth, PanelButtonCustom buttonContainer, int xOffset, int yOffset, String text) {
PanelTextBox questName = new PanelTextBox(new GuiRectangle(xOffset, yOffset, cachedWidth - xOffset, 16), QuestTranslation.translate(text));
buttonContainer.addPanel(questName);
}

private PanelButtonCustom createContainerButton(QuestSearchEntry entry, int index, int cachedWidth){
PanelButtonCustom buttonContainer = new PanelButtonCustom(new GuiRectangle(0, index * 32, cachedWidth, 32, 0), 2);
buttonContainer.setCallback(panelButtonCustom -> {
if (!buttonContainer.isActive())
return;
if (questHighlightCallback != null)
questHighlightCallback.accept(entry);
});
buttonContainer.setActive(QuestCache.isQuestShown(entry.getQuest().getValue(), questingUUID, player));
this.addPanel(buttonContainer);

buttonContainer.addPanel(createQuestPanelButton(entry));

buttonContainer.addPanel(
new PanelGeneric(
new GuiRectangle(36, 2, 14, 14, 0),
new OreDictTexture(1F, entry.getQuestLineEntry().getValue().getProperty(NativeProps.ICON),
false,
true)
)
);
return buttonContainer;
}

private PanelButtonQuest createQuestPanelButton(QuestSearchEntry entry){
PanelButtonQuest questButton = new PanelButtonQuest(
new GuiRectangle(2, 2, 28, 28),
0,
"",
entry.getQuest()
);

questButton.setCallback(value -> {
if (!questButton.isActive())
return;
if (questOpenCallback != null)
questOpenCallback.accept(entry);
});
return questButton;
}

public PanelButtonQuest getButtonAt(int mx, int my) {
for(IGuiPanel panel : getChildren()) {
if(panel instanceof PanelButtonCustom){
int smx = mx - getTransform().getX() + getScrollX();
int smy = my - getTransform().getY() + getScrollY();
if(panel.getTransform().contains(smx, smy)){
for(IGuiPanel btn : ((PanelButtonCustom) panel).getChildren()){
if(btn instanceof PanelButtonQuest){
return (PanelButtonQuest) btn;
}
}
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ public enum PresetIcon
ICON_ZOOM("icon_zoom"),
ICON_ZOOM_IN("icon_zoom_in"),
ICON_ZOOM_OUT("icon_zoom_out"),

ICON_BOOKMARK("icon_bookmark"),
ICON_QUEST("icon_quest"),

ICON_CHEST("icon_chest"),
ICON_CHEST_ALL("icon_chest_all"),
ICON_BOOKMARK("icon_bookmark"),
ICON_MENU("icon_menu"),

ICON_PATREON("icon_patreon"),
Expand Down Expand Up @@ -207,6 +209,7 @@ public static void registerIcons(IThemeRegistry reg)
reg.setDefaultTexture(ICON_CHEST.key, new SimpleTexture(TX_ICONS, new GuiRectangle(160, 64, 16, 16)).maintainAspect(true));
reg.setDefaultTexture(ICON_BOOKMARK.key, new SimpleTexture(TX_ICONS, new GuiRectangle(176, 64, 16, 16)).maintainAspect(true));
reg.setDefaultTexture(ICON_MENU.key, new SimpleTexture(TX_ICONS, new GuiRectangle(192, 64, 16, 16)).maintainAspect(true));
reg.setDefaultTexture(ICON_QUEST.key, new SimpleTexture(TX_ICONS, new GuiRectangle(80, 80, 16, 16)).maintainAspect(true));

reg.setDefaultTexture(ICON_PATREON.key, new SimpleTexture(TX_ICONS, new GuiRectangle(144, 80, 16, 16)).maintainAspect(true));
reg.setDefaultTexture(ICON_TWITCH.key, new SimpleTexture(TX_ICONS, new GuiRectangle(160, 80, 16, 16)).maintainAspect(true));
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/betterquesting/client/BookmarkHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package betterquesting.client;

import betterquesting.core.BetterQuesting;
import cpw.mods.fml.common.FMLCommonHandler;
import net.minecraft.client.Minecraft;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class BookmarkHandler {

private static final List<String> BOOKMARKS = new ArrayList<>();
private static final String BOOKMARK_DIR = BetterQuesting.MODID + "/bookmarks/";
private static File bookmarkFile;
private static boolean hasChanged = true;

public static boolean bookmarkQuest(UUID questId) {
String uuid = questId.toString();
boolean added = false;
if(BOOKMARKS.contains(uuid)){
BOOKMARKS.remove(uuid);
}else{
BOOKMARKS.add(uuid);
added = true;
}

hasChanged = true;
saveBookmarks();
return added;
}

public static int getIndexOf(UUID questId) {
return BOOKMARKS.indexOf(questId.toString());
}

public static boolean isBookmarked(UUID questId) {
return BOOKMARKS.contains(questId.toString());
}

public static boolean hasChanged() {
if(hasChanged) {
hasChanged = false;
return true;
}
return false;
}

private static void saveBookmarks() {
try{
FileUtils.writeLines(bookmarkFile, BOOKMARKS);
} catch(IOException ignored) {
BetterQuesting.logger.warn("Failed to save bookmarks.");
}
}

public static void loadBookmarks(String address) {
String identifier = getIdentifier(address);
bookmarkFile = new File(BOOKMARK_DIR, String.format("%s.txt", identifier));
BOOKMARKS.clear();
hasChanged = true;

if(!bookmarkFile.exists()) return;

try{
List<String> bookmarks = FileUtils.readLines(bookmarkFile, StandardCharsets.UTF_8);
BOOKMARKS.addAll(bookmarks);
} catch (IOException ignored) {
BetterQuesting.logger.warn("Failed to load bookmarks for {}", identifier);
}
}

private static String getIdentifier(String address){
if(Minecraft.getMinecraft().isSingleplayer()){
return FMLCommonHandler.instance().getMinecraftServerInstance().getFolderName();
}

int index = address.indexOf("/") + 1;
return address.substring(index).replace(":", ".");
}
}
Loading