Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the ability to store persistent data for islands and players (#…
…1142) * Added persistent data container * Added persistent data containers to islands and players
- Loading branch information
1 parent
de6d726
commit 2e354d3
Showing
35 changed files
with
1,059 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
API/src/main/java/com/bgsoftware/superiorskyblock/api/persistence/IPersistentDataHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.bgsoftware.superiorskyblock.api.persistence; | ||
|
||
/** | ||
* Represents an object that can store custom persistent data. | ||
*/ | ||
public interface IPersistentDataHolder { | ||
|
||
/** | ||
* Get the container of custom persistent data of this object. | ||
*/ | ||
PersistentDataContainer getPersistentDataContainer(); | ||
|
||
} |
138 changes: 138 additions & 0 deletions
138
...rc/main/java/com/bgsoftware/superiorskyblock/api/persistence/PersistentDataContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.bgsoftware.superiorskyblock.api.persistence; | ||
|
||
import org.apache.commons.lang.IllegalClassException; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public interface PersistentDataContainer { | ||
|
||
/** | ||
* Check if the provided key has a matching metadata value. | ||
* | ||
* @param key The key to check. | ||
*/ | ||
boolean has(String key); | ||
|
||
/** | ||
* Check if the provided key has a matching metadata value of the provided type. | ||
* | ||
* @param key The key to check. | ||
* @param type The type to check. | ||
*/ | ||
<T> boolean hasKeyOfType(String key, PersistentDataType<T> type); | ||
|
||
/** | ||
* Store a metadata value matching the provided key and type. | ||
* | ||
* @param key The key to store. | ||
* @param type The type of the metadata value. | ||
* @param value The metadata value to store. | ||
* @return The old metadata value that was stored matching the key, if exists. | ||
* @throws IllegalClassException If the old metadata value is not of type {@param type}. | ||
* @throws IllegalStateException If {@param type} doesn't have a valid serializer available. | ||
*/ | ||
@Nullable | ||
<T> T put(String key, PersistentDataType<T> type, T value) throws IllegalClassException, IllegalStateException; | ||
|
||
/** | ||
* Store a metadata value matching the provided key and type. | ||
* | ||
* @param key The key to store. | ||
* @param type The type of the metadata. | ||
* @param value The metadata value to store. | ||
* @param returnType The type of the old metadata value. | ||
* @return The old metadata value that was stored matching the key, if exists. | ||
* @throws IllegalClassException If the old metadata value is not of type {@param returnType}. | ||
* @throws IllegalStateException If {@param type} doesn't have a valid serializer available. | ||
*/ | ||
@Nullable | ||
<T, R> R put(String key, PersistentDataType<T> type, T value, PersistentDataType<R> returnType) throws IllegalClassException, IllegalStateException; | ||
|
||
/** | ||
* Remove a metadata value matching the provided key. | ||
* | ||
* @param key The key to remove. | ||
* @return The old metadata value that was stored matching the key, if exists. | ||
*/ | ||
@Nullable | ||
Object remove(String key); | ||
|
||
/** | ||
* Remove a metadata value matching the provided key and type. | ||
* If the metadata value doesn't match the {@param type}, it will not get removed. | ||
* | ||
* @param key The key to remove. | ||
* @param type The type of the metadata value to remove. | ||
* @return The old metadata value that was stored matching the key, if exists. | ||
* If the metadata value does not match the current type, null will be returned. | ||
*/ | ||
@Nullable | ||
<T> T removeKeyOfType(String key, PersistentDataType<T> type); | ||
|
||
/** | ||
* Get a metadata value matching the provided key and type. | ||
* | ||
* @param key The key to fetch. | ||
* @param type The type of the metadata value to fetch. | ||
* @return The metadata value that is stored matching the key, if exists. | ||
* @throws IllegalClassException If the metadata value is not of type {@param type}. | ||
*/ | ||
@Nullable | ||
<T> T get(String key, PersistentDataType<T> type) throws IllegalClassException; | ||
|
||
/** | ||
* Get a metadata value matching the provided key. | ||
* | ||
* @param key The key to fetch. | ||
* @return The metadata value that is stored matching the key, if exists. | ||
*/ | ||
@Nullable | ||
Object get(String key); | ||
|
||
/** | ||
* Get a metadata value matching the provided key and type. | ||
* | ||
* @param key The key to fetch. | ||
* @param type The type of the metadata value to fetch. | ||
* @param def Value to return in case there is no metadata value matching the provided key. | ||
* @return The metadata value that is stored matching the key, or {@param def} otherwise. | ||
* @throws IllegalClassException If the metadata value is not of type {@param type}. | ||
*/ | ||
<T> T getOrDefault(String key, PersistentDataType<T> type, T def) throws IllegalClassException; | ||
|
||
/** | ||
* Get a metadata value matching the provided key. | ||
* | ||
* @param key The key to fetch. | ||
* @param def Value to return in case there is no metadata value matching the provided key. | ||
* @return The metadata value that is stored matching the key, or {@param def} otherwise. | ||
*/ | ||
Object getOrDefault(String key, Object def); | ||
|
||
/** | ||
* Check whether the container is empty. | ||
*/ | ||
boolean isEmpty(); | ||
|
||
/** | ||
* Get the size of the container. | ||
*/ | ||
int size(); | ||
|
||
/** | ||
* Get the serialized contents of the container as a bytes array. | ||
* The format of the serialized data may be different depending on the implementation of the container. | ||
* The serialized data must be loaded without any errors using {@link #load(byte[])}. | ||
*/ | ||
byte[] serialize(); | ||
|
||
/** | ||
* Load contents from the serialized data into the container. | ||
* The format of the serialized data may be different depending on the implementation of the container. | ||
* | ||
* @param data The serialized data. | ||
* @throws IllegalArgumentException If the given data cannot be serialized correctly. | ||
*/ | ||
void load(byte[] data) throws IllegalArgumentException; | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
API/src/main/java/com/bgsoftware/superiorskyblock/api/persistence/PersistentDataType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.bgsoftware.superiorskyblock.api.persistence; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import javax.annotation.Nullable; | ||
import java.math.BigDecimal; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Represents a data type that can be stored inside a {@link PersistentDataContainer}. | ||
* | ||
* @param <T> The type of the value to store. | ||
*/ | ||
public final class PersistentDataType<T> { | ||
|
||
public static final PersistentDataType<BigDecimal> BIG_DECIMAL = new PersistentDataType<>(BigDecimal.class); | ||
public static final PersistentDataType<byte[]> BYTE_ARRAY = new PersistentDataType<>(byte[].class); | ||
public static final PersistentDataType<Byte> BYTE = new PersistentDataType<>(Byte.class); | ||
public static final PersistentDataType<Double> DOUBLE = new PersistentDataType<>(Double.class); | ||
public static final PersistentDataType<Float> FLOAT = new PersistentDataType<>(Float.class); | ||
public static final PersistentDataType<int[]> INT_ARRAY = new PersistentDataType<>(int[].class); | ||
public static final PersistentDataType<Integer> INTEGER = new PersistentDataType<>(Integer.class); | ||
public static final PersistentDataType<Long> LONG = new PersistentDataType<>(Long.class); | ||
public static final PersistentDataType<Short> SHORT = new PersistentDataType<>(Short.class); | ||
public static final PersistentDataType<String> STRING = new PersistentDataType<>(String.class); | ||
public static final PersistentDataType<UUID> UUID = new PersistentDataType<>(UUID.class); | ||
|
||
private final Class<T> type; | ||
private final PersistentDataTypeContext<T> context; | ||
|
||
/** | ||
* Custom type constructor. | ||
* | ||
* @param type The type. | ||
* @param context The context class used to serialize and deserialize this data type. | ||
*/ | ||
public PersistentDataType(Class<T> type, PersistentDataTypeContext<T> context) { | ||
this.type = Preconditions.checkNotNull(type, "type parameter cannot be null"); | ||
this.context = Preconditions.checkNotNull(context, "context parameter cannot be null"); | ||
} | ||
|
||
/** | ||
* Built-in type constructor. | ||
* | ||
* @param type The type. | ||
*/ | ||
private PersistentDataType(Class<T> type) { | ||
this.type = type; | ||
this.context = null; | ||
} | ||
|
||
public Class<T> getType() { | ||
return this.type; | ||
} | ||
|
||
@Nullable | ||
public PersistentDataTypeContext<T> getContext() { | ||
return this.context; | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
.../main/java/com/bgsoftware/superiorskyblock/api/persistence/PersistentDataTypeContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.bgsoftware.superiorskyblock.api.persistence; | ||
|
||
/** | ||
* The context class is used to serialize and deserialize a custom data type. | ||
* | ||
* @param <T> The type of the data value. | ||
*/ | ||
public interface PersistentDataTypeContext<T> { | ||
|
||
/** | ||
* Serialize the provided value into a bytes buffer. | ||
* | ||
* @param value The value to serialize. | ||
* @return The serialized data. | ||
*/ | ||
byte[] serialize(T value); | ||
|
||
/** | ||
* Deserialize the provided bytes buffer into a valid value. | ||
* | ||
* @param data The serialized data. | ||
* @return The deserialized value. | ||
*/ | ||
T deserialize(byte[] data); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.