From e9305425cfce6a20317f5f9313fc471deb6ae03d Mon Sep 17 00:00:00 2001 From: adabugra <57899270+adabugra@users.noreply.github.com> Date: Sun, 26 Jan 2025 17:20:25 +0300 Subject: [PATCH 1/2] Folia Support --- build.gradle | 12 ++++++++ .../smartinventory/SmartHolder.java | 10 +++++++ .../smartinventory/SmartInventory.java | 28 +++++++++++++------ .../event/PgBottomClickEvent.java | 14 ++++++++-- .../holder/SmartInventoryHolder.java | 7 +++++ .../listener/InventoryClickListener.java | 2 +- .../manager/BasicSmartInventory.java | 15 +++++++--- .../util/CancellableRunnable.java | 28 +++++++++++++++++++ 8 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/kingOf0/smartinventory/smartinventory/util/CancellableRunnable.java diff --git a/build.gradle b/build.gradle index 0bd180f..bc212cd 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java' + id 'io.github.goooler.shadow' version '8.1.8' } group = 'com.kingOf0.smartinventory' @@ -15,12 +16,23 @@ repositories { maven { url = "https://repo.codemc.io/repository/maven-snapshots/" } + + maven { + name = "jitpack" + url = "https://jitpack.io" + } } dependencies { compileOnly("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT") compileOnly("org.jetbrains:annotations:26.0.2") + + implementation "com.github.technicallycoded:FoliaLib:0.4.3" +} + +shadowJar { + relocate "com.tcoded.folialib", "com.kingOf0.smartinventory.lib.folialib" } test { diff --git a/src/main/java/com/kingOf0/smartinventory/smartinventory/SmartHolder.java b/src/main/java/com/kingOf0/smartinventory/smartinventory/SmartHolder.java index f523ec5..27a52ae 100644 --- a/src/main/java/com/kingOf0/smartinventory/smartinventory/SmartHolder.java +++ b/src/main/java/com/kingOf0/smartinventory/smartinventory/SmartHolder.java @@ -25,6 +25,7 @@ package com.kingOf0.smartinventory.smartinventory; +import com.tcoded.folialib.FoliaLib; import org.bukkit.entity.Player; import org.bukkit.inventory.InventoryHolder; import org.bukkit.plugin.Plugin; @@ -67,6 +68,15 @@ public interface SmartHolder extends InventoryHolder { @NotNull Plugin getPlugin(); + + /** + * obtains the FoliaLib. + * + * @return FoliaLib. + */ + @NotNull + FoliaLib getFolialib(); + /** * checks if the holder is active. * diff --git a/src/main/java/com/kingOf0/smartinventory/smartinventory/SmartInventory.java b/src/main/java/com/kingOf0/smartinventory/smartinventory/SmartInventory.java index b795fca..6f8b856 100644 --- a/src/main/java/com/kingOf0/smartinventory/smartinventory/SmartInventory.java +++ b/src/main/java/com/kingOf0/smartinventory/smartinventory/SmartInventory.java @@ -28,13 +28,15 @@ import com.kingOf0.smartinventory.smartinventory.event.PgTickEvent; import com.kingOf0.smartinventory.smartinventory.listener.*; import com.kingOf0.smartinventory.smartinventory.opener.ChestInventoryOpener; +import com.kingOf0.smartinventory.smartinventory.util.CancellableRunnable; +import com.tcoded.folialib.FoliaLib; +import com.tcoded.folialib.wrapper.task.WrappedTask; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.InventoryHolder; import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; import java.util.*; @@ -186,14 +188,22 @@ default Optional findOpener(@NotNull final InventoryType type) @NotNull Plugin getPlugin(); + /** + * obtains the folialib. + * + * @return the plugin. + */ + @NotNull + FoliaLib getFoliaLib(); + /** * obtains the given uniqueId's task. * * @param uniqueId the uniqueId to obtain. - * @return a {@link BukkitRunnable} instance. + * @return a {@link WrappedTask} instance. */ @NotNull - default Optional getTask(@NotNull final UUID uniqueId) { + default Optional getTask(@NotNull final UUID uniqueId) { return Optional.ofNullable(this.getTasks().get(uniqueId)); } @@ -203,7 +213,7 @@ default Optional getTask(@NotNull final UUID uniqueId) { * @return tasks. */ @NotNull - Map getTasks(); + Map getTasks(); /** * initiates the manager. @@ -237,7 +247,7 @@ default void removeTask(@NotNull final UUID uniqueId) { * @param uniqueId the unique id to set. * @param task the task to set. */ - default void setTask(@NotNull final UUID uniqueId, @NotNull final BukkitRunnable task) { + default void setTask(@NotNull final UUID uniqueId, @NotNull final CancellableRunnable task) { this.getTasks().put(uniqueId, task); } @@ -248,7 +258,7 @@ default void setTask(@NotNull final UUID uniqueId, @NotNull final BukkitRunnable */ default void stopTick(@NotNull final UUID uniqueId) { this.getTask(uniqueId).ifPresent(runnable -> { - Bukkit.getScheduler().cancelTask(runnable.getTaskId()); + runnable.cancel(); this.removeTask(uniqueId); }); } @@ -260,7 +270,7 @@ default void stopTick(@NotNull final UUID uniqueId) { * @param page the page to start. */ default void tick(@NotNull final UUID uniqueId, @NotNull final Page page) { - final BukkitRunnable task = new BukkitRunnable() { + final CancellableRunnable task = new CancellableRunnable() { @Override public void run() { SmartInventory.getHolder(uniqueId) @@ -273,9 +283,9 @@ public void run() { }; this.setTask(uniqueId, task); if (page.async()) { - task.runTaskTimerAsynchronously(this.getPlugin(), page.startDelay(), page.tick()); + getFoliaLib().getScheduler().runTimerAsync(task, page.startDelay(), page.tick()); } else { - task.runTaskTimer(this.getPlugin(), page.startDelay(), page.tick()); + getFoliaLib().getScheduler().runTimer(task, page.startDelay(), page.tick()); } } diff --git a/src/main/java/com/kingOf0/smartinventory/smartinventory/event/PgBottomClickEvent.java b/src/main/java/com/kingOf0/smartinventory/smartinventory/event/PgBottomClickEvent.java index 49b9866..4e19992 100644 --- a/src/main/java/com/kingOf0/smartinventory/smartinventory/event/PgBottomClickEvent.java +++ b/src/main/java/com/kingOf0/smartinventory/smartinventory/event/PgBottomClickEvent.java @@ -27,7 +27,7 @@ import com.kingOf0.smartinventory.smartinventory.InventoryContents; import com.kingOf0.smartinventory.smartinventory.event.abs.BottomClickEvent; -import org.bukkit.Bukkit; +import com.tcoded.folialib.FoliaLib; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; @@ -55,10 +55,18 @@ public final class PgBottomClickEvent implements BottomClickEvent { @NotNull private final Plugin plugin; - public PgBottomClickEvent(@NotNull InventoryContents contents, @NotNull InventoryClickEvent event, @NotNull Plugin plugin) { + /** + * the folialib. + */ + @NotNull + private final FoliaLib foliaLib; + + + public PgBottomClickEvent(@NotNull InventoryContents contents, @NotNull InventoryClickEvent event, @NotNull Plugin plugin, @NotNull FoliaLib foliaLib) { this.contents = contents; this.event = event; this.plugin = plugin; + this.foliaLib = foliaLib; } @Override @@ -68,7 +76,7 @@ public void cancel() { @Override public void close() { - Bukkit.getScheduler().runTask(this.plugin, () -> + foliaLib.getScheduler().runAtEntity(this.contents.player(), task -> this.contents.page().close(this.contents.player())); } diff --git a/src/main/java/com/kingOf0/smartinventory/smartinventory/holder/SmartInventoryHolder.java b/src/main/java/com/kingOf0/smartinventory/smartinventory/holder/SmartInventoryHolder.java index 7e25521..dd48479 100644 --- a/src/main/java/com/kingOf0/smartinventory/smartinventory/holder/SmartInventoryHolder.java +++ b/src/main/java/com/kingOf0/smartinventory/smartinventory/holder/SmartInventoryHolder.java @@ -28,9 +28,11 @@ import com.kingOf0.smartinventory.smartinventory.InventoryContents; import com.kingOf0.smartinventory.smartinventory.Page; import com.kingOf0.smartinventory.smartinventory.SmartHolder; +import com.tcoded.folialib.FoliaLib; import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; /** @@ -83,6 +85,11 @@ public Plugin getPlugin() { return this.getPage().inventory().getPlugin(); } + @Override + public @NotNull FoliaLib getFolialib() { + return new FoliaLib((JavaPlugin) getPlugin()); + } + @Override public boolean isActive() { return active; diff --git a/src/main/java/com/kingOf0/smartinventory/smartinventory/listener/InventoryClickListener.java b/src/main/java/com/kingOf0/smartinventory/smartinventory/listener/InventoryClickListener.java index 6ced4b8..bb941f5 100644 --- a/src/main/java/com/kingOf0/smartinventory/smartinventory/listener/InventoryClickListener.java +++ b/src/main/java/com/kingOf0/smartinventory/smartinventory/listener/InventoryClickListener.java @@ -76,7 +76,7 @@ public void onInventoryClick(final InventoryClickEvent event) { } final HumanEntity player = event.getWhoClicked(); if (clicked.equals(player.getOpenInventory().getBottomInventory())) { - page.accept(new PgBottomClickEvent(contents, event, plugin)); + page.accept(new PgBottomClickEvent(contents, event, plugin, smartHolder.getFolialib())); return; } final ItemStack current = event.getCurrentItem(); diff --git a/src/main/java/com/kingOf0/smartinventory/smartinventory/manager/BasicSmartInventory.java b/src/main/java/com/kingOf0/smartinventory/smartinventory/manager/BasicSmartInventory.java index 6a2a342..7268426 100644 --- a/src/main/java/com/kingOf0/smartinventory/smartinventory/manager/BasicSmartInventory.java +++ b/src/main/java/com/kingOf0/smartinventory/smartinventory/manager/BasicSmartInventory.java @@ -27,8 +27,10 @@ import com.kingOf0.smartinventory.smartinventory.InventoryOpener; import com.kingOf0.smartinventory.smartinventory.SmartInventory; +import com.kingOf0.smartinventory.smartinventory.util.CancellableRunnable; +import com.tcoded.folialib.FoliaLib; import org.bukkit.plugin.Plugin; -import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.util.*; @@ -56,10 +58,15 @@ public Plugin getPlugin() { return plugin; } + @Override + public @NotNull FoliaLib getFoliaLib() { + return new FoliaLib((JavaPlugin) plugin); + } + /** * the tasks. */ - private final Map tasks = new ConcurrentHashMap<>(); + private final Map tasks = new ConcurrentHashMap<>(); static { try { @@ -81,12 +88,12 @@ public Collection getOpeners() { @NotNull @Override - public Map getTasks() { + public Map getTasks() { return tasks; } @Override - public @NotNull Optional getTask(@NotNull UUID uniqueId) { + public @NotNull Optional getTask(@NotNull UUID uniqueId) { return SmartInventory.super.getTask(uniqueId); } } diff --git a/src/main/java/com/kingOf0/smartinventory/smartinventory/util/CancellableRunnable.java b/src/main/java/com/kingOf0/smartinventory/smartinventory/util/CancellableRunnable.java new file mode 100644 index 0000000..5b0a9d5 --- /dev/null +++ b/src/main/java/com/kingOf0/smartinventory/smartinventory/util/CancellableRunnable.java @@ -0,0 +1,28 @@ +package com.kingOf0.smartinventory.smartinventory.util; + +import com.tcoded.folialib.wrapper.task.WrappedTask; + +import java.util.function.Consumer; + +public abstract class CancellableRunnable implements Consumer { + private boolean cancelled = false; + + public void cancel() { + cancelled = true; + } + + public abstract void run(); + + @Override + public void accept(WrappedTask wrappedTask) { + // Run the task if it hasn't been cancelled + if (!cancelled) { + run(); + } + + // Cancel the task if it has been cancelled after running + if (cancelled) { + wrappedTask.cancel(); + } + } +} \ No newline at end of file From 905b8dc6e9e341b0ecb184aba2d2d505fa76ea7d Mon Sep 17 00:00:00 2001 From: adabugra <57899270+adabugra@users.noreply.github.com> Date: Sun, 26 Jan 2025 18:53:24 +0300 Subject: [PATCH 2/2] Update shadow plugin --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index bc212cd..76f3af9 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { id 'java' - id 'io.github.goooler.shadow' version '8.1.8' + id 'com.gradleup.shadow' version '9.0.0-beta6' } group = 'com.kingOf0.smartinventory'