From 47d481bbfc46a1ab290701144356fe1f69212190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Tue, 21 May 2019 23:42:13 +0800 Subject: [PATCH] + update MenuBuilder v2 --- build.gradle | 2 +- .../commands/internal/BaseMainCommand.java | 1 - .../inventory/builder/MenuBuilder.java | 1 + .../inventory/builder/v2/ClickEvent.java | 38 ++++++ .../inventory/builder/v2/ClickListener.java | 66 +++++++++ .../builder/v2/ClickListener1_9.java | 22 +++ .../inventory/builder/v2/ClickTask.java | 11 ++ .../inventory/builder/v2/ClickType.java | 11 ++ .../inventory/builder/v2/CloseTask.java | 13 ++ .../inventory/builder/v2/MenuBuilder.java | 127 ++++++++++++++++++ .../inventory/builder/v2/MenuHolder.java | 26 ++++ 11 files changed, 316 insertions(+), 2 deletions(-) create mode 100644 src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickEvent.java create mode 100644 src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickListener.java create mode 100644 src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickListener1_9.java create mode 100644 src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickTask.java create mode 100644 src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickType.java create mode 100644 src/main/scala/me/skymc/taboolib/inventory/builder/v2/CloseTask.java create mode 100644 src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuBuilder.java create mode 100644 src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuHolder.java diff --git a/build.gradle b/build.gradle index cd86c99..8c37f2f 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'com.github.johnrengelman.shadow' version '4.0.4' } group = 'me.skymc' -version = '4.79' +version = '4.8' sourceCompatibility = 1.8 targetCompatibility = 1.8 diff --git a/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java b/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java index 6210fa7..72d2031 100644 --- a/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java +++ b/src/main/scala/me/skymc/taboolib/commands/internal/BaseMainCommand.java @@ -96,7 +96,6 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor { public void registerSubCommand(BaseSubCommand subCommand) { if (subCommand != null) { Preconditions.checkArgument(subCommand.getLabel() != null, "Command label can not be null"); - Preconditions.checkArgument(subCommand.getDescription() != null, "Command description can not be null"); Preconditions.checkArgument(subCommand.getArguments() != null, "Command arguments can not be null"); } subCommands.add(subCommand); diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/MenuBuilder.java b/src/main/scala/me/skymc/taboolib/inventory/builder/MenuBuilder.java index 1b8bd9c..f064bc9 100644 --- a/src/main/scala/me/skymc/taboolib/inventory/builder/MenuBuilder.java +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/MenuBuilder.java @@ -15,6 +15,7 @@ import java.util.HashMap; * @Since 2018-08-22 13:40 * @BuilderVersion 1.0 */ +@Deprecated public class MenuBuilder { private boolean lock; diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickEvent.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickEvent.java new file mode 100644 index 0000000..8471b58 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickEvent.java @@ -0,0 +1,38 @@ +package me.skymc.taboolib.inventory.builder.v2; + +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.inventory.InventoryInteractEvent; + +/** + * @Author 坏黑 + * @Since 2019-05-21 18:09 + */ +public class ClickEvent { + + private ClickType clickType; + private Event event; + + public ClickEvent(ClickType clickType, Event event) { + this.clickType = clickType; + this.event = event; + } + + public InventoryClickEvent castClick() { + return (InventoryClickEvent) event; + } + + public InventoryDragEvent castDrag() { + return (InventoryDragEvent) event; + } + + public ClickType getClickType() { + return clickType; + } + + public Player getClicker() { + return (Player) ((InventoryInteractEvent) event).getWhoClicked(); + } +} diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickListener.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickListener.java new file mode 100644 index 0000000..67e20f2 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickListener.java @@ -0,0 +1,66 @@ +package me.skymc.taboolib.inventory.builder.v2; + +import me.skymc.taboolib.listener.TListener; +import org.bukkit.Bukkit; +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerItemHeldEvent; +import org.bukkit.event.server.PluginDisableEvent; + +import java.util.Optional; + +/** + * @Author 坏黑 + * @Since 2019-05-21 18:16 + */ +@TListener +class ClickListener implements Listener { + + @EventHandler + public void onDisable(PluginDisableEvent e) { + Bukkit.getOnlinePlayers().stream().filter(player -> player.getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && e.getPlugin().equals(((MenuHolder) player.getOpenInventory().getTopInventory().getHolder()).getBuilder().getPlugin())).forEach(HumanEntity::closeInventory); + } + + @EventHandler + public void onClick(InventoryClickEvent e) { + if (e.getInventory().getHolder() instanceof MenuHolder) { + if (((MenuHolder) e.getInventory().getHolder()).getBuilder().isLockHand() && (e.getRawSlot() - e.getInventory().getSize() - 27 == e.getWhoClicked().getInventory().getHeldItemSlot() || (e.getClick() == org.bukkit.event.inventory.ClickType.NUMBER_KEY && e.getHotbarButton() == e.getWhoClicked().getInventory().getHeldItemSlot()))) { + e.setCancelled(true); + } + Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.CLICK, e))); + } + } + + @EventHandler + public void onDrag(InventoryDragEvent e) { + if (e.getInventory().getHolder() instanceof MenuHolder) { + Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getClickTask()).ifPresent(t -> t.run(new ClickEvent(ClickType.DRAG, e))); + } + } + + @EventHandler + public void onDrag(InventoryCloseEvent e) { + if (e.getInventory().getHolder() instanceof MenuHolder) { + Optional.ofNullable(((MenuHolder) e.getInventory().getHolder()).getBuilder().getCloseTask()).ifPresent(t -> t.run(e)); + } + } + + @EventHandler + public void onDrop(PlayerDropItemEvent e) { + if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) { + e.setCancelled(true); + } + } + + @EventHandler + public void onHeld(PlayerItemHeldEvent e) { + if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) { + e.setCancelled(true); + } + } +} diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickListener1_9.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickListener1_9.java new file mode 100644 index 0000000..f53644a --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickListener1_9.java @@ -0,0 +1,22 @@ +package me.skymc.taboolib.inventory.builder.v2; + +import me.skymc.taboolib.listener.TListener; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerSwapHandItemsEvent; + +/** + * @Author 坏黑 + * @Since 2019-05-21 22:04 + */ +@TListener +class ClickListener1_9 implements Listener { + + @EventHandler + public void onSwap(PlayerSwapHandItemsEvent e) { + if (e.getPlayer().getOpenInventory().getTopInventory().getHolder() instanceof MenuHolder && ((MenuHolder) e.getPlayer().getOpenInventory().getTopInventory().getHolder()).getBuilder().isLockHand()) { + e.setCancelled(true); + } + } + +} diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickTask.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickTask.java new file mode 100644 index 0000000..fec7922 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickTask.java @@ -0,0 +1,11 @@ +package me.skymc.taboolib.inventory.builder.v2; + +/** + * @Author 坏黑 + * @Since 2019-05-21 18:14 + */ +public interface ClickTask { + + void run(ClickEvent event); + +} diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickType.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickType.java new file mode 100644 index 0000000..5af5852 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/ClickType.java @@ -0,0 +1,11 @@ +package me.skymc.taboolib.inventory.builder.v2; + +/** + * @Author 坏黑 + * @Since 2019-05-21 18:10 + */ +public enum ClickType { + + CLICK, DRAG + +} diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/CloseTask.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/CloseTask.java new file mode 100644 index 0000000..3e672a8 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/CloseTask.java @@ -0,0 +1,13 @@ +package me.skymc.taboolib.inventory.builder.v2; + +import org.bukkit.event.inventory.InventoryCloseEvent; + +/** + * @Author 坏黑 + * @Since 2019-05-21 18:14 + */ +public interface CloseTask { + + void run(InventoryCloseEvent event); + +} diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuBuilder.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuBuilder.java new file mode 100644 index 0000000..930284a --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuBuilder.java @@ -0,0 +1,127 @@ +package me.skymc.taboolib.inventory.builder.v2; + +import com.google.common.collect.Maps; +import com.ilummc.tlib.util.Ref; +import me.skymc.taboolib.Main; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; + +import java.util.Map; + +/** + * @Author 坏黑 + * @Since 2019-05-21 15:54 + */ +public class MenuBuilder { + + private Map keys = Maps.newHashMap(); + private Plugin plugin; + private String title; + private int rows; + private char[][] items; + private ClickTask clickTask; + private CloseTask closeTask; + private boolean lockHand; + + public MenuBuilder(Plugin plugin) { + this.plugin = plugin; + } + + public static MenuBuilder builder(Plugin plugin) { + return new MenuBuilder(plugin); + } + + public static MenuBuilder builder() { + return new MenuBuilder(Ref.getCallerPlugin(Ref.getCallerClass(3).orElse(Main.class))); + } + + public MenuBuilder lockHand() { + this.lockHand = true; + return this; + } + + public MenuBuilder event(ClickTask clickTask) { + this.clickTask = clickTask; + return this; + } + + public MenuBuilder close(CloseTask closeTask) { + this.closeTask = closeTask; + return this; + } + + public MenuBuilder title(String title) { + this.title = title; + return this; + } + + public MenuBuilder rows(int rows) { + this.rows = rows * 9; + return this; + } + + public MenuBuilder put(char key, ItemStack item) { + keys.put(key, item); + return this; + } + + public MenuBuilder items(String... placeholder) { + items = new char[placeholder.length][]; + for (int i = 0; i < placeholder.length; i++) { + items[i] = placeholder[i].toCharArray(); + } + return this; + } + + public Inventory build() { + Inventory inventory = Bukkit.createInventory(new MenuHolder(this), rows, String.valueOf(title)); + for (int i = 0; i < items.length && i < rows; i++) { + char[] line = items[i]; + for (int j = 0; j < line.length && j < 9; j++) { + inventory.setItem(i * 9 + j, keys.getOrDefault(line[j], new ItemStack(Material.AIR))); + } + } + return inventory; + } + + // ********************************* + // + // Getter and Setter + // + // ********************************* + + public Map getKeys() { + return keys; + } + + public Plugin getPlugin() { + return plugin; + } + + public String getTitle() { + return title; + } + + public int getRows() { + return rows; + } + + public char[][] getItems() { + return items; + } + + public ClickTask getClickTask() { + return clickTask; + } + + public CloseTask getCloseTask() { + return closeTask; + } + + public boolean isLockHand() { + return lockHand; + } +} diff --git a/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuHolder.java b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuHolder.java new file mode 100644 index 0000000..09912d3 --- /dev/null +++ b/src/main/scala/me/skymc/taboolib/inventory/builder/v2/MenuHolder.java @@ -0,0 +1,26 @@ +package me.skymc.taboolib.inventory.builder.v2; + +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryHolder; + +/** + * @Author 坏黑 + * @Since 2019-05-21 20:28 + */ +class MenuHolder implements InventoryHolder { + + private MenuBuilder builder; + + public MenuHolder(MenuBuilder builder) { + this.builder = builder; + } + + public MenuBuilder getBuilder() { + return builder; + } + + @Override + public Inventory getInventory() { + return null; + } +}