Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Folia support #384

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ jobs:
cd ../
- name: Build with Maven
run: mvn -B package --file pom.xml
- name: Artifacts
uses: actions/upload-artifact@v4
with:
path: |
IF/target/*.jar
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);
}
}
}
Loading