ScriptsLab
WikiDownloadsSourcesSupport
ScriptsLab
DocumentationDownloadsGitHubDiscord

© 2026 ScriptsLab

Back to src/main/java/com/scriptslab/core/script
J

ScriptStorage.java

Java · 172 lines · 5.0 KB

src/main/java/com/scriptslab/core/script/ScriptStorage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.scriptslab.core.script;

import com.scriptslab.ScriptsLabPlugin;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

/**
 * Simple persistent key-value storage for JavaScript scripts.
 * Each namespace maps to a separate YAML file.
 *
 * Usage in JS:
 *   var db = Storage.open('mydata');
 *   db.set('key', 'value');
 *   var val = db.get('key');
 *   db.has('key');
 *   db.remove('key');
 *   db.save();
 *   db.keys();
 */
public final class ScriptStorage {

    private final File storageDir;
    private final Map<String, Namespace> namespaces;

    public ScriptStorage(ScriptsLabPlugin plugin) {
        this.storageDir = new File(plugin.getDataFolder(), "script-data");
        this.namespaces = new ConcurrentHashMap<>();
        storageDir.mkdirs();
    }

    /**
     * Opens (or creates) a storage namespace.
     * Returns a Namespace object with get/set/save/etc.
     */
    public Namespace open(String name) {
        return namespaces.computeIfAbsent(name, n -> new Namespace(n, new File(storageDir, n + ".yml")));
    }

    /**
     * Saves all open namespaces to disk.
     */
    public void saveAll() {
        for (Namespace ns : namespaces.values()) {
            ns.save();
        }
    }

    /**
     * A single storage namespace backed by a YAML file.
     */
    public static final class Namespace {

        private final String name;
        private final File file;
        private final YamlConfiguration yaml;
        private final Map<String, Object> cache;

        Namespace(String name, File file) {
            this.name = name;
            this.file = file;
            this.cache = new ConcurrentHashMap<>();

            if (file.exists()) {
                this.yaml = YamlConfiguration.loadConfiguration(file);
                // Populate cache from file
                for (String key : yaml.getKeys(true)) {
                    if (!yaml.isConfigurationSection(key)) {
                        cache.put(key, yaml.get(key));
                    }
                }
            } else {
                this.yaml = new YamlConfiguration();
            }
        }

        /** Sets a value. Supports dot-notation paths (e.g. "player.score"). */
        public void set(String key, Object value) {
            cache.put(key, value);
            yaml.set(key, value);
        }

        /** Gets a value, or null if not found. */
        public Object get(String key) {
            if (cache.containsKey(key)) {
                return cache.get(key);
            }
            return yaml.get(key);
        }

        /** Gets a value with a default fallback. */
        public Object getOrDefault(String key, Object defaultValue) {
            Object val = get(key);
            return val != null ? val : defaultValue;
        }

        /** Returns true if the key exists. */
        public boolean has(String key) {
            return cache.containsKey(key) || yaml.contains(key);
        }

        /** Removes a key. */
        public void remove(String key) {
            cache.remove(key);
            yaml.set(key, null);
        }

        /** Returns all top-level keys. */
        public Set<String> keys() {
            return yaml.getKeys(false);
        }

        /** Returns all keys (deep). */
        public Set<String> allKeys() {
            return yaml.getKeys(true);
        }

        /** Increments a numeric value by 1 (creates with 0 if missing). */
        public double increment(String key) {
            return increment(key, 1);
        }

        /** Increments a numeric value by amount. */
        public double increment(String key, double amount) {
            Object current = get(key);
            double newVal = (current instanceof Number n ? n.doubleValue() : 0) + amount;
            set(key, newVal);
            return newVal;
        }

        /** Saves this namespace to disk. */
        public void save() {
            try {
                yaml.save(file);
            } catch (IOException e) {
                Logger.getLogger("ScriptStorage").warning(
                        "[ScriptStorage] Failed to save namespace '" + name + "': " + e.getMessage());
            }
        }

        /** Clears all data in this namespace (in-memory and on disk). */
        public void clear() {
            cache.clear();
            for (String key : yaml.getKeys(false)) {
                yaml.set(key, null);
            }
            save();
        }

        public String getName() {
            return name;
        }

        /** Returns a snapshot of all data as a plain map (for JS iteration). */
        public Map<String, Object> toMap() {
            Map<String, Object> result = new HashMap<>();
            for (String key : yaml.getKeys(true)) {
                if (!yaml.isConfigurationSection(key)) {
                    result.put(key, yaml.get(key));
                }
            }
            return result;
        }
    }
}