Skip to content
Draft
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
12 changes: 12 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'com.gradleup.shadow' version '9.0.0-beta6'
}

group = 'com.kingOf0.smartinventory'
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -186,14 +188,22 @@ default Optional<InventoryOpener> 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<BukkitRunnable> getTask(@NotNull final UUID uniqueId) {
default Optional<CancellableRunnable> getTask(@NotNull final UUID uniqueId) {
return Optional.ofNullable(this.getTasks().get(uniqueId));
}

Expand All @@ -203,7 +213,7 @@ default Optional<BukkitRunnable> getTask(@NotNull final UUID uniqueId) {
* @return tasks.
*/
@NotNull
Map<UUID, BukkitRunnable> getTasks();
Map<UUID, CancellableRunnable> getTasks();

/**
* initiates the manager.
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
});
}
Expand All @@ -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)
Expand All @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -56,10 +58,15 @@ public Plugin getPlugin() {
return plugin;
}

@Override
public @NotNull FoliaLib getFoliaLib() {
return new FoliaLib((JavaPlugin) plugin);
}

/**
* the tasks.
*/
private final Map<UUID, BukkitRunnable> tasks = new ConcurrentHashMap<>();
private final Map<UUID, CancellableRunnable> tasks = new ConcurrentHashMap<>();

static {
try {
Expand All @@ -81,12 +88,12 @@ public Collection<InventoryOpener> getOpeners() {

@NotNull
@Override
public Map<UUID, BukkitRunnable> getTasks() {
public Map<UUID, CancellableRunnable> getTasks() {
return tasks;
}

@Override
public @NotNull Optional<BukkitRunnable> getTask(@NotNull UUID uniqueId) {
public @NotNull Optional<CancellableRunnable> getTask(@NotNull UUID uniqueId) {
return SmartInventory.super.getTask(uniqueId);
}
}
Original file line number Diff line number Diff line change
@@ -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<WrappedTask> {
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();
}
}
}