#17: Start implementing more of the block restore and queue systems.
This commit is contained in:
parent
c03261fd7a
commit
01f269e123
5 changed files with 142 additions and 3 deletions
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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<PrimitiveBlock> getQueue() {
|
||||
return BLOCK_QUEUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the block queue, without notifying listeners
|
||||
* @param queue
|
||||
*/
|
||||
public void setQueueNoNotify(List<PrimitiveBlock> queue)
|
||||
{
|
||||
BLOCK_QUEUE = queue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the block queue, and notifies any listeners that blocks were potentially added
|
||||
* @param queue
|
||||
*/
|
||||
public void setQueue(List<PrimitiveBlock> queue)
|
||||
{
|
||||
BLOCK_QUEUE = queue;
|
||||
notifyDirtyQueue(true);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -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<PrimitiveBlock> queue = getQueue();
|
||||
List<Vector3> positions = new ArrayList<>();
|
||||
|
||||
Iterator<PrimitiveBlock> it = queue.iterator();
|
||||
|
||||
while(it.hasNext()) {
|
||||
PrimitiveBlock blk = it.next();
|
||||
positions.add(new Vector3i(blk.position));
|
||||
}
|
||||
|
||||
positions = PositionUtil.sortAscending(positions);
|
||||
|
||||
List<PrimitiveBlock> 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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -14,7 +14,7 @@ import java.util.List;
|
|||
public class PositionUtil
|
||||
{
|
||||
|
||||
public List<Vector3> makeCube(Vector3 p1, Vector3 p2)
|
||||
public static List<Vector3> makeCube(Vector3 p1, Vector3 p2)
|
||||
{
|
||||
List<Vector3> vecs = new ArrayList<>();
|
||||
Vector3 work = new Vector3d();
|
||||
|
@ -114,7 +114,7 @@ public class PositionUtil
|
|||
}
|
||||
|
||||
|
||||
public List<Vector3> sortAscending(List<Vector3> vecs)
|
||||
public static List<Vector3> sortAscending(List<Vector3> vecs)
|
||||
{
|
||||
|
||||
List<Vector3> copy = new ArrayList<>(vecs);
|
||||
|
|
Reference in a new issue