Compare commits

...
4 Commits
4 changed files with 58 additions and 27 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ val nexusUser = providers.gradleProperty("alttdSnapshotUsername").orNull ?: Syst
val nexusPass = providers.gradleProperty("alttdSnapshotPassword").orNull ?: System.getenv("NEXUS_PASSWORD")
group = "com.alttd.inventory_gui"
version = "1.1.1-SNAPSHOT"
version = "1.1.5-SNAPSHOT"
repositories {
mavenCentral()
@@ -5,7 +5,8 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.Inventory;
import org.jetbrains.annotations.Nullable;
import java.util.Optional;
/**
* The {@code GuiListener} class implements the {@code Listener} interface to handle
@@ -19,17 +20,36 @@ import org.jetbrains.annotations.Nullable;
* The {@code GuiListener} is associated with a specific plugin by its name to ensure
* that event handling is scoped to the appropriate plugin's GUI instances.
*/
@SuppressWarnings("ClassCanBeRecord")
public final class GuiListener implements Listener {
private final String ownerPluginName;
/**
* Constructs a new {@code GuiListener} instance.
* @param ownerPluginName the name of the plugin that owns the GUI instances to which this listener should be attached
*/
public GuiListener(String ownerPluginName) {
this.ownerPluginName = ownerPluginName;
public GuiListener() {
}
/**
* Handles inventory click interactions in the context of GUI sessions. This method ensures
* that the event is processed consistently with other inventory interactions by delegating
* the handling to the {@code onInventoryInteract} method.
*
* @param event the {@code InventoryClickEvent} triggered when a player clicks within an inventory
*/
@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
onInventoryInteract(event);
}
/**
* Handles inventory drag interactions in the context of GUI sessions. This method delegates
* the handling of the event to the {@code onInventoryInteract} method to ensure consistent
* processing of inventory interactions.
*
* @param event the {@code InventoryDragEvent} triggered when a player drags items within an inventory
*/
@EventHandler
public void onInventoryDrag(InventoryDragEvent event) {
onInventoryInteract(event);
}
/**
@@ -40,20 +60,19 @@ public final class GuiListener implements Listener {
*
* @param event the {@code InventoryInteractEvent} triggered when a player interacts with an inventory
*/
@EventHandler
public void onInventoryInteract(InventoryInteractEvent event) {
if (!(event.getWhoClicked() instanceof Player player)) {
return;
}
GuiSession.Entry entry = getEntry(player, event.getInventory());
if (entry == null) {
Optional<GuiSession.Entry> optionalEntry = getEntry(player, event.getInventory());
if (optionalEntry.isEmpty()) {
return;
}
event.setCancelled(true);
if (event instanceof InventoryClickEvent inventoryClickEvent) {
entry.gui().handleClick(inventoryClickEvent);
optionalEntry.get().gui().handleClick(inventoryClickEvent);
}
}
@@ -67,26 +86,20 @@ public final class GuiListener implements Listener {
return;
}
GuiSession.Entry entry = getEntry(player, event.getInventory());
if (entry == null) {
Optional<GuiSession.Entry> optionalEntry = getEntry(player, event.getInventory());
if (optionalEntry.isEmpty()) {
return;
}
entry.gui().handleClose(player);
optionalEntry.get().gui().handleClose(player);
GuiSession.clear(player.getUniqueId());
}
private GuiSession.@Nullable Entry getEntry(Player player, Inventory event) {
private Optional<GuiSession.Entry> getEntry(Player player, Inventory event) {
GuiSession.Entry entry = GuiSession.get(player.getUniqueId());
if (entry == null) {
return null;
return Optional.empty();
}
if (!entry.ownerPluginName().equals(ownerPluginName)) {
return null;
}
if (event != entry.inv()) {
return null;
}
return entry;
return Optional.of(entry);
}
}
@@ -6,6 +6,7 @@ import com.alttd.inventory_gui.pane.SimplePane;
import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.HumanEntity;
@@ -24,6 +25,7 @@ import org.jetbrains.annotations.NotNull;
* This class is built with flexibility and ease of use in mind, supporting various
* customization options, event handling, and row validation to maintain consistency.
*/
@Slf4j
@Builder
public class InventoryGui {
@@ -69,11 +71,28 @@ public class InventoryGui {
* Renders the GUI's contents into the specified inventory.
* @param inv the inventory into which to render the GUI's contents
*/
public void render(Inventory inv) {
protected void render(Inventory inv) {
inv.clear();
root.render(inv);
}
/**
* Renders the GUI's contents into the open inventory of the specified human entity if it exists.
* @param humanEntity the human entity for which to render the GUI's contents
*/
public void render(HumanEntity humanEntity) {
GuiSession.Entry entry = GuiSession.get(humanEntity.getUniqueId());
if (entry == null) {
log.warn("No GUI session found for player {} during render", humanEntity.getName());
return;
}
Inventory inv = entry.inv();
render(inv);
if (humanEntity instanceof Player player) {
player.updateInventory();
}
}
/**
* Opens the GUI for the specified human entity.
* @param humanEntity the human entity for whom to open the GUI
@@ -105,7 +124,6 @@ public class InventoryGui {
}
if (!item.isClickable()) {
event.setCancelled(true);
return;
}
item.handle(event);
@@ -28,7 +28,7 @@ public final class InventoryGuiLib {
Objects.requireNonNull(plugin, "plugin cannot be null");
REGISTRATIONS.computeIfAbsent(plugin.getName(), name -> {
Listener listener = new GuiListener(name);
Listener listener = new GuiListener();
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
return new Registration(plugin, listener);
});