diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestore.java b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestore.java new file mode 100644 index 0000000..a3d4d20 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestore.java @@ -0,0 +1,14 @@ +package dev.zontreck.libzontreck.memory.world; + +public class BlockRestore extends BlockRestoreQueue +{ + @Override + public String getRestoreQueueName() { + return "BasicBlockSnapshots"; + } + + @Override + public void notifyDirtyQueue(boolean blockAdded) { + return; // We dont care. This is a basic queue + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java index 53ede6b..a322007 100644 --- a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java @@ -30,7 +30,7 @@ public abstract class BlockRestoreQueue */ public void enqueueBlock(SavedBlock block) { - BLOCK_QUEUE.add(block.getBlockPrimitive()); + enqueueBlock(block.getBlockPrimitive()); } /** @@ -40,5 +40,61 @@ public abstract class BlockRestoreQueue public void enqueueBlock(PrimitiveBlock block) { BLOCK_QUEUE.add(block); + + notifyDirtyQueue(true); + } + + /** + * Executed when the queue is modified. + * @param blockAdded Whether a block was added or removed from the queue + */ + public abstract void notifyDirtyQueue(boolean blockAdded); + + /** + * Pops a block off the queue, and returns it + * @return A PrimitiveBlock instance of the SavedBlock + */ + public PrimitiveBlock getNextBlock() + { + PrimitiveBlock blk = BLOCK_QUEUE.get(0); + BLOCK_QUEUE.remove(0); + notifyDirtyQueue(false); + return blk; + } + + /** + * Clears the entire queue, discarding the saved blocks permanently. + */ + public void clear() + { + BLOCK_QUEUE.clear(); + notifyDirtyQueue(false); + } + + /** + * Returns the raw block queue instance + * @return + */ + public List getQueue() { + return BLOCK_QUEUE; + } + + /** + * Sets the block queue, without notifying listeners + * @param queue + */ + public void setQueueNoNotify(List queue) + { + BLOCK_QUEUE = queue; + } + + /** + * Sets the block queue, and notifies any listeners that blocks were potentially added + * @param queue + */ + public void setQueue(List queue) + { + BLOCK_QUEUE = queue; + notifyDirtyQueue(true); } } diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreRunner.java b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreRunner.java new file mode 100644 index 0000000..bcfa76b --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreRunner.java @@ -0,0 +1,15 @@ +package dev.zontreck.libzontreck.memory.world; + +public class BlockRestoreRunner implements Runnable +{ + private BlockRestoreQueue queue; + + @Override + public void run() { + if(queue.getQueuedBlocks() == 0) return; // We'll be queued back up later + + PrimitiveBlock prim = queue.getNextBlock(); + + + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/SortedBlockQueue.java b/src/main/java/dev/zontreck/libzontreck/memory/world/SortedBlockQueue.java new file mode 100644 index 0000000..efe7f65 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/SortedBlockQueue.java @@ -0,0 +1,54 @@ +package dev.zontreck.libzontreck.memory.world; + +import dev.zontreck.libzontreck.api.Vector3; +import dev.zontreck.libzontreck.util.PositionUtil; +import dev.zontreck.libzontreck.vectors.Vector3i; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class SortedBlockQueue extends BlockRestoreQueue +{ + @Override + public String getRestoreQueueName() { + return "SortedBlockQueue"; + } + + @Override + public void notifyDirtyQueue(boolean blockAdded) { + if(blockAdded) { + // Perform sorting + + List queue = getQueue(); + List positions = new ArrayList<>(); + + Iterator it = queue.iterator(); + + while(it.hasNext()) { + PrimitiveBlock blk = it.next(); + positions.add(new Vector3i(blk.position)); + } + + positions = PositionUtil.sortAscending(positions); + + List retQueue = new ArrayList<>(); + + it = queue.iterator(); + for(Vector3 pos : positions) { + it = queue.iterator(); + while(it.hasNext()) { + PrimitiveBlock blk = it.next(); + if(blk.position.equals(pos.asBlockPos())) { + retQueue.add(blk); + it.remove(); + break; + } + } + } + + setQueueNoNotify(retQueue); + + } + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/util/PositionUtil.java b/src/main/java/dev/zontreck/libzontreck/util/PositionUtil.java index fdcb9f6..39ee999 100644 --- a/src/main/java/dev/zontreck/libzontreck/util/PositionUtil.java +++ b/src/main/java/dev/zontreck/libzontreck/util/PositionUtil.java @@ -14,7 +14,7 @@ import java.util.List; public class PositionUtil { - public List makeCube(Vector3 p1, Vector3 p2) + public static List makeCube(Vector3 p1, Vector3 p2) { List vecs = new ArrayList<>(); Vector3 work = new Vector3d(); @@ -114,7 +114,7 @@ public class PositionUtil } - public List sortAscending(List vecs) + public static List sortAscending(List vecs) { List copy = new ArrayList<>(vecs);