Skip to content

Commit

Permalink
Add Folia support
Browse files Browse the repository at this point in the history
Fixes #383.
  • Loading branch information
sowelipililimute committed Mar 31, 2023
1 parent 27a4c08 commit 669082a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
8 changes: 4 additions & 4 deletions IF/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
<id>papermc</id>
<url>https://repo.papermc.io/repository/maven-public/</url>
</repository>
<repository>
<id>mojang-repo</id>
Expand Down Expand Up @@ -132,8 +132,8 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.19.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# IF <a href="https://discord.gg/RXmy4HdR4x"><img align="right" src="https://img.shields.io/discord/780514939293925407" alt="Discord guild"></a>

*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

Expand Down

0 comments on commit 669082a

Please sign in to comment.