From a853dc7a931daaacc553118402c195fb9cdf5e66 Mon Sep 17 00:00:00 2001 From: Athishh <106370011+Athishh@users.noreply.github.com> Date: Sat, 24 Aug 2024 23:01:48 +0530 Subject: [PATCH] Improve Block copying speeds (~650%+ faster) --- src/main/java/me/athish/tachyon/Schematic.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/me/athish/tachyon/Schematic.java b/src/main/java/me/athish/tachyon/Schematic.java index 0b067d0..457e617 100644 --- a/src/main/java/me/athish/tachyon/Schematic.java +++ b/src/main/java/me/athish/tachyon/Schematic.java @@ -6,7 +6,9 @@ import org.bukkit.inventory.ItemStack; import java.io.*; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; @@ -21,7 +23,7 @@ public class Schematic implements Serializable { // Set your custom schematic file extension here. private static final String FILE_EXTENSION = ".tachyon"; - private Map blocks = new HashMap<>(); + private Map blocks = new ConcurrentHashMap<>(); private SerializableLocation origin; /** @@ -64,14 +66,21 @@ private void copyBlocks(Location start, Location end, Location origin) { this.origin = new SerializableLocation(origin); + // Create a list of all coordinates within the specified range + List coordinates = new ArrayList<>(); for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { for (int z = minZ; z <= maxZ; z++) { - Location loc = new Location(world, x, y, z); - blocks.put(new SerializableLocation(loc), world.getBlockAt(loc).getType()); + coordinates.add(new int[]{x, y, z}); } } } + + // Use parallelStream to process the coordinates in parallel + coordinates.parallelStream().forEach(coord -> { + Location loc = new Location(world, coord[0], coord[1], coord[2]); + blocks.put(new SerializableLocation(loc), world.getBlockAt(loc).getType()); + }); } /**