diff --git a/api/pom.xml b/api/pom.xml index 71ad88f..541750a 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -5,7 +5,7 @@ parent dev.cerus.visualcrafting - 1.4.0 + 1.4.1 4.0.0 diff --git a/bukkit-16R3/pom.xml b/bukkit-16R3/pom.xml index 4ca92cb..0e2404a 100644 --- a/bukkit-16R3/pom.xml +++ b/bukkit-16R3/pom.xml @@ -5,7 +5,7 @@ parent dev.cerus.visualcrafting - 1.4.0 + 1.4.1 4.0.0 diff --git a/bukkit-17R1/pom.xml b/bukkit-17R1/pom.xml index a6ce3ae..0834e40 100644 --- a/bukkit-17R1/pom.xml +++ b/bukkit-17R1/pom.xml @@ -5,7 +5,7 @@ parent dev.cerus.visualcrafting - 1.4.0 + 1.4.1 4.0.0 diff --git a/bukkit-18R1/pom.xml b/bukkit-18R1/pom.xml index 9cfeab7..107e7d4 100644 --- a/bukkit-18R1/pom.xml +++ b/bukkit-18R1/pom.xml @@ -5,7 +5,7 @@ parent dev.cerus.visualcrafting - 1.4.0 + 1.4.1 4.0.0 diff --git a/bukkit-18R2/pom.xml b/bukkit-18R2/pom.xml index 7ee08e2..d2125ba 100644 --- a/bukkit-18R2/pom.xml +++ b/bukkit-18R2/pom.xml @@ -5,7 +5,7 @@ parent dev.cerus.visualcrafting - 1.4.0 + 1.4.1 4.0.0 diff --git a/bukkit-19R1/pom.xml b/bukkit-19R1/pom.xml index 1e9a9eb..e9f8d37 100644 --- a/bukkit-19R1/pom.xml +++ b/bukkit-19R1/pom.xml @@ -5,7 +5,7 @@ parent dev.cerus.visualcrafting - 1.4.0 + 1.4.1 4.0.0 diff --git a/bukkit-19R2/pom.xml b/bukkit-19R2/pom.xml index 1ce9ab8..7a12921 100644 --- a/bukkit-19R2/pom.xml +++ b/bukkit-19R2/pom.xml @@ -5,7 +5,7 @@ parent dev.cerus.visualcrafting - 1.4.0 + 1.4.1 4.0.0 diff --git a/bukkit-19R3/pom.xml b/bukkit-19R3/pom.xml index 5772edd..e78902d 100644 --- a/bukkit-19R3/pom.xml +++ b/bukkit-19R3/pom.xml @@ -6,7 +6,7 @@ dev.cerus.visualcrafting parent - 1.4.0 + 1.4.1 bukkit-19R3 diff --git a/bukkit-20R1/pom.xml b/bukkit-20R1/pom.xml index b11d628..0566f8b 100644 --- a/bukkit-20R1/pom.xml +++ b/bukkit-20R1/pom.xml @@ -6,7 +6,7 @@ dev.cerus.visualcrafting parent - 1.4.0 + 1.4.1 bukkit-20R1 diff --git a/bukkit-20R2/pom.xml b/bukkit-20R2/pom.xml index be12e21..5bcb985 100644 --- a/bukkit-20R2/pom.xml +++ b/bukkit-20R2/pom.xml @@ -6,7 +6,7 @@ dev.cerus.visualcrafting parent - 1.4.0 + 1.4.1 bukkit-20R2 diff --git a/bukkit-20R3/pom.xml b/bukkit-20R3/pom.xml index f07777c..8d42965 100644 --- a/bukkit-20R3/pom.xml +++ b/bukkit-20R3/pom.xml @@ -6,7 +6,7 @@ dev.cerus.visualcrafting parent - 1.4.0 + 1.4.1 bukkit-20R3 diff --git a/bukkit-20R4/pom.xml b/bukkit-20R4/pom.xml index 1263afa..47f8b21 100644 --- a/bukkit-20R4/pom.xml +++ b/bukkit-20R4/pom.xml @@ -6,7 +6,7 @@ dev.cerus.visualcrafting parent - 1.4.0 + 1.4.1 bukkit-20R4 diff --git a/bukkit-21R1/pom.xml b/bukkit-21R1/pom.xml index 5ce6126..35fc125 100644 --- a/bukkit-21R1/pom.xml +++ b/bukkit-21R1/pom.xml @@ -6,7 +6,7 @@ dev.cerus.visualcrafting parent - 1.4.0 + 1.4.1 bukkit-21R1 diff --git a/bukkit-21R2/pom.xml b/bukkit-21R2/pom.xml index 5e9f5bc..dea2674 100644 --- a/bukkit-21R2/pom.xml +++ b/bukkit-21R2/pom.xml @@ -6,7 +6,7 @@ dev.cerus.visualcrafting parent - 1.4.0 + 1.4.1 bukkit-21R2 diff --git a/bukkit-21R3/pom.xml b/bukkit-21R3/pom.xml index 95334ff..7884aac 100644 --- a/bukkit-21R3/pom.xml +++ b/bukkit-21R3/pom.xml @@ -6,7 +6,7 @@ dev.cerus.visualcrafting parent - 1.4.0 + 1.4.1 bukkit-21R3 diff --git a/folia/pom.xml b/folia/pom.xml new file mode 100644 index 0000000..115e2e1 --- /dev/null +++ b/folia/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + dev.cerus.visualcrafting + parent + 1.4.1 + + + folia + + + 21 + 21 + UTF-8 + + + + + papermc + https://repo.papermc.io/repository/maven-public/ + + + + + + dev.folia + folia-api + 1.20.1-R0.1-SNAPSHOT + provided + + + \ No newline at end of file diff --git a/folia/src/main/java/dev/cerus/visualcrafting/folia/FoliaUtil.java b/folia/src/main/java/dev/cerus/visualcrafting/folia/FoliaUtil.java new file mode 100644 index 0000000..53778d2 --- /dev/null +++ b/folia/src/main/java/dev/cerus/visualcrafting/folia/FoliaUtil.java @@ -0,0 +1,39 @@ +package dev.cerus.visualcrafting.folia; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Entity; +import org.bukkit.plugin.java.JavaPlugin; + +public class FoliaUtil { + // Source: https://github.com/pop4959/Chunky/blob/0f5d81d5ea4c4062f514fd6d7d59c3f7006cc91d/folia/src/main/java/org/popcraft/chunky/platform/Folia.java + private static final boolean IS_FOLIA = classExists("io.papermc.paper.threadedregions.RegionizedServer") || classExists("io.papermc.paper.threadedregions.RegionizedServerInitEvent"); + + public static boolean isFolia() { + return IS_FOLIA; + } + + public static void runIfFolia(Runnable runFolia, Runnable runElse) { + if (isFolia()) { + runFolia.run(); + } else { + runElse.run(); + } + } + + public static void scheduleOnEntity(JavaPlugin plugin, Entity entity, Runnable runnable, int delay) { + entity.getScheduler().execute(plugin, runnable, null, delay); + } + + public static void scheduleOnServer(JavaPlugin plugin, Runnable runnable) { + Bukkit.getServer().getGlobalRegionScheduler().execute(plugin, runnable); + } + + private static boolean classExists(final String clazz) { + try { + Class.forName(clazz); + return true; + } catch (ClassNotFoundException e) { + return false; + } + } +} diff --git a/plugin/pom.xml b/plugin/pom.xml index 1e586df..b9fec6d 100644 --- a/plugin/pom.xml +++ b/plugin/pom.xml @@ -5,7 +5,7 @@ parent dev.cerus.visualcrafting - 1.4.0 + 1.4.1 4.0.0 @@ -29,6 +29,12 @@ ${project.parent.version} compile + + dev.cerus.visualcrafting + folia + ${project.parent.version} + compile + dev.cerus.visualcrafting bukkit-16R3 diff --git a/plugin/src/main/java/dev/cerus/visualcrafting/plugin/VisualCraftingPlugin.java b/plugin/src/main/java/dev/cerus/visualcrafting/plugin/VisualCraftingPlugin.java index f233004..dfb7c72 100644 --- a/plugin/src/main/java/dev/cerus/visualcrafting/plugin/VisualCraftingPlugin.java +++ b/plugin/src/main/java/dev/cerus/visualcrafting/plugin/VisualCraftingPlugin.java @@ -2,6 +2,7 @@ import dev.cerus.visualcrafting.api.config.Config; import dev.cerus.visualcrafting.api.version.VersionAdapter; +import dev.cerus.visualcrafting.folia.FoliaUtil; import dev.cerus.visualcrafting.plugin.listener.CancelCraftingListener; import dev.cerus.visualcrafting.plugin.listener.CraftingInventoryInteractListener; import dev.cerus.visualcrafting.plugin.listener.PlayerJoinListener; @@ -115,6 +116,13 @@ public void onEnable() { versionAdapter.init(this, (player, integer) -> this.getServer().getScheduler().runTask(this, () -> visualizationController.entityClick(player, integer))); + versionAdapter.init(this, (player, integer) -> { + Runnable cmd = () -> visualizationController.entityClick(player, integer); + FoliaUtil.runIfFolia( + () -> FoliaUtil.scheduleOnEntity(VisualCraftingPlugin.this, player, cmd, 0), + () -> getServer().getScheduler().runTask(this, cmd) + ); + }); this.getServer().getPluginManager().registerEvents(new CancelCraftingListener(visualizationController), this); this.getServer().getPluginManager().registerEvents(new PlayerJoinListener(this, versionAdapter), this); diff --git a/plugin/src/main/java/dev/cerus/visualcrafting/plugin/listener/CraftingInventoryInteractListener.java b/plugin/src/main/java/dev/cerus/visualcrafting/plugin/listener/CraftingInventoryInteractListener.java index 4645b95..908c0bd 100644 --- a/plugin/src/main/java/dev/cerus/visualcrafting/plugin/listener/CraftingInventoryInteractListener.java +++ b/plugin/src/main/java/dev/cerus/visualcrafting/plugin/listener/CraftingInventoryInteractListener.java @@ -1,16 +1,21 @@ package dev.cerus.visualcrafting.plugin.listener; +import dev.cerus.visualcrafting.folia.FoliaUtil; import dev.cerus.visualcrafting.plugin.VisualCraftingPlugin; import dev.cerus.visualcrafting.plugin.visualizer.VisualizationController; import java.util.Arrays; import java.util.Objects; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.inventory.InventoryEvent; import org.bukkit.event.inventory.InventoryInteractEvent; +import org.bukkit.event.inventory.PrepareItemCraftEvent; import org.bukkit.inventory.CraftingInventory; +import org.bukkit.inventory.InventoryView; public class CraftingInventoryInteractListener implements Listener { private final VisualCraftingPlugin plugin; @@ -23,20 +28,25 @@ public CraftingInventoryInteractListener(VisualCraftingPlugin plugin, Visualizat @EventHandler public void onDrag(InventoryDragEvent event) { - onInteract(event); + onInteract(event.getView()); } @EventHandler public void onClick(InventoryClickEvent event) { - onInteract(event); + onInteract(event.getView()); } - public void onInteract(InventoryInteractEvent event) { - if (!(event.getView().getTopInventory() instanceof CraftingInventory inv)) { + @EventHandler + public void onPrepareCraft(PrepareItemCraftEvent event) { + onInteract(event.getView()); + } + + public void onInteract(InventoryView view) { + if (!(view.getTopInventory() instanceof CraftingInventory inv)) { return; } - Player player = (Player) event.getWhoClicked(); - plugin.getServer().getScheduler().runTaskLater(plugin, () -> { + Player player = (Player) view.getPlayer(); + Runnable cmd = () -> { if (!player.isOnline()) { return; } @@ -49,6 +59,10 @@ public void onInteract(InventoryInteractEvent event) { player, inv.getLocation().getBlock()); } - }, 1); + }; + FoliaUtil.runIfFolia( + () -> FoliaUtil.scheduleOnEntity(plugin, player, cmd, 1), + () -> Bukkit.getServer().getScheduler().runTaskLater(plugin, cmd, 1) + ); } } diff --git a/plugin/src/main/resources/plugin.yml b/plugin/src/main/resources/plugin.yml index 1568dbc..d5abcca 100644 --- a/plugin/src/main/resources/plugin.yml +++ b/plugin/src/main/resources/plugin.yml @@ -6,4 +6,5 @@ api-version: 1.16 description: "Visualizes the crafting process using invisible item frames and maps." prefix: "Visual Crafting" -website: "https://github.com/cerus/visual-crafting" \ No newline at end of file +website: "https://github.com/cerus/visual-crafting" +folia-supported: true \ No newline at end of file diff --git a/pom.xml b/pom.xml index 82dcd20..b80d9b6 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ dev.cerus.visualcrafting parent pom - 1.4.0 + 1.4.1 plugin api @@ -25,6 +25,7 @@ bukkit-21R1 bukkit-21R2 bukkit-21R3 + folia