mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 06:28:49 +00:00
New task system & improve DB case-insensitive SELECTs, support fabric 1.20.1 (#433)
This commit is contained in:
1 parent
d4b37e3677
commit
fb052340c0
27 files changed
+687
-505
No files matched your search
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* This file is part of HuskHomes, licensed under the Apache License 2.0.
|
||||
*
|
||||
* Copyright (c) William278 <[email protected]>
|
||||
* Copyright (c) contributors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package net.william278.huskhomes.util;
|
||||
|
||||
import net.william278.huskhomes.HuskHomes;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public interface Task extends Runnable {
|
||||
|
||||
abstract class Base implements Task {
|
||||
|
||||
protected final HuskHomes plugin;
|
||||
protected final Runnable runnable;
|
||||
protected boolean cancelled = false;
|
||||
|
||||
protected Base(@NotNull HuskHomes plugin, @NotNull Runnable runnable) {
|
||||
this.plugin = plugin;
|
||||
this.runnable = runnable;
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
cancelled = true;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public HuskHomes getPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
abstract class Async extends Base {
|
||||
|
||||
protected Async(@NotNull HuskHomes plugin, @NotNull Runnable runnable) {
|
||||
super(plugin, runnable);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
abstract class Sync extends Base {
|
||||
|
||||
protected long delayTicks;
|
||||
|
||||
protected Sync(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long delayTicks) {
|
||||
super(plugin, runnable);
|
||||
this.delayTicks = delayTicks;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
abstract class Repeating extends Base {
|
||||
|
||||
protected final long repeatingTicks;
|
||||
|
||||
protected Repeating(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long repeatingTicks) {
|
||||
super(plugin, runnable);
|
||||
this.repeatingTicks = repeatingTicks;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
interface Supplier {
|
||||
|
||||
@NotNull
|
||||
Task.Sync getSyncTask(@NotNull Runnable runnable, long delayTicks);
|
||||
|
||||
@NotNull
|
||||
Task.Async getAsyncTask(@NotNull Runnable runnable);
|
||||
|
||||
@NotNull
|
||||
Task.Repeating getRepeatingTask(@NotNull Runnable runnable, long repeatingTicks);
|
||||
|
||||
@NotNull
|
||||
default Task.Sync runSyncDelayed(@NotNull Runnable runnable, long delayTicks) {
|
||||
final Task.Sync task = getSyncTask(runnable, delayTicks);
|
||||
task.run();
|
||||
return task;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
default Task.Sync runSync(@NotNull Runnable runnable) {
|
||||
return runSyncDelayed(runnable, 0);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
default Task.Async runAsync(@NotNull Runnable runnable) {
|
||||
final Task.Async task = getAsyncTask(runnable);
|
||||
task.run();
|
||||
return task;
|
||||
}
|
||||
|
||||
default <T> CompletableFuture<T> supplyAsync(@NotNull java.util.function.Supplier<T> supplier) {
|
||||
final CompletableFuture<T> future = new CompletableFuture<>();
|
||||
runAsync(() -> {
|
||||
try {
|
||||
future.complete(supplier.get());
|
||||
} catch (Throwable throwable) {
|
||||
future.completeExceptionally(throwable);
|
||||
}
|
||||
});
|
||||
return future;
|
||||
}
|
||||
|
||||
void cancelTasks();
|
||||
|
||||
@NotNull
|
||||
HuskHomes getPlugin();
|
||||
|
||||
}
|
||||
|
||||
@NotNull
|
||||
HuskHomes getPlugin();
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user