mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-21 07:31:47 +01:00
Added StorageFolder#getOrDefault and some cleanup
This commit is contained in:
parent
fa4b083ab8
commit
fe7e56da8d
@ -53,6 +53,7 @@ public class StorageFolder {
|
|||||||
DataType<T> dataType = DATA_MANAGER.getDataType(type);
|
DataType<T> dataType = DATA_MANAGER.getDataType(type);
|
||||||
if (dataType == null)
|
if (dataType == null)
|
||||||
throw new NullPointerException("You can only save registered DataType type!");
|
throw new NullPointerException("You can only save registered DataType type!");
|
||||||
|
|
||||||
PacketWriter packetWriter = new PacketWriter();
|
PacketWriter packetWriter = new PacketWriter();
|
||||||
dataType.encode(packetWriter, object); // Encode
|
dataType.encode(packetWriter, object); // Encode
|
||||||
byte[] encodedValue = packetWriter.toByteArray(); // Retrieve bytes
|
byte[] encodedValue = packetWriter.toByteArray(); // Retrieve bytes
|
||||||
@ -65,12 +66,21 @@ public class StorageFolder {
|
|||||||
if (dataType == null)
|
if (dataType == null)
|
||||||
throw new NullPointerException("You can only save registered DataType type!");
|
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);
|
PacketReader packetReader = new PacketReader(buffer);
|
||||||
T value = dataType.decode(packetReader);
|
T value = dataType.decode(packetReader);
|
||||||
return value;
|
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) {
|
public void getAndCloneData(String key, DataContainer dataContainer) {
|
||||||
synchronized (cachedData) {
|
synchronized (cachedData) {
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ public class StorageManager {
|
|||||||
|
|
||||||
public void defineDefaultStorageSystem(Supplier<StorageSystem> storageSystemSupplier) {
|
public void defineDefaultStorageSystem(Supplier<StorageSystem> storageSystemSupplier) {
|
||||||
if (this.defaultStorageSystemSupplier != null) {
|
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;
|
this.defaultStorageSystemSupplier = storageSystemSupplier;
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,37 @@ package net.minestom.server.storage;
|
|||||||
|
|
||||||
public interface StorageSystem {
|
public interface StorageSystem {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when a foler is opened with this StorageSystem
|
||||||
|
*
|
||||||
|
* @param folderName the name of the folder
|
||||||
|
*/
|
||||||
void open(String folderName);
|
void open(String folderName);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param key
|
||||||
|
* @return the retrieved data
|
||||||
|
*/
|
||||||
byte[] get(String key);
|
byte[] get(String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the specified data to the defined key
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @param data
|
||||||
|
*/
|
||||||
void set(String key, byte[] data);
|
void set(String key, byte[] data);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete the specified key from the database
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
void delete(String key);
|
void delete(String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the folder is closed, generally during server shutdown
|
||||||
|
*/
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user