mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-27 02:21:42 +01:00
setBlock & setEntity as CompletableFuture (#2019)
* setBlock & setEntity as CompletableFuture * use collectingAndThen toArray
This commit is contained in:
parent
9088ea4b80
commit
12926f9ee7
@ -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]))
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
Loading…
Reference in New Issue
Block a user