mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-16 13:11:40 +01:00
Added StorageFolder#getAndCloneData
This commit is contained in:
parent
7881e3ca76
commit
cad5dfb8eb
@ -42,11 +42,13 @@ public class StorageFolder {
|
|||||||
this.storageSystem.close();
|
this.storageSystem.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void getAndCacheData(String key, DataContainer dataContainer, Runnable callback) {
|
public void getAndCloneData(String key, DataContainer dataContainer, Runnable callback) {
|
||||||
|
synchronized (cachedData) {
|
||||||
|
|
||||||
// Keep giving the same SerializableData if already loaded
|
// Copy data from the cachedMap
|
||||||
if (cachedData.containsKey(key)) {
|
if (cachedData.containsKey(key)) {
|
||||||
dataContainer.setData(cachedData.get(key));
|
SerializableData data = cachedData.get(key);
|
||||||
|
dataContainer.setData(data.clone());
|
||||||
if (callback != null)
|
if (callback != null)
|
||||||
callback.run();
|
callback.run();
|
||||||
return;
|
return;
|
||||||
@ -64,11 +66,47 @@ public class StorageFolder {
|
|||||||
|
|
||||||
dataContainer.setData(data);
|
dataContainer.setData(data);
|
||||||
|
|
||||||
|
if (callback != null)
|
||||||
|
callback.run();
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getAndCloneData(String key, DataContainer dataContainer) {
|
||||||
|
getAndCloneData(key, dataContainer, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getAndCacheData(String key, DataContainer dataContainer, Runnable callback) {
|
||||||
|
synchronized (cachedData) {
|
||||||
|
|
||||||
|
// Give the cached SerializableData if already loaded
|
||||||
|
if (cachedData.containsKey(key)) {
|
||||||
|
dataContainer.setData(cachedData.get(key));
|
||||||
|
if (callback != null)
|
||||||
|
callback.run();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load it from the storage system and cache it
|
||||||
|
get(key, bytes -> {
|
||||||
|
SerializableData data;
|
||||||
|
|
||||||
|
if (bytes != null) {
|
||||||
|
data = DataReader.readData(Unpooled.wrappedBuffer(bytes));
|
||||||
|
} else {
|
||||||
|
data = new SerializableData();
|
||||||
|
}
|
||||||
|
|
||||||
|
dataContainer.setData(data);
|
||||||
|
|
||||||
this.cachedData.put(key, data);
|
this.cachedData.put(key, data);
|
||||||
|
|
||||||
if (callback != null)
|
if (callback != null)
|
||||||
callback.run();
|
callback.run();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void getAndCacheData(String key, DataContainer dataContainer) {
|
public void getAndCacheData(String key, DataContainer dataContainer) {
|
||||||
@ -77,12 +115,14 @@ public class StorageFolder {
|
|||||||
|
|
||||||
public void saveCachedData() {
|
public void saveCachedData() {
|
||||||
try {
|
try {
|
||||||
|
synchronized (cachedData) {
|
||||||
for (Map.Entry<String, SerializableData> entry : cachedData.entrySet()) {
|
for (Map.Entry<String, SerializableData> entry : cachedData.entrySet()) {
|
||||||
String key = entry.getKey();
|
String key = entry.getKey();
|
||||||
SerializableData data = entry.getValue();
|
SerializableData data = entry.getValue();
|
||||||
|
|
||||||
set(key, data.getSerializedData());
|
set(key, data.getSerializedData());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user