diff --git a/api/src/main/java/net/okocraft/box/api/BoxAPI.java b/api/src/main/java/net/okocraft/box/api/BoxAPI.java index 7102185684..060ec08848 100644 --- a/api/src/main/java/net/okocraft/box/api/BoxAPI.java +++ b/api/src/main/java/net/okocraft/box/api/BoxAPI.java @@ -10,11 +10,13 @@ import net.okocraft.box.api.model.manager.StockManager; import net.okocraft.box.api.model.manager.UserManager; import net.okocraft.box.api.player.BoxPlayerMap; +import net.okocraft.box.api.taskfactory.TaskFactory; import net.okocraft.box.api.util.ExecutorProvider; import org.bukkit.NamespacedKey; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Unmodifiable; @@ -106,11 +108,20 @@ public interface BoxAPI { */ @NotNull CustomDataContainer getCustomDataContainer(); + /** + * Gets the {@link TaskFactory}. + * + * @return the {@link TaskFactory} + */ + @NotNull TaskFactory getTaskFactory(); + /** * Gets the {@link ExecutorProvider}. * * @return the {@link ExecutorProvider} */ + @Deprecated(forRemoval = true) + @ApiStatus.ScheduledForRemoval(inVersion = "4.2.0") @NotNull ExecutorProvider getExecutorProvider(); /** diff --git a/api/src/main/java/net/okocraft/box/api/taskfactory/TaskFactory.java b/api/src/main/java/net/okocraft/box/api/taskfactory/TaskFactory.java new file mode 100644 index 0000000000..af65e81221 --- /dev/null +++ b/api/src/main/java/net/okocraft/box/api/taskfactory/TaskFactory.java @@ -0,0 +1,47 @@ +package net.okocraft.box.api.taskfactory; + +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Supplier; + +/** + * An interface to create {@link CompletableFuture}s. + */ +public interface TaskFactory { + + /** + * Creates a {@link CompletableFuture} to run the task on main thread. + * + * @param task the task to run + * @return the new {@link CompletableFuture} + */ + @NotNull CompletableFuture run(@NotNull Runnable task); + + /** + * Creates a {@link CompletableFuture} to supply values on main thread. + * + * @param supplier the supplier + * @param the value type + * @return the new {@link CompletableFuture} + */ + @NotNull CompletableFuture supply(@NotNull Supplier supplier); + + /** + * Creates a {@link CompletableFuture} to run the task asynchronously. + * + * @param task the task to run + * @return the new {@link CompletableFuture} + */ + @NotNull CompletableFuture runAsync(@NotNull Runnable task); + + /** + * Creates a {@link CompletableFuture} to supply values asynchronously. + * + * @param supplier the supplier + * @param the value type + * @return the new {@link CompletableFuture} + */ + @NotNull CompletableFuture supplyAsync(@NotNull Supplier supplier); + +} diff --git a/api/src/main/java/net/okocraft/box/api/util/ExecutorProvider.java b/api/src/main/java/net/okocraft/box/api/util/ExecutorProvider.java index 063ecd5d4a..7f81efe01e 100644 --- a/api/src/main/java/net/okocraft/box/api/util/ExecutorProvider.java +++ b/api/src/main/java/net/okocraft/box/api/util/ExecutorProvider.java @@ -1,5 +1,6 @@ package net.okocraft.box.api.util; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import java.util.concurrent.Executor; @@ -7,6 +8,8 @@ /** * An interface for providing {@link Executor}s. */ +@Deprecated(forRemoval = true) +@ApiStatus.ScheduledForRemoval(inVersion = "4.2.0") public interface ExecutorProvider { /** diff --git a/core/src/main/java/net/okocraft/box/core/BoxPlugin.java b/core/src/main/java/net/okocraft/box/core/BoxPlugin.java index ef77d58f73..6a6f5c804b 100644 --- a/core/src/main/java/net/okocraft/box/core/BoxPlugin.java +++ b/core/src/main/java/net/okocraft/box/core/BoxPlugin.java @@ -18,6 +18,7 @@ import net.okocraft.box.api.model.manager.StockManager; import net.okocraft.box.api.model.manager.UserManager; import net.okocraft.box.api.player.BoxPlayerMap; +import net.okocraft.box.api.taskfactory.TaskFactory; import net.okocraft.box.api.util.ExecutorProvider; import net.okocraft.box.core.command.BoxAdminCommandImpl; import net.okocraft.box.core.command.BoxCommandImpl; @@ -35,6 +36,7 @@ import net.okocraft.box.core.storage.Storage; import net.okocraft.box.core.storage.implementations.yaml.YamlStorage; import net.okocraft.box.core.task.AutoSaveTask; +import net.okocraft.box.core.taskfactory.BoxTaskFactory; import net.okocraft.box.core.util.executor.BoxExecutorProvider; import net.okocraft.box.core.util.executor.InternalExecutors; import org.bukkit.Bukkit; @@ -70,6 +72,7 @@ public class BoxPlugin implements BoxAPI { private final DebugListener debugListener = new DebugListener(); private final EventBus eventBus = EventBus.newEventBus(); + private final BoxTaskFactory taskFactory = new BoxTaskFactory(); private final BoxExecutorProvider executorProvider = new BoxExecutorProvider(); private final BoxCommandImpl boxCommand = new BoxCommandImpl(); @@ -221,7 +224,7 @@ public void disable() { getLogger().info("Shutting down executors..."); try { - executorProvider.shutdown(); + taskFactory.shutdown(); InternalExecutors.shutdownAll(); } catch (InterruptedException e) { getLogger().log(Level.SEVERE, "Could not shutdown executors", e); @@ -339,6 +342,11 @@ private void saveDefaultLanguages(@NotNull Path directory) throws IOException { return customDataContainer; } + @Override + public @NotNull TaskFactory getTaskFactory() { + return taskFactory; + } + @Override public @NotNull ExecutorProvider getExecutorProvider() { return executorProvider; diff --git a/core/src/main/java/net/okocraft/box/core/command/BaseCommand.java b/core/src/main/java/net/okocraft/box/core/command/BaseCommand.java index 8f90512794..cb7ec92e98 100644 --- a/core/src/main/java/net/okocraft/box/core/command/BaseCommand.java +++ b/core/src/main/java/net/okocraft/box/core/command/BaseCommand.java @@ -19,7 +19,6 @@ import java.util.Collections; import java.util.List; import java.util.Locale; -import java.util.concurrent.CompletableFuture; import java.util.logging.Level; import static net.kyori.adventure.text.Component.text; @@ -171,10 +170,9 @@ private void sendHelp(@NotNull CommandSender sender) { } private void runCommandAsync(@NotNull Command command, @NotNull CommandSender sender, @NotNull String[] args) { - CompletableFuture.runAsync( - () -> command.onCommand(sender, args), - BoxProvider.get().getExecutorProvider().getExecutor() - ).exceptionallyAsync(e -> reportError(sender, args, e)); + BoxProvider.get().getTaskFactory() + .runAsync(() -> command.onCommand(sender, args)) + .exceptionallyAsync(e -> reportError(sender, args, e)); } private @Nullable Void reportError(@NotNull CommandSender sender, @NotNull String[] args, @NotNull Throwable throwable) { diff --git a/core/src/main/java/net/okocraft/box/core/taskfactory/BoxTaskFactory.java b/core/src/main/java/net/okocraft/box/core/taskfactory/BoxTaskFactory.java new file mode 100644 index 0000000000..dbf1f5e7fd --- /dev/null +++ b/core/src/main/java/net/okocraft/box/core/taskfactory/BoxTaskFactory.java @@ -0,0 +1,70 @@ +package net.okocraft.box.core.taskfactory; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import net.okocraft.box.api.BoxProvider; +import net.okocraft.box.api.taskfactory.TaskFactory; +import org.bukkit.Bukkit; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.function.Supplier; +import java.util.logging.Level; + +public class BoxTaskFactory implements TaskFactory { + + private final ExecutorService executor = Executors.newFixedThreadPool( + Math.min(Runtime.getRuntime().availableProcessors(), 4), + new ThreadFactoryBuilder() + .setDaemon(true) + .setNameFormat("box-worker-%d") + .setUncaughtExceptionHandler(this::reportUncaughtException) + .build() + ); + + @Override + public @NotNull CompletableFuture run(@NotNull Runnable task) { + return CompletableFuture.runAsync(task, getMainThread()); + } + + @Override + public @NotNull CompletableFuture supply(@NotNull Supplier supplier) { + return CompletableFuture.supplyAsync(supplier, getMainThread()); + } + + @Override + public @NotNull CompletableFuture runAsync(@NotNull Runnable task) { + return CompletableFuture.runAsync(task, executor); + } + + @Override + public @NotNull CompletableFuture supplyAsync(@NotNull Supplier supplier) { + return CompletableFuture.supplyAsync(supplier, executor); + } + + public void shutdown() throws InterruptedException { + executor.shutdown(); + + //noinspection ResultOfMethodCallIgnored + executor.awaitTermination(1, TimeUnit.MINUTES); + } + + private void reportUncaughtException(@NotNull Thread thread, @NotNull Throwable throwable) { + BoxProvider.get().getLogger().log( + Level.SEVERE, + "An exception occurred in the thread " + thread.getName(), + throwable + ); + } + + public @NotNull Executor getExecutor() { + return executor; + } + + public @NotNull Executor getMainThread() { + return Bukkit.getScheduler().getMainThreadExecutor(BoxProvider.get().getPluginInstance()); + } +} diff --git a/core/src/main/java/net/okocraft/box/core/util/executor/BoxExecutorProvider.java b/core/src/main/java/net/okocraft/box/core/util/executor/BoxExecutorProvider.java index 5eb496f15c..d27430fead 100644 --- a/core/src/main/java/net/okocraft/box/core/util/executor/BoxExecutorProvider.java +++ b/core/src/main/java/net/okocraft/box/core/util/executor/BoxExecutorProvider.java @@ -1,51 +1,21 @@ package net.okocraft.box.core.util.executor; -import com.google.common.util.concurrent.ThreadFactoryBuilder; import net.okocraft.box.api.BoxProvider; import net.okocraft.box.api.util.ExecutorProvider; -import org.bukkit.Bukkit; +import net.okocraft.box.core.taskfactory.BoxTaskFactory; import org.jetbrains.annotations.NotNull; import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; -import java.util.logging.Level; public class BoxExecutorProvider implements ExecutorProvider { - private final ExecutorService executor = Executors.newFixedThreadPool( - Math.min(Runtime.getRuntime().availableProcessors(), 4), - new ThreadFactoryBuilder() - .setDaemon(true) - .setNameFormat("box-worker-%d") - .setUncaughtExceptionHandler(this::reportUncaughtException) - .build() - ); - @Override public @NotNull Executor getExecutor() { - return executor; + return ((BoxTaskFactory) BoxProvider.get().getTaskFactory()).getExecutor(); } @Override public @NotNull Executor getMainThread() { - return Bukkit.getScheduler().getMainThreadExecutor(BoxProvider.get().getPluginInstance()); - } - - public void shutdown() throws InterruptedException { - executor.shutdown(); - - //noinspection ResultOfMethodCallIgnored - executor.awaitTermination(1, TimeUnit.MINUTES); - } - - - private void reportUncaughtException(@NotNull Thread thread, @NotNull Throwable throwable) { - BoxProvider.get().getLogger().log( - Level.SEVERE, - "An exception occurred in the thread " + thread.getName(), - throwable - ); + return ((BoxTaskFactory) BoxProvider.get().getTaskFactory()).getMainThread(); } } diff --git a/features/command/src/main/java/net/okocraft/box/feature/command/box/DepositCommand.java b/features/command/src/main/java/net/okocraft/box/feature/command/box/DepositCommand.java index 1a4679d351..0b81d2c79f 100644 --- a/features/command/src/main/java/net/okocraft/box/feature/command/box/DepositCommand.java +++ b/features/command/src/main/java/net/okocraft/box/feature/command/box/DepositCommand.java @@ -21,7 +21,6 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -97,10 +96,9 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String[] args) { private void depositItemInMainHand(@NotNull Player player, int amount) { var result = - CompletableFuture.supplyAsync( - () -> InventoryTransaction.depositItemInMainHand(player, amount), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory() + .supply(() -> InventoryTransaction.depositItemInMainHand(player, amount)) + .join(); if (result.getType().isModified()) { var item = result.getItem(); @@ -122,10 +120,9 @@ private void depositItemInMainHand(@NotNull Player player, int amount) { private void depositAll(@NotNull Player player) { var resultList = - CompletableFuture.supplyAsync( - () -> InventoryTransaction.depositItemsInInventory(player.getInventory()), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory() + .supply(() -> InventoryTransaction.depositItemsInInventory(player.getInventory())) + .join(); var stockHolder = BoxProvider.get().getBoxPlayerMap().get(player).getCurrentStockHolder(); @@ -138,10 +135,9 @@ private void depositAll(@NotNull Player player) { private void depositItem(@NotNull Player player, @NotNull BoxItem boxItem, int amount) { var resultList = - CompletableFuture.supplyAsync( - () -> InventoryTransaction.depositItem(player.getInventory(), boxItem, amount), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory() + .supply(() -> InventoryTransaction.depositItem(player.getInventory(), boxItem, amount)) + .join(); var stockHolder = BoxProvider.get().getBoxPlayerMap().get(player).getCurrentStockHolder(); diff --git a/features/command/src/main/java/net/okocraft/box/feature/command/box/WithdrawCommand.java b/features/command/src/main/java/net/okocraft/box/feature/command/box/WithdrawCommand.java index cf73ca50df..ebdec4b028 100644 --- a/features/command/src/main/java/net/okocraft/box/feature/command/box/WithdrawCommand.java +++ b/features/command/src/main/java/net/okocraft/box/feature/command/box/WithdrawCommand.java @@ -16,7 +16,6 @@ import java.util.List; import java.util.Locale; import java.util.Set; -import java.util.concurrent.CompletableFuture; import java.util.stream.Collectors; public class WithdrawCommand extends AbstractCommand { @@ -70,10 +69,9 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String[] args) { } var result = - CompletableFuture.supplyAsync( - () -> InventoryTransaction.withdraw(player.getInventory(), boxItem, amount), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory() + .supply(() -> InventoryTransaction.withdraw(player.getInventory(), boxItem, amount)) + .join(); var resultType = result.getType(); diff --git a/features/craft/src/main/java/net/okocraft/box/feature/craft/util/ItemCrafter.java b/features/craft/src/main/java/net/okocraft/box/feature/craft/util/ItemCrafter.java index d7acf19881..dfb746b737 100644 --- a/features/craft/src/main/java/net/okocraft/box/feature/craft/util/ItemCrafter.java +++ b/features/craft/src/main/java/net/okocraft/box/feature/craft/util/ItemCrafter.java @@ -9,7 +9,6 @@ import org.jetbrains.annotations.NotNull; import java.util.HashMap; -import java.util.concurrent.CompletableFuture; public class ItemCrafter { @@ -52,13 +51,10 @@ public static boolean craft(@NotNull Player crafter, @NotNull SelectedRecipe rec int storeAmount = resultAmount; if (Distribution.toInventory(crafter)) { - var result = CompletableFuture.supplyAsync( - () -> InventoryTransaction.withdraw( - crafter.getInventory(), - recipe.result(), - resultAmount - ), BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + var result = + BoxProvider.get().getTaskFactory().supply( + () -> InventoryTransaction.withdraw(crafter.getInventory(), recipe.result(), resultAmount) + ).join(); if (result.getType().isModified()) { storeAmount = resultAmount - result.getAmount(); diff --git a/features/gui/src/main/java/net/okocraft/box/feature/gui/GuiFeature.java b/features/gui/src/main/java/net/okocraft/box/feature/gui/GuiFeature.java index 3a45ee3472..fd070e5b23 100644 --- a/features/gui/src/main/java/net/okocraft/box/feature/gui/GuiFeature.java +++ b/features/gui/src/main/java/net/okocraft/box/feature/gui/GuiFeature.java @@ -16,8 +16,6 @@ import org.bukkit.event.HandlerList; import org.jetbrains.annotations.NotNull; -import java.util.concurrent.CompletableFuture; - public class GuiFeature extends AbstractBoxFeature implements Reloadable { private final MenuOpenCommand command = new MenuOpenCommand(); @@ -53,10 +51,7 @@ public void disable() { if (Bukkit.isPrimaryThread()) { closeMenus(); } else { - CompletableFuture.runAsync( - this::closeMenus, - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory().run(this::closeMenus).join(); } HandlerList.unregisterAll(listener); diff --git a/features/gui/src/main/java/net/okocraft/box/feature/gui/api/buttons/CloseButton.java b/features/gui/src/main/java/net/okocraft/box/feature/gui/api/buttons/CloseButton.java index 73f27808cc..82c687f409 100644 --- a/features/gui/src/main/java/net/okocraft/box/feature/gui/api/buttons/CloseButton.java +++ b/features/gui/src/main/java/net/okocraft/box/feature/gui/api/buttons/CloseButton.java @@ -13,8 +13,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.concurrent.CompletableFuture; - public class CloseButton implements Button { @Override @@ -40,10 +38,7 @@ public int getSlot() { @Override public void onClick(@NotNull Player clicker, @NotNull ClickType clickType) { - CompletableFuture.runAsync( - clicker::closeInventory, BoxProvider.get().getExecutorProvider().getMainThread() - ); - + BoxProvider.get().getTaskFactory().run(clicker::closeInventory); clicker.playSound(clicker.getLocation(), Sound.BLOCK_CHEST_CLOSE, SoundCategory.MASTER, 100f, 1.5f); } } diff --git a/features/gui/src/main/java/net/okocraft/box/feature/gui/api/util/MenuOpener.java b/features/gui/src/main/java/net/okocraft/box/feature/gui/api/util/MenuOpener.java index 2b428bd411..0c69009050 100644 --- a/features/gui/src/main/java/net/okocraft/box/feature/gui/api/util/MenuOpener.java +++ b/features/gui/src/main/java/net/okocraft/box/feature/gui/api/util/MenuOpener.java @@ -7,8 +7,6 @@ import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; -import java.util.concurrent.CompletableFuture; - public final class MenuOpener { public static void open(@NotNull Menu menu, @NotNull Player viewer) { @@ -17,10 +15,9 @@ public static void open(@NotNull Menu menu, @NotNull Player viewer) { holder.initializeMenu(viewer); holder.applyContents(); - CompletableFuture.runAsync( - () -> viewer.openInventory(holder.getInventory()), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory() + .run(() -> viewer.openInventory(holder.getInventory())) + .join(); viewer.playSound(viewer.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 100f, 2.0f); } diff --git a/features/gui/src/main/java/net/okocraft/box/feature/gui/internal/listener/InventoryListener.java b/features/gui/src/main/java/net/okocraft/box/feature/gui/internal/listener/InventoryListener.java index 0a60a625a7..b534fe2bed 100644 --- a/features/gui/src/main/java/net/okocraft/box/feature/gui/internal/listener/InventoryListener.java +++ b/features/gui/src/main/java/net/okocraft/box/feature/gui/internal/listener/InventoryListener.java @@ -58,17 +58,17 @@ public void onClick(@NotNull InventoryClickEvent event) { this.lastClickTime.put(clicker.getUniqueId(), System.currentTimeMillis()); - var task = CompletableFuture.runAsync( - () -> processClick(holder, clicker, event.getSlot(), event.getClick()), - BoxProvider.get().getExecutorProvider().getExecutor() - ).exceptionallyAsync(throwable -> { - BoxProvider.get().getLogger().log( - Level.SEVERE, - "Could not complete click task (" + clicker.getName() + ")", - throwable - ); - return null; - }); + var task = + BoxProvider.get().getTaskFactory() + .runAsync(() -> processClick(holder, clicker, event.getSlot(), event.getClick())) + .exceptionallyAsync(throwable -> { + BoxProvider.get().getLogger().log( + Level.SEVERE, + "Could not complete click task (" + clicker.getName() + ")", + throwable + ); + return null; + }); clickTaskMap.put(clicker.getUniqueId(), task); } @@ -78,10 +78,7 @@ private void processClick(@NotNull BoxInventoryHolder holder, @NotNull Player cl holder.processClick(clicker, slot, type); if (holder.updateMenu(clicker)) { - CompletableFuture.runAsync( - () -> holder.updateInventory(clicker), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory().run(() -> holder.updateInventory(clicker)).join(); } } } diff --git a/features/gui/src/main/java/net/okocraft/box/feature/gui/internal/mode/StorageMode.java b/features/gui/src/main/java/net/okocraft/box/feature/gui/internal/mode/StorageMode.java index 669663d503..55a6844da8 100644 --- a/features/gui/src/main/java/net/okocraft/box/feature/gui/internal/mode/StorageMode.java +++ b/features/gui/src/main/java/net/okocraft/box/feature/gui/internal/mode/StorageMode.java @@ -22,7 +22,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import java.util.concurrent.CompletableFuture; public class StorageMode implements BoxItemClickMode { @@ -94,14 +93,10 @@ private void processDeposit(@NotNull Context context) { int transactionAmount = PlayerSession.get(player).getCustomNumberHolder(TRANSACTION_AMOUNT_NAME).getAmount(); - var resultList = CompletableFuture.supplyAsync( - () -> InventoryTransaction.depositItem( - player.getInventory(), - context.item(), - transactionAmount - ), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + var resultList = + BoxProvider.get().getTaskFactory().supply( + () -> InventoryTransaction.depositItem(player.getInventory(), context.item(), transactionAmount) + ).join(); if (!resultList.getType().isModified()) { player.playSound(player.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 100f, 1.5f); @@ -134,10 +129,10 @@ private void processWithdraw(@NotNull Context context) { var amount = Math.min(currentStock, transactionAmount); - var result = CompletableFuture.supplyAsync( - () -> InventoryTransaction.withdraw(player.getInventory(), context.item(), amount), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + var result = + BoxProvider.get().getTaskFactory() + .supply(() -> InventoryTransaction.withdraw(player.getInventory(), context.item(), amount)) + .join(); if (result.getType().isModified()) { stockHolder.decrease(result.getItem(), result.getAmount()); @@ -180,10 +175,10 @@ public void onClick(@NotNull Player clicker, @NotNull ClickType clickType) { return; } - var resultList = CompletableFuture.supplyAsync( - () -> InventoryTransaction.depositItemsInInventory(clicker.getInventory()), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + var resultList = + BoxProvider.get().getTaskFactory() + .supply(() -> InventoryTransaction.depositItemsInInventory(clicker.getInventory())) + .join(); if (!resultList.getType().isModified()) { clicker.playSound(clicker.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 100f, 1.5f); diff --git a/features/stick/src/main/java/net/okocraft/box/feature/stick/command/CustomStickCommand.java b/features/stick/src/main/java/net/okocraft/box/feature/stick/command/CustomStickCommand.java index 4396cf7598..b29d108ee9 100644 --- a/features/stick/src/main/java/net/okocraft/box/feature/stick/command/CustomStickCommand.java +++ b/features/stick/src/main/java/net/okocraft/box/feature/stick/command/CustomStickCommand.java @@ -10,8 +10,6 @@ import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.NotNull; -import java.util.concurrent.CompletableFuture; - import static net.kyori.adventure.text.Component.text; import static net.kyori.adventure.text.Component.translatable; import static net.kyori.adventure.text.format.NamedTextColor.AQUA; @@ -58,10 +56,7 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String[] args) { return; } - CompletableFuture.runAsync( - () -> makeStick(player), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory().run(() -> makeStick(player)).join(); } private void makeStick(@NotNull Player player) { diff --git a/features/stick/src/main/java/net/okocraft/box/feature/stick/command/StickCommand.java b/features/stick/src/main/java/net/okocraft/box/feature/stick/command/StickCommand.java index 0807a2fd4f..162c14428d 100644 --- a/features/stick/src/main/java/net/okocraft/box/feature/stick/command/StickCommand.java +++ b/features/stick/src/main/java/net/okocraft/box/feature/stick/command/StickCommand.java @@ -10,7 +10,6 @@ import org.jetbrains.annotations.NotNull; import java.util.Set; -import java.util.concurrent.CompletableFuture; import static net.kyori.adventure.text.Component.text; import static net.kyori.adventure.text.Component.translatable; @@ -49,10 +48,7 @@ public void onCommand(@NotNull CommandSender sender, @NotNull String[] args) { return; } - CompletableFuture.runAsync( - () -> runCommand(player), - BoxProvider.get().getExecutorProvider().getMainThread() - ).join(); + BoxProvider.get().getTaskFactory().run(() -> runCommand(player)).join(); } private void runCommand(@NotNull Player player) {