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 global storage #17

Merged
merged 3 commits into from
Jun 23, 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
@@ -1,7 +1,10 @@
package com.teamresourceful.resourcefullib;

import com.teamresourceful.resourcefullib.common.utils.files.GlobalStorage;

public class ResourcefulLib {
public static final String MOD_ID = "resourcefullib";
public static void init() {
GlobalStorage.init();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.teamresourceful.resourcefullib.common.utils.files;

import com.teamresourceful.resourcefullib.common.lib.Constants;
import net.minecraft.Optionull;
import org.jetbrains.annotations.ApiStatus;
import org.lwjgl.system.Platform;

import java.nio.file.Files;
import java.nio.file.Path;

/**
* Creates and manages the cache and data directories for mods.
* <p>
* You may use the getCacheDirectory and getDataDirectory methods to get the directories for your mod.
* <p>
* This does not handle any of the file IO for you, it only provides the directories paths.
* You must handle the file IO and creation of the directories yourself.
*/
public class GlobalStorage {

private static final String ID = "resourcefullib";
private static final String README = """
This is created by ResourcefulLib for usage by mods.
This directory is used to store data/caches for mods.
""";

private static boolean initialized = false;

private static final Path cache;
private static final Path data;

static {
switch (Platform.get()) {
case WINDOWS -> {
cache = Path.of(System.getenv("LOCALAPPDATA"), "." + ID, "cache");
data = Path.of(System.getenv("LOCALAPPDATA"), "." + ID, "data");
}
case MACOSX -> {
cache = Path.of(System.getProperty("user.home"), "Library", "Caches", ID);
data = Path.of(System.getProperty("user.home"), "Library", "Application Support", ID);
}
default -> {
cache = Optionull.mapOrElse(
System.getenv("XDG_CACHE_HOME"),
home -> Path.of(home, ID),
() -> Path.of(System.getProperty("user.home"), ".cache", ID)
);
data = Optionull.mapOrElse(
System.getenv("XDG_DATA_HOME"),
home -> Path.of(home, ID),
() -> Path.of(System.getProperty("user.home"), ".local", "share", ID)
);
}
}
}

@ApiStatus.Internal
public static void init() {
if (initialized) return;
initialized = true;
try {
Files.createDirectories(cache);
Files.createDirectories(data);

Path readme = cache.resolve("README.txt");
if (!Files.exists(readme)) {
Files.writeString(readme, README);
}

readme = data.resolve("README.txt");
if (!Files.exists(readme)) {
Files.writeString(readme, README);
}
} catch (Exception e) {
Constants.LOGGER.warn("Failed to create cache/data directories!", e);
}
}

/**
* Returns the cache directory for the mod.
* @param modid The modid of the mod.
* @return The cache directory for the mod.
*/
public static Path getCacheDirectory(String modid) {
init();
return cache.resolve(modid);
}

/**
* Returns the data directory for the mod.
* @param modid The modid of the mod.
* @return The data directory for the mod.
*/
public static Path getDataDirectory(String modid) {
init();
return data.resolve(modid);
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
package com.teamresourceful.resourcefullib.fabric;

import com.teamresourceful.resourcefullib.client.sysinfo.SystemInfo;
import com.teamresourceful.resourcefullib.common.utils.files.GlobalStorage;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.PackType;

import java.nio.file.Files;
import java.nio.file.Path;

public class ResourcefulLibFabricClient implements ClientModInitializer {

@Override
public void onInitializeClient() {
ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new FabricHighlightHandler());
FabricResourcePackHandler.load();

ClientCommandRegistrationCallback.EVENT.register((dispatcher, dedicated) ->
dispatcher.register(ClientCommandManager.literal("rlib")
.then(ClientCommandManager.literal("info")
.executes(context -> {
String info = SystemInfo.buildForDiscord();
Minecraft.getInstance().keyboardHandler.setClipboard(info);
context.getSource().sendFeedback(Component.literal("[Resourceful Lib]: Info copied to clipboard!"));
return 1;
})
)
)
);
ClientCommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
var command = ClientCommandManager.literal("rlib");
command.then(ClientCommandManager.literal("info")
.executes(context -> {
String info = SystemInfo.buildForDiscord();
Minecraft.getInstance().keyboardHandler.setClipboard(info);
context.getSource().sendFeedback(Component.literal("[Resourceful Lib]: Info copied to clipboard!"));
return 1;
})
);
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
command.then(ClientCommandManager.literal("teststorage")
.executes(context -> {
try {
Path file = GlobalStorage.getCacheDirectory("test").resolve("test.txt");
Files.deleteIfExists(file);
Files.createDirectories(file.getParent());
Files.write(file, "This is a test file".getBytes());
}catch (Exception e) {
throw new RuntimeException(e);
}
return 1;
})
);
}

dispatcher.register(command);
});
}
}
3 changes: 3 additions & 0 deletions fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"entrypoints": {
"client": [
"com.teamresourceful.resourcefullib.fabric.ResourcefulLibFabricClient"
],
"main": [
"com.teamresourceful.resourcefullib.ResourcefulLib::init"
]
},
"mixins": [
Expand Down
Loading