broken-arrow

Untitled

Dec 6th, 2021
956
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.brokenarrow.storage.menus;
  2.  
  3. import org.brokenarrow.storage.api.ContainerRegistryAPI;
  4. import org.brokenarrow.storage.util.GuiTempletsYaml;
  5. import org.brokenarrow.storage.util.ItemUtily;
  6. import org.brokenarrow.storage.util.MetadataEnumsForGui;
  7. import org.bukkit.Bukkit;
  8. import org.bukkit.GameMode;
  9. import org.bukkit.Location;
  10. import org.bukkit.Material;
  11. import org.bukkit.entity.HumanEntity;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.inventory.InventoryClickEvent;
  14. import org.bukkit.event.inventory.InventoryCloseEvent;
  15. import org.bukkit.event.inventory.InventoryType;
  16. import org.bukkit.inventory.Inventory;
  17. import org.bukkit.inventory.ItemStack;
  18. import org.bukkit.metadata.FixedMetadataValue;
  19. import org.mineacademy.fo.PlayerUtil;
  20. import org.mineacademy.fo.collection.StrictMap;
  21. import org.mineacademy.fo.remain.CompMaterial;
  22. import org.mineacademy.fo.remain.CompMetadata;
  23.  
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.UUID;
  28.  
  29. public class Craftingfiltergui extends MenuHolder {
  30.     //private final static Craftingfiltergui instance = new Craftingfiltergui();
  31.     private final ContainerRegistryAPI registry = ContainerRegistryAPI.getInstance();
  32.     private final StrictMap<UUID, Inventory> craftingFilterGui = new StrictMap<>();
  33.     private final MetadataEnumsForGui metadataGui = MetadataEnumsForGui.INVENTORY_CRAFTINGGUI;
  34.     private Inventory inventory;
  35.     private int viwers;
  36.  
  37.  
  38.     public Craftingfiltergui(Location location, Player player) {
  39.         super(location, player);
  40.     }
  41.  
  42.  
  43.     @Override
  44.     public void setItemsInSlot(int slot) {
  45.  
  46.         if (GuiTempletsYaml.getSlot("Crafting_Filtergui", "Placeholders").contains(slot))
  47.             this.inventory.setItem(slot, ItemUtily.createItemStack(GuiTempletsYaml.getIcon("Crafting_Filtergui", "Placeholders", null),
  48.                     GuiTempletsYaml.getDisplayName("Crafting_Filtergui", "Placeholders"), GuiTempletsYaml.getLore("Crafting_Filtergui", "Placeholders"),
  49.                     "Guimatrial", "GuiPlaceholders"));
  50.         if (GuiTempletsYaml.getSlot("Crafting_Filtergui", "Info_Button").contains(slot))
  51.             this.inventory.setItem(slot, ItemUtily.createItemStack(GuiTempletsYaml.getIcon("Crafting_Filtergui", "Info_Button", null),
  52.                     GuiTempletsYaml.getDisplayName("Crafting_Filtergui", "Info_Button"), GuiTempletsYaml.getLore("Crafting_Filtergui", "Info_Button"),
  53.                     "Guimatrial", "GuiPlaceholders"));
  54.         if (GuiTempletsYaml.getSlot("Crafting_Filtergui", "Back_button").contains(slot))
  55.             this.inventory.setItem(slot, ItemUtily.createItemStack(GuiTempletsYaml.getIcon("Crafting_Filtergui", "Back_button", null),
  56.                     GuiTempletsYaml.getDisplayName("Crafting_Filtergui", "Back_button"), GuiTempletsYaml.getLore("Crafting_Filtergui", "Back_button"),
  57.                     "Guimatrial", "GuiPlaceholders"));
  58.     }
  59.  
  60.  
  61.     @Override
  62.     public void onMenuOpen(Player player, Location location) {
  63.  
  64.         viwers++;
  65.         setItems();
  66.         player.openInventory(getMenuForPlayers());
  67.         PlayerUtil.updateInventoryTitle(player, GuiTempletsYaml.getGuiTitle("Crafting_Filtergui"));
  68.     }
  69.  
  70.     @Override
  71.     public void setItems() {
  72.         ItemStack[] items = registry.getFiltreritems(location);
  73.  
  74.         for (int i = 0; i < this.inventory.getSize(); i++) {
  75.             setItemsInSlot(i);
  76.         }
  77.         if (items != null)
  78.             for (int i = 0; i < items.length; i++) {
  79.                 ItemStack itemstacks = items[i];
  80.                 if (itemstacks != null) {
  81.                     ItemStack setItem = ItemUtily.createItemStackAsOne(itemstacks);
  82.                     this.inventory.setItem(i, setItem);
  83.                 }
  84.             }
  85.     }
  86.  
  87.     @Override
  88.     public void whenBlockBreak() {
  89.         this.inventory = null;
  90.     }
  91.  
  92.     @Override
  93.     public void onMenuClose(final InventoryCloseEvent event) {
  94.         ContainerRegistryAPI registry = ContainerRegistryAPI.getInstance();
  95.         Map<Integer, ItemStack> map = getItemsExceptBottomBar(this.inventory, (Player) event.getPlayer());
  96.  
  97.         registry.setFiltreritems(this.location, map.values().toArray(new ItemStack[]{}));
  98.         removeMenuDataForPlayer((Player) event.getPlayer());
  99.  
  100.         Arrays.stream(this.inventory.getContents()).forEach(itemStack -> {
  101.             if (itemStack != null) this.inventory.remove(itemStack);
  102.         });
  103.         setItems();
  104.         if (this.inventory.equals(event.getInventory()))
  105.             this.viwers--;
  106.         if (viwers <= 0) {
  107.             this.inventory.clear();
  108.             this.viwers = 0;
  109.         }
  110.     }
  111.  
  112.     @Override
  113.     public void clickedMenu(InventoryClickEvent event, ItemStack clicked) {
  114.         if (clicked != null && event.getSlotType() != InventoryType.SlotType.OUTSIDE) {
  115.             Material clickedItem = clicked.getType();
  116.  
  117.             if (CompMetadata.getMetadata(clicked, "Guimatrial") != null) {
  118.  
  119.  
  120.                 if (clickedItem == CompMaterial.fromItem(GuiTempletsYaml.getIcon("Crafting_Filtergui", "Placeholders", null)).getMaterial() && CompMetadata.getMetadata(clicked, "Guimatrial").equals("GuiPlaceholders")) {
  121.                     event.setCancelled(true);
  122.                 }
  123.                 if (clickedItem == CompMaterial.fromItem(GuiTempletsYaml.getIcon("Crafting_Filtergui", "Back_button", null)).getMaterial() && CompMetadata.getMetadata(clicked, "Guimatrial").equals("GuiPlaceholders")) {
  124.                     event.setCancelled(true);
  125.                     event.getWhoClicked().closeInventory();
  126.                 }
  127.                 if (clickedItem == CompMaterial.fromItem(GuiTempletsYaml.getIcon("Crafting_Filtergui", "Info_Button", null)).getMaterial() && CompMetadata.getMetadata(clicked, "Guimatrial").equals("GuiPlaceholders"))
  128.                     event.setCancelled(true);
  129.  
  130.             }
  131.         }
  132.     }
  133.  
  134.     @Override
  135.     protected void setMenuInCache(Player player) {
  136.  
  137.         if (inventory == null)
  138.             inventory = Bukkit.createInventory(null, GuiTempletsYaml.getGuiSize("Crafting_Filtergui"));
  139.         player.setMetadata(metadataGui.toString(), new FixedMetadataValue(metadataGui.getPlugin(), metadataGui.getMetadataKey()));
  140.  
  141.     }
  142.  
  143.     @Override
  144.     public Inventory getMenuForPlayers() {
  145.         return inventory;
  146.     }
  147.  
  148.     public boolean PlayerViwePageEmpty(Player player) {
  149.         return craftingFilterGui.contains(player.getUniqueId());
  150.     }
  151.  
  152.     @Override
  153.     protected void removeMenuDataForPlayer(Player player) {
  154.         MetadataEnumsForGui metadataGui = MetadataEnumsForGui.INVENTORY_CRAFTINGGUI;
  155.         craftingFilterGui.removeWeak(player.getUniqueId());
  156.         player.removeMetadata(metadataGui.toString(), metadataGui.getPlugin());
  157.     }
  158.  
  159.     public int getViewersAmount(List<HumanEntity> viewersList) {
  160.         int viewers = 0;
  161.  
  162.         for (HumanEntity viewer : viewersList) {
  163.             if (viewer.getGameMode() != GameMode.SPECTATOR)
  164.                 viewers++;
  165.         }
  166.         return viewers;
  167.     }
  168. }
  169.  
RAW Paste Data