Skip to content

Commit a9deee9

Browse files
committed
Variables - fix variables not having a final saving
1 parent a5db968 commit a9deee9

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/main/java/io/github/syst3ms/skriptparser/variables/VariableMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public class VariableMap {
1818

1919
private final Map<String, Object> map = new HashMap<>(); // Ordering is not important right now
2020

21+
public Map<String, Object> getMap() {
22+
return this.map;
23+
}
24+
2125
private static String[] splitList(String name) {
2226
return Pattern.compile(Pattern.quote(Variables.LIST_SEPARATOR)).split(name);
2327
}

src/main/java/io/github/syst3ms/skriptparser/variables/Variables.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ public static ReentrantLock getLock() {
5656

5757
public static void shutdown() {
5858
for (VariableStorage storage : STORAGES) {
59+
// Write all variables to their storages
60+
// This needs to be done on shutdown to make sure changes to an object
61+
// are properly serialized and saved after their initial save
62+
variableMap.getMap().forEach((key, object) -> {
63+
if (!storage.accept(key)) return;
64+
65+
SerializedVariable serialized = storage.serialize(key, object);
66+
if (serialized != null) {
67+
SerializedVariable.Value value = serialized.value();
68+
if (value == null) {
69+
storage.save(key, null, null);
70+
} else {
71+
storage.save(key, value.type(), value.data());
72+
}
73+
}
74+
75+
});
76+
// Close storage
5977
try {
6078
storage.close();
6179
} catch (IOException e) {

0 commit comments

Comments
 (0)