New task system & improve DB case-insensitive SELECTs, support fabric 1.20.1 (#433)

This commit is contained in:
William authored and GitHub committed 2023-06-16 12:19:31 +01:00
1 parent d4b37e3677
commit fb052340c0
27 files changed
+687 -505

No files matched your search

@@ -62,7 +62,7 @@ import net.william278.huskhomes.user.FabricUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.util.FabricSafetyResolver;
import net.william278.huskhomes.util.FabricTaskRunner;
import net.william278.huskhomes.util.FabricTask;
import net.william278.huskhomes.util.UnsafeBlocks;
import net.william278.huskhomes.util.Validator;
import org.jetbrains.annotations.NotNull;
@@ -77,13 +77,11 @@ import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes,
FabricTaskRunner, FabricEventDispatcher, FabricSafetyResolver, ServerPlayNetworking.PlayChannelHandler {
public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes, FabricTask.Supplier,
FabricEventDispatcher, FabricSafetyResolver, ServerPlayNetworking.PlayChannelHandler {
public static final Logger LOGGER = LoggerFactory.getLogger("HuskHomes");
private static FabricHuskHomes instance;
@@ -93,10 +91,9 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
return instance;
}
private final ModContainer modContainer = FabricLoader.getInstance()
.getModContainer("huskhomes").orElseThrow(() -> new RuntimeException("Failed to get Mod Container"));
private final ModContainer modContainer = FabricLoader.getInstance().getModContainer("huskhomes")
.orElseThrow(() -> new RuntimeException("Failed to get Mod Container"));
private MinecraftServer minecraftServer;
private ConcurrentHashMap<UUID, CompletableFuture<?>> tasks;
private Map<String, Boolean> permissions;
private Set<SavedUser> savedUsers;
private Settings settings;
@@ -123,7 +120,6 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
instance = this;
// Get plugin version from mod container
this.tasks = new ConcurrentHashMap<>();
this.permissions = new HashMap<>();
this.savedUsers = new HashSet<>();
this.globalPlayerList = new HashMap<>();
@@ -151,6 +147,13 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
// Create adventure audience
this.audiences = FabricServerAudiences.of(minecraftServer);
// Temporarily log about sounds being disabled - todo: Remove when adventure-platform-fabric is updated
if (getSettings().doPlaySoundEffects()) {
log(Level.WARNING, "Sound effects are currently disabled for HuskHomes v" +
getVersion() + " on Fabric servers running Minecraft " +
getMinecraftServer().getVersion());
}
// Initialize the database
initialize(getSettings().getDatabaseType().getDisplayName() + " database connection", (plugin) -> {
this.database = switch (getSettings().getDatabaseType()) {
@@ -209,7 +212,7 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
audiences.close();
audiences = null;
}
cancelAllTasks();
cancelTasks();
}
@Override
@@ -479,12 +482,6 @@ public class FabricHuskHomes implements DedicatedServerModInitializer, HuskHomes
return minecraftServer;
}
@Override
@NotNull
public ConcurrentHashMap<UUID, CompletableFuture<?>> getTasks() {
return tasks;
}
@Override
@NotNull
public FabricHuskHomes getPlugin() {
@@ -120,9 +120,15 @@ public class FabricUser extends OnlineUser {
return player;
}
@Override
public void playSound(@NotNull String soundEffect) throws IllegalArgumentException {
// Do nothing - todo: Fix sounds when adventure-platform-fabric is updated.
}
@Override
public void teleportLocally(@NotNull Location location, boolean asynchronous) throws TeleportationException {
final MinecraftServer server = player.getServer();
assert server != null : "Server is null";
final Identifier worldId = Identifier.tryParse(location.getWorld().getName());
player.teleport(
server.getWorld(server.getWorldRegistryKeys().stream()
@@ -0,0 +1,142 @@
/*
* 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.FabricHuskHomes;
import net.william278.huskhomes.HuskHomes;
import org.jetbrains.annotations.NotNull;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public interface FabricTask extends Task {
class Sync extends Task.Sync implements FabricTask {
protected Sync(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long delayTicks) {
super(plugin, runnable, delayTicks);
}
@Override
public void cancel() {
super.cancel();
}
@Override
public void run() {
if (!cancelled) {
if (delayTicks > 0) {
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(delayTicks * 50);
} catch (InterruptedException e) {
e.printStackTrace();
}
((FabricHuskHomes) getPlugin()).getMinecraftServer().executeSync(runnable);
}, ((FabricHuskHomes) getPlugin()).getMinecraftServer());
} else {
((FabricHuskHomes) getPlugin()).getMinecraftServer().executeSync(runnable);
}
}
}
}
class Async extends Task.Async implements FabricTask {
private CompletableFuture<Void> task;
protected Async(@NotNull HuskHomes plugin, @NotNull Runnable runnable) {
super(plugin, runnable);
}
@Override
public void cancel() {
if (task != null && !cancelled) {
task.cancel(true);
}
super.cancel();
}
@Override
public void run() {
if (!cancelled) {
this.task = CompletableFuture.runAsync(runnable, ((FabricHuskHomes) getPlugin()).getMinecraftServer());
}
}
}
class Repeating extends Task.Repeating implements FabricTask {
private ScheduledFuture<?> task;
protected Repeating(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long repeatingTicks) {
super(plugin, runnable, repeatingTicks);
}
@Override
public void cancel() {
if (task != null && !cancelled) {
task.cancel(true);
}
super.cancel();
}
@Override
public void run() {
if (!cancelled) {
this.task = Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(
runnable, 0, repeatingTicks * 50, TimeUnit.MILLISECONDS
);
}
}
}
interface Supplier extends Task.Supplier {
@NotNull
@Override
default Task.Sync getSyncTask(@NotNull Runnable runnable, long delayTicks) {
return new Sync(getPlugin(), runnable, delayTicks);
}
@NotNull
@Override
default Task.Async getAsyncTask(@NotNull Runnable runnable) {
return new Async(getPlugin(), runnable);
}
@NotNull
@Override
default Task.Repeating getRepeatingTask(@NotNull Runnable runnable, long repeatingTicks) {
return new Repeating(getPlugin(), runnable, repeatingTicks);
}
@Override
default void cancelTasks() {
// Do nothing
}
}
}
@@ -1,101 +0,0 @@
/*
* 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.FabricHuskHomes;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
import java.util.concurrent.*;
import java.util.function.Supplier;
public interface FabricTaskRunner extends TaskRunner {
@Override
default void runAsync(@NotNull Runnable runnable) {
CompletableFuture.runAsync(runnable, getPlugin().getMinecraftServer());
}
@Override
default <T> CompletableFuture<T> supplyAsync(@NotNull Supplier<T> supplier) {
return CompletableFuture.supplyAsync(supplier, getPlugin().getMinecraftServer());
}
@Override
default void runSync(@NotNull Runnable runnable) {
getPlugin().getMinecraftServer().executeSync(runnable);
}
@NotNull
@Override
default UUID runAsyncRepeating(@NotNull Runnable runnable, long delay) {
final UUID taskId = UUID.randomUUID();
final CompletableFuture<?> future = new CompletableFuture<>();
final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(() -> {
if (future.isCancelled()) {
executor.shutdown();
return;
}
runnable.run();
}, 0, delay * 50, TimeUnit.MILLISECONDS);
getTasks().put(taskId, future);
return taskId;
}
@Override
default void runLater(@NotNull Runnable runnable, long delay) {
CompletableFuture.runAsync(() -> {
try {
Thread.sleep(delay * 50);
} catch (InterruptedException e) {
e.printStackTrace();
return;
}
runnable.run();
}, getPlugin().getMinecraftServer());
}
@Override
default void cancelTask(@NotNull UUID taskId) {
getTasks().computeIfPresent(taskId, (uuid, completableFuture) -> {
completableFuture.cancel(true);
return null;
});
getTasks().remove(taskId);
}
@Override
default void cancelAllTasks() {
getTasks().values().forEach(task -> task.cancel(true));
getTasks().clear();
}
@NotNull
@Override
ConcurrentHashMap<UUID, CompletableFuture<?>> getTasks();
@Override
@NotNull
FabricHuskHomes getPlugin();
}