diff --git a/README.md b/README.md
index bb223a5..2b8fd0f 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@ repositories {
}
dependencies {
- implementation 'com.github.itsMatoosh:block-metadata:1.3.1'
+ implementation 'com.github.itsMatoosh:block-metadata:1.4.0'
}
```
### Adding Block Metadata by Maven
@@ -28,7 +28,7 @@ Modify your pom.xml file to include the following:
com.github.itsMatoosh
block-metadata
- 1.3.1
+ 1.4.0
```
diff --git a/build.gradle b/build.gradle
index e22b7c5..c9b1a8e 100644
--- a/build.gradle
+++ b/build.gradle
@@ -4,7 +4,7 @@ plugins {
}
group 'me.matoosh'
-version '1.3.1'
+version '1.4.0'
sourceCompatibility = JavaVersion.VERSION_1_8
diff --git a/src/main/java/me/matoosh/blockmetadata/BlockMetadataStorage.java b/src/main/java/me/matoosh/blockmetadata/BlockMetadataStorage.java
index 26132c9..2e32564 100644
--- a/src/main/java/me/matoosh/blockmetadata/BlockMetadataStorage.java
+++ b/src/main/java/me/matoosh/blockmetadata/BlockMetadataStorage.java
@@ -12,9 +12,6 @@
import me.matoosh.blockmetadata.event.BlockMoveHandler;
import me.matoosh.blockmetadata.event.ChunkLoadHandler;
import me.matoosh.blockmetadata.event.PluginDisableHandler;
-import me.matoosh.blockmetadata.exception.ChunkAlreadyLoadedException;
-import me.matoosh.blockmetadata.exception.ChunkBusyException;
-import me.matoosh.blockmetadata.exception.ChunkNotLoadedException;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.block.Block;
@@ -30,7 +27,6 @@
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
-import java.util.concurrent.ExecutionException;
import java.util.function.Function;
/**
@@ -131,113 +127,124 @@ public BlockMetadataStorage(JavaPlugin plugin, Path dataPath) {
* Get metadata of a block.
* @param block The block.
* @return Current metadata of the block. Null if no data stored.
- * @throws ChunkBusyException Thrown if the chunk is busy.
*/
- public T getMetadata(@NonNull Block block) throws ChunkBusyException {
+ public CompletableFuture getMetadata(@NonNull Block block) {
// get chunk
- Map metadata = getMetadataInChunk(block.getChunk());
- if (metadata == null) {
- // no data for this chunk
- return null;
- }
+ return getMetadataInChunk(block.getChunk()).thenApply((metadata) -> {
+ if (metadata == null) {
+ // no data for this chunk
+ return null;
+ }
- // get block metadata
- return metadata.get(getBlockKeyInChunk(block));
+ // get block metadata
+ return metadata.get(getBlockKeyInChunk(block));
+ });
}
/**
* Set metadata of a block.
* @param block The block.
- * @param metadata Metadata to set to the block.
- * @throws ChunkBusyException Thrown if the chunk is busy.
+ * @param data Metadata to set to the block.
*/
- public void setMetadata(@NonNull Block block, T metadata) throws ChunkBusyException {
- if (metadata == null) {
+ public CompletableFuture setMetadata(@NonNull Block block, T data) {
+ if (data == null) {
// clear metadata
- removeMetadata(block);
+ return removeMetadata(block).thenApply((s) -> null);
} else {
// set metadata
- modifyMetadataInChunk(block.getChunk()).put(getBlockKeyInChunk(block), metadata);
+ return getMetadataInChunk(block.getChunk()).thenApply((d) -> {
+ // make sure theres a map to put data in
+ if (d == null) {
+ d = new HashMap<>();
+ }
+ d.put(getBlockKeyInChunk(block), data);
+
+ // set chunk as dirty
+ LoadedChunkData loadedChunkData = loadedChunks.get(block.getChunk());
+ loadedChunkData.setDirty(true);
+
+ // update the data
+ metadata.put(block.getChunk(), d);
+
+ return null;
+ });
}
}
/**
* Removes metadata for a block.
* @param block The block.
- * @throws ChunkBusyException Thrown if the chunk is busy.
* @return The removed metadata value. Null if no value was stored.
*/
- public T removeMetadata(@NonNull Block block) throws ChunkBusyException {
- // cache chunk
+ public CompletableFuture removeMetadata(@NonNull Block block) {
Chunk chunk = block.getChunk();
+ return getMetadataInChunk(chunk).thenApply((metadata) -> {
+ // no metadata in chunk
+ if (metadata == null) {
+ return null;
+ }
- // check if there are durabilities in the chunk
- if (!hasMetadataForChunk(chunk)) {
- return null;
- }
-
- // get chunk
- Map metadata = modifyMetadataInChunk(chunk);
-
- // remove from map
- T value = metadata.remove(getBlockKeyInChunk(block));
+ // remove metadata value from map
+ T value = metadata.remove(getBlockKeyInChunk(block));
- // check if last value
- if (metadata.size() < 1) {
- // remove chunk from storage
- removeMetadataForChunk(chunk);
- }
+ // if this was the last one left remove metadata entry for this chunk
+ if (metadata.size() == 0) {
+ removeMetadataForChunk(chunk);
+ }
- return value;
+ return value;
+ });
}
/**
* Checks whether there are metadata stored for a given chunk.
* @param chunk The chunk to check.
* @return Whether there are metadata stored for the given chunk.
- * @throws ChunkBusyException Thrown if the chunk is busy.
*/
- public boolean hasMetadataForChunk(@NonNull Chunk chunk) throws ChunkBusyException {
- ensureChunkReady(chunk);
- return metadata.containsKey(chunk);
+ public CompletableFuture hasMetadataForChunk(@NonNull Chunk chunk) {
+ return loadChunk(chunk).thenApply((s) -> metadata.containsKey(chunk));
}
/**
* Removes all metadata stored for a given chunk.
* @param chunk The chunk to remove metadata from.
- * @throws ChunkBusyException Thrown if the chunk is busy.
*/
- public void removeMetadataForChunk(@NonNull Chunk chunk) throws ChunkBusyException {
- ensureChunkReady(chunk);
- metadata.remove(chunk);
- LoadedChunkData loadedChunkData = loadedChunks.get(chunk);
- loadedChunkData.setDirty(true);
+ public CompletableFuture