DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamfireStorageManager.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamfireStorage;
25
26import java.util.LinkedHashMap;
27import java.util.Map;
28import java.util.UUID;
29
54
60 private final Map<String, DreamfireStorageObject<?>> serverStorage = new LinkedHashMap<>();
61
67 private final Map<UUID, Map<String, DreamfireStorageObject<?>>> playerStorage = new LinkedHashMap<>();
68
79 @SuppressWarnings("unchecked")
80 public <T> DreamfireStorageObject<T> getData(Object key, UUID uuid) {
81 return (DreamfireStorageObject<T>) getStorageMap(uuid).get(keyToString(key));
82 }
83
94 public <T> T getValue(Object key, UUID uuid) {
95 DreamfireStorageObject<T> storageObject = getData(key, uuid);
96 return storageObject == null ? null : storageObject.storageData();
97 }
98
118 public <T> DreamfireStorageObject<T> storeData(Object key, DreamfireStorageObject<T> obj, UUID uuid) {
119 if(!new StorageObjectAddedEvent(obj).isCancelled()){
120 getStorageMap(uuid).put(keyToString(key), obj);
121 }
122 return obj;
123 }
124
134 public boolean containsData(Object key, UUID uuid) {
135 return getStorageMap(uuid).containsKey(keyToString(key));
136 }
137
159 public DreamfireStorageObject<?> removeData(Object key, UUID uuid) {
160 var data = getStorageMap(uuid).remove(keyToString(key));
161 if(data != null && !new StorageObjectRemovedEvent(data).isCancelled()){
162 getStorageMap(uuid).remove(keyToString(key));
163 }
164 return data;
165 }
166
175 private Map<String, DreamfireStorageObject<?>> getStorageMap(UUID uuid) {
176 return (uuid == null) ? serverStorage : playerStorage.computeIfAbsent(uuid, k -> new LinkedHashMap<>());
177 }
178
187 private String keyToString(Object key) {
188 return key.toString();
189 }
190}
Manages simple, plugin-scoped storage objects for both server-wide (global) and per-player contexts.
boolean containsData(Object key, UUID uuid)
Checks whether a key exists for the given namespace.
DreamfireStorageObject<?> removeData(Object key, UUID uuid)
Removes a stored object (if present) for the given key/namespace.
Fired before a DreamfireStorageObject is stored in DreamfireStorageManager.
Fired after a DreamfireStorageObject has been removed from DreamfireStorageManager.
record DreamfireStorageObject< T >(T storageData)
Immutable wrapper that holds a single stored value for use with DreamfireStorageManager.