DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamPersistentEntity.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.DreamPersistentData;
25
26import com.dreamfirestudios.dreamcore.DreamChat.DreamChat;
27import com.dreamfirestudios.dreamcore.DreamChat.DreamMessageSettings;
28import com.dreamfirestudios.dreamcore.DreamCore;
29import org.bukkit.Bukkit;
30import org.bukkit.NamespacedKey;
31import org.bukkit.entity.Entity;
32import org.bukkit.persistence.PersistentDataContainer;
33import org.bukkit.persistence.PersistentDataType;
34import org.bukkit.plugin.java.JavaPlugin;
35
36import java.util.LinkedHashMap;
37
46
52 public static boolean isValidKey(String key) {
53 return key != null && key.matches("[a-z0-9/._-]{1,256}");
54 }
55
62 public static PersistentDataContainer ReturnPersistentDataContainer(Entity entity) {
63 if (entity == null) {
64 throw new IllegalArgumentException("Entity cannot be null.");
65 }
66 return entity.getPersistentDataContainer();
67 }
68
74 public static LinkedHashMap<PersistentDataTypes, LinkedHashMap<String, Object>> GetALl(Entity entity) {
75 var data = new LinkedHashMap<PersistentDataTypes, LinkedHashMap<String, Object>>();
76 for (var persistentDataType : PersistentDataTypes.values()) {
77 data.put(persistentDataType, GetALl(entity, persistentDataType));
78 }
79 return data;
80 }
81
88 public static LinkedHashMap<String, Object> GetALl(Entity entity, PersistentDataTypes persistentDataType) {
89 var data = new LinkedHashMap<String, Object>();
90 try {
91 var persistentDataContainer = ReturnPersistentDataContainer(entity);
92 var persistentData = persistentDataType.persistentDataType;
93 for (var namespacedKey : persistentDataContainer.getKeys()) {
94 data.put(namespacedKey.getKey(), persistentDataContainer.get(namespacedKey, persistentData));
95 }
96 } catch (Exception e) {
97 DreamChat.SendMessageToConsole("Error while retrieving persistent data", DreamMessageSettings.all());
98 }
99 return data;
100 }
101
115 public static <T> T Get(Entity entity, String key, PersistentDataType<?, T> type) {
116 if (entity == null) {
117 DreamChat.SendMessageToConsole("Entity is null. Cannot retrieve persistent data.", DreamMessageSettings.all());
118 return null;
119 }
120 if (!isValidKey(key)) {
121 DreamChat.SendMessageToConsole("Invalid key: " + key, DreamMessageSettings.all());
122 return null;
123 }
124 try {
125 var container = ReturnPersistentDataContainer(entity);
126 var namespacedKey = new NamespacedKey(com.dreamfirestudios.dreamcore.DreamCore.DreamCore, key);
127 return container.has(namespacedKey, type) ? container.get(namespacedKey, type) : null;
128 } catch (Exception e) {
129 DreamChat.SendMessageToConsole("Error while retrieving persistent data for key: " + key, DreamMessageSettings.all());
130 return null;
131 }
132 }
133
142 public static boolean Has(JavaPlugin javaPlugin, Entity entity, PersistentDataType persistentDataType, String key) {
143 if (entity == null) {
144 DreamChat.SendMessageToConsole("Entity is null. Cannot check for persistent data.", DreamMessageSettings.all());
145 return false;
146 }
147 if (!isValidKey(key)) {
148 DreamChat.SendMessageToConsole("Invalid key: " + key, DreamMessageSettings.all());
149 return false;
150 }
151 try {
152 javaPlugin = javaPlugin == null ? DreamCore.DreamCore : javaPlugin;
153 var persistentDataContainer = ReturnPersistentDataContainer(entity);
154 var namespacedKey = new NamespacedKey(javaPlugin, key);
155 return persistentDataContainer.has(namespacedKey, persistentDataType);
156 } catch (Exception e) {
157 DreamChat.SendMessageToConsole( "Error while checking persistent data for key: " + key, DreamMessageSettings.all());
158 return false;
159 }
160 }
161
177 public static <T> boolean Add(JavaPlugin javaPlugin, Entity entity, PersistentDataType<?, T> type, String key, T value) {
178 if (entity == null) {
179 DreamChat.SendMessageToConsole("Entity is null. Cannot add persistent data.", DreamMessageSettings.all());
180 return false;
181 }
182 if (!isValidKey(key)) {
183 DreamChat.SendMessageToConsole("Invalid key: " + key, DreamMessageSettings.all());
184 return false;
185 }
186 try {
187 javaPlugin = javaPlugin == null ? DreamCore.DreamCore : javaPlugin;
188 var container = ReturnPersistentDataContainer(entity);
189 var namespacedKey = new NamespacedKey(javaPlugin, key);
190 container.set(namespacedKey, type, value);
191 new PersistentEntityAddedEvent(entity, namespacedKey, value);
192 return true;
193 } catch (Exception e) {
194 DreamChat.SendMessageToConsole("Error while adding persistent data for key: " + key, DreamMessageSettings.all());
195 return false;
196 }
197 }
198
206 public static boolean Remove(JavaPlugin javaPlugin, Entity entity, String key) {
207 if (entity == null) {
208 DreamChat.SendMessageToConsole("Entity is null. Cannot remove persistent data.", DreamMessageSettings.all());
209 return false;
210 }
211 if (!isValidKey(key)) {
212 DreamChat.SendMessageToConsole("Invalid key: " + key, DreamMessageSettings.all());
213 return false;
214 }
215 try {
216 javaPlugin = javaPlugin == null ? DreamCore.DreamCore : javaPlugin;
217 var persistentDataContainer = ReturnPersistentDataContainer(entity);
218 var namespacedKey = new NamespacedKey(javaPlugin, key);
219 persistentDataContainer.remove(namespacedKey);
220 new PersistentEntityRemovedEvent(entity, namespacedKey);
221 return true;
222 } catch (Exception e) {
223 DreamChat.SendMessageToConsole("Error while removing persistent data for key: " + key, DreamMessageSettings.all());
224 return false;
225 }
226 }
227
238 public static void CloneData(Entity from, Entity to) {
239 if (from == null || to == null) {
240 DreamChat.SendMessageToConsole("Source or target entity is null. Cannot clone data.", DreamMessageSettings.all());
241 return;
242 }
243 try {
244 var fromData = GetALl(from);
245 fromData.forEach((type, values) -> {
246 values.forEach((key, value) -> Add(null, to, type.persistentDataType, key, value));
247 });
248 } catch (Exception e) {
249 DreamChat.SendMessageToConsole("Error while cloning persistent data.", DreamMessageSettings.all());
250 }
251 }
252
269 public static <T> boolean AddExpiring(JavaPlugin javaPlugin, Entity entity, PersistentDataType<?, T> type, String key, T value, long expiryMillis) {
270 if (entity == null) {
271 DreamChat.SendMessageToConsole("Entity is null. Cannot add expiring persistent data.", DreamMessageSettings.all());
272 return false;
273 }
274 if (!isValidKey(key)) {
275 DreamChat.SendMessageToConsole("Invalid key: " + key, DreamMessageSettings.all());
276 return false;
277 }
278 try {
279 Add(javaPlugin, entity, type, key, value);
280 Bukkit.getScheduler().runTaskLater(javaPlugin, () -> Remove(javaPlugin, entity, key), expiryMillis / 50);
281 return true;
282 } catch (Exception e) {
283 DreamChat.SendMessageToConsole("Error while adding expiring persistent data for key: " + key, DreamMessageSettings.all());
284 return false;
285 }
286 }
287}
Utilities for reading and writing PersistentDataContainer on Entity.
static< T > boolean Add(JavaPlugin javaPlugin, Entity entity, PersistentDataType<?, T > type, String key, T value)
Adds or updates a typed value in an entity container.
static< T > T Get(Entity entity, String key, PersistentDataType<?, T > type)
Retrieves a typed value from an entity container.
static LinkedHashMap< String, Object > GetALl(Entity entity, PersistentDataTypes persistentDataType)
Retrieves all data for a specific logical type.
static boolean Has(JavaPlugin javaPlugin, Entity entity, PersistentDataType persistentDataType, String key)
Checks if a key exists in an entity container.
static boolean Remove(JavaPlugin javaPlugin, Entity entity, String key)
Removes a value by key from an entity container.
static< T > boolean AddExpiring(JavaPlugin javaPlugin, Entity entity, PersistentDataType<?, T > type, String key, T value, long expiryMillis)
Adds a value to an entity that automatically expires after the given duration.
static boolean isValidKey(String key)
Validates a local key for use with NamespacedKey.
static void CloneData(Entity from, Entity to)
Clones every key/value across logical data types from one entity to another.
static LinkedHashMap< PersistentDataTypes, LinkedHashMap< String, Object > > GetALl(Entity entity)
Retrieves all persistent data grouped by logical types.
static PersistentDataContainer ReturnPersistentDataContainer(Entity entity)
Gets an entity's PersistentDataContainer.
Event fired when a persistent data entry is added to an Entity.
Event fired when a persistent data entry is removed from an Entity.
Logical wrappers over Bukkit PersistentDataType constants, used for grouping and dumping containers b...