diff --git a/gradle.properties b/gradle.properties index 40969a3..7950d8d 100644 --- a/gradle.properties +++ b/gradle.properties @@ -53,7 +53,7 @@ mod_name=Zontreck's Library Mod # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=GPLv3 # The mod version. See https://semver.org/ -mod_version=1201.13.042524.0327 +mod_version=1201.13.042524.0409 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html 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 530b61c..f8b309a 100644 --- a/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/BlockRestoreQueue.java @@ -24,6 +24,7 @@ public abstract class BlockRestoreQueue private List BLOCK_QUEUE = new ArrayList<>(); private final BlockRestoreRunner RUNNER; private ScheduledFuture RUNNING_TASK; + private ScheduledFuture DATABASE_UPLOAD_RUNNER; /** * When in database mode, this flag will be checked by the Restore Runner so a database call is not made unnecessarily. @@ -75,13 +76,14 @@ public abstract class BlockRestoreQueue */ public void enqueueBlock(PrimitiveBlock block) { + /* if(usesDatabase()) { databaseUpdate(block); notifyDirtyQueue(true); hasBlocks=true; return; - } + }*/ BLOCK_QUEUE.add(block); notifyDirtyQueue(true); @@ -94,6 +96,7 @@ public abstract class BlockRestoreQueue public void databaseUpdate(PrimitiveBlock block) { + hasBlocks=true; PreparedStatement pstmt = null; try { pstmt = DatabaseWrapper.get().prepareStatement("INSERT INTO `blocks` (queueName, posX, posY, posZ, snapshotID, block) VALUES (?, ?, ?, ?, ?, ?);"); @@ -256,6 +259,10 @@ public abstract class BlockRestoreQueue public void schedule(long interval, TimeUnit unit) { RUNNING_TASK = LibZontreck.executor.scheduleAtFixedRate(RUNNER, 2000, interval, unit); + + DATABASE_UPLOAD_RUNNER = LibZontreck.executor.scheduleAtFixedRate(new DatabaseUploadRunner(this), 2000, 250, TimeUnit.MILLISECONDS); + + isCancelled=false; } /** @@ -263,9 +270,29 @@ public abstract class BlockRestoreQueue */ public void cancel() { + isCancelled=true; RUNNING_TASK.cancel(false); } + public boolean isCancelled=false; + + /** + * Remove a block from the local queue. This does not impact the database and is used internally + * @param block + */ + public void dequeue(PrimitiveBlock block) + { + BLOCK_QUEUE.remove(block); + } + + /** + * Cancels the repeating upload to database task. This is automatically invoked when cancel has been invoked, and no more tasks are to be uploaded. + */ + public void cancelUploader() + { + DATABASE_UPLOAD_RUNNER.cancel(true); + } + @SubscribeEvent public void onServerStopping(ServerStoppingEvent event) { diff --git a/src/main/java/dev/zontreck/libzontreck/memory/world/DatabaseUploadRunner.java b/src/main/java/dev/zontreck/libzontreck/memory/world/DatabaseUploadRunner.java new file mode 100644 index 0000000..2430114 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/memory/world/DatabaseUploadRunner.java @@ -0,0 +1,26 @@ +package dev.zontreck.libzontreck.memory.world; + +public class DatabaseUploadRunner implements Runnable { + private BlockRestoreQueue QUEUE; + public DatabaseUploadRunner(BlockRestoreQueue queue) + { + QUEUE = queue; + } + + @Override + public void run() { + if(QUEUE.getQueuedBlocks() == 0) + { + if(QUEUE.isCancelled) + { + QUEUE.cancelUploader(); + return; + } + } else { + PrimitiveBlock block = QUEUE.getQueue().get(0); + QUEUE.dequeue(block); + + QUEUE.databaseUpdate(block); + } + } +} diff --git a/src/main/java/dev/zontreck/libzontreck/util/TagUtils.java b/src/main/java/dev/zontreck/libzontreck/util/TagUtils.java index fe8fc0c..d2eca8a 100644 --- a/src/main/java/dev/zontreck/libzontreck/util/TagUtils.java +++ b/src/main/java/dev/zontreck/libzontreck/util/TagUtils.java @@ -29,4 +29,17 @@ public class TagUtils if(tag.contains(entry)) return tag.getString(entry); else return other; } + + /** + * Get either the entry, or supply a default value + * @param tag + * @param entry + * @param other + * @return + */ + public static boolean boolOr(CompoundTag tag, String entry, boolean other) + { + if(tag.contains(entry)) return tag.getBoolean(entry); + else return other; + } }