From d7b519a4cbdcd4e7c55a7f6c8bdaf717e7359dca Mon Sep 17 00:00:00 2001 From: BuildTools Date: Fri, 16 Sep 2022 17:46:07 +0900 Subject: [PATCH] =?UTF-8?q?=EC=95=84=EC=9D=B4=ED=85=9C,=20=EB=A9=94?= =?UTF-8?q?=EB=89=B4=20=EA=B4=80=EB=A0=A8=20=EB=9D=BC=EC=9D=B4=EB=B8=8C?= =?UTF-8?q?=EB=9F=AC=EB=A6=AC=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=96=88?= =?UTF-8?q?=EC=8A=B5=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../itemutilapi/ItemCreator.java | 182 ++++++++++++++++++ .../junseclibrary/itemutilapi/Menu.java | 181 +++++++++++++++++ 2 files changed, 363 insertions(+) create mode 100644 src/main/java/me/else_junsuk/junseclibrary/itemutilapi/ItemCreator.java create mode 100644 src/main/java/me/else_junsuk/junseclibrary/itemutilapi/Menu.java diff --git a/src/main/java/me/else_junsuk/junseclibrary/itemutilapi/ItemCreator.java b/src/main/java/me/else_junsuk/junseclibrary/itemutilapi/ItemCreator.java new file mode 100644 index 0000000..5b16f62 --- /dev/null +++ b/src/main/java/me/else_junsuk/junseclibrary/itemutilapi/ItemCreator.java @@ -0,0 +1,182 @@ +package me.else_junsuk.junseclibrary.itemutilapi; + +import me.else_junsuk.junseclibrary.stringsapi.Format; +import net.kyori.adventure.text.Component; +import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.List; + +/** + * 아이템을 생성할 때 도움을 줄 아이템 제작 클래스입니다. + * 해당 클래스에는 ItemStack 을 리턴하는 메소드를 + * 제공합니다. 타입 아이템을 만들거나 그 아이템에 + * 이름, 설명 등을 부여할 수 있습니다. + * @since 11-09-22 lib-2.2.1 + * @author Else_JunSuk + */ +public class ItemCreator { + + /** + * 해당 타입으로 아이템을 생성합니다. + * @param material 아이템 타입 + * @return {@link ItemStack} + */ + public static ItemStack create(Material material) { + return new ItemStack(material); + } + + /** + * 해당 타입의 아이템에 이름을 부여합니다. + * @param material + * @param name 아이템 이름 + * @return {@link ItemStack} + */ + public static ItemStack create(Material material, Component name) { + ItemStack item = create(material); + ItemMeta meta = item.getItemMeta(); + if (meta == null) return item; + meta.displayName(name); + item.setItemMeta(meta); + return item; + } + + /** + * 아이템 타입과 이름, 설명을 부여합니다. + * @param material + * @param name + * @param lore 아이템 설명 + * @return {@link ItemStack} + */ + public static ItemStack create(Material material, Component name, List lore) { + ItemStack item = create(material, name); + ItemMeta meta = item.getItemMeta(); + if (meta == null) return item; + meta.lore(lore); + item.setItemMeta(meta); + return item; + } + + /** + * 아이템 타입과 이름, 설명을 부여하고 + * 부수적으로 플래그(Flags)를 숨길 것인지 + * 설정합니다. + * @param material + * @param name + * @param lore + * @param hideFlags 플래그 하이딩 + * @return {@link ItemStack} + */ + public static ItemStack create(Material material, Component name, List lore, boolean hideFlags) { + ItemStack item = create(material, name, lore); + ItemMeta meta = item.getItemMeta(); + if (meta == null) return item; + if (hideFlags) meta.addItemFlags(ItemFlag.values()); + else meta.removeItemFlags(ItemFlag.values()); + item.setItemMeta(meta); + return item; + } + + /** + * 아이템 타입과 이름, 설명을 부여하고 + * 플래그를 숨길 것인지 설정한 뒤 최종적으로 + * 아이템이 인첸트 효과를 보일 것인지 설정합니다. + * @param material + * @param name + * @param lore + * @param hideFlags + * @param isGlowing 아이템 인첸팅 효과 + * @return {@link ItemStack} + */ + public static ItemStack create(Material material, Component name, List lore, boolean hideFlags, boolean isGlowing) { + ItemStack item = create(material, name, lore, hideFlags); + ItemMeta meta = item.getItemMeta(); + if (meta == null) return item; + if (isGlowing) { + item.addUnsafeEnchantment(Enchantment.DURABILITY, 1); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + } + return item; + } + + /** + * 아이템 타입과 이름, 설명을 부여하고 + * 플래그와 인첸트 효과를 보일 것인지 설정하고 + * 인첸트를 부여할 수 있습니다. + * @param material + * @param name + * @param lore + * @param hideFlags + * @param isGlowing + * @param enchantment 인첸트 + * @return {@link ItemStack} + */ + public static ItemStack create(Material material, Component name, List lore, boolean hideFlags, boolean isGlowing, Enchantment enchantment) { + ItemStack item = create(material, name, lore, hideFlags, isGlowing); + ItemMeta meta = item.getItemMeta(); + if (meta == null) return item; + item.addUnsafeEnchantment(enchantment, 1); + return item; + } + + /** + * 아이템 타입과 이름, 설명을 부여하고 + * 플래그와 인첸트 효과를 보일 것인지 설정하고 + * 인첸트와 그 레벨을 부여할 수 있습니다. + * @param material + * @param name + * @param lore + * @param hideFlags + * @param isGlowing + * @param enchantment + * @param level 인첸트 레벨 + * @return {@link ItemStack} + */ + public static ItemStack create(Material material, Component name, List lore, boolean hideFlags, boolean isGlowing, Enchantment enchantment, int level) { + ItemStack item = create(material, name, lore, hideFlags, isGlowing); + ItemMeta meta = item.getItemMeta(); + if (meta == null) return item; + item.addUnsafeEnchantment(enchantment, level); + return item; + } + + /** + * 갑옷 메타데이터를 ItemStack[] (배열)형태로 컨버팅합니다. + * ex) public static final ItemStack[] ARMOR = equipArmor(ItemStack...); + * 사용할 땐 플레이어(Player) 클래스의 'equip' 메소드를 사용하세요. + * @param helmet 헬멧 + * @param chestplate 상의 + * @param leggings 하의 + * @param boots 신발 + * @return {@link ItemStack} + */ + public static ItemStack[] equipArmor(ItemStack helmet, ItemStack chestplate, ItemStack leggings, ItemStack boots) { + ItemStack[] armor = new ItemStack[4]; + armor[3] = helmet; + armor[2] = chestplate; + armor[1] = leggings; + armor[0] = boots; + return armor; + } + + /** + * WRITTEN_BOOK 아이템에 내용을 삽입 할 수 있습니다. + * @param title 책의 제목 + * @param author 책의 저자 + * @param content 책의 내용 + * @return {@link ItemStack} + */ + public static ItemStack createBook(String title, String author, List content) { + ItemStack book = new ItemStack(Material.WRITTEN_BOOK); + BookMeta bookMeta = (BookMeta) book.getItemMeta(); + bookMeta.setAuthor(author); + bookMeta.setTitle(Format.format(title)); + bookMeta.pages(content); + book.setItemMeta(bookMeta); + return book; + } +} diff --git a/src/main/java/me/else_junsuk/junseclibrary/itemutilapi/Menu.java b/src/main/java/me/else_junsuk/junseclibrary/itemutilapi/Menu.java new file mode 100644 index 0000000..414efb9 --- /dev/null +++ b/src/main/java/me/else_junsuk/junseclibrary/itemutilapi/Menu.java @@ -0,0 +1,181 @@ +package me.else_junsuk.junseclibrary.itemutilapi; + +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryDragEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import java.util.*; + +/** + * 메뉴 API + */ +public class Menu { + + private static final Map openMenus = new HashMap<>(); + private static final Map> viewers = new HashMap<>(); + + private final Map menuClickActions = new HashMap<>(); + + private MenuClick generalClickAction; + private MenuClick generalInvClickAction; + private MenuDrag generalDragAction; + + private MenuOpen openAction; + private MenuClose closeAction; + + public final UUID uuid; + private final Inventory inventory; + private final String viewerID; + + public Menu(int size, Component name) { + uuid = UUID.randomUUID(); + inventory = Bukkit.createInventory(null, size, name); + viewerID = null; + } + + public Menu(int size, Component name, String viewerID) { + uuid = UUID.randomUUID(); + inventory = Bukkit.createInventory(null, size, name); + this.viewerID = viewerID; + } + + public static Menu getMenu(Player player) { + return openMenus.getOrDefault(player.getUniqueId(), null); + } + + public void open(Player player) { + player.openInventory(inventory); + openMenus.put(player.getUniqueId(), this); + if (viewerID != null) addViewer(player); + if (openAction != null) openAction.open(player); + } + + public void remove() { + openMenus.entrySet().removeIf(entry -> { + if (entry.getValue().getUuid().equals(uuid)) { + Player player = Bukkit.getPlayer(entry.getKey()); + if (player != null) { + if (viewerID != null) removeViewer(player); + if (closeAction != null) closeAction.close(player); + } + return true; + } + return false; + }); + } + + public UUID getUuid() { + return uuid; + } + + public void close(Player player) { + player.closeInventory(); + openMenus.entrySet().removeIf(entry -> { + if (entry.getKey().equals(player.getUniqueId())) { + if (viewerID != null) removeViewer(player); + if (closeAction != null) closeAction.close(player); + return true; + } + return false; + }); + } + + private void addViewer(Player player) { + if (viewerID == null) return; + Set list = viewers.getOrDefault(viewerID, new HashSet<>()); + list.add(player.getUniqueId()); + viewers.put(viewerID, list); + } + + private void removeViewer(Player player) { + if (viewerID == null) return; + Set list = viewers.getOrDefault(viewerID, null); + if (list == null) return; + list.remove(player.getUniqueId()); + if (list.isEmpty()) viewers.remove(viewerID); + else viewers.put(viewerID, list); + } + + public Set getViewers() { + if (viewerID == null) return new HashSet<>(); + Set viewerList = new HashSet<>(); + for (UUID uuid : viewers.getOrDefault(viewerID, new HashSet<>())) { + Player player = Bukkit.getPlayer(uuid); + if (player == null) continue; + viewerList.add(player); + } + return viewerList; + } + + public MenuClick getAction(int index) { + return menuClickActions.getOrDefault(index, null); + } + + public MenuClick getGeneralClickAction() { + return generalClickAction; + } + + protected void setGeneralClickAction(MenuClick generalClickAction) { + this.generalClickAction = generalClickAction; + } + + public MenuClick getGeneralInvClickAction() { + return generalInvClickAction; + } + + protected void setGeneralInvClickAction(MenuClick generalInvClickAction) { + this.generalInvClickAction = generalInvClickAction; + } + + public MenuDrag getGeneralDragAction() { + return generalDragAction; + } + + protected void setGeneralDragAction(MenuDrag generalDragAction) { + this.generalDragAction = generalDragAction; + } + + protected void setOpenAction(MenuOpen openAction) { + this.openAction = openAction; + } + + protected void setCloseAction(MenuClose closeAction) { + this.closeAction = closeAction; + } + + public interface MenuClick { + void click(Player player, InventoryClickEvent event); + } + + public interface MenuDrag { + void drag(Player player, InventoryDragEvent event); + } + + public interface MenuOpen { + void open(Player player); + } + + public interface MenuClose { + void close(Player player); + } + + public void setItem(int index, ItemStack item) { + inventory.setItem(index, item); + } + + public void setItem(int index, ItemStack item, MenuClick action) { + inventory.setItem(index, item); + if (action == null) menuClickActions.remove(index); + else menuClickActions.put(index, action); + } + + public Inventory getInventory() { + return inventory; + } + + +}