diff --git a/Database/build.gradle b/Database/build.gradle index 03d3323..27285c6 100644 --- a/Database/build.gradle +++ b/Database/build.gradle @@ -1,18 +1,8 @@ -plugins { - id 'java' - id 'maven-publish' -} - dependencies { implementation 'org.postgresql:postgresql:42.5.0' testImplementation 'com.google.code.gson:gson:2.10' - testImplementation "org.junit.jupiter:junit-jupiter-api:5.9.0" - testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.9.0" -} - -java { - withSourcesJar() - withJavadocJar() + testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junit_version}" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junit_version}" } test { diff --git a/Tasks/build.gradle b/Tasks/build.gradle index a94536e..adcbbf1 100644 --- a/Tasks/build.gradle +++ b/Tasks/build.gradle @@ -1,18 +1,44 @@ plugins { - id 'java' - id 'maven-publish' + id 'fabric-loom' version '1.0-SNAPSHOT' + } dependencies { + minecraft "com.mojang:minecraft:${project.minecraft_version}" + mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" + modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" + + modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junit_version}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junit_version}" } -java { - withSourcesJar() - withJavadocJar() +processResources { + inputs.property "version", project.version + + filesMatching("fabric.mod.json") { + expand "version": project.version + } } +tasks.withType(JavaCompile).configureEach { + // ensure that the encoding is set to UTF-8, no matter what the system default is + // this fixes some edge cases with special characters not displaying correctly + // see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html + // If Javadoc is generated, this must be specified in that task too. + it.options.encoding = "UTF-8" + + // The Minecraft launcher currently installs Java 8 for users, so your mod probably wants to target Java 8 too + // JDK 9 introduced a new way of specifying this that will make sure no newer classes or methods are used. + // We'll use that if it's available, but otherwise we'll use the older option. + def targetVersion = 17 + if (JavaVersion.current().isJava9Compatible()) { + it.options.release = targetVersion + } +} + + test { useJUnitPlatform() } diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/AltarikRunnable.java b/Tasks/src/main/java/fr/altarik/toolbox/task/AltarikRunnable.java new file mode 100644 index 0000000..8941675 --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/AltarikRunnable.java @@ -0,0 +1,18 @@ +package fr.altarik.toolbox.task; + +public abstract class AltarikRunnable implements Runnable { + + private boolean isCancelled = false; + + /** + * Warning: Some task cannot be cancelled (mostly async tasks like {@link fr.altarik.toolbox.task.asyncTasks.AsyncTasks} + * The result of this call is ignored in this case, you can still add a way to not execute its content (like if(isCancelled) return;) + */ + public void cancel() { + this.isCancelled = true; + } + + public boolean isCancelled() { + return isCancelled; + } +} diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java b/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java new file mode 100644 index 0000000..67ece7d --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/PeriodicTaskI.java @@ -0,0 +1,14 @@ +package fr.altarik.toolbox.task; + +public interface PeriodicTaskI extends TaskI { + + /** + * Run a task periodically + * @param function the function to execute + * @param delay delay before starting the task + * @param period time to wait between runs + * @throws InterruptedException When executed asynchronously, task may be interrupted + * @see fr.altarik.toolbox.task.syncTasks.PeriodicSyncTask + */ + public void addTask(AltarikRunnable function, long delay, long period) throws InterruptedException; +} diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java b/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java new file mode 100644 index 0000000..f0a0ff4 --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/Task.java @@ -0,0 +1,18 @@ +package fr.altarik.toolbox.task; + +import fr.altarik.toolbox.task.asyncTasks.AsyncTasks; +import net.fabricmc.api.ModInitializer; + +public class Task implements ModInitializer { + + public TaskI asyncWorkers = AsyncTasks.initialize(); + + @Override + public void onInitialize() { + + } + + public TaskI getAsyncWorkers() { + return asyncWorkers; + } +} \ No newline at end of file diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java b/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java new file mode 100644 index 0000000..8a45bec --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/TaskI.java @@ -0,0 +1,7 @@ +package fr.altarik.toolbox.task; + +public interface TaskI extends AutoCloseable { + + public void addTask(AltarikRunnable function) throws InterruptedException; + +} diff --git a/Tasks/src/main/java/fr/altarik/toolbox/asynctasks/AsyncTasks.java b/Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncTasks.java similarity index 84% rename from Tasks/src/main/java/fr/altarik/toolbox/asynctasks/AsyncTasks.java rename to Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncTasks.java index 0372b01..e99a9e2 100644 --- a/Tasks/src/main/java/fr/altarik/toolbox/asynctasks/AsyncTasks.java +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/asyncTasks/AsyncTasks.java @@ -1,4 +1,7 @@ -package fr.altarik.toolbox.asynctasks; +package fr.altarik.toolbox.task.asyncTasks; + +import fr.altarik.toolbox.task.AltarikRunnable; +import fr.altarik.toolbox.task.TaskI; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -7,7 +10,7 @@ import java.util.concurrent.TimeUnit; /** * A non-blocking small sized time-consuming tasks to executed asynchronously. */ -public class AsyncTasks implements AutoCloseable { +public class AsyncTasks implements TaskI { private final ExecutorService worker; @@ -27,11 +30,11 @@ public class AsyncTasks implements AutoCloseable { * * @return an instance of AsyncTasks */ - public static AsyncTasks initialize(int numberOfWorker) { + public static TaskI initialize(int numberOfWorker) { return new AsyncTasks(numberOfWorker); } - public static AsyncTasks initialize() { + public static TaskI initialize() { return initialize(Runtime.getRuntime().availableProcessors()); } @@ -65,7 +68,7 @@ public class AsyncTasks implements AutoCloseable { * @param function task to be executed * @throws InterruptedException when worker thread or BlockQueue has been interrupted while waiting (which is anormal) */ - public void addTask(Runnable function) throws InterruptedException { + public void addTask(AltarikRunnable function) throws InterruptedException { if(worker.isTerminated() || worker.isShutdown()) { throw new InterruptedException("Worker has been terminated or shutdown, it's impossible to add new task"); } @@ -75,9 +78,11 @@ public class AsyncTasks implements AutoCloseable { /** * This method is call when you want to close workers and wait for waiting tasks to finish * + * @throws UnfinishedTasksException when all tasks cannot be terminated in 10 seconds + * @throws InterruptedException if interrupted while waiting for tasks to finish */ @Override - public void close() throws Exception { + public void close() throws UnfinishedTasksException, InterruptedException { worker.shutdown(); boolean result = worker.awaitTermination(10, TimeUnit.SECONDS); if(!result) { diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/PeriodicSyncTask.java b/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/PeriodicSyncTask.java new file mode 100644 index 0000000..9da0aa5 --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/PeriodicSyncTask.java @@ -0,0 +1,47 @@ +package fr.altarik.toolbox.task.syncTasks; + +import fr.altarik.toolbox.task.AltarikRunnable; +import fr.altarik.toolbox.task.TaskI; + +import java.util.ArrayList; +import java.util.List; + +public class PeriodicSyncTask implements TaskI, Runnable { + + private ServerTickListener listener; + private List tasks; + + private PeriodicSyncTask() { + this.listener = new ServerTickListener(this); + this.tasks = new ArrayList<>(2); + } + + + public static TaskI initialize() { + return new PeriodicSyncTask(); + } + + @Override + public void addTask(AltarikRunnable function) throws InterruptedException { + tasks.add(function); + } + + @Override + public void close() throws Exception { + + } + + @Override + public void run() { + List removeList = new ArrayList<>(tasks.size()); + for(AltarikRunnable task : tasks) { + if(task.isCancelled()) { + removeList.add(task); + } else { + + task.run(); + } + } + tasks.removeAll(removeList); + } +} diff --git a/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/ServerTickListener.java b/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/ServerTickListener.java new file mode 100644 index 0000000..663114f --- /dev/null +++ b/Tasks/src/main/java/fr/altarik/toolbox/task/syncTasks/ServerTickListener.java @@ -0,0 +1,19 @@ +package fr.altarik.toolbox.task.syncTasks; + +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; +import net.minecraft.server.MinecraftServer; + +public class ServerTickListener { + + private final PeriodicSyncTask task; + + public ServerTickListener(PeriodicSyncTask syncTask) { + this.task = syncTask; + ServerTickEvents.START_SERVER_TICK.register(this::onServerTick); + } + + private void onServerTick(MinecraftServer minecraftServer) { + + } + +} diff --git a/Tasks/src/main/resources/Task.mixins.json b/Tasks/src/main/resources/Task.mixins.json new file mode 100644 index 0000000..b9c35e9 --- /dev/null +++ b/Tasks/src/main/resources/Task.mixins.json @@ -0,0 +1,15 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "fr.altarik.toolbox.task.mixin", + "compatibilityLevel": "JAVA_17", + "mixins": [ + ], + "client": [ + ], + "verbose": false, + "injectors": { + "defaultRequire": 1 + } + } + \ No newline at end of file diff --git a/Tasks/src/main/resources/fabric.mod.json b/Tasks/src/main/resources/fabric.mod.json new file mode 100644 index 0000000..8f15e03 --- /dev/null +++ b/Tasks/src/main/resources/fabric.mod.json @@ -0,0 +1,34 @@ +{ + "schemaVersion": 1, + "id": "task", + "version": "${version}", + "name": "Task", + "description": "A mod to use as a dependency for others to schedule tasks", + "authors": [ + "Altarik" + ], + "contributors": [ + "Legot Quentin" + ], + "contact": { + "homepage": "https://altarik.fr" + }, + "license": "Altarik @ All-Rights-Reserved ", + "icon": "assets/quests/icon.png", + "environment": "*", + "entrypoints": { + "main": [ + "fr.altarik.toolbox.task.Task" + ] + }, + "mixins": [ + "Task.mixins.json" + ], + "depends": { + "fabricloader": ">=0.14.12", + "fabric": "*", + "minecraft": "1.19.3", + "npcs": "2.0.1-SNAPSHOT" + } + } + \ No newline at end of file diff --git a/Tasks/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java b/Tasks/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java index 6c73178..6dd8593 100644 --- a/Tasks/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java +++ b/Tasks/src/test/java/fr/altarik/toolbox/AsyncTaskTest.java @@ -1,6 +1,8 @@ package fr.altarik.toolbox; -import fr.altarik.toolbox.asynctasks.AsyncTasks; +import fr.altarik.toolbox.task.AltarikRunnable; +import fr.altarik.toolbox.task.TaskI; +import fr.altarik.toolbox.task.asyncTasks.AsyncTasks; import org.junit.jupiter.api.Test; import java.util.Date; @@ -19,14 +21,17 @@ class AsyncTaskTest { void testAsyncOp() throws Exception { int numberOfTasks = 10000; System.out.println("Initializing async tasks worker"); - AsyncTasks worker = AsyncTasks.initialize(1); // only testing on a single worker, otherwise result have a high chance to not be in the order we want + TaskI worker = AsyncTasks.initialize(1); // only testing on a single worker, otherwise result have a high chance to not be in the order we want Stack results = new Stack<>(); for(int i = 0; i < numberOfTasks; i++) { System.out.println(log("sending task " + i)); AtomicInteger atomicInteger = new AtomicInteger(i); - worker.addTask(() -> { - System.out.println(log(" task " + atomicInteger.get())); - results.push(atomicInteger.get()); + worker.addTask(new AltarikRunnable() { + @Override + public void run() { + System.out.println(log(" task " + atomicInteger.get())); + results.push(atomicInteger.get()); + } }); } worker.close(); // wait until all worker terminated diff --git a/build.gradle b/build.gradle index 0eee5ab..f860cd0 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,13 @@ subprojects { } } + + java { + withSourcesJar() + withJavadocJar() + } + + repositories { maven { name 'altarik-snapshots' diff --git a/gradle.properties b/gradle.properties index 903ef89..335d1ed 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,12 @@ +org.gradle.jvmargs=-Xmx5G + + junit_version=5.9.0 +minecraft_version=1.19.3 +yarn_mappings=1.19.3+build.5 +loader_version=0.14.12 +fabric_version=0.70.0+1.19.3 + maven_group=fr.altarik.toolbox maven_version=2.0.0-SNAPSHOT repo_username=Altarik diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 41dfb87..070cb70 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/settings.gradle b/settings.gradle index b871795..ea6151c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,13 @@ +pluginManagement { + repositories { + mavenCentral() + maven { + name = 'Fabric' + url = 'https://maven.fabricmc.net/' + } + gradlePluginPortal() + } +} + rootProject.name = 'Toolbox' include(':Tasks', ':Database')