From a5112bc17aecea89d296579d32a43734a2471134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Endre=C3=9F?= Date: Sat, 21 Aug 2021 12:55:45 +0200 Subject: [PATCH] Updated status lib and added IRegenerationHelper --- legacy-api/pom.xml | 2 +- .../legacy/api/RealisticWorldGenerator.java | 4 + .../api/regeneration/IRegenerationHelper.java | 64 ++++++++++ .../legacy/api/util/java/info/IStatus.java | 35 ++++++ .../api/util/java/{ => info}/Status.java | 109 ++++++++---------- .../legacy/api/util/java/wait/Awaiter.java | 7 +- .../api/util/java/wait/WaitFunction.java | 8 +- 7 files changed, 158 insertions(+), 71 deletions(-) create mode 100644 legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/regeneration/IRegenerationHelper.java create mode 100644 legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/info/IStatus.java rename legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/{ => info}/Status.java (62%) diff --git a/legacy-api/pom.xml b/legacy-api/pom.xml index 0a5127f..0db5f67 100644 --- a/legacy-api/pom.xml +++ b/legacy-api/pom.xml @@ -4,7 +4,7 @@ 4.0.0 net.sourcewriters.spigot.rwg legacy-api - 2.0.0 + 2.1.0 diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/RealisticWorldGenerator.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/RealisticWorldGenerator.java index 6f66890..7f9888c 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/RealisticWorldGenerator.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/RealisticWorldGenerator.java @@ -11,6 +11,7 @@ import net.sourcewriters.spigot.rwg.legacy.api.chest.IChestStorage; import net.sourcewriters.spigot.rwg.legacy.api.compatibility.ICompatibilityManager; import net.sourcewriters.spigot.rwg.legacy.api.data.fix.IDataFixHandler; +import net.sourcewriters.spigot.rwg.legacy.api.regeneration.IRegenerationHelper; import net.sourcewriters.spigot.rwg.legacy.api.schematic.ISchematicStorage; import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull; import net.sourcewriters.spigot.rwg.legacy.api.version.IVersionAccess; @@ -41,6 +42,9 @@ protected RealisticWorldGenerator(Plugin plugin) { @NonNull public abstract ISchematicStorage getSchematicStorage(); + + @NonNull + public abstract IRegenerationHelper getRegenerationHelper(); @NonNull public abstract ICompatibilityManager getCompatibilityManager(); diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/regeneration/IRegenerationHelper.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/regeneration/IRegenerationHelper.java new file mode 100644 index 0000000..0d7f304 --- /dev/null +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/regeneration/IRegenerationHelper.java @@ -0,0 +1,64 @@ +package net.sourcewriters.spigot.rwg.legacy.api.regeneration; + +import org.bukkit.Chunk; +import org.bukkit.Location; + +import net.sourcewriters.spigot.rwg.legacy.api.util.java.info.IStatus; + +public interface IRegenerationHelper { + + /** + * Regenerates a chunk + * + * @param chunk to be regenerated + * + * @return the current status object to keep track of the process + */ + IStatus regenerate(Chunk chunk); + + /** + * Regenerates a range of chunks + * + * @param chunks to be regenerated + * + * @return the current status objects to keep track of the process + * + * The status of this method will contain two different status objects. + * The first one is a normal Status object which keeps track of the + * chunk progress while the other object is just a reference which + * passes the values of the current chunk through, basically the second + * one is the same as the one of {@code regenerate(Chunk)} while the + * other one is to keep track how many chunks are done and if the + * process is complete. + */ + IStatus[] regenerate(Chunk[] chunks); + + /** + * Gets the chunks between two positions + * + * @param first position + * @param second position + * + * @return the chunks between the first and second position + */ + Chunk[] getChunks(Location first, Location second); + + /** + * Gets the chunks between two positions and merges them with an existing array + * of chunks + * + * @param first position + * @param second position + * @param array the existing array of chunks + * + * @return the chunks between the first and second position + */ + default Chunk[] getChunksAndMerge(Location first, Location second, Chunk... array) { + Chunk[] current = getChunks(first, second); + Chunk[] output = new Chunk[current.length + array.length]; + System.arraycopy(current, 0, output, 0, current.length); + System.arraycopy(array, 0, output, current.length, array.length); + return output; + } + +} diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/info/IStatus.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/info/IStatus.java new file mode 100644 index 0000000..035f363 --- /dev/null +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/info/IStatus.java @@ -0,0 +1,35 @@ +package net.sourcewriters.spigot.rwg.legacy.api.util.java.info; + +public interface IStatus { + + void done(); + + boolean isDone(); + + boolean success(); + + boolean failed(); + + boolean skip(); + + boolean cancel(); + + void add(); + + void add(long amount); + + void add(IStatus status); + + long getTotal(); + + long getMarked(); + + long getFailed(); + + long getSuccess(); + + long getSkipped(); + + long getCancelled(); + +} diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/Status.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/info/Status.java similarity index 62% rename from legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/Status.java rename to legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/info/Status.java index 0f79a8a..0e8de14 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/Status.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/info/Status.java @@ -1,6 +1,9 @@ -package net.sourcewriters.spigot.rwg.legacy.api.util.java; +package net.sourcewriters.spigot.rwg.legacy.api.util.java.info; -public class Status { +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; + +public final class Status implements IStatus { public static final Status EMPTY = new Status(true); @@ -8,17 +11,15 @@ public static final Status create() { return new Status(false); }; - private final Object sync = new Object(); - - private int total = 0; - private int failed = 0; - private int success = 0; - private int skipped = 0; - private int cancelled = 0; + private final AtomicLong total = new AtomicLong(0); + private final AtomicLong marked = new AtomicLong(0); + + private final AtomicLong failed = new AtomicLong(0); + private final AtomicLong success = new AtomicLong(0); + private final AtomicLong skipped = new AtomicLong(0); + private final AtomicLong cancelled = new AtomicLong(0); - private int marked = 0; - - private boolean done = false; + private final AtomicBoolean done = new AtomicBoolean(); /** * Constructs a Status with an specific done state @@ -26,9 +27,7 @@ public static final Status create() { * @param loaded - defines if it was loaded or not */ private Status(boolean done) { - synchronized (sync) { - this.done = done; - } + this.done.set(done); } /** @@ -36,19 +35,15 @@ private Status(boolean done) { * * @param total - starting total amount of objects to load */ - public Status(int total) { - synchronized (sync) { - this.total = total; - } + public Status(long total) { + this.total.set(total); } /** * Set the loading to done */ public void done() { - synchronized (sync) { - done = true; - } + done.getAndSet(true); } /** @@ -57,7 +52,7 @@ public void done() { * @return if loading is done */ public boolean isDone() { - return done; + return done.get(); } /** @@ -69,9 +64,7 @@ public boolean success() { if (isDone() || !mark()) { return false; } - synchronized (sync) { - success++; - } + success.incrementAndGet(); return true; } @@ -84,9 +77,7 @@ public boolean failed() { if (isDone() || !mark()) { return false; } - synchronized (sync) { - failed++; - } + failed.incrementAndGet(); return true; } @@ -99,9 +90,7 @@ public boolean skip() { if (isDone() || !mark()) { return false; } - synchronized (sync) { - skipped++; - } + skipped.incrementAndGet(); return true; } @@ -114,9 +103,7 @@ public boolean cancel() { if (isDone() || !mark()) { return false; } - synchronized (sync) { - cancelled++; - } + cancelled.incrementAndGet(); return true; } @@ -126,12 +113,10 @@ public boolean cancel() { * @return if it was marked or not */ private boolean mark() { - if (marked == total) { + if (marked.longValue() == total.longValue()) { return false; } - synchronized (sync) { - marked++; - } + marked.incrementAndGet(); return true; } @@ -147,13 +132,11 @@ public void add() { * * @param amount - amount to add */ - public void add(int amount) { + public void add(long amount) { if (isDone()) { return; } - synchronized (sync) { - total += amount; - } + total.addAndGet(amount); } /** @@ -161,18 +144,16 @@ public void add(int amount) { * * @param status - LoadingStatus to add */ - public void add(Status status) { + public void add(IStatus status) { if (isDone()) { return; } - synchronized (sync) { - total += status.total; - failed += status.failed; - marked += status.marked; - success += status.success; - skipped += status.skipped; - cancelled += status.cancelled; - } + total.addAndGet(status.getTotal()); + marked.addAndGet(status.getMarked()); + failed.addAndGet(status.getFailed()); + success.addAndGet(status.getSuccess()); + skipped.addAndGet(status.getSkipped()); + cancelled.addAndGet(status.getCancelled()); } /** @@ -180,8 +161,8 @@ public void add(Status status) { * * @return the total amount */ - public int getTotal() { - return total; + public long getTotal() { + return total.longValue(); } /** @@ -189,8 +170,8 @@ public int getTotal() { * * @return the marked amount */ - public int getMarked() { - return marked; + public long getMarked() { + return marked.longValue(); } /** @@ -198,8 +179,8 @@ public int getMarked() { * * @return the failed amount */ - public int getFailed() { - return failed; + public long getFailed() { + return failed.longValue(); } /** @@ -207,8 +188,8 @@ public int getFailed() { * * @return the successful amount */ - public int getSuccess() { - return success; + public long getSuccess() { + return success.longValue(); } /** @@ -216,8 +197,8 @@ public int getSuccess() { * * @return the not loaded amount */ - public int getSkipped() { - return skipped; + public long getSkipped() { + return skipped.longValue(); } /** @@ -225,8 +206,8 @@ public int getSkipped() { * * @return the cancelled amount */ - public int getCancelled() { - return cancelled; + public long getCancelled() { + return cancelled.longValue(); } } diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/wait/Awaiter.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/wait/Awaiter.java index 14f011e..125bbea 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/wait/Awaiter.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/wait/Awaiter.java @@ -5,9 +5,10 @@ import java.util.Map; import java.util.concurrent.Future; +import com.syntaxphoenix.syntaxapi.utils.general.Status; import com.syntaxphoenix.syntaxapi.utils.java.tools.Container; -import net.sourcewriters.spigot.rwg.legacy.api.util.java.Status; +import net.sourcewriters.spigot.rwg.legacy.api.util.java.info.IStatus; public final class Awaiter { @@ -36,8 +37,8 @@ public static void register(Class clazz, WaitFunction function) { } static { - register(com.syntaxphoenix.syntaxapi.utils.general.Status.class, WaitFunction.SYNTAX_STATUS); - register(Status.class, WaitFunction.STATUS); + register(Status.class, WaitFunction.SYNTAX_STATUS); + register(IStatus.class, WaitFunction.STATUS); register(Future.class, WaitFunction.FUTURE); } diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/wait/WaitFunction.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/wait/WaitFunction.java index 0ead791..f81bc99 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/wait/WaitFunction.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/util/java/wait/WaitFunction.java @@ -2,14 +2,16 @@ import java.util.concurrent.Future; -import net.sourcewriters.spigot.rwg.legacy.api.util.java.Status; +import com.syntaxphoenix.syntaxapi.utils.general.Status; + +import net.sourcewriters.spigot.rwg.legacy.api.util.java.info.IStatus; @FunctionalInterface @SuppressWarnings("rawtypes") public interface WaitFunction { - public static final WaitFunction SYNTAX_STATUS = com.syntaxphoenix.syntaxapi.utils.general.Status::isDone; - public static final WaitFunction STATUS = Status::isDone; + public static final WaitFunction SYNTAX_STATUS = Status::isDone; + public static final WaitFunction STATUS = IStatus::isDone; public static final WaitFunction FUTURE = Future::isDone; /*