broken-arrow

Untitled

May 27th, 2021 (edited)
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.86 KB | None | 0 0
  1.  
  2. import lombok.Getter;
  3. import org.bukkit.Location;
  4. import org.bukkit.entity.Player;
  5. import org.bukkit.inventory.Inventory;
  6. import org.bukkit.inventory.ItemStack;
  7. import org.mineacademy.fo.SerializeUtil;
  8. import org.mineacademy.fo.collection.StrictMap;
  9. import org.mineacademy.fo.menu.model.InventoryDrawer;
  10. import org.mineacademy.fo.remain.CompMetadata;
  11.  
  12. import java.util.*;
  13.  
  14. public class InventoryHolder {
  15.  
  16.     @Getter
  17.     private static final InventoryHolder instance = new InventoryHolder();
  18.  
  19.     StrictMap<Location, ArrayList<Inventory>> openedInventoriesTest = new StrictMap<>();
  20.     StrictMap<UUID, Integer> wiversOfPage = new StrictMap<>();
  21.  
  22.     public int getPageIndex = 2;
  23.     public int GuiSize = 9 * 4;
  24.  
  25.     /**
  26.      * @param location location of the chest.
  27.      * @param player   player some hopen the gui.
  28.      * @return one gui player open.
  29.      */
  30.     public Inventory findAndLoadInventor(Location location, Player player) {
  31.         InventoryRegistry registry = InventoryRegistry.getInstance();
  32.         ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
  33.         int pageNumber = wiversOfPage.get(player.getUniqueId());
  34.         Inventory inventory = null;
  35.         System.out.println("inventory inventories.size " + inventories.size());
  36.         if (inventories != null && inventories.size() > pageNumber) {
  37.             System.out.println("inventory test " + pageNumber);
  38.             inventory = inventories.get(pageNumber);
  39.             System.out.println("inventory open " + inventory);
  40.  
  41.         } else if (inventories == null || inventories.size() <= getPageIndex) {
  42.             inventory = setPage(location, pageNumber, GuiSize, "");
  43.             if (this.openedInventoriesTest != null)
  44.                 inventory = getPage(pageNumber, location);
  45.         }
  46.         //if (inventory != null)
  47.         //inventory.setContents(registry.getContentsLocation(location));
  48.         //InventoryDrawer.setContent(registry.getContentsLocation(location));
  49.         return inventory;
  50.     }
  51.  
  52.     /**
  53.      * @param location where hopper try to add items to a chest or other containers
  54.      * @return return inventory or null if it allredy created.
  55.      */
  56.     public Inventory setPageHopper(Location location) {
  57.         ArrayList<Inventory> inv = new ArrayList<>();
  58.         //ArrayList<Inventory> inventorys = createInventory(GuiSize, "");
  59.         ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
  60.  
  61.         Inventory inventory = null;
  62.         Inventory inventorys = createInventory(GuiSize, "");
  63.         if (inventories == null || inventories.size() < getPageIndex) {
  64.             for (int i = 0; i < getPageIndex; i++) {
  65.                 inventory = inventorys;
  66.                 inv.add(inventory);
  67.             }
  68.             this.openedInventoriesTest.override(location, inv);
  69.         }
  70.         return inventory;
  71.     }
  72.  
  73.     /**
  74.      * @param location location where the gui shall be open.
  75.      * @param page     what page player open and if not exist create new
  76.      * @param size     size of the gui.
  77.      * @param title    The description of the gui
  78.      * @return return array of invetorys, lock on specific cordinats
  79.      */
  80.     public Inventory setPage(Location location, int page, int size, String title) {
  81.         ArrayList<Inventory> inv = new ArrayList<>();
  82.         System.out.println("pagenumber " + page);
  83.         for (int i = 0; i < page + 1; i++) {
  84.             System.out.println("pagenumber loop ");
  85.             Inventory inventory = createInventory(size, title);
  86.             inv.add(inventory);
  87.         }
  88.         this.openedInventoriesTest.override(location, inv);
  89.         return inv.get(page);
  90.     }
  91.  
  92.     /**
  93.      * @param size  set the size of this gui.
  94.      * @param title set the title on the gui some be created.
  95.      * @return part of the gui.
  96.      */
  97.     public InventoryDrawer inventoryDrawer(int size, String title) {
  98.         return InventoryDrawer.of(size, title);
  99.     }
  100.  
  101.     /**
  102.      * @param size  size set the size of this gui.
  103.      * @param title set the title on the gui some be created.
  104.      * @return a complite gui, with size and title.
  105.      */
  106.     public Inventory createInventory(int size, String title) {
  107.         Inventory inventory = inventoryDrawer(size, title).build();
  108.         return inventory;
  109.     }
  110.  
  111.     /**
  112.      * @param page     get the page of this gui.
  113.      * @param location what location it target this gui
  114.      * @return inventory on specific page
  115.      */
  116.     public Inventory getPage(int page, Location location) {
  117.         ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
  118.         return page < 0 || page >= inventories.size() ? null : inventories.get(page);
  119.     }
  120.  
  121.     /**
  122.      * @param inventory inventory you looking for
  123.      * @param location  what location the gui is located
  124.      * @return the page of the gui you looking for.
  125.      */
  126.     public int getPageIndex(Inventory inventory, Location location) {
  127.         ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
  128.         for (int i = 0; i < inventories.size(); i++) {
  129.             if (inventories.get(i).equals(inventory))
  130.                 return i;
  131.         }
  132.  
  133.         return 0;
  134.     }
  135.  
  136.     /**
  137.      * @param location location for the gui
  138.      * @return items from curent gui you close, will save all pages.
  139.      */
  140.  
  141.     public ItemStack[] getContents(Location location) {
  142.         ItemStack[] contents = new ItemStack[0];
  143.         int pagesAmount = getPageIndex;
  144.  
  145.         if (pagesAmount == 0)
  146.             return contents;
  147.  
  148.         for (int page = 0; page < pagesAmount; page++) {
  149.             Inventory inventory = getPage(page, location);
  150.  
  151.             if (inventory == null)
  152.                 return null;
  153.  
  154.             int oldLength = contents.length;
  155.             contents = Arrays.copyOf(contents, contents.length + inventory.getSize());
  156.             System.arraycopy(inventory.getContents(), 0, contents, oldLength, inventory.getSize());
  157.         }
  158.  
  159.         return contents;
  160.     }
  161.  
  162.     /**
  163.      * @param location   where hopper try to add items to a container
  164.      * @param itemStacks items hopper try to add
  165.      * @return items some not fit in the gui.
  166.      */
  167.     public Map<Integer, ItemStack> addItems(Location location, ItemStack... itemStacks) {
  168.         Map<Integer, ItemStack> additionalItems = new HashMap<>();
  169.         Map<Integer, ItemStack> itemAdditionalItems = new HashMap<>();
  170.  
  171.         for (ItemStack itemStack : itemStacks) {
  172.             if (itemStack != null) {
  173.                 int currentInventory = 0;
  174.  
  175.                 do {
  176.                     Inventory inventory = getPage(currentInventory, location);
  177.                     if (inventory != null) {
  178.                         itemAdditionalItems = inventory.addItem(itemStack);
  179.                     }
  180.                     currentInventory++;
  181.                 } while (!itemAdditionalItems.isEmpty() && currentInventory < getPagesAmount() + 1);
  182.  
  183.                 additionalItems.putAll(itemAdditionalItems);
  184.                 System.out.println("inventory loop additionalItems " + additionalItems);
  185.             }
  186.         }
  187.  
  188.         return additionalItems;
  189.     }
  190.  
  191.     /**
  192.      * @param location  location where hopper has try push items
  193.      * @param itemStack items some you add to the chest
  194.      * @return true if chest are not full.
  195.      */
  196.     public boolean checkIfChestFull(Location location, ItemStack itemStack) {
  197.         return addItems(location, itemStack).isEmpty();
  198.     }
  199.  
  200.     /**
  201.      * @return get number of pages the gui has.
  202.      */
  203.     public int getPagesAmount() {
  204.         return this.openedInventoriesTest.size();
  205.     }
  206.  
  207.     /**
  208.      * @param location location of your you you want to find
  209.      * @return return inventoy on location you has requested
  210.      * or null if it not find the gui.
  211.      */
  212.     public ArrayList<Inventory> getInventory(Location location) {
  213.         return this.openedInventoriesTest.get(location);
  214.     }
  215.  
  216.     /**
  217.      * @param player get the player some close the gui
  218.      *               and it save the items to database
  219.      *               if chest are not null.
  220.      */
  221.     public void handleInventoryClose(Player player) {
  222.         InventoryRegistry registry = InventoryRegistry.getInstance();
  223.         Location chestLocation = SerializeUtil.deserializeLocation(CompMetadata.getMetadata(player, "SharedInventory"));
  224.         ArrayList<Inventory> inventorys = this.openedInventoriesTest.get(chestLocation);
  225.  
  226.         int inputNumber = wiversOfPage.get(player.getUniqueId());
  227.  
  228.         Inventory inventory = inventorys.get(inputNumber);
  229.  
  230.         if (inventory != null) {
  231.             registry.addItemsToChestData(chestLocation, getContents(chestLocation));
  232.             //this.savedChests.override(chestLocation, inventory.getContents());
  233.         }
  234.     }
  235.  
  236.     /**
  237.      * @param player     the player some open the gui
  238.      * @param pageNumber save what pagenumber he is on.
  239.      */
  240.     public void playerViwesPage(Player player, int pageNumber) {
  241.         this.wiversOfPage.override(player.getUniqueId(), pageNumber);
  242.     }
  243.  
  244.     /**
  245.      * @param player     set the player some open the gui and
  246.      *                   waht pagenumer he is on.
  247.      * @param pageNumber save what pagenumber he is on.
  248.      */
  249.  
  250.     public void setPlayerViwePage(Player player, int pageNumber) {
  251.         this.wiversOfPage.override(player.getUniqueId(), pageNumber);
  252.     }
  253.  
  254.     /**
  255.      * @param player get the player some open the gui.
  256.      */
  257.  
  258.     public int getPlayerViwePage(Player player) {
  259.         return this.wiversOfPage.get(player.getUniqueId());
  260.     }
  261.  
  262.     /**
  263.      * @param player check if the cache are emtey.
  264.      */
  265.  
  266.     public boolean PlayerViwePageEmpty(Player player) {
  267.         return this.wiversOfPage.contains(player.getUniqueId());
  268.     }
  269.  
  270.     /**
  271.      * @param player remove the players cache data for page.
  272.      */
  273.  
  274.     public void removePlayerViwePage(Player player) {
  275.         this.wiversOfPage.remove(player.getUniqueId());
  276.     }
  277.  
  278. }
  279.  
Add Comment
Please, Sign In to add comment