Skip to content

Commit

Permalink
feat: add folia support
Browse files Browse the repository at this point in the history
  • Loading branch information
cerus committed Dec 15, 2024
1 parent 7962356 commit 5f03df9
Show file tree
Hide file tree
Showing 22 changed files with 129 additions and 25 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-16R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-17R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-18R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-18R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>bukkit-19R3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>bukkit-20R1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>bukkit-20R2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>bukkit-20R3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>bukkit-20R4</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-21R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>bukkit-21R1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-21R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>bukkit-21R2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-21R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<artifactId>bukkit-21R3</artifactId>
Expand Down
35 changes: 35 additions & 0 deletions folia/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.4.1</version>
</parent>

<artifactId>folia</artifactId>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<repositories>
<repository>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
39 changes: 39 additions & 0 deletions folia/src/main/java/dev/cerus/visualcrafting/folia/FoliaUtil.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
8 changes: 7 additions & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -29,6 +29,12 @@
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>folia</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>bukkit-16R3</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
Expand All @@ -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)
);
}
}
3 changes: 2 additions & 1 deletion plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
website: "https://github.com/cerus/visual-crafting"
folia-supported: true
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.4.0</version>
<version>1.4.1</version>
<modules>
<module>plugin</module>
<module>api</module>
Expand All @@ -25,6 +25,7 @@
<module>bukkit-21R1</module>
<module>bukkit-21R2</module>
<module>bukkit-21R3</module>
<module>folia</module>
</modules>

<properties>
Expand Down

0 comments on commit 5f03df9

Please sign in to comment.