diff --git a/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java b/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java index 1234942..25465cc 100644 --- a/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java +++ b/src/main/java/me/youhavetrouble/blockedit/BEPlayer.java @@ -13,7 +13,7 @@ public class BEPlayer { private static final HashMap playerHashMap = new HashMap<>(); private BoundingBox selection; private Location selectionPoint1, selectionPoint2; - private World selectionWorld; + private UUID selectionWorldUuid; public BoundingBox getSelection() { return selection; @@ -32,7 +32,7 @@ public class BEPlayer { selection = null; return; } - selectionWorld = selectionPoint1.getWorld(); + selectionWorldUuid = selectionPoint1.getWorld().getUID(); selection = BoundingBox.of(selectionPoint1, selectionPoint2); // bounding boxes are dumb. selection.expand(0.5, 0.5, 0.5); @@ -54,8 +54,9 @@ public class BEPlayer { /** * @return World withinn which the selection is made. */ - public World getSelectionWorld() { - return selectionWorld; + public UUID getSelectionWorld() { + if (selection == null) return null; + return selectionPoint1.getWorld().getUID(); } /** diff --git a/src/main/java/me/youhavetrouble/blockedit/WorkSplitter.java b/src/main/java/me/youhavetrouble/blockedit/WorkSplitter.java index 6a0fdcc..6e2d3f3 100644 --- a/src/main/java/me/youhavetrouble/blockedit/WorkSplitter.java +++ b/src/main/java/me/youhavetrouble/blockedit/WorkSplitter.java @@ -1,14 +1,13 @@ package me.youhavetrouble.blockedit; import me.youhavetrouble.blockedit.util.ChunkWork; -import org.bukkit.World; import org.bukkit.util.BoundingBox; import java.util.HashSet; public class WorkSplitter { - public static HashSet getOperatedOnChunks(BoundingBox boundingBox, World world) { + public static HashSet getOperatedOnChunks(BoundingBox boundingBox) { HashSet chunks = new HashSet<>(); ChunkWork chunkWork = new ChunkWork(0,0); // TODO Find a way to get chunks in the selection more efficiently diff --git a/src/main/java/me/youhavetrouble/blockedit/commands/SetCommand.java b/src/main/java/me/youhavetrouble/blockedit/commands/SetCommand.java index c64ae82..ad6b610 100644 --- a/src/main/java/me/youhavetrouble/blockedit/commands/SetCommand.java +++ b/src/main/java/me/youhavetrouble/blockedit/commands/SetCommand.java @@ -38,7 +38,7 @@ public class SetCommand implements TabExecutor { player.sendMessage(Component.text("You need to select 2 points to do this")); return true; } - new SetOperation(WorkSplitter.getOperatedOnChunks(selection, bePlayer.getSelectionWorld()), bePlayer.getSelectionWorld(), selection, blockData); + new SetOperation(WorkSplitter.getOperatedOnChunks(selection), bePlayer, blockData, 1); } return true; } diff --git a/src/main/java/me/youhavetrouble/blockedit/operations/SetOperation.java b/src/main/java/me/youhavetrouble/blockedit/operations/SetOperation.java index 70ef3a4..44a0d58 100644 --- a/src/main/java/me/youhavetrouble/blockedit/operations/SetOperation.java +++ b/src/main/java/me/youhavetrouble/blockedit/operations/SetOperation.java @@ -1,5 +1,6 @@ package me.youhavetrouble.blockedit.operations; +import me.youhavetrouble.blockedit.BEPlayer; import me.youhavetrouble.blockedit.BlockEdit; import me.youhavetrouble.blockedit.util.ChunkWork; import org.bukkit.Bukkit; @@ -7,24 +8,40 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; import org.bukkit.util.BoundingBox; - +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; public class SetOperation { private final BoundingBox selection; private final BlockData blockToSet; + private final List chunkwork = new ArrayList<>(); - public SetOperation(HashSet chunkWorks, World world, BoundingBox selection, BlockData blockToSet) { - this.selection = selection; + public SetOperation(HashSet chunkWorks, BEPlayer bePlayer, BlockData blockToSet, int chunksPerTick) { + this.selection = bePlayer.getSelection(); this.blockToSet = blockToSet; - int stagger = 0; - for (ChunkWork chunkWork : chunkWorks) { - Bukkit.getScheduler().runTaskLater(BlockEdit.getPlugin(), () -> processChunkWork(chunkWork, world), stagger++); - } + this.chunkwork.addAll(chunkWorks); + AtomicInteger element = new AtomicInteger(chunkwork.size()-1); + + Bukkit.getScheduler().runTaskTimer(BlockEdit.getPlugin(), (task) -> { + if (element.get() < 0) { + task.cancel(); + return; + } + for (int i = 0; i<= chunksPerTick; i++) { + processChunkWork(chunkwork.get(element.getAndDecrement()), bePlayer.getSelectionWorld()); + } + }, 0, 1); + + } - private void processChunkWork(ChunkWork chunkWork, World world) { + private void processChunkWork(ChunkWork chunkWork, UUID worldUuid) { + World world = Bukkit.getWorld(worldUuid); + if (world == null) return; chunkWork.getChunkAsync(world).thenAccept(chunk -> { // skip y levels that are not in the selection for (int y = (int) selection.getMinY(); y <= selection.getMaxY(); y++) { diff --git a/src/main/java/me/youhavetrouble/blockedit/util/ChunkWork.java b/src/main/java/me/youhavetrouble/blockedit/util/ChunkWork.java index fd5e12b..9646e59 100644 --- a/src/main/java/me/youhavetrouble/blockedit/util/ChunkWork.java +++ b/src/main/java/me/youhavetrouble/blockedit/util/ChunkWork.java @@ -2,7 +2,6 @@ package me.youhavetrouble.blockedit.util; import org.bukkit.Chunk; import org.bukkit.World; -import org.bukkit.util.BoundingBox; import java.util.concurrent.CompletableFuture; @@ -14,19 +13,10 @@ public class ChunkWork { setCoords(x,z); } - public BoundingBox getWorkspace(BoundingBox selection, World world) { - // TODO make it return shared space of getChunkBox and selection to cull some of the blocks from iterations - return getChunkBox(world); - } - public CompletableFuture getChunkAsync(World world) { return world.getChunkAtAsync(x,z, true); } - private BoundingBox getChunkBox(World world) { - return new BoundingBox(x*16, world.getMinHeight(), z*16, (x+1)*16, world.getMaxHeight(), (z+1)*16); - } - public int getX() { return x; }