Refactor GuiListener to use Optional for null safety bump version to 1.1.3-SNAPSHOT

This commit is contained in:
akastijn 2026-02-01 03:41:09 +01:00
parent 3a4a1c97c2
commit d0398b1486
3 changed files with 14 additions and 23 deletions

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.2-SNAPSHOT"
version = "1.1.3-SNAPSHOT"
repositories {
mavenCentral()

View File

@ -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
@ -22,14 +23,10 @@ import org.jetbrains.annotations.Nullable;
@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() {
}
/**
@ -46,14 +43,14 @@ 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;
}
event.setCancelled(true);
if (event instanceof InventoryClickEvent inventoryClickEvent) {
entry.gui().handleClick(inventoryClickEvent);
optionalEntry.get().gui().handleClick(inventoryClickEvent);
}
}
@ -67,26 +64,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()) {//FIXME: does this equality check fail, and is it necessary?
return null;
}
return entry;
return Optional.of(entry);
}
}

View File

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