Skip to content

Commit

Permalink
Мove bookmarks.ini (#194)
Browse files Browse the repository at this point in the history
* move bookmarks.ini

* add world specific bookmarks option

Co-authored-by: Serghei Borovetchi <[email protected]>
  • Loading branch information
slprime and Serghei Borovetchi authored Jan 9, 2022
1 parent 144a37c commit cab0aff
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
59 changes: 50 additions & 9 deletions src/main/java/codechicken/nei/BookmarkPanel.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codechicken.nei;

import codechicken.core.CommonUtils;
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.util.NBTJson;
import codechicken.nei.recipe.StackInfo;
Expand All @@ -21,6 +22,10 @@
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -30,6 +35,7 @@
public class BookmarkPanel extends ItemPanel
{

protected File bookmarkFile;
protected boolean bookmarksIsLoaded = false;
public int sortedStackIndex = -1;
public int sortedNamespaceIndex = -1;
Expand Down Expand Up @@ -403,6 +409,43 @@ protected boolean nextNamespace()
return true;
}

public void setBookmarkFile(String worldPath)
{

final File dir = new File(CommonUtils.getMinecraftDir(), "saves/NEI/" + worldPath);

if (!dir.exists()) {
dir.mkdirs();
}

bookmarkFile = new File(dir, "bookmarks.ini");

if (!bookmarkFile.exists()) {
final File globalBookmarks = new File(CommonUtils.getMinecraftDir(), "saves/NEI/global/bookmarks.ini");
final File configBookmarks = new File(NEIClientConfig.configDir, "bookmarks.ini");
final File defaultBookmarks = configBookmarks.exists()? configBookmarks: globalBookmarks;

if (defaultBookmarks.exists()) {

try {
bookmarkFile.createNewFile();

InputStream src = new FileInputStream(defaultBookmarks);
OutputStream dst = new FileOutputStream(bookmarkFile);

IOUtils.copy(src, dst);

src.close();
dst.close();

} catch(IOException e) {}

}

}

bookmarksIsLoaded = false;
}

public void saveBookmarks()
{
Expand Down Expand Up @@ -444,12 +487,11 @@ public void saveBookmarks()

}

File file = NEIClientConfig.bookmarkFile;
if (file != null) {
try(FileWriter writer = new FileWriter(file)) {
if (bookmarkFile != null) {
try(FileWriter writer = new FileWriter(bookmarkFile)) {
IOUtils.writeLines(strings, "\n", writer);
} catch (IOException e) {
NEIClientConfig.logger.error("Filed to save bookmarks list to file {}", file, e);
NEIClientConfig.logger.error("Filed to save bookmarks list to file {}", bookmarkFile, e);
}
}

Expand All @@ -464,17 +506,16 @@ public void loadBookmarksIfNeeded()

bookmarksIsLoaded = true;

File file = NEIClientConfig.bookmarkFile;
if (file == null || !file.exists()) {
if (bookmarkFile == null || !bookmarkFile.exists()) {
return;
}

List<String> itemStrings;
try (FileReader reader = new FileReader(file)) {
NEIClientConfig.logger.info("Loading bookmarks from file {}", file);
try (FileReader reader = new FileReader(bookmarkFile)) {
NEIClientConfig.logger.info("Loading bookmarks from file {}", bookmarkFile);
itemStrings = IOUtils.readLines(reader);
} catch (IOException e) {
NEIClientConfig.logger.error("Failed to load bookmarks from file {}", file, e);
NEIClientConfig.logger.error("Failed to load bookmarks from file {}", bookmarkFile, e);
return;
}

Expand Down
41 changes: 34 additions & 7 deletions src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,22 @@
public class NEIClientConfig {
private static boolean configLoaded;
private static boolean enabledOverride;
private static String worldPath;

public static Logger logger = LogManager.getLogger("NotEnoughItems");
public static File configDir = new File(CommonUtils.getMinecraftDir(), "config/NEI/");
public static ConfigSet global = new ConfigSet(
new File("saves/NEI/client.dat"),
new ConfigFile(new File(configDir, "client.cfg")));
public static ConfigSet world;
public static final File bookmarkFile = new File(configDir, "bookmarks.ini");
public static final File handlerFile = new File(configDir, "handlers.csv");
public static final File serialHandlersFile = new File(configDir, "serialhandlers.cfg");
public static final File heightHackHandlersFile = new File(configDir, "heighthackhandlers.cfg");
public static final File handlerOrderingFile = new File(configDir, "handlerordering.csv");

@Deprecated
public static File bookmarkFile;

// Set of handlers that need to be run in serial
public static HashSet<String> serialHandlers = new HashSet<>();

Expand Down Expand Up @@ -177,6 +180,15 @@ public boolean isEnabled() {
tag.getTag("command.heal").setDefaultValue("");
API.addOption(new OptionTextField("command.heal"));

tag.getTag("inventory.worldSpecificBookmarks").setComment("Global or world specific bookmarks").getBooleanValue(false);
API.addOption(new OptionToggleButton("inventory.worldSpecificBookmarks", true) {
@Override
public boolean onClick(int button) {
super.onClick(button);
initBookmarkFile(worldPath);
return true;
}
});
tag.getTag("inventory.bookmarksEnabled").setComment("Enable/disable bookmarks").getBooleanValue(true);
API.addOption(new OptionToggleButton("inventory.bookmarksEnabled", true));
tag.getTag("inventory.saveCurrentRecipeInBookmarksEnabled").setComment("Save Current Recipe in Bookmarks").getBooleanValue(true);
Expand Down Expand Up @@ -262,17 +274,22 @@ public static OptionList getOptionList() {
return OptionList.getOptionList("nei.options");
}

public static void loadWorld(String saveName) {
public static void loadWorld(String worldPath) {
NEIClientConfig.worldPath = worldPath;

setInternalEnabled(true);
logger.debug("Loading "+(Minecraft.getMinecraft().isSingleplayer() ? "Local" : "Remote")+" World");
bootNEI(ClientUtils.getWorld());

File saveDir = new File(CommonUtils.getMinecraftDir(), "saves/NEI/" + saveName);
boolean newWorld = !saveDir.exists();
if (newWorld)
saveDir.mkdirs();
final File specificDir = new File(CommonUtils.getMinecraftDir(), "saves/NEI/" + worldPath);
final boolean newWorld = !specificDir.exists();

if (newWorld) {
specificDir.mkdirs();
}

world = new ConfigSet(new File(saveDir, "NEI.dat"), new ConfigFile(new File(saveDir, "NEI.cfg")));
initBookmarkFile(worldPath);
world = new ConfigSet(new File(specificDir, "NEI.dat"), new ConfigFile(new File(specificDir, "NEI.cfg")));
onWorldLoad(newWorld);
}

Expand Down Expand Up @@ -344,6 +361,16 @@ public void run() {
ItemSorter.loadConfig();
}

private static void initBookmarkFile(String worldPath)
{

if (!global.config.getTag("inventory.worldSpecificBookmarks").getBooleanValue()) {
worldPath = "global";
}

ItemPanels.bookmarkPanel.setBookmarkFile(worldPath);
}

public static boolean isWorldSpecific(String setting) {
if(world == null) return false;
ConfigTag tag = world.config.getTag(setting, false);
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/assets/nei/lang/en_US.lang
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ nei.options.keys.world.creative=Gamemode
nei.options.keys.world.creative.tip=Cycle Gamemode

nei.options.inventory=Inventory
nei.options.inventory.worldSpecificBookmarks=World Specific Bookmarks
nei.options.inventory.worldSpecificBookmarks.true=Yes
nei.options.inventory.worldSpecificBookmarks.false=No
nei.options.inventory.bookmarksEnabled=Bookmarks Panel Visibility
nei.options.inventory.bookmarksEnabled.true=Visible
nei.options.inventory.bookmarksEnabled.false=Hidden
Expand Down

0 comments on commit cab0aff

Please sign in to comment.