From d9d9abefa4e781daa6fc4f54d770567d8485ec9b Mon Sep 17 00:00:00 2001 From: acog Date: Wed, 19 Feb 2025 08:50:56 +0900 Subject: [PATCH 1/3] feat: bottom inventory click event --- .../main/java/io/typst/bukkit/view/BukkitView.java | 14 ++++++++++++++ .../main/java/io/typst/bukkit/view/ChestView.java | 2 ++ 2 files changed, 16 insertions(+) diff --git a/core/src/main/java/io/typst/bukkit/view/BukkitView.java b/core/src/main/java/io/typst/bukkit/view/BukkitView.java index 8850f83..4d28c6c 100644 --- a/core/src/main/java/io/typst/bukkit/view/BukkitView.java +++ b/core/src/main/java/io/typst/bukkit/view/BukkitView.java @@ -106,6 +106,20 @@ public void onClick(InventoryClickEvent e) { // update user input items Player p = (Player) e.getWhoClicked(); ViewControl viewControl = view.getContents().getControls().get(e.getRawSlot()); + + // bottom inventory click event + if (e.getView().getBottomInventory() == e.getClickedInventory()) { + ViewAction action; + try { + action = view.getOnBottomClick().apply(new ClickEvent(view, p, e.getClick(), e.getAction(), e.getHotbarButton())); + } catch (Exception ex) { + plugin.getLogger().log(Level.WARNING, ex, () -> "Error on bottom inventory click!"); + // To block after actions + action = ViewAction.CLOSE; + } + handleAction(p, holder, action); + } + // Cancel if tried to move the control items switch (e.getClick()) { case LEFT: diff --git a/core/src/main/java/io/typst/bukkit/view/ChestView.java b/core/src/main/java/io/typst/bukkit/view/ChestView.java index 756e729..a4f2996 100644 --- a/core/src/main/java/io/typst/bukkit/view/ChestView.java +++ b/core/src/main/java/io/typst/bukkit/view/ChestView.java @@ -25,6 +25,8 @@ public class ChestView { @Builder.Default Function onClose = e -> ViewAction.NOTHING; @Builder.Default + Function onBottomClick = e -> ViewAction.NOTHING; + @Builder.Default Consumer onContentsUpdate = e -> { }; @Builder.Default From 6c2834eaec3fa1415e42666b5779ebcba92a733a Mon Sep 17 00:00:00 2001 From: acog Date: Wed, 19 Feb 2025 08:51:24 +0900 Subject: [PATCH 2/3] feat: bukkitItem flags --- .../java/io/typst/bukkit/view/item/BukkitItem.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/io/typst/bukkit/view/item/BukkitItem.java b/core/src/main/java/io/typst/bukkit/view/item/BukkitItem.java index b8860a0..1f26e44 100644 --- a/core/src/main/java/io/typst/bukkit/view/item/BukkitItem.java +++ b/core/src/main/java/io/typst/bukkit/view/item/BukkitItem.java @@ -4,6 +4,7 @@ import lombok.With; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -19,9 +20,10 @@ public class BukkitItem { private final List lore; private final Integer modelData; private final Map enchants; + private final Set flags; public static BukkitItem ofJust(Material material) { - return of(material, 1, (short) 0, "", Collections.emptyList(), 0, Collections.emptyMap()); + return of(material, 1, (short) 0, "", Collections.emptyList(), 0, Collections.emptyMap(), Collections.emptySet()); } public static BukkitItem from(ItemStack item) { @@ -33,7 +35,8 @@ public static BukkitItem from(ItemStack item) { meta != null && meta.hasDisplayName() ? meta.getDisplayName() : "", meta != null && meta.hasLore() ? meta.getLore() : Collections.emptyList(), meta != null && meta.hasCustomModelData() ? meta.getCustomModelData() : 0, - meta != null && meta.hasEnchants() ? meta.getEnchants() : Collections.emptyMap() + meta != null && meta.hasEnchants() ? meta.getEnchants() : Collections.emptyMap(), + meta != null && meta.getItemFlags().isEmpty() ? meta.getItemFlags() : Collections.emptySet() ); } @@ -64,6 +67,9 @@ public void update(ItemStack x) { if (getModelData() != 0) { meta.setCustomModelData(getModelData()); } + if (!getFlags().isEmpty()) { + meta.addItemFlags(getFlags().toArray(new ItemFlag[0])); + } x.setItemMeta(meta); } for (Map.Entry pair : getEnchants().entrySet()) { From 1515c1bf34f9e4226e79d6d3a6c32c885e305dff Mon Sep 17 00:00:00 2001 From: acog Date: Wed, 19 Feb 2025 10:48:58 +0900 Subject: [PATCH 3/3] feat: replace ClickEvent with BottomClickEvent --- .../io/typst/bukkit/view/BottomClickEvent.java | 18 ++++++++++++++++++ .../java/io/typst/bukkit/view/BukkitView.java | 2 +- .../java/io/typst/bukkit/view/ChestView.java | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 core/src/main/java/io/typst/bukkit/view/BottomClickEvent.java diff --git a/core/src/main/java/io/typst/bukkit/view/BottomClickEvent.java b/core/src/main/java/io/typst/bukkit/view/BottomClickEvent.java new file mode 100644 index 0000000..77d78eb --- /dev/null +++ b/core/src/main/java/io/typst/bukkit/view/BottomClickEvent.java @@ -0,0 +1,18 @@ +package io.typst.bukkit.view; + +import lombok.Data; +import lombok.With; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryAction; +import org.bukkit.inventory.ItemStack; + +@Data +@With +public class BottomClickEvent { + private final ChestView view; + private final Player player; + private final ClickType click; + private final ItemStack clickItem; + private final InventoryAction action; +} diff --git a/core/src/main/java/io/typst/bukkit/view/BukkitView.java b/core/src/main/java/io/typst/bukkit/view/BukkitView.java index 4d28c6c..fd67591 100644 --- a/core/src/main/java/io/typst/bukkit/view/BukkitView.java +++ b/core/src/main/java/io/typst/bukkit/view/BukkitView.java @@ -111,7 +111,7 @@ public void onClick(InventoryClickEvent e) { if (e.getView().getBottomInventory() == e.getClickedInventory()) { ViewAction action; try { - action = view.getOnBottomClick().apply(new ClickEvent(view, p, e.getClick(), e.getAction(), e.getHotbarButton())); + action = view.getOnBottomClick().apply(new BottomClickEvent(view, p, e.getClick(), e.getCurrentItem(), e.getAction())); } catch (Exception ex) { plugin.getLogger().log(Level.WARNING, ex, () -> "Error on bottom inventory click!"); // To block after actions diff --git a/core/src/main/java/io/typst/bukkit/view/ChestView.java b/core/src/main/java/io/typst/bukkit/view/ChestView.java index a4f2996..5589f6d 100644 --- a/core/src/main/java/io/typst/bukkit/view/ChestView.java +++ b/core/src/main/java/io/typst/bukkit/view/ChestView.java @@ -25,7 +25,7 @@ public class ChestView { @Builder.Default Function onClose = e -> ViewAction.NOTHING; @Builder.Default - Function onBottomClick = e -> ViewAction.NOTHING; + Function onBottomClick = e -> ViewAction.NOTHING; @Builder.Default Consumer onContentsUpdate = e -> { };