warmupTaskId = new AtomicReference<>();
- final Runnable countdownRunnable = (() -> {
- // Display a countdown action bar message
- if (timeLeft > 0) {
- plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_WARMUP)
- .ifPresent(teleporter::playSound);
- plugin.getLocales().getLocale("teleporting_action_bar_warmup", Integer.toString(timeLeft))
- .ifPresent(this::sendStatusMessage);
- } else {
- plugin.getLocales().getLocale("teleporting_action_bar_processing")
- .ifPresent(this::sendStatusMessage);
- try {
- super.execute();
- } catch (TeleportationException e) {
- e.displayMessage(teleporter, plugin);
- }
- }
-
- // Tick (decrement) the timed teleport timer and end it if done
- if (tickAndGetIfDone()) {
- plugin.getCurrentlyOnWarmup().remove(teleporter.getUuid());
- plugin.cancelTask(warmupTaskId.get());
- }
- });
- warmupTaskId.set(plugin.runAsyncRepeating(countdownRunnable, 20L));
+ this.task = plugin.getRepeatingTask(this, 20L);
+ this.task.run();
});
}
+ @Override
+ public void run() {
+ // Display a countdown action bar message
+ if (timeLeft > 0) {
+ plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_WARMUP)
+ .ifPresent(teleporter::playSound);
+ plugin.getLocales().getLocale("teleporting_action_bar_warmup", Integer.toString(timeLeft))
+ .ifPresent(this::sendStatusMessage);
+ } else {
+ plugin.getLocales().getLocale("teleporting_action_bar_processing")
+ .ifPresent(this::sendStatusMessage);
+ try {
+ super.execute();
+ } catch (TeleportationException e) {
+ e.displayMessage(teleporter, plugin);
+ }
+ }
+
+ // Tick (decrement) the timed teleport timer and end it if done
+ if (tickAndGetIfDone()) {
+ task.cancel();
+ plugin.getCurrentlyOnWarmup().remove(teleporter.getUuid());
+ }
+ }
+
/**
* Ticks a timed teleport, decrementing the time left until the teleport is complete
*
diff --git a/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java
index 65112123..e58aec7b 100644
--- a/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java
+++ b/common/src/main/java/net/william278/huskhomes/user/OnlineUser.java
@@ -90,7 +90,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs
* @param mineDown the parsed {@link MineDown} to send
* @param subTitle whether to send the title as a subtitle ({@code true} for a subtitle, {@code false} for a title)
*/
- public final void sendTitle(@NotNull MineDown mineDown, boolean subTitle) {
+ public void sendTitle(@NotNull MineDown mineDown, boolean subTitle) {
getAudience().sendTitlePart(subTitle ? TitlePart.SUBTITLE : TitlePart.TITLE, mineDown
.disable(MineDownParser.Option.SIMPLE_FORMATTING)
.replace().toComponent());
@@ -101,7 +101,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs
*
* @param mineDown the parsed {@link MineDown} to send
*/
- public final void sendActionBar(@NotNull MineDown mineDown) {
+ public void sendActionBar(@NotNull MineDown mineDown) {
getAudience().sendActionBar(mineDown
.disable(MineDownParser.Option.SIMPLE_FORMATTING)
.replace().toComponent());
@@ -113,7 +113,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs
*
* @param mineDown the parsed {@link MineDown} to send
*/
- public final void sendMessage(@NotNull MineDown mineDown) {
+ public void sendMessage(@NotNull MineDown mineDown) {
getAudience().sendMessage(mineDown
.disable(MineDownParser.Option.SIMPLE_FORMATTING)
.replace().toComponent());
@@ -125,7 +125,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs
* @param mineDown the parsed {@link MineDown} to send
* @param slot the {@link Locales.DisplaySlot} to send the message to
*/
- public final void sendMessage(@NotNull MineDown mineDown, @NotNull Locales.DisplaySlot slot) {
+ public void sendMessage(@NotNull MineDown mineDown, @NotNull Locales.DisplaySlot slot) {
if (slot == Locales.DisplaySlot.CHAT) {
sendMessage(mineDown);
} else if (slot == Locales.DisplaySlot.ACTION_BAR) {
@@ -144,7 +144,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs
* @implNote This method is intended for use with Minecraft's built-in translation keys. If the key is invalid,
* it will be substituted with {@code minecraft:block.minecraft.spawn.not_valid}
*/
- public final void sendTranslatableMessage(@Subst(Key.MINECRAFT_NAMESPACE + "block.minecraft.spawn.not_valid")
+ public void sendTranslatableMessage(@Subst(Key.MINECRAFT_NAMESPACE + "block.minecraft.spawn.not_valid")
@NotNull String translationKey) {
getAudience().sendMessage(Component.translatable(translationKey));
}
@@ -155,7 +155,7 @@ public abstract class OnlineUser extends User implements Teleportable, CommandUs
* @param soundEffect the sound effect to play. If the sound name is invalid, the sound will not play
* @implNote If the key is invalid, it will be substituted with {@code minecraft:block.note_block.banjo}
*/
- public final void playSound(@Subst(Key.MINECRAFT_NAMESPACE + "block.note_block.banjo")
+ public void playSound(@Subst(Key.MINECRAFT_NAMESPACE + "block.note_block.banjo")
@NotNull String soundEffect) throws IllegalArgumentException {
try {
getAudience().playSound(
diff --git a/common/src/main/java/net/william278/huskhomes/util/Task.java b/common/src/main/java/net/william278/huskhomes/util/Task.java
new file mode 100644
index 00000000..08152a23
--- /dev/null
+++ b/common/src/main/java/net/william278/huskhomes/util/Task.java
@@ -0,0 +1,135 @@
+/*
+ * This file is part of HuskHomes, licensed under the Apache License 2.0.
+ *
+ * Copyright (c) William278
+ * 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 CompletableFuture supplyAsync(@NotNull java.util.function.Supplier supplier) {
+ final CompletableFuture 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();
+
+}
diff --git a/common/src/main/java/net/william278/huskhomes/util/TaskRunner.java b/common/src/main/java/net/william278/huskhomes/util/TaskRunner.java
deleted file mode 100644
index d1de3d6c..00000000
--- a/common/src/main/java/net/william278/huskhomes/util/TaskRunner.java
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * This file is part of HuskHomes, licensed under the Apache License 2.0.
- *
- * Copyright (c) William278
- * 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.UUID;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.function.Supplier;
-
-public interface TaskRunner {
-
- void runAsync(@NotNull Runnable runnable);
-
- CompletableFuture supplyAsync(@NotNull Supplier supplier);
-
- void runSync(@NotNull Runnable runnable);
-
- @NotNull
- UUID runAsyncRepeating(@NotNull Runnable runnable, long delay);
-
- void runLater(@NotNull Runnable runnable, long delay);
-
- void cancelTask(@NotNull UUID taskId);
-
- void cancelAllTasks();
-
- @NotNull
- ConcurrentHashMap getTasks();
- @NotNull
- HuskHomes getPlugin();
-
-}
diff --git a/fabric/build.gradle b/fabric/build.gradle
index 431f8b9f..21edaad7 100644
--- a/fabric/build.gradle
+++ b/fabric/build.gradle
@@ -6,9 +6,9 @@ apply plugin: 'fabric-loom'
loom.serverOnlyMinecraftJar()
dependencies {
- minecraft 'com.mojang:minecraft:1.20'
- mappings 'net.fabricmc:yarn:1.20+build.1:v2'
- modImplementation 'net.fabricmc:fabric-loader:0.14.21'
+ minecraft "com.mojang:minecraft:${fabric_minecraft_version}"
+ mappings "net.fabricmc:yarn:${fabric_yarn_mappings}:v2"
+ modImplementation "net.fabricmc:fabric-loader:${fabric_loader_version}"
// Fabric API modules
Set fabricApiModules = [
@@ -19,9 +19,9 @@ dependencies {
'fabric-networking-api-v1'
]
fabricApiModules.forEach {
- modImplementation(fabricApi.module(it, '0.83.0+1.20'))
+ modImplementation(fabricApi.module(it, fabric_api_version))
}
- modImplementation include('net.kyori:adventure-platform-fabric:5.8.0')
+ modImplementation include('net.kyori:adventure-platform-fabric:5.9.0-SNAPSHOT')
modImplementation include('me.lucko:fabric-permissions-api:0.2-SNAPSHOT')
implementation include('redis.clients:jedis:' + jedis_version)
diff --git a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java
index 937c65bd..36e12265 100644
--- a/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java
+++ b/fabric/src/main/java/net/william278/huskhomes/FabricHuskHomes.java
@@ -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> tasks;
private Map permissions;
private Set 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> getTasks() {
- return tasks;
- }
-
@Override
@NotNull
public FabricHuskHomes getPlugin() {
diff --git a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java
index 28c13084..175a13c6 100644
--- a/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java
+++ b/fabric/src/main/java/net/william278/huskhomes/user/FabricUser.java
@@ -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()
diff --git a/fabric/src/main/java/net/william278/huskhomes/util/FabricTask.java b/fabric/src/main/java/net/william278/huskhomes/util/FabricTask.java
new file mode 100644
index 00000000..70b9efe8
--- /dev/null
+++ b/fabric/src/main/java/net/william278/huskhomes/util/FabricTask.java
@@ -0,0 +1,142 @@
+/*
+ * This file is part of HuskHomes, licensed under the Apache License 2.0.
+ *
+ * Copyright (c) William278
+ * 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 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
+ }
+
+ }
+
+}
diff --git a/fabric/src/main/java/net/william278/huskhomes/util/FabricTaskRunner.java b/fabric/src/main/java/net/william278/huskhomes/util/FabricTaskRunner.java
deleted file mode 100644
index 62a80be9..00000000
--- a/fabric/src/main/java/net/william278/huskhomes/util/FabricTaskRunner.java
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
- * This file is part of HuskHomes, licensed under the Apache License 2.0.
- *
- * Copyright (c) William278
- * 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 CompletableFuture supplyAsync(@NotNull Supplier 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> getTasks();
-
- @Override
- @NotNull
- FabricHuskHomes getPlugin();
-
-}
diff --git a/fabric/src/main/resources/fabric.mod.json b/fabric/src/main/resources/fabric.mod.json
index 03a5728d..cb21c4aa 100644
--- a/fabric/src/main/resources/fabric.mod.json
+++ b/fabric/src/main/resources/fabric.mod.json
@@ -27,8 +27,8 @@
]
},
"depends": {
- "fabricloader": ">=0.14.21",
- "minecraft": ">=1.20",
+ "fabricloader": ">=${fabric_loader_version}",
+ "minecraft": ">=${fabric_minecraft_version}",
"fabric-api": "*"
},
"custom": {
diff --git a/gradle.properties b/gradle.properties
index 26d97fa7..08c6a8ac 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -5,8 +5,13 @@ org.gradle.daemon=true
plugin_version=4.3.3
plugin_archive=huskhomes
-plugin_description=The powerful and intuitive set homes, warps, and teleports plugin/mod
+plugin_description=The powerful and intuitive homes, warps, and teleports plugin/mod
jedis_version=4.4.2
sqlite_driver_version=3.42.0.0
-mysql_driver_version=8.0.33
\ No newline at end of file
+mysql_driver_version=8.0.33
+
+fabric_minecraft_version=1.20.1
+fabric_loader_version=0.14.21
+fabric_yarn_mappings=1.20.1+build.2
+fabric_api_version=0.83.1+1.20.1
\ No newline at end of file
diff --git a/paper/build.gradle b/paper/build.gradle
index fc48a48e..3a074a23 100644
--- a/paper/build.gradle
+++ b/paper/build.gradle
@@ -47,6 +47,6 @@ shadowJar {
tasks {
runServer {
- minecraftVersion('1.20')
+ minecraftVersion('1.20.1')
}
}
\ No newline at end of file
diff --git a/sponge/src/main/java/net/william278/huskhomes/SpongeHuskHomes.java b/sponge/src/main/java/net/william278/huskhomes/SpongeHuskHomes.java
index c31c19ae..ae07699f 100644
--- a/sponge/src/main/java/net/william278/huskhomes/SpongeHuskHomes.java
+++ b/sponge/src/main/java/net/william278/huskhomes/SpongeHuskHomes.java
@@ -48,7 +48,7 @@ import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.user.SavedUser;
import net.william278.huskhomes.user.SpongeUser;
import net.william278.huskhomes.util.SpongeSafetyResolver;
-import net.william278.huskhomes.util.SpongeTaskRunner;
+import net.william278.huskhomes.util.SpongeTask;
import net.william278.huskhomes.util.UnsafeBlocks;
import net.william278.huskhomes.util.Validator;
import org.bstats.charts.SimplePie;
@@ -80,12 +80,12 @@ import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.nio.file.Path;
import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.stream.Collectors;
@Plugin("huskhomes")
-public class SpongeHuskHomes implements HuskHomes, SpongeTaskRunner, SpongeSafetyResolver, SpongeEventDispatcher, RawPlayDataHandler {
+public class SpongeHuskHomes implements HuskHomes, SpongeTask.Supplier, SpongeSafetyResolver, SpongeEventDispatcher,
+ RawPlayDataHandler {
/**
* Metrics ID for HuskHomes on Sponge.
@@ -110,7 +110,6 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTaskRunner, SpongeSafet
@Inject
private Metrics.Factory metricsFactory;
- private ConcurrentHashMap tasks;
private Set savedUsers;
private Settings settings;
private Locales locales;
@@ -134,7 +133,6 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTaskRunner, SpongeSafet
instance = this;
// Get plugin version from mod container
- this.tasks = new ConcurrentHashMap<>();
this.savedUsers = new HashSet<>();
this.globalPlayerList = new HashMap<>();
this.currentlyOnWarmup = new HashSet<>();
@@ -210,7 +208,7 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTaskRunner, SpongeSafet
if (broker != null) {
broker.close();
}
- cancelAllTasks();
+ cancelTasks();
}
@Listener
@@ -516,12 +514,6 @@ public class SpongeHuskHomes implements HuskHomes, SpongeTaskRunner, SpongeSafet
return channel;
}
- @NotNull
- @Override
- public ConcurrentHashMap getTasks() {
- return tasks;
- }
-
@NotNull
@Override
public SpongeHuskHomes getPlugin() {
diff --git a/sponge/src/main/java/net/william278/huskhomes/util/SpongeTask.java b/sponge/src/main/java/net/william278/huskhomes/util/SpongeTask.java
new file mode 100644
index 00000000..80fd13a3
--- /dev/null
+++ b/sponge/src/main/java/net/william278/huskhomes/util/SpongeTask.java
@@ -0,0 +1,145 @@
+/*
+ * This file is part of HuskHomes, licensed under the Apache License 2.0.
+ *
+ * Copyright (c) William278
+ * 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 net.william278.huskhomes.SpongeHuskHomes;
+import org.jetbrains.annotations.NotNull;
+import org.spongepowered.api.scheduler.ScheduledTask;
+import org.spongepowered.api.util.Ticks;
+
+import static org.spongepowered.api.scheduler.Task.builder;
+
+public interface SpongeTask extends Task {
+
+ class Sync extends Task.Sync implements SpongeTask {
+
+ private ScheduledTask task;
+
+ protected Sync(@NotNull HuskHomes plugin, @NotNull Runnable runnable, long delayTicks) {
+ super(plugin, runnable, delayTicks);
+ }
+
+ @Override
+ public void cancel() {
+ if (task != null && !cancelled) {
+ task.cancel();
+ }
+ super.cancel();
+ }
+
+ @Override
+ public void run() {
+ if (!cancelled) {
+ final SpongeHuskHomes plugin = ((SpongeHuskHomes) getPlugin());
+ this.task = plugin.getGame().server().scheduler().submit(builder()
+ .plugin(plugin.getPluginContainer())
+ .delay(Ticks.of(delayTicks))
+ .execute(runnable)
+ .build());
+ }
+ }
+ }
+
+ class Async extends Task.Async implements SpongeTask {
+
+ private ScheduledTask task;
+
+ protected Async(@NotNull HuskHomes plugin, @NotNull Runnable runnable) {
+ super(plugin, runnable);
+ }
+
+ @Override
+ public void cancel() {
+ if (task != null && !cancelled) {
+ task.cancel();
+ }
+ super.cancel();
+ }
+
+ @Override
+ public void run() {
+ if (!cancelled) {
+ final SpongeHuskHomes plugin = ((SpongeHuskHomes) getPlugin());
+ this.task = plugin.getGame().asyncScheduler().submit(builder()
+ .plugin(plugin.getPluginContainer())
+ .execute(runnable)
+ .build());
+ }
+ }
+ }
+
+ class Repeating extends Task.Repeating implements SpongeTask {
+
+ private ScheduledTask 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();
+ }
+ super.cancel();
+ }
+
+ @Override
+ public void run() {
+ if (!cancelled) {
+ final SpongeHuskHomes plugin = ((SpongeHuskHomes) getPlugin());
+ this.task = plugin.getGame().asyncScheduler().submit(builder()
+ .plugin(plugin.getPluginContainer())
+ .interval(Ticks.of(repeatingTicks))
+ .execute(runnable)
+ .build());
+ }
+ }
+ }
+
+ 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
+ }
+
+ }
+
+}
diff --git a/sponge/src/main/java/net/william278/huskhomes/util/SpongeTaskRunner.java b/sponge/src/main/java/net/william278/huskhomes/util/SpongeTaskRunner.java
deleted file mode 100644
index 0dc9c263..00000000
--- a/sponge/src/main/java/net/william278/huskhomes/util/SpongeTaskRunner.java
+++ /dev/null
@@ -1,142 +0,0 @@
-/*
- * This file is part of HuskHomes, licensed under the Apache License 2.0.
- *
- * Copyright (c) William278
- * 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.SpongeHuskHomes;
-import org.jetbrains.annotations.NotNull;
-import org.spongepowered.api.scheduler.Task;
-import org.spongepowered.api.util.Ticks;
-
-import java.util.UUID;
-import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.function.Supplier;
-
-public interface SpongeTaskRunner extends TaskRunner {
-
- @Override
- default void runAsync(@NotNull Runnable runnable) {
- getPlugin().getGame().asyncScheduler()
- .submit(Task.builder()
- .plugin(getPlugin().getPluginContainer())
- .execute(wrap(runnable))
- .build());
- }
-
- @Override
- default CompletableFuture supplyAsync(@NotNull Supplier supplier) {
- final CompletableFuture future = new CompletableFuture<>();
- getPlugin().getGame().asyncScheduler()
- .submit(Task.builder()
- .plugin(getPlugin().getPluginContainer())
- .execute(() -> future.complete(supplier.get()))
- .build());
-
- return future;
- }
-
- @Override
- default void runSync(@NotNull Runnable runnable) {
- getPlugin().getGame().server().scheduler()
- .submit(Task.builder()
- .plugin(getPlugin().getPluginContainer())
- .execute(runnable)
- .build());
- }
-
- @NotNull
- @Override
- default UUID runAsyncRepeating(@NotNull Runnable runnable, long delay) {
- final UUID taskId = UUID.randomUUID();
- final CancellableRunnable task = wrap(runnable);
- getTasks().put(taskId, task);
- getPlugin().getGame().asyncScheduler()
- .submit(Task.builder()
- .plugin(getPlugin().getPluginContainer())
- .execute(task)
- .interval(Ticks.of(delay))
- .build());
-
- return taskId;
- }
-
- @Override
- default void runLater(@NotNull Runnable runnable, long delay) {
- getPlugin().getGame().server().scheduler()
- .submit(Task.builder()
- .plugin(getPlugin().getPluginContainer())
- .execute(runnable)
- .delay(Ticks.of(delay))
- .build());
- }
-
- @Override
- default void cancelTask(@NotNull UUID taskId) {
- getTasks().computeIfPresent(taskId, (id, task) -> {
- task.cancel();
- return null;
- });
- getTasks().remove(taskId);
- }
-
- @Override
- default void cancelAllTasks() {
- getTasks().values().forEach(CancellableRunnable::cancel);
- getTasks().clear();
- }
-
- @NotNull
- private CancellableRunnable wrap(@NotNull Runnable runnable) {
- return new CancellableRunnable(runnable);
- }
-
- @NotNull
- @Override
- ConcurrentHashMap getTasks();
-
- @NotNull
- @Override
- SpongeHuskHomes getPlugin();
-
- /**
- * A wrapper for a {@link Runnable} that can be canceled
- */
- class CancellableRunnable implements Runnable {
- private final Runnable runnable;
- private boolean cancelled = false;
-
- CancellableRunnable(@NotNull Runnable runnable) {
- this.runnable = runnable;
- }
-
- @Override
- public void run() {
- if (cancelled) {
- return;
- }
- runnable.run();
- }
-
- public void cancel() {
- cancelled = true;
- }
-
- }
-}