From 8e54e55e3a510f0cc0bbd64f9365d66976fdf770 Mon Sep 17 00:00:00 2001 From: filoghost Date: Sun, 16 Aug 2020 16:03:26 +0200 Subject: [PATCH] Simplify code --- .../inventory/DefaultMenuView.java | 18 ++---------------- .../listener/InventoryListener.java | 8 ++++---- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/plugin/src/main/java/me/filoghost/chestcommands/inventory/DefaultMenuView.java b/plugin/src/main/java/me/filoghost/chestcommands/inventory/DefaultMenuView.java index 0d8a5b3..89f0a9b 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/inventory/DefaultMenuView.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/inventory/DefaultMenuView.java @@ -5,7 +5,6 @@ */ package me.filoghost.chestcommands.inventory; -import me.filoghost.chestcommands.api.ClickResult; import me.filoghost.chestcommands.api.Icon; import me.filoghost.chestcommands.api.MenuView; import me.filoghost.chestcommands.icon.RefreshableIcon; @@ -49,17 +48,12 @@ public class DefaultMenuView implements MenuView { viewer.openInventory(bukkitInventory.getInventory()); } - public SlotClickHandler getSlotClickHandler(int slot, Player clicker) { + public Icon getIcon(int slot) { if (slot < 0 || slot >= bukkitInventory.getSize()) { return null; } - Icon icon = menu.getIcons().getByIndex(slot); - if (icon == null) { - return null; - } - - return () -> icon.onClick(this, clicker); + return menu.getIcons().getByIndex(slot); } @Override @@ -72,12 +66,4 @@ public class DefaultMenuView implements MenuView { return viewer; } - - @FunctionalInterface - public interface SlotClickHandler { - - ClickResult onClick(); - - } - } diff --git a/plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java b/plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java index 631ab10..faee40a 100644 --- a/plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java +++ b/plugin/src/main/java/me/filoghost/chestcommands/listener/InventoryListener.java @@ -7,9 +7,9 @@ package me.filoghost.chestcommands.listener; import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.api.ClickResult; +import me.filoghost.chestcommands.api.Icon; import me.filoghost.chestcommands.config.Settings; import me.filoghost.chestcommands.inventory.DefaultMenuView; -import me.filoghost.chestcommands.inventory.DefaultMenuView.SlotClickHandler; import me.filoghost.chestcommands.menu.MenuManager; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -63,8 +63,8 @@ public class InventoryListener implements Listener { int slot = event.getRawSlot(); Player clicker = (Player) event.getWhoClicked(); - SlotClickHandler slotClickHandler = menuView.getSlotClickHandler(slot, clicker); - if (slotClickHandler == null) { + Icon icon = menuView.getIcon(slot); + if (icon == null) { return; } @@ -82,7 +82,7 @@ public class InventoryListener implements Listener { // Only handle the click AFTER the event has finished Bukkit.getScheduler().runTask(ChestCommands.getPluginInstance(), () -> { - ClickResult result = slotClickHandler.onClick(); + ClickResult result = icon.onClick(menuView, clicker); if (result == ClickResult.CLOSE) { clicker.closeInventory();