Skip to content

Commit d4eced8

Browse files
Add Folia support
Fixes #383.
1 parent 2768608 commit d4eced8

5 files changed

Lines changed: 68 additions & 29 deletions

File tree

IF/pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
<repositories>
2121
<repository>
22-
<id>spigot-repo</id>
23-
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
22+
<id>papermc</id>
23+
<url>https://repo.papermc.io/repository/maven-public/</url>
2424
</repository>
2525
<repository>
2626
<id>mojang-repo</id>
@@ -156,9 +156,9 @@
156156
<scope>compile</scope>
157157
</dependency>
158158
<dependency>
159-
<groupId>org.spigotmc</groupId>
160-
<artifactId>spigot-api</artifactId>
161-
<version>1.20.3-R0.1-SNAPSHOT</version>
159+
<groupId>dev.folia</groupId>
160+
<artifactId>folia-api</artifactId>
161+
<version>1.20.4-R0.1-SNAPSHOT</version>
162162
<scope>provided</scope>
163163
<exclusions>
164164
<!-- Provided, but not accessible -->
@@ -292,4 +292,4 @@
292292
</plugin>
293293
</plugins>
294294
</build>
295-
</project>
295+
</project>

IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/GuiListener.java

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.github.stefvanschie.inventoryframework.gui.type.*;
44
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
5+
import com.github.stefvanschie.inventoryframework.util.DispatchUtil;
6+
57
import org.bukkit.Bukkit;
68
import org.bukkit.entity.HumanEntity;
79
import org.bukkit.entity.Player;
@@ -82,7 +84,7 @@ public void onInventoryClick(@NotNull InventoryClickEvent event) {
8284
gui.click(event);
8385

8486
if (event.isCancelled()) {
85-
Bukkit.getScheduler().runTask(this.plugin, () -> {
87+
DispatchUtil.runTaskFor(event.getWhoClicked(), this.plugin, () -> {
8688
PlayerInventory playerInventory = event.getWhoClicked().getInventory();
8789

8890
/* due to a client issue off-hand items appear as ghost items, this updates the off-hand correctly
@@ -352,26 +354,31 @@ public void onInventoryClose(@NotNull InventoryCloseEvent event) {
352354
playerInventory.setItemInOffHand(playerInventory.getItemInOffHand());
353355

354356
if (!gui.isUpdating()) {
355-
gui.callOnClose(event);
356-
357-
event.getInventory().clear(); //clear inventory to prevent items being put back
358-
359-
gui.getHumanEntityCache().restoreAndForget(humanEntity);
360-
361-
if (gui.getViewerCount() == 1) {
362-
activeGuiInstances.remove(gui);
363-
}
364-
365-
if (gui instanceof AnvilGui) {
366-
((AnvilGui) gui).handleClose(humanEntity);
367-
} else if (gui instanceof MerchantGui) {
368-
((MerchantGui) gui).handleClose(humanEntity);
369-
} else if (gui instanceof ModernSmithingTableGui) {
370-
((ModernSmithingTableGui) gui).handleClose(humanEntity);
371-
}
372-
373-
//Bukkit doesn't like it if you open an inventory while the previous one is being closed
374-
Bukkit.getScheduler().runTask(this.plugin, () -> gui.navigateToParent(humanEntity));
357+
DispatchUtil.runTaskFor(humanEntity, this.plugin, () -> {
358+
gui.callOnClose(event);
359+
360+
event.getInventory().clear(); //clear inventory to prevent items being put back
361+
362+
});
363+
364+
DispatchUtil.runTaskFor(humanEntity, this.plugin, () -> {
365+
gui.getHumanEntityCache().restoreAndForget(humanEntity);
366+
367+
if (gui.getViewerCount() == 1) {
368+
activeGuiInstances.remove(gui);
369+
}
370+
371+
if (gui instanceof AnvilGui) {
372+
((AnvilGui) gui).handleClose(humanEntity);
373+
} else if (gui instanceof MerchantGui) {
374+
((MerchantGui) gui).handleClose(humanEntity);
375+
} else if (gui instanceof ModernSmithingTableGui) {
376+
((ModernSmithingTableGui) gui).handleClose(humanEntity);
377+
}
378+
379+
//Bukkit doesn't like it if you open an inventory while the previous one is being closed
380+
DispatchUtil.runTaskFor(humanEntity, this.plugin, () -> { gui.navigateToParent(humanEntity); });
381+
});
375382
}
376383
}
377384

IF/src/main/java/com/github/stefvanschie/inventoryframework/gui/type/CartographyTableGui.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.github.stefvanschie.inventoryframework.gui.InventoryComponent;
88
import com.github.stefvanschie.inventoryframework.gui.type.util.InventoryBased;
99
import com.github.stefvanschie.inventoryframework.gui.type.util.NamedGui;
10+
import com.github.stefvanschie.inventoryframework.util.DispatchUtil;
1011
import com.github.stefvanschie.inventoryframework.util.version.Version;
1112
import com.github.stefvanschie.inventoryframework.util.version.VersionMatcher;
1213
import org.bukkit.Bukkit;
@@ -236,7 +237,7 @@ public void handleClickEvent(@NotNull InventoryClickEvent event) {
236237
cartographyTableInventory.sendItems(player, getTopItems());
237238
} else if (slot >= 0 && slot <= 2) {
238239
//the client rejects the output item if send immediately
239-
Bukkit.getScheduler().runTask(super.plugin, () ->
240+
DispatchUtil.runTaskFor(player, this.plugin, () ->
240241
cartographyTableInventory.sendItems(player, getTopItems()));
241242

242243
if (event.isCancelled()) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.stefvanschie.inventoryframework.util;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.entity.Entity;
5+
import org.bukkit.plugin.Plugin;
6+
7+
public class DispatchUtil {
8+
private static boolean isFolia() {
9+
try {
10+
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
11+
return true;
12+
} catch (ClassNotFoundException e) {
13+
return false;
14+
}
15+
}
16+
17+
/*
18+
* Schedules a task to run for a given entity.
19+
*
20+
* For non-Folia servers, runs on Bukkit scheduler.
21+
* For Folia servers, runs on the entity's scheduler.
22+
*/
23+
@SuppressWarnings("deprecation")
24+
public static void runTaskFor(Entity entity, Plugin plugin, Runnable task) {
25+
if (isFolia()) {
26+
entity.getScheduler().run(plugin, e -> task.run(), null);
27+
} else {
28+
Bukkit.getScheduler().runTask(plugin, task);
29+
}
30+
}
31+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# IF <a href="https://discord.gg/RXmy4HdR4x"><img align="right" src="https://img.shields.io/discord/780514939293925407" alt="Discord guild"></a>
22

3-
*This framework works for Minecraft versions 1.14-1.20*
3+
*This framework works for Minecraft versions 1.14-1.20, and supports Folia*
44

55
An inventory framework for managing GUIs
66

0 commit comments

Comments
 (0)