Skip to content

Commit

Permalink
Updated status lib and added IRegenerationHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Aug 21, 2021
1 parent f62f202 commit a5112bc
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 71 deletions.
2 changes: 1 addition & 1 deletion legacy-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.sourcewriters.spigot.rwg</groupId>
<artifactId>legacy-api</artifactId>
<version>2.0.0</version>
<version>2.1.0</version>

<distributionManagement>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,6 +42,9 @@ protected RealisticWorldGenerator(Plugin plugin) {

@NonNull
public abstract ISchematicStorage getSchematicStorage();

@NonNull
public abstract IRegenerationHelper getRegenerationHelper();

@NonNull
public abstract ICompatibilityManager getCompatibilityManager();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
@@ -1,54 +1,49 @@
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);

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
*
* @param loaded - defines if it was loaded or not
*/
private Status(boolean done) {
synchronized (sync) {
this.done = done;
}
this.done.set(done);
}

/**
* Constructs a Status
*
* @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);
}

/**
Expand All @@ -57,7 +52,7 @@ public void done() {
* @return if loading is done
*/
public boolean isDone() {
return done;
return done.get();
}

/**
Expand All @@ -69,9 +64,7 @@ public boolean success() {
if (isDone() || !mark()) {
return false;
}
synchronized (sync) {
success++;
}
success.incrementAndGet();
return true;
}

Expand All @@ -84,9 +77,7 @@ public boolean failed() {
if (isDone() || !mark()) {
return false;
}
synchronized (sync) {
failed++;
}
failed.incrementAndGet();
return true;
}

Expand All @@ -99,9 +90,7 @@ public boolean skip() {
if (isDone() || !mark()) {
return false;
}
synchronized (sync) {
skipped++;
}
skipped.incrementAndGet();
return true;
}

Expand All @@ -114,9 +103,7 @@ public boolean cancel() {
if (isDone() || !mark()) {
return false;
}
synchronized (sync) {
cancelled++;
}
cancelled.incrementAndGet();
return true;
}

Expand All @@ -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;
}

Expand All @@ -147,86 +132,82 @@ 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);
}

/**
* Add LoadingStatus to this status
*
* @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());
}

/**
* Get the total amount of objects to load
*
* @return the total amount
*/
public int getTotal() {
return total;
public long getTotal() {
return total.longValue();
}

/**
* Get the total amount of objects that were marked
*
* @return the marked amount
*/
public int getMarked() {
return marked;
public long getMarked() {
return marked.longValue();
}

/**
* Get the total amount of objects that failed to load
*
* @return the failed amount
*/
public int getFailed() {
return failed;
public long getFailed() {
return failed.longValue();
}

/**
* Get the amount of objects that we successfully loaded
*
* @return the successful amount
*/
public int getSuccess() {
return success;
public long getSuccess() {
return success.longValue();
}

/**
* Get the amount of objects that were not loaded
*
* @return the not loaded amount
*/
public int getSkipped() {
return skipped;
public long getSkipped() {
return skipped.longValue();
}

/**
* Get the amount of objects that were cancelled on load
*
* @return the cancelled amount
*/
public int getCancelled() {
return cancelled;
public long getCancelled() {
return cancelled.longValue();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {

Expand Down Expand Up @@ -36,8 +37,8 @@ public static <E> void register(Class<E> clazz, WaitFunction<E> 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);
}

Expand Down
Loading

0 comments on commit a5112bc

Please sign in to comment.