mirror of
https://github.com/ViaVersion/ViaFabricPlus.git
synced 2025-03-11 13:28:50 +01:00
Only create one shutdown hook for save files
This commit is contained in:
parent
c62aa7516c
commit
ddec37d8c8
9
.github/workflows/build.yml
vendored
9
.github/workflows/build.yml
vendored
@ -15,15 +15,6 @@ jobs:
|
|||||||
distribution: 'temurin'
|
distribution: 'temurin'
|
||||||
java-version: 17
|
java-version: 17
|
||||||
check-latest: true
|
check-latest: true
|
||||||
- name: Cache Dependencies
|
|
||||||
uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: |
|
|
||||||
~/.gradle/caches
|
|
||||||
~/.gradle/wrapper
|
|
||||||
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-gradle-
|
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew build
|
run: ./gradlew build
|
||||||
- name: Upload Artifacts
|
- name: Upload Artifacts
|
||||||
|
@ -45,7 +45,6 @@ public abstract class AbstractSave {
|
|||||||
/**
|
/**
|
||||||
* This method should be called when the file should be initialized.
|
* This method should be called when the file should be initialized.
|
||||||
* It will read the file and call the {@link #read(JsonObject)} method.
|
* It will read the file and call the {@link #read(JsonObject)} method.
|
||||||
* It will also write the file when the program is closed using the {@link #write(JsonObject)}.
|
|
||||||
*/
|
*/
|
||||||
public void init() {
|
public void init() {
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
@ -59,24 +58,27 @@ public abstract class AbstractSave {
|
|||||||
read(parentNode);
|
read(parentNode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
/**
|
||||||
try {
|
* This method should be called when the file should be saved.
|
||||||
file.delete();
|
*/
|
||||||
file.createNewFile();
|
public void save() {
|
||||||
} catch (IOException e) {
|
try {
|
||||||
ViaFabricPlus.global().getLogger().error("Failed to create file: " + file.getName() + "!");
|
file.delete();
|
||||||
}
|
file.createNewFile();
|
||||||
|
} catch (IOException e) {
|
||||||
|
ViaFabricPlus.global().getLogger().error("Failed to create file: " + file.getName() + "!");
|
||||||
|
}
|
||||||
|
|
||||||
try (final FileWriter fw = new FileWriter(file)) {
|
try (final FileWriter fw = new FileWriter(file)) {
|
||||||
final JsonObject parentNode = new JsonObject();
|
final JsonObject parentNode = new JsonObject();
|
||||||
write(parentNode);
|
write(parentNode);
|
||||||
fw.write(GSON.toJson(parentNode));
|
fw.write(GSON.toJson(parentNode));
|
||||||
fw.flush();
|
fw.flush();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
ViaFabricPlus.global().getLogger().error("Failed to write file: " + file.getName() + "!");
|
ViaFabricPlus.global().getLogger().error("Failed to write file: " + file.getName() + "!");
|
||||||
}
|
}
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void write(final JsonObject object);
|
public abstract void write(final JsonObject object);
|
||||||
|
@ -44,6 +44,12 @@ public class SaveManager {
|
|||||||
for (AbstractSave save : saves) {
|
for (AbstractSave save : saves) {
|
||||||
save.init();
|
save.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||||
|
for (AbstractSave save : saves) {
|
||||||
|
save.save();
|
||||||
|
}
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(final AbstractSave... saves) {
|
public void add(final AbstractSave... saves) {
|
||||||
|
Loading…
Reference in New Issue
Block a user