Added StorageFolder#getOrDefault and some cleanup

This commit is contained in:
Felix Cravic 2020-05-16 22:07:24 +02:00
parent fa4b083ab8
commit fe7e56da8d
3 changed files with 35 additions and 2 deletions

View File

@ -53,6 +53,7 @@ public class StorageFolder {
DataType<T> dataType = DATA_MANAGER.getDataType(type);
if (dataType == null)
throw new NullPointerException("You can only save registered DataType type!");
PacketWriter packetWriter = new PacketWriter();
dataType.encode(packetWriter, object); // Encode
byte[] encodedValue = packetWriter.toByteArray(); // Retrieve bytes
@ -65,12 +66,21 @@ public class StorageFolder {
if (dataType == null)
throw new NullPointerException("You can only save registered DataType type!");
ByteBuf buffer = Unpooled.wrappedBuffer(get(key));
byte[] data = get(key);
if (data == null)
return null;
ByteBuf buffer = Unpooled.wrappedBuffer(data);
PacketReader packetReader = new PacketReader(buffer);
T value = dataType.decode(packetReader);
return value;
}
public <T> T getOrDefault(String key, Class<T> type, T defaultValue) {
T value;
return (value = get(key, type)) != null ? value : defaultValue;
}
public void getAndCloneData(String key, DataContainer dataContainer) {
synchronized (cachedData) {

View File

@ -52,7 +52,7 @@ public class StorageManager {
public void defineDefaultStorageSystem(Supplier<StorageSystem> storageSystemSupplier) {
if (this.defaultStorageSystemSupplier != null) {
LOGGER.error("The default storage-system has been changed. This could lead to issues!");
LOGGER.warn("The default storage-system has been changed. This could lead to issues!");
}
this.defaultStorageSystemSupplier = storageSystemSupplier;
}

View File

@ -2,14 +2,37 @@ package net.minestom.server.storage;
public interface StorageSystem {
/**
* Called when a foler is opened with this StorageSystem
*
* @param folderName the name of the folder
*/
void open(String folderName);
/**
* @param key
* @return the retrieved data
*/
byte[] get(String key);
/**
* Set the specified data to the defined key
*
* @param key
* @param data
*/
void set(String key, byte[] data);
/**
* Delete the specified key from the database
*
* @param key
*/
void delete(String key);
/**
* Called when the folder is closed, generally during server shutdown
*/
void close();
}