setBlock & setEntity as CompletableFuture (#2019)

* setBlock & setEntity as CompletableFuture

* use collectingAndThen toArray
This commit is contained in:
Huynh Tien 2022-09-05 13:28:16 +07:00 committed by GitHub
parent 9088ea4b80
commit 12926f9ee7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View File

@ -11,17 +11,30 @@ import world.bentobox.bentobox.util.DefaultPasteUtil;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class PasteHandlerImpl implements PasteHandler {
@Override
public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) {
blockMap.forEach((location, block) -> DefaultPasteUtil.setBlock(island, location, block));
return CompletableFuture.completedFuture(null);
return blockMap.entrySet().parallelStream()
.map(entry -> DefaultPasteUtil.setBlock(island, entry.getKey(), entry.getValue()))
.collect(
Collectors.collectingAndThen(
Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))
)
);
}
@Override
public CompletableFuture<Void> pasteEntities(Island island, World world, Map<Location, List<BlueprintEntity>> entityMap) {
entityMap.forEach((location, blueprintEntities) -> DefaultPasteUtil.setEntity(island, location, blueprintEntities));
return CompletableFuture.completedFuture(null);
return entityMap.entrySet().parallelStream()
.map(entry -> DefaultPasteUtil.setEntity(island, entry.getKey(), entry.getValue()))
.collect(
Collectors.collectingAndThen(
Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))
)
);
}
}

View File

@ -21,6 +21,7 @@ import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.nms.PasteHandler;
import java.util.*;
import java.util.concurrent.CompletableFuture;
/**
* A utility class for {@link PasteHandler}
@ -43,8 +44,8 @@ public class DefaultPasteUtil {
* @param location - location
* @param bpBlock - blueprint block
*/
public static void setBlock(Island island, Location location, BlueprintBlock bpBlock) {
Util.getChunkAtAsync(location).thenRun(() -> {
public static CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) {
return Util.getChunkAtAsync(location).thenRun(() -> {
Block block = location.getBlock();
// Set the block data - default is AIR
BlockData bd = createBlockData(bpBlock);
@ -152,10 +153,10 @@ public class DefaultPasteUtil {
* @param location - location
* @param list - blueprint entities
*/
public static void setEntity(Island island, Location location, List<BlueprintEntity> list) {
public static CompletableFuture<Void> setEntity(Island island, Location location, List<BlueprintEntity> list) {
World world = location.getWorld();
assert world != null;
Util.getChunkAtAsync(location).thenRun(() -> list.stream().filter(k -> k.getType() != null).forEach(k -> {
return Util.getChunkAtAsync(location).thenRun(() -> list.stream().filter(k -> k.getType() != null).forEach(k -> {
LivingEntity e = (LivingEntity) location.getWorld().spawnEntity(location, k.getType());
if (k.getCustomName() != null) {
String customName = k.getCustomName();