From d856d4d476a8a41cfef7f69b17d8326b7fe55706 Mon Sep 17 00:00:00 2001 From: jojo Date: Sat, 5 Sep 2020 14:30:31 +0200 Subject: [PATCH] Add SWActionListInv Add SWActionBar --- .../de/steamwar/inventory/SWActionBar.java | 31 ++++++++ .../steamwar/inventory/SWActionListInv.java | 73 +++++++++++++++++++ .../src/de/steamwar/inventory/SWItem.java | 36 ++++----- 3 files changed, 120 insertions(+), 20 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/inventory/SWActionBar.java create mode 100644 SpigotCore_Main/src/de/steamwar/inventory/SWActionListInv.java diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWActionBar.java b/SpigotCore_Main/src/de/steamwar/inventory/SWActionBar.java new file mode 100644 index 0000000..c824347 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWActionBar.java @@ -0,0 +1,31 @@ +package de.steamwar.inventory; + +public class SWActionBar { + + public enum Slot { + ZERO(0), + ONE(1), + TWO(2), + THREE(3), + FOUR(4), + FIVE(5), + SIX(6); + + private int index; + + Slot(int index) { + this.index = index; + } + + int getIndex() { + return index; + } + } + + SWItem[] actionBar = new SWItem[7]; + + public void setItem(Slot slot, SWItem item) { + actionBar[slot.getIndex()] = item; + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWActionListInv.java b/SpigotCore_Main/src/de/steamwar/inventory/SWActionListInv.java new file mode 100644 index 0000000..68ef71c --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWActionListInv.java @@ -0,0 +1,73 @@ +package de.steamwar.inventory; + +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; + +import java.util.List; + +public class SWActionListInv extends SWInventory { + + private SWListInv.ListCallback callback; + private List> elements; + private SWActionBar actionBar = null; + private int page; + + public SWActionListInv(Player p, String t, List> l, SWListInv.ListCallback c){ + super(p, 54, t); + callback = c; + elements = l; + page = 0; + } + + public void setActionBar(SWActionBar actionBar) { + this.actionBar = actionBar; + } + + @Override + public void open(){ + inventory.clear(); + setCallback(-999, (ClickType click) -> player.closeInventory()); + if (page != 0) { + setItem(0, SWItem.getDye(10), (byte) 10, "§eSeite zurück", (ClickType click) -> { + page--; + open(); + }); + } else { + setItem(0, SWItem.getDye(8), (byte) 8, "§7Seite zurück", (ClickType click) -> {}); + } + if (page < elements.size() / 45) { + setItem(8, SWItem.getDye(10), (byte) 10, "§eSeite vor", (ClickType click) -> { + page++; + open(); + }); + } else { + setItem(8, SWItem.getDye(8), (byte) 8, "§7Seite vor", (ClickType click) -> {}); + } + + if (actionBar != null) { + for (int i = 0; i < 7; i++) { + setItem(i + 1, actionBar.actionBar[i]); + } + } + + int ipageLimit = elements.size() - page*45; + if(ipageLimit > 45 && elements.size() > 45){ + ipageLimit = 45; + } + int i = page*45; + for(int ipage=0; ipage < ipageLimit; ipage++ ){ + SWItem e = elements.get(i).getItem(); + + final int pos = i; + setItem(ipage + 9, e); + setCallback(ipage, (ClickType click) -> callback.clicked(click, elements.get(pos).getObject())); + i++; + } + super.open(); + } + + public void setCallback(SWListInv.ListCallback c){ + callback = c; + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index afd845b..ce69796 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -85,7 +85,7 @@ public class SWItem { } } - public SWItem(){ + public SWItem() { itemStack = new ItemStack(Material.AIR); itemMeta = itemStack.getItemMeta(); hideAttributes(); @@ -95,34 +95,31 @@ public class SWItem { this(material, (byte)0, name, new ArrayList<>(), false, null); } - public SWItem(Material material, String name, List lore, boolean enchanted, InvCallback c){ + public SWItem(Material material, String name, List lore, boolean enchanted, InvCallback c) { this(material, (byte)0, name, lore, enchanted, c); } - public SWItem(Material material, byte meta, String name, List lore, boolean enchanted, InvCallback c){ - try{ + public SWItem(Material material, byte meta, String name, List lore, boolean enchanted, InvCallback c) { + try { itemStack = new ItemStack(material, 1, (short)0, meta); - }catch(IllegalArgumentException e){ + } catch (IllegalArgumentException e) { itemStack = new ItemStack(material, 1); } itemMeta = itemStack.getItemMeta(); - if(itemMeta != null){ + if (itemMeta != null) { hideAttributes(); itemMeta.setDisplayName(name); - if(lore != null && !lore.isEmpty()) - itemMeta.setLore(lore); - if(enchanted) - itemMeta.addEnchant(Enchantment.DURABILITY , 10, true); + if (lore != null && !lore.isEmpty()) itemMeta.setLore(lore); + if (enchanted) itemMeta.addEnchant(Enchantment.DURABILITY , 10, true); itemStack.setItemMeta(itemMeta); } callback = c; } - private void hideAttributes(){ - if(itemMeta == null) - return; + private void hideAttributes() { + if (itemMeta == null) return; itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES); itemMeta.addItemFlags(ItemFlag.HIDE_DESTROYS); itemMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE); @@ -159,23 +156,22 @@ public class SWItem { this.callback = callback; } - public void setName(String name){ + public void setName(String name) { itemMeta.setDisplayName(name); itemStack.setItemMeta(itemMeta); } - public void setLore(List lore){ + public void setLore(List lore) { itemMeta.setLore(lore); itemStack.setItemMeta(itemMeta); } - public void setEnchanted(boolean enchanted){ - if(enchanted){ + public void setEnchanted(boolean enchanted) { + if (enchanted){ itemMeta.addEnchant(Enchantment.DURABILITY , 10, true); - itemStack.setItemMeta(itemMeta); - }else{ + } else { itemMeta.removeEnchant(Enchantment.DURABILITY); - itemStack.setItemMeta(itemMeta); } + itemStack.setItemMeta(itemMeta); } }