mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-28 02:51:42 +01:00
Added StorageFolder + fixes
This commit is contained in:
parent
57ad3397d1
commit
14ef482f42
@ -18,7 +18,6 @@ import net.minestom.server.item.Material;
|
||||
import net.minestom.server.network.ConnectionManager;
|
||||
import net.minestom.server.ping.ResponseDataConsumer;
|
||||
import net.minestom.server.storage.StorageFolder;
|
||||
import net.minestom.server.storage.StorageManager;
|
||||
import net.minestom.server.timer.TaskRunnable;
|
||||
import net.minestom.server.utils.MathUtils;
|
||||
import net.minestom.server.utils.Position;
|
||||
@ -34,7 +33,7 @@ public class PlayerInit {
|
||||
private static volatile InstanceContainer instanceContainer;
|
||||
|
||||
static {
|
||||
StorageFolder storageFolder = StorageManager.getFolder("chunk_data");
|
||||
StorageFolder storageFolder = MinecraftServer.getStorageManager().getFolder("chunk_data");
|
||||
ChunkGeneratorDemo chunkGeneratorDemo = new ChunkGeneratorDemo();
|
||||
NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator();
|
||||
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(storageFolder);
|
||||
|
82
src/main/java/net/minestom/server/storage/StorageFolder.java
Normal file
82
src/main/java/net/minestom/server/storage/StorageFolder.java
Normal file
@ -0,0 +1,82 @@
|
||||
package net.minestom.server.storage;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minestom.server.data.DataContainer;
|
||||
import net.minestom.server.data.SerializableData;
|
||||
import net.minestom.server.reader.DataReader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class StorageFolder {
|
||||
|
||||
private StorageSystem storageSystem;
|
||||
private String folderPath;
|
||||
|
||||
private Map<String, SerializableData> cachedData;
|
||||
|
||||
protected StorageFolder(StorageSystem storageSystem, String folderPath) {
|
||||
this.storageSystem = storageSystem;
|
||||
this.folderPath = folderPath;
|
||||
|
||||
this.cachedData = new HashMap<>();
|
||||
|
||||
this.storageSystem.open(folderPath);
|
||||
}
|
||||
|
||||
public void get(String key, Consumer<byte[]> callback) {
|
||||
this.storageSystem.get(key, callback);
|
||||
}
|
||||
|
||||
public void set(String key, byte[] data) {
|
||||
this.storageSystem.set(key, data);
|
||||
}
|
||||
|
||||
public void delete(String key) {
|
||||
this.storageSystem.delete(key);
|
||||
}
|
||||
|
||||
public void close() {
|
||||
this.storageSystem.close();
|
||||
}
|
||||
|
||||
public void getAndCacheData(String key, DataContainer dataContainer, Runnable callback) {
|
||||
get(key, bytes -> {
|
||||
SerializableData data;
|
||||
|
||||
if (bytes != null) {
|
||||
data = DataReader.readData(Unpooled.wrappedBuffer(bytes));
|
||||
} else {
|
||||
data = new SerializableData();
|
||||
}
|
||||
|
||||
dataContainer.setData(data);
|
||||
|
||||
if (callback != null)
|
||||
callback.run();
|
||||
});
|
||||
}
|
||||
|
||||
public void getAndCacheData(String key, DataContainer dataContainer) {
|
||||
getAndCacheData(key, dataContainer, null);
|
||||
}
|
||||
|
||||
public void saveCachedData() {
|
||||
try {
|
||||
for (Map.Entry<String, SerializableData> entry : cachedData.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
SerializableData data = entry.getValue();
|
||||
|
||||
set(key, data.getSerializedData());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String getFolderPath() {
|
||||
return folderPath;
|
||||
}
|
||||
}
|
@ -8,22 +8,22 @@ import java.util.function.Supplier;
|
||||
|
||||
public class StorageManager {
|
||||
|
||||
private static Supplier<StorageSystem> storageSystemSupplier = null;
|
||||
private Supplier<StorageSystem> storageSystemSupplier = null;
|
||||
|
||||
private static Map<String, StorageFolder> folderMap = new HashMap<>();
|
||||
private Map<String, StorageFolder> folderMap = new HashMap<>();
|
||||
|
||||
public static StorageFolder getFolder(String folderName) {
|
||||
public StorageFolder getFolder(String folderName) {
|
||||
StorageSystem storageSystem = storageSystemSupplier.get();
|
||||
return folderMap.computeIfAbsent(folderName, s -> new StorageFolder(storageSystem, folderName));
|
||||
}
|
||||
|
||||
public static Collection<StorageFolder> getLoadedFolders() {
|
||||
public Collection<StorageFolder> getLoadedFolders() {
|
||||
return Collections.unmodifiableCollection(folderMap.values());
|
||||
}
|
||||
|
||||
public static void defineStorageSystem(Supplier<StorageSystem> storageSystemSupplier) {
|
||||
if (StorageManager.storageSystemSupplier != null)
|
||||
public void defineStorageSystem(Supplier<StorageSystem> storageSystemSupplier) {
|
||||
if (this.storageSystemSupplier != null)
|
||||
System.out.println("WARNING: the current StorageSystem is being changed, could lead to issue!");
|
||||
StorageManager.storageSystemSupplier = storageSystemSupplier;
|
||||
this.storageSystemSupplier = storageSystemSupplier;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user