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 Aug 24, 2024
1 parent 4fbf28d commit 87bb8ac
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 28 deletions.
12 changes: 6 additions & 6 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 @@ -192,9 +192,9 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.3-R0.1-SNAPSHOT</version>
<groupId>dev.folia</groupId>
<artifactId>folia-api</artifactId>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
<exclusions>
<!-- Provided, but not accessible -->
Expand Down Expand Up @@ -328,4 +328,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.github.stefvanschie.inventoryframework.gui.type.*;
import com.github.stefvanschie.inventoryframework.gui.type.util.Gui;
import com.github.stefvanschie.inventoryframework.util.InventoryViewUtil;
import com.github.stefvanschie.inventoryframework.util.DispatchUtil;

import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.LivingEntity;
Expand Down Expand Up @@ -84,7 +86,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 @@ -356,26 +358,31 @@ public void onInventoryClose(@NotNull InventoryCloseEvent event) {
playerInventory.setItemInOffHand(playerInventory.getItemInOffHand());

if (!gui.isUpdating()) {
gui.callOnClose(event);

event.getInventory().clear(); //clear inventory to prevent items being put back

gui.getHumanEntityCache().restoreAndForget(humanEntity);

if (gui.getViewerCount() == 1) {
activeGuiInstances.remove(gui);
}

if (gui instanceof AnvilGui) {
((AnvilGui) gui).handleClose(humanEntity);
} else if (gui instanceof MerchantGui) {
((MerchantGui) gui).handleClose(humanEntity);
} else if (gui instanceof ModernSmithingTableGui) {
((ModernSmithingTableGui) gui).handleClose(humanEntity);
}

//Bukkit doesn't like it if you open an inventory while the previous one is being closed
Bukkit.getScheduler().runTask(this.plugin, () -> gui.navigateToParent(humanEntity));
DispatchUtil.runTaskFor(humanEntity, this.plugin, () -> {
gui.callOnClose(event);

event.getInventory().clear(); //clear inventory to prevent items being put back

});

DispatchUtil.runTaskFor(humanEntity, this.plugin, () -> {
gui.getHumanEntityCache().restoreAndForget(humanEntity);

if (gui.getViewerCount() == 1) {
activeGuiInstances.remove(gui);
}

if (gui instanceof AnvilGui) {
((AnvilGui) gui).handleClose(humanEntity);
} else if (gui instanceof MerchantGui) {
((MerchantGui) gui).handleClose(humanEntity);
} else if (gui instanceof ModernSmithingTableGui) {
((ModernSmithingTableGui) gui).handleClose(humanEntity);
}

//Bukkit doesn't like it if you open an inventory while the previous one is being closed
DispatchUtil.runTaskFor(humanEntity, this.plugin, () -> { gui.navigateToParent(humanEntity); });
});
}
}

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);
}
}
}

0 comments on commit 87bb8ac

Please sign in to comment.