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.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class PasteHandlerImpl implements PasteHandler { public class PasteHandlerImpl implements PasteHandler {
@Override @Override
public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) { public CompletableFuture<Void> pasteBlocks(Island island, World world, Map<Location, BlueprintBlock> blockMap) {
blockMap.forEach((location, block) -> DefaultPasteUtil.setBlock(island, location, block)); return blockMap.entrySet().parallelStream()
return CompletableFuture.completedFuture(null); .map(entry -> DefaultPasteUtil.setBlock(island, entry.getKey(), entry.getValue()))
.collect(
Collectors.collectingAndThen(
Collectors.toList(),
list -> CompletableFuture.allOf(list.toArray(new CompletableFuture[0]))
)
);
} }
@Override @Override
public CompletableFuture<Void> pasteEntities(Island island, World world, Map<Location, List<BlueprintEntity>> entityMap) { public CompletableFuture<Void> pasteEntities(Island island, World world, Map<Location, List<BlueprintEntity>> entityMap) {
entityMap.forEach((location, blueprintEntities) -> DefaultPasteUtil.setEntity(island, location, blueprintEntities)); return entityMap.entrySet().parallelStream()
return CompletableFuture.completedFuture(null); .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 world.bentobox.bentobox.nms.PasteHandler;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
/** /**
* A utility class for {@link PasteHandler} * A utility class for {@link PasteHandler}
@ -43,8 +44,8 @@ public class DefaultPasteUtil {
* @param location - location * @param location - location
* @param bpBlock - blueprint block * @param bpBlock - blueprint block
*/ */
public static void setBlock(Island island, Location location, BlueprintBlock bpBlock) { public static CompletableFuture<Void> setBlock(Island island, Location location, BlueprintBlock bpBlock) {
Util.getChunkAtAsync(location).thenRun(() -> { return Util.getChunkAtAsync(location).thenRun(() -> {
Block block = location.getBlock(); Block block = location.getBlock();
// Set the block data - default is AIR // Set the block data - default is AIR
BlockData bd = createBlockData(bpBlock); BlockData bd = createBlockData(bpBlock);
@ -152,10 +153,10 @@ public class DefaultPasteUtil {
* @param location - location * @param location - location
* @param list - blueprint entities * @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(); World world = location.getWorld();
assert world != null; 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()); LivingEntity e = (LivingEntity) location.getWorld().spawnEntity(location, k.getType());
if (k.getCustomName() != null) { if (k.getCustomName() != null) {
String customName = k.getCustomName(); String customName = k.getCustomName();