Skip to content

Commit c95ee21

Browse files
committed
Some fixes for deleting
1 parent 8569bca commit c95ee21

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ public final boolean loadConfiguration(FileSection section) {
195195
return false;
196196

197197
this.file = getFile(fileName).getAbsoluteFile();
198-
199198
if (file.exists() && !file.isFile()) {
200199
logger.error("The database file '" + file.getName() + "' does not exist or is a directory.", ErrorType.SEMANTIC_ERROR);
201200
return false;
@@ -225,15 +224,20 @@ public final boolean loadConfiguration(FileSection section) {
225224
}
226225

227226
/**
228-
* Loads configurations and variables.
227+
* Loads configurations and should start loading variables too.
229228
*
230229
* @return Whether the database could be loaded successfully,
231230
* i.e. whether the configuration is correct and all variables could be loaded.
232231
*/
233232
protected abstract boolean load(FileSection section);
234233

235-
protected void load(String name, SerializedVariable variable) {
236-
load(name, variable.value.type, variable.value.data);
234+
protected void loadVariable(String name, SerializedVariable variable) {
235+
Value value = variable.value;
236+
if (value == null) {
237+
Variables.queueVariableChange(name, null);
238+
return;
239+
}
240+
loadVariable(name, value.type, value.data);
237241
}
238242

239243
/**
@@ -243,7 +247,7 @@ protected void load(String name, SerializedVariable variable) {
243247
* @param type the type of the variable.
244248
* @param value the serialized value of the variable.
245249
*/
246-
protected void load(String name, @NotNull String type, @NotNull byte[] value) {
250+
protected void loadVariable(String name, @NotNull String type, @NotNull byte[] value) {
247251
if (value == null || type == null)
248252
throw new IllegalArgumentException("value and/or typeName cannot be null");
249253
Variables.queueVariableChange(name, deserialize(type, value));
@@ -298,9 +302,9 @@ boolean accept(@Nullable String variableName) {
298302
* @return the serialized variable.
299303
*/
300304
@SuppressWarnings("unchecked")
301-
public <T> SerializedVariable serialize(String name, @NotNull T value) {
305+
public <T> SerializedVariable serialize(String name, @Nullable T value) {
302306
if (value == null)
303-
throw new IllegalArgumentException("value cannot be null");
307+
return new SerializedVariable(name, null);
304308
Type<T> type = (Type<T>) TypeManager.getByClassExact(value.getClass()).orElse(null);
305309
if (type == null)
306310
throw new UnsupportedOperationException("Class '" + value.getClass().getName() + "' cannot be serialized. No type registered.");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private static void processChangeQueue() {
232232
.filter(storage -> storage.accept(change.name))
233233
.forEach(storage -> {
234234
SerializedVariable serialized = storage.serialize(change.name, change.value);
235-
storage.save(serialized);
235+
storage.save(serialized);
236236
});
237237
}
238238
}

src/test/java/io/github/syst3ms/skriptparser/variables/DatabaseTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public void testVariables() throws InterruptedException {
6868
Optional<?> value = RamStorage.SELF.deserialize(variable.value.type, variable.value.data);
6969
assert value.isPresent();
7070
assert value.get().equals("Hello New World!") : value.get();
71+
Variables.setVariable("test", null, null, false);
72+
Thread.sleep(1);
73+
assert !Variables.getVariable("test", null, false).isPresent();
74+
assert !RamStorage.VARIABLES.containsKey("test");
7175
}
7276

7377
}

src/test/java/io/github/syst3ms/skriptparser/variables/RamStorage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.util.Map;
2020
import java.util.Optional;
2121

22+
/**
23+
* A VariableStorage that only saves into ram memory.
24+
*/
2225
public class RamStorage extends VariableStorage {
2326

2427
public static final Map<String, SerializedVariable> VARIABLES = new HashMap<>();

0 commit comments

Comments
 (0)