diff --git a/IF/pom.xml b/IF/pom.xml index 77d4000c..1ea743fd 100644 --- a/IF/pom.xml +++ b/IF/pom.xml @@ -19,8 +19,8 @@ - spigot-repo - https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + papermc + https://repo.papermc.io/repository/maven-public/ mojang-repo @@ -132,8 +132,8 @@ compile - org.spigotmc - spigot-api + dev.folia + folia-api 1.19.4-R0.1-SNAPSHOT provided diff --git a/IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/GuiListener.java b/IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/GuiListener.java index cfaaf9a5..f2e49ed3 100644 --- a/IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/GuiListener.java +++ b/IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/GuiListener.java @@ -2,6 +2,8 @@ import com.github.stefvanschie.inventoryframework.gui.type.*; import com.github.stefvanschie.inventoryframework.gui.type.util.Gui; +import com.github.stefvanschie.inventoryframework.util.DispatchUtil; + import org.bukkit.Bukkit; import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; @@ -82,7 +84,7 @@ public void onInventoryClick(@NotNull InventoryClickEvent event) { gui.click(event); if (event.isCancelled()) { - Bukkit.getScheduler().runTask(this.plugin, () -> { + DispatchUtil.runTaskFor(event.getWhoClicked(), this.plugin, () -> { PlayerInventory playerInventory = event.getWhoClicked().getInventory(); /* due to a client issue off-hand items appear as ghost items, this updates the off-hand correctly @@ -352,7 +354,7 @@ public void onInventoryClose(@NotNull InventoryCloseEvent event) { playerInventory.setItemInOffHand(playerInventory.getItemInOffHand()); if (!gui.isUpdating()) {//this is a hack to remove items correctly when players press the x button in a beacon - Bukkit.getScheduler().runTask(this.plugin, () -> { + DispatchUtil.runTaskFor(humanEntity, this.plugin, () -> { gui.callOnClose(event); if (humanEntity.getOpenInventory().getTopInventory() instanceof PlayerInventory) { @@ -361,7 +363,7 @@ public void onInventoryClose(@NotNull InventoryCloseEvent event) { }); //delay because merchants put items in slots back in the player inventory - Bukkit.getScheduler().runTask(this.plugin, () -> { + DispatchUtil.runTaskFor(humanEntity, this.plugin, ()-> { gui.getHumanEntityCache().restoreAndForget(humanEntity); if (gui.getViewerCount() == 1) { diff --git a/IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/type/CartographyTableGui.java b/IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/type/CartographyTableGui.java index 53c7c705..c2a1e55d 100644 --- a/IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/type/CartographyTableGui.java +++ b/IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/type/CartographyTableGui.java @@ -7,6 +7,7 @@ import com.github.stefvanschie.inventoryframework.gui.InventoryComponent; import com.github.stefvanschie.inventoryframework.gui.type.util.InventoryBased; import com.github.stefvanschie.inventoryframework.gui.type.util.NamedGui; +import com.github.stefvanschie.inventoryframework.util.DispatchUtil; import com.github.stefvanschie.inventoryframework.util.version.Version; import com.github.stefvanschie.inventoryframework.util.version.VersionMatcher; import org.bukkit.Bukkit; @@ -236,7 +237,7 @@ public void handleClickEvent(@NotNull InventoryClickEvent event) { cartographyTableInventory.sendItems(player, getTopItems()); } else if (slot >= 0 && slot <= 2) { //the client rejects the output item if send immediately - Bukkit.getScheduler().runTask(super.plugin, () -> + DispatchUtil.runTaskFor(player, this.plugin, () -> cartographyTableInventory.sendItems(player, getTopItems())); if (event.isCancelled()) { diff --git a/IF/src/main/java/com/github/stefvanschie/inventoryframework/util/DispatchUtil.java b/IF/src/main/java/com/github/stefvanschie/inventoryframework/util/DispatchUtil.java new file mode 100644 index 00000000..13594c66 --- /dev/null +++ b/IF/src/main/java/com/github/stefvanschie/inventoryframework/util/DispatchUtil.java @@ -0,0 +1,31 @@ +package com.github.stefvanschie.inventoryframework.util; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.Plugin; + +public class DispatchUtil { + private static boolean isFolia() { + try { + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } + + /* + * Schedules a task to run for a given entity. + * + * For non-Folia servers, runs on Bukkit scheduler. + * For Folia servers, runs on the entity's scheduler. + */ + @SuppressWarnings("deprecation") + public static void runTaskFor(Entity entity, Plugin plugin, Runnable task) { + if (isFolia()) { + entity.getScheduler().run(plugin, e -> task.run(), null); + } else { + Bukkit.getScheduler().runTask(plugin, task); + } + } +} diff --git a/README.md b/README.md index 9ce7c0b4..14779331 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # IF Discord guild -*This framework works for Minecraft versions 1.14-1.19* +*This framework works for Minecraft versions 1.14-1.19, and supports Folia* An inventory framework for managing GUIs