mirror of
https://github.com/songoda/SongodaCore.git
synced 2024-11-27 20:45:27 +01:00
Merge branch 'development'
This commit is contained in:
commit
54c8b51ef5
@ -4,7 +4,7 @@ stages:
|
||||
variables:
|
||||
name: "SongodaCore"
|
||||
path: "/builds/$CI_PROJECT_PATH"
|
||||
version: "2.3.22"
|
||||
version: "2.3.23"
|
||||
|
||||
build:
|
||||
stage: build
|
||||
|
@ -404,8 +404,6 @@ public class SongodaCore {
|
||||
if (pi != null) {
|
||||
registeredPlugins.remove(pi);
|
||||
}
|
||||
// Terminate all active threads
|
||||
DataManagerAbstract.terminateAllThreads();
|
||||
if (event.getPlugin() == piggybackedPlugin) {
|
||||
// uh-oh! Abandon ship!!
|
||||
Bukkit.getServicesManager().unregisterAll(piggybackedPlugin);
|
||||
|
@ -8,17 +8,17 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class DataManagerAbstract {
|
||||
|
||||
protected final DatabaseConnector databaseConnector;
|
||||
protected final Plugin plugin;
|
||||
|
||||
private static Map<String, ScheduledExecutorService> threads = new HashMap<>();
|
||||
private static Map<String, LinkedList<Runnable>> queues = new HashMap<>();
|
||||
|
||||
public DataManagerAbstract(DatabaseConnector databaseConnector, Plugin plugin) {
|
||||
this.databaseConnector = databaseConnector;
|
||||
@ -62,7 +62,6 @@ public class DataManagerAbstract {
|
||||
|
||||
/**
|
||||
* Queue a task to be run asynchronously. <br>
|
||||
* TODO: This needs to be separated from BukkitScheduler
|
||||
*
|
||||
* @param runnable task to run
|
||||
*/
|
||||
@ -80,43 +79,33 @@ public class DataManagerAbstract {
|
||||
}
|
||||
|
||||
/**
|
||||
* Queue a task to be run synchronously on a new thread.
|
||||
* Queue tasks to be ran asynchronously.
|
||||
*
|
||||
* @param runnable task to run on the next server tick
|
||||
* @param threadKey the thread key to run on.
|
||||
* @param runnable task to put into queue.
|
||||
* @param queueKey the queue key to add the runnable to.
|
||||
*/
|
||||
public void sync(Runnable runnable, String threadKey) {
|
||||
threads.computeIfAbsent(threadKey.toUpperCase(),
|
||||
t -> Executors.newSingleThreadScheduledExecutor()).execute(runnable);
|
||||
public void queueAsync(Runnable runnable, String queueKey) {
|
||||
if (queueKey == null) return;
|
||||
List<Runnable> queue = queues.computeIfAbsent(queueKey, t -> new LinkedList<>());
|
||||
queue.add(runnable);
|
||||
if (queue.size() == 1) runQueue(queueKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminate thread once all tasks have been completed.
|
||||
*
|
||||
* @param threadKey the thread key to terminate.
|
||||
*/
|
||||
public static void terminateThread(String threadKey) {
|
||||
ScheduledExecutorService service = threads.get(threadKey);
|
||||
if (service != null) {
|
||||
threads.remove(threadKey);
|
||||
try {
|
||||
service.awaitTermination(0, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
private void runQueue(String queueKey) {
|
||||
doQueue(queueKey, (s) -> {
|
||||
if (!queues.get(queueKey).isEmpty())
|
||||
runQueue(queueKey);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Terminate all active threads.
|
||||
*/
|
||||
public static void terminateAllThreads() {
|
||||
for (ScheduledExecutorService service : threads.values()) {
|
||||
try {
|
||||
service.awaitTermination(0, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
private void doQueue(String queueKey, Consumer<Boolean> callback) {
|
||||
Runnable runnable = queues.get(queueKey).getFirst();
|
||||
async(() -> {
|
||||
runnable.run();
|
||||
sync(() -> {
|
||||
queues.get(queueKey).remove(runnable);
|
||||
callback.accept(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
11
Core/src/main/java/com/songoda/core/utils/LocationUtils.java
Normal file
11
Core/src/main/java/com/songoda/core/utils/LocationUtils.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.songoda.core.utils;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class LocationUtils {
|
||||
|
||||
public static boolean isLocationMatching(Location location1, Location location2) {
|
||||
return location1.getBlockX() == location2.getBlockX() && location1.getBlockY() == location2.getBlockY() && location1.getBlockZ() == location2.getBlockZ();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user