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

[1.10.2] Adding bookmark feature #3792

Open
wants to merge 8 commits into
base: 1.10
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ out/
build/*
/bin/
changelog.html

.DS_Store
.vscode/
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ curse_project_id=238222

version_major=3
version_minor=14
version_patch=8
version_patch=9
88 changes: 88 additions & 0 deletions src/main/java/mezz/jei/IngredientBookmarks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package mezz.jei;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import mezz.jei.api.ingredients.IIngredientBookmarks;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.api.ingredients.IIngredientRenderer;
import mezz.jei.config.Config;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.util.IngredientListElement;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;

public class IngredientBookmarks implements IIngredientBookmarks {
private final IIngredientRegistry ingredientRegistry;

// Using both cause linked list will retain order of insertion
private List<String> bookmarkIds = new LinkedList<String>();
private HashMap<String, Object> bookmarkList = new HashMap<String, Object>();

public IngredientBookmarks(IIngredientRegistry ingredientRegistry) {
this.ingredientRegistry = ingredientRegistry;

String[] bookmarks = Config.getBookmarks();

for (String uniqueId : bookmarks) {
bookmarkIds.add(uniqueId);
bookmarkList.put(uniqueId, findIngredientFromUniqueId(uniqueId));
}
}

@Override
public <V> void toggleIngredientBookmark(V ingredient) {
IIngredientHelper<V> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredient);
String uniqueId = ingredientHelper.getUniqueId(ingredient);

// System.out.println(ingredientHelper.getUniqueId(ingredient));
if (bookmarkIds.contains(uniqueId)) {
bookmarkIds.remove(uniqueId);
bookmarkList.remove(uniqueId);
} else {
bookmarkIds.add(uniqueId);
bookmarkList.put(uniqueId, findIngredientFromUniqueId(uniqueId));
}
Config.updateBookmarks(bookmarkIds.toArray(new String[bookmarkIds.size()]));
}

private <V> V findIngredientFromUniqueId(String uniqueId) {
ImmutableCollection<Class> classes = ingredientRegistry.getRegisteredIngredientClasses();
for (Class<V> clazz : classes) {
ImmutableList<V> iList = ingredientRegistry.getIngredients(clazz);
IIngredientHelper<V> iHelper = ingredientRegistry.getIngredientHelper(clazz);
for (V i : iList) {
if (iHelper.getUniqueId(i).equals(uniqueId)) {
return i;
}
}
}
return null;
}

@Override
public List<IIngredientListElement> getIngredientList() {
List<IIngredientListElement> ingredientListElements = new LinkedList<IIngredientListElement>();

for (String uniqueId : bookmarkIds) {
Object ingredient = bookmarkList.get(uniqueId);

IIngredientHelper<Object> ingredientHelper = ingredientRegistry.getIngredientHelper(ingredient);
IIngredientRenderer<Object> ingredientRenderer = ingredientRegistry.getIngredientRenderer(ingredient);
IngredientListElement<Object> ingredientListElement = IngredientListElement.create(ingredient, ingredientHelper,
ingredientRenderer);
if (ingredientListElement != null) {
ingredientListElements.add(ingredientListElement);
}
}
return ingredientListElements;
}

@Override
public void clear() {
bookmarkIds.clear();
bookmarkList.clear();
}
}
9 changes: 8 additions & 1 deletion src/main/java/mezz/jei/JeiRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import mezz.jei.api.IJeiRuntime;
import mezz.jei.api.gui.IAdvancedGuiHandler;
import mezz.jei.api.ingredients.IIngredientBookmarks;
import mezz.jei.gui.ItemListOverlay;
import mezz.jei.gui.recipes.RecipesGui;
import net.minecraft.client.gui.GuiScreen;
Expand All @@ -17,13 +18,15 @@ public class JeiRuntime implements IJeiRuntime {
private final RecipesGui recipesGui;
private final IngredientRegistry ingredientRegistry;
private final List<IAdvancedGuiHandler<?>> advancedGuiHandlers;
private final IngredientBookmarks ingredientBookmarks;

public JeiRuntime(RecipeRegistry recipeRegistry, ItemListOverlay itemListOverlay, RecipesGui recipesGui, IngredientRegistry ingredientRegistry, List<IAdvancedGuiHandler<?>> advancedGuiHandlers) {
public JeiRuntime(RecipeRegistry recipeRegistry, ItemListOverlay itemListOverlay, RecipesGui recipesGui, IngredientRegistry ingredientRegistry, List<IAdvancedGuiHandler<?>> advancedGuiHandlers, IngredientBookmarks ingredientBookmarks) {
this.recipeRegistry = recipeRegistry;
this.itemListOverlay = itemListOverlay;
this.recipesGui = recipesGui;
this.ingredientRegistry = ingredientRegistry;
this.advancedGuiHandlers = advancedGuiHandlers;
this.ingredientBookmarks = ingredientBookmarks;
}

public void close() {
Expand Down Expand Up @@ -54,6 +57,10 @@ public IngredientRegistry getIngredientRegistry() {
return ingredientRegistry;
}

public IIngredientBookmarks getIngredientBookmarks() {
return ingredientBookmarks;
}

public List<IAdvancedGuiHandler<?>> getActiveAdvancedGuiHandlers(GuiScreen guiScreen) {
List<IAdvancedGuiHandler<?>> activeAdvancedGuiHandler = new ArrayList<IAdvancedGuiHandler<?>>();
if (guiScreen instanceof GuiContainer) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/mezz/jei/JeiStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ public void start(List<IModPlugin> plugins, boolean resourceReload) {

Log.info("Building runtime...");
start_time = System.currentTimeMillis();
IngredientBookmarks ingredientBookmarks = new IngredientBookmarks(ingredientRegistry);
List<IAdvancedGuiHandler<?>> advancedGuiHandlers = modRegistry.getAdvancedGuiHandlers();
ItemListOverlay itemListOverlay = new ItemListOverlay(itemFilter, advancedGuiHandlers, ingredientRegistry);
ItemListOverlay itemListOverlay = new ItemListOverlay(itemFilter, advancedGuiHandlers, ingredientRegistry, ingredientBookmarks);
RecipesGui recipesGui = new RecipesGui(recipeRegistry);
JeiRuntime jeiRuntime = new JeiRuntime(recipeRegistry, itemListOverlay, recipesGui, ingredientRegistry, advancedGuiHandlers);
JeiRuntime jeiRuntime = new JeiRuntime(recipeRegistry, itemListOverlay, recipesGui, ingredientRegistry, advancedGuiHandlers, ingredientBookmarks);
Internal.setRuntime(jeiRuntime);
Log.info("Built runtime in {} ms", System.currentTimeMillis() - start_time);

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/mezz/jei/api/ingredients/IIngredientBookmarks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mezz.jei.api.ingredients;

import java.util.List;

import mezz.jei.gui.ingredients.IIngredientListElement;

public interface IIngredientBookmarks {
/**
* Toggles visibility of ingredient in the bookmark list.
*/
<V> void toggleIngredientBookmark(V ingredient);

List<IIngredientListElement> getIngredientList();

void clear();
}
52 changes: 50 additions & 2 deletions src/main/java/mezz/jei/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Set;

Expand Down Expand Up @@ -40,6 +42,8 @@ public class Config {
private static LocalizedConfiguration itemBlacklistConfig;
@Nullable
private static LocalizedConfiguration searchColorsConfig;
@Nullable
private static LocalizedConfiguration bookmarksConfig;

// advanced
private static boolean debugModeEnabled = false;
Expand Down Expand Up @@ -73,7 +77,11 @@ public class Config {

// item blacklist
private static final Set<String> itemBlacklist = new HashSet<String>();
private static final String[] defaultItemBlacklist = new String[]{};
private static final String[] defaultItemBlacklist = new String[] {};

// bookmarks
private static final String[] defaultBookmarks = new String[] {};
private static String[] bookmarks;

private Config() {

Expand Down Expand Up @@ -226,6 +234,8 @@ public static void preInit(FMLPreInitializationEvent event) {
final File itemBlacklistConfigFile = new File(jeiConfigurationDir, "itemBlacklist.cfg");
final File searchColorsConfigFile = new File(jeiConfigurationDir, "searchColors.cfg");
final File worldConfigFile = new File(jeiConfigurationDir, "worldSettings.cfg");
final File bookmarksConfigFile = new File(jeiConfigurationDir, "bookmarks.cfg");

worldConfig = new Configuration(worldConfigFile, "0.1.0");

{
Expand Down Expand Up @@ -257,10 +267,12 @@ public static void preInit(FMLPreInitializationEvent event) {
config = new LocalizedConfiguration(configKeyPrefix, configFile, "0.2.0");
itemBlacklistConfig = new LocalizedConfiguration(configKeyPrefix, itemBlacklistConfigFile, "0.1.0");
searchColorsConfig = new LocalizedConfiguration(configKeyPrefix, searchColorsConfigFile, "0.1.0");
bookmarksConfig = new LocalizedConfiguration(configKeyPrefix, bookmarksConfigFile, "0.1.0");

syncConfig();
syncItemBlacklistConfig();
syncSearchColorsConfig();
syncBookmarksConfig();
}

public static boolean syncAllConfig() {
Expand Down Expand Up @@ -315,7 +327,7 @@ private static boolean syncConfig() {
searchCategory.remove("prefixRequiredForCreativeTabSearch");
searchCategory.remove("prefixRequiredForColorSearch");

SearchMode[] searchModes = SearchMode.values();
SearchMode[] searchModes = SearchMode.values();
modNameSearchMode = config.getEnum("modNameSearchMode", CATEGORY_SEARCH, defaultModNameSearchMode, searchModes);
tooltipSearchMode = config.getEnum("tooltipSearchMode", CATEGORY_SEARCH, defaultTooltipSearchMode, searchModes);
oreDictSearchMode = config.getEnum("oreDictSearchMode", CATEGORY_SEARCH, defaultOreDictSearchMode, searchModes);
Expand Down Expand Up @@ -402,6 +414,42 @@ private static boolean syncItemBlacklistConfig() {
return configChanged;
}

private static boolean syncBookmarksConfig() {
if (bookmarksConfig == null) {
return false;
}

bookmarksConfig.addCategory(CATEGORY_ADVANCED);

bookmarks = bookmarksConfig.getStringList("bookmarks", CATEGORY_ADVANCED, defaultBookmarks);

final boolean configChanged = bookmarksConfig.hasChanged();
if (configChanged) {
bookmarksConfig.save();
}
return configChanged;
}

public static boolean updateBookmarks(String[] bookmarkIds) {
bookmarks = bookmarkIds;
if (bookmarksConfig == null) {
return false;
}
Property property = bookmarksConfig.get(CATEGORY_ADVANCED, "bookmarks", defaultBookmarks);

property.set(bookmarks);

boolean changed = bookmarksConfig.hasChanged();
if (changed) {
bookmarksConfig.save();
}
return changed;
}

public static String[] getBookmarks() {
return bookmarks;
}

public static boolean syncWorldConfig() {
if (worldConfig == null) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mezz/jei/config/KeyBindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class KeyBindings {
public static final KeyBinding showUses = new KeyBinding("key.jei.showUses", KeyConflictContext.GUI, Keyboard.KEY_U, categoryName);
public static final KeyBinding recipeBack = new KeyBinding("key.jei.recipeBack", KeyConflictContext.GUI, Keyboard.KEY_BACK, categoryName);
public static final KeyBinding toggleCheatMode = new KeyBinding("key.jei.toggleCheatMode", KeyConflictContext.GUI, Keyboard.KEY_NONE, categoryName);
public static final KeyBinding toggleBookmark = new KeyBinding("key.jei.toggleBookmark", KeyConflictContext.GUI, Keyboard.KEY_A, categoryName);

public static void init() {
ClientRegistry.registerKeyBinding(toggleOverlay);
Expand All @@ -23,5 +24,6 @@ public static void init() {
ClientRegistry.registerKeyBinding(showUses);
ClientRegistry.registerKeyBinding(recipeBack);
ClientRegistry.registerKeyBinding(toggleCheatMode);
ClientRegistry.registerKeyBinding(toggleBookmark);
}
}
9 changes: 8 additions & 1 deletion src/main/java/mezz/jei/gui/ItemListOverlay.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import mezz.jei.ItemFilter;
import mezz.jei.api.IItemListOverlay;
import mezz.jei.api.gui.IAdvancedGuiHandler;
import mezz.jei.api.ingredients.IIngredientBookmarks;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.config.Config;
import mezz.jei.util.Log;
Expand All @@ -21,14 +22,16 @@ public class ItemListOverlay implements IItemListOverlay {
private final List<IAdvancedGuiHandler<?>> advancedGuiHandlers;
private final IIngredientRegistry ingredientRegistry;
private final Set<ItemStack> highlightedStacks = new HashSet<ItemStack>();
private final IIngredientBookmarks ingredientBookmarks;

@Nullable
private ItemListOverlayInternal internal;

public ItemListOverlay(ItemFilter itemFilter, List<IAdvancedGuiHandler<?>> advancedGuiHandlers, IIngredientRegistry ingredientRegistry) {
public ItemListOverlay(ItemFilter itemFilter, List<IAdvancedGuiHandler<?>> advancedGuiHandlers, IIngredientRegistry ingredientRegistry, IIngredientBookmarks ingredientBookmarks) {
this.itemFilter = itemFilter;
this.advancedGuiHandlers = advancedGuiHandlers;
this.ingredientRegistry = ingredientRegistry;
this.ingredientBookmarks = ingredientBookmarks;
}

@Nullable
Expand Down Expand Up @@ -132,4 +135,8 @@ public void close() {
}
internal = null;
}

public IIngredientBookmarks getIngredientBookmarks() {
return ingredientBookmarks;
}
}
Loading