forked from cookies-mod/mod
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into reforgetooltip
- Loading branch information
Showing
40 changed files
with
1,141 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/main/generated/.cache/cabcb80d088276cffde41e74584028f1c00b99b8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
// 1.21.3 2024-12-11T14:15:47.3017455 cookies-mod/Language (en_us) | ||
b38b189b066d12ab0c2d533a84fa00f9718ed60c assets\cookies-mod\lang\en_us.json | ||
b38b189b066d12ab0c2d533a84fa00f9718ed60c assets\cookies-mod\lang\en_us.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/codes/cookies/mod/data/cookiesmoddata/CookieDataInstances.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package codes.cookies.mod.data.cookiesmoddata; | ||
|
||
import codes.cookies.mod.data.farming.GardenKeybindsData; | ||
|
||
public final class CookieDataInstances { | ||
public static final GardenKeybindsData gardenKeybindsData = new GardenKeybindsData(); | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/codes/cookies/mod/data/cookiesmoddata/CookieDataManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package codes.cookies.mod.data.cookiesmoddata; | ||
|
||
import codes.cookies.mod.utils.exceptions.ExceptionHandler; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParser; | ||
import lombok.SneakyThrows; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.Field; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
|
||
public class CookieDataManager { | ||
public static final Path MOD_DATA_FOLDER = Path.of(FabricLoader.getInstance().getConfigDir().toString(), "cookiesmod/data"); | ||
|
||
public static void load() { | ||
if(!Files.exists(MOD_DATA_FOLDER)) { | ||
ExceptionHandler.tryCatch(() -> Files.createDirectories(MOD_DATA_FOLDER)); | ||
} | ||
|
||
for (Field declaredField : CookieDataInstances.class.getDeclaredFields()) { | ||
ExceptionHandler.tryCatch(() -> loadField(declaredField)); | ||
} | ||
} | ||
|
||
public static void save(CookiesModData modData) { | ||
final Path dataLocation = MOD_DATA_FOLDER.resolve(modData.getFileLocation()); | ||
final JsonElement jsonElement = modData.write(); | ||
final byte[] content = jsonElement.toString().getBytes(StandardCharsets.UTF_8); | ||
|
||
ExceptionHandler.tryCatch(() -> { | ||
saveFile(dataLocation, content); | ||
}); | ||
} | ||
|
||
private static void loadField(Field declaredField) { | ||
declaredField.setAccessible(true); | ||
|
||
final Object instance; | ||
try { | ||
instance = declaredField.get(null); | ||
} catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (!(instance instanceof CookiesModData data)) { | ||
return; | ||
} | ||
|
||
final Path dataLocation = MOD_DATA_FOLDER.resolve(data.getFileLocation()); | ||
if (!Files.exists(dataLocation)) { | ||
return; | ||
} | ||
|
||
final byte[] bytes; | ||
try { | ||
bytes = readFile(dataLocation); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
final String fileContent = new String(bytes, StandardCharsets.UTF_8); | ||
final JsonElement fileElement = JsonParser.parseString(fileContent); | ||
if(fileElement.isJsonObject()) | ||
data.read(fileElement.getAsJsonObject()); | ||
} | ||
|
||
private static byte[] readFile(Path filePath) throws IOException { | ||
return Files.readAllBytes(filePath); | ||
} | ||
|
||
@SneakyThrows | ||
private static void saveFile(Path filePath, byte[] content) { | ||
Files.write(filePath, content, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/codes/cookies/mod/data/cookiesmoddata/CookiesModData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package codes.cookies.mod.data.cookiesmoddata; | ||
|
||
import codes.cookies.mod.utils.json.JsonSerializable; | ||
import lombok.SneakyThrows; | ||
|
||
public interface CookiesModData extends JsonSerializable { | ||
String getFileLocation(); | ||
|
||
default void save() { | ||
CookieDataManager.save(this); | ||
} | ||
} |
Oops, something went wrong.