Skip to content

Commit

Permalink
Improve Block copying speeds (~650%+ faster)
Browse files Browse the repository at this point in the history
  • Loading branch information
athulsib committed Aug 24, 2024
1 parent 45149cb commit a853dc7
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/main/java/me/athish/tachyon/Schematic.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<SerializableLocation, Material> blocks = new HashMap<>();
private Map<SerializableLocation, Material> blocks = new ConcurrentHashMap<>();
private SerializableLocation origin;

/**
Expand Down Expand Up @@ -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<int[]> 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());
});
}

/**
Expand Down

0 comments on commit a853dc7

Please sign in to comment.