Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import lombok.Getter;
- import org.bukkit.Location;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.mineacademy.fo.SerializeUtil;
- import org.mineacademy.fo.collection.StrictMap;
- import org.mineacademy.fo.menu.model.InventoryDrawer;
- import org.mineacademy.fo.remain.CompMetadata;
- import java.util.*;
- public class InventoryHolder {
- @Getter
- private static final InventoryHolder instance = new InventoryHolder();
- StrictMap<Location, ArrayList<Inventory>> openedInventoriesTest = new StrictMap<>();
- StrictMap<UUID, Integer> wiversOfPage = new StrictMap<>();
- public int getPageIndex = 2;
- public int GuiSize = 9 * 4;
- /**
- * @param location location of the chest.
- * @param player player some hopen the gui.
- * @return one gui player open.
- */
- public Inventory findAndLoadInventor(Location location, Player player) {
- InventoryRegistry registry = InventoryRegistry.getInstance();
- ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
- int pageNumber = wiversOfPage.get(player.getUniqueId());
- Inventory inventory = null;
- System.out.println("inventory inventories.size " + inventories.size());
- if (inventories != null && inventories.size() > pageNumber) {
- System.out.println("inventory test " + pageNumber);
- inventory = inventories.get(pageNumber);
- System.out.println("inventory open " + inventory);
- } else if (inventories == null || inventories.size() <= getPageIndex) {
- inventory = setPage(location, pageNumber, GuiSize, "");
- if (this.openedInventoriesTest != null)
- inventory = getPage(pageNumber, location);
- }
- //if (inventory != null)
- //inventory.setContents(registry.getContentsLocation(location));
- //InventoryDrawer.setContent(registry.getContentsLocation(location));
- return inventory;
- }
- /**
- * @param location where hopper try to add items to a chest or other containers
- * @return return inventory or null if it allredy created.
- */
- public Inventory setPageHopper(Location location) {
- ArrayList<Inventory> inv = new ArrayList<>();
- //ArrayList<Inventory> inventorys = createInventory(GuiSize, "");
- ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
- Inventory inventory = null;
- Inventory inventorys = createInventory(GuiSize, "");
- if (inventories == null || inventories.size() < getPageIndex) {
- for (int i = 0; i < getPageIndex; i++) {
- inventory = inventorys;
- inv.add(inventory);
- }
- this.openedInventoriesTest.override(location, inv);
- }
- return inventory;
- }
- /**
- * @param location location where the gui shall be open.
- * @param page what page player open and if not exist create new
- * @param size size of the gui.
- * @param title The description of the gui
- * @return return array of invetorys, lock on specific cordinats
- */
- public Inventory setPage(Location location, int page, int size, String title) {
- ArrayList<Inventory> inv = new ArrayList<>();
- System.out.println("pagenumber " + page);
- for (int i = 0; i < page + 1; i++) {
- System.out.println("pagenumber loop ");
- Inventory inventory = createInventory(size, title);
- inv.add(inventory);
- }
- this.openedInventoriesTest.override(location, inv);
- return inv.get(page);
- }
- /**
- * @param size set the size of this gui.
- * @param title set the title on the gui some be created.
- * @return part of the gui.
- */
- public InventoryDrawer inventoryDrawer(int size, String title) {
- return InventoryDrawer.of(size, title);
- }
- /**
- * @param size size set the size of this gui.
- * @param title set the title on the gui some be created.
- * @return a complite gui, with size and title.
- */
- public Inventory createInventory(int size, String title) {
- Inventory inventory = inventoryDrawer(size, title).build();
- return inventory;
- }
- /**
- * @param page get the page of this gui.
- * @param location what location it target this gui
- * @return inventory on specific page
- */
- public Inventory getPage(int page, Location location) {
- ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
- return page < 0 || page >= inventories.size() ? null : inventories.get(page);
- }
- /**
- * @param inventory inventory you looking for
- * @param location what location the gui is located
- * @return the page of the gui you looking for.
- */
- public int getPageIndex(Inventory inventory, Location location) {
- ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
- for (int i = 0; i < inventories.size(); i++) {
- if (inventories.get(i).equals(inventory))
- return i;
- }
- return 0;
- }
- /**
- * @param location location for the gui
- * @return items from curent gui you close, will save all pages.
- */
- public ItemStack[] getContents(Location location) {
- ItemStack[] contents = new ItemStack[0];
- int pagesAmount = getPageIndex;
- if (pagesAmount == 0)
- return contents;
- for (int page = 0; page < pagesAmount; page++) {
- Inventory inventory = getPage(page, location);
- if (inventory == null)
- return null;
- int oldLength = contents.length;
- contents = Arrays.copyOf(contents, contents.length + inventory.getSize());
- System.arraycopy(inventory.getContents(), 0, contents, oldLength, inventory.getSize());
- }
- return contents;
- }
- /**
- * @param location where hopper try to add items to a container
- * @param itemStacks items hopper try to add
- * @return items some not fit in the gui.
- */
- public Map<Integer, ItemStack> addItems(Location location, ItemStack... itemStacks) {
- Map<Integer, ItemStack> additionalItems = new HashMap<>();
- Map<Integer, ItemStack> itemAdditionalItems = new HashMap<>();
- for (ItemStack itemStack : itemStacks) {
- if (itemStack != null) {
- int currentInventory = 0;
- do {
- Inventory inventory = getPage(currentInventory, location);
- if (inventory != null) {
- itemAdditionalItems = inventory.addItem(itemStack);
- }
- currentInventory++;
- } while (!itemAdditionalItems.isEmpty() && currentInventory < getPagesAmount() + 1);
- additionalItems.putAll(itemAdditionalItems);
- System.out.println("inventory loop additionalItems " + additionalItems);
- }
- }
- return additionalItems;
- }
- /**
- * @param location location where hopper has try push items
- * @param itemStack items some you add to the chest
- * @return true if chest are not full.
- */
- public boolean checkIfChestFull(Location location, ItemStack itemStack) {
- return addItems(location, itemStack).isEmpty();
- }
- /**
- * @return get number of pages the gui has.
- */
- public int getPagesAmount() {
- return this.openedInventoriesTest.size();
- }
- /**
- * @param location location of your you you want to find
- * @return return inventoy on location you has requested
- * or null if it not find the gui.
- */
- public ArrayList<Inventory> getInventory(Location location) {
- return this.openedInventoriesTest.get(location);
- }
- /**
- * @param player get the player some close the gui
- * and it save the items to database
- * if chest are not null.
- */
- public void handleInventoryClose(Player player) {
- InventoryRegistry registry = InventoryRegistry.getInstance();
- Location chestLocation = SerializeUtil.deserializeLocation(CompMetadata.getMetadata(player, "SharedInventory"));
- ArrayList<Inventory> inventorys = this.openedInventoriesTest.get(chestLocation);
- int inputNumber = wiversOfPage.get(player.getUniqueId());
- Inventory inventory = inventorys.get(inputNumber);
- if (inventory != null) {
- registry.addItemsToChestData(chestLocation, getContents(chestLocation));
- //this.savedChests.override(chestLocation, inventory.getContents());
- }
- }
- /**
- * @param player the player some open the gui
- * @param pageNumber save what pagenumber he is on.
- */
- public void playerViwesPage(Player player, int pageNumber) {
- this.wiversOfPage.override(player.getUniqueId(), pageNumber);
- }
- /**
- * @param player set the player some open the gui and
- * waht pagenumer he is on.
- * @param pageNumber save what pagenumber he is on.
- */
- public void setPlayerViwePage(Player player, int pageNumber) {
- this.wiversOfPage.override(player.getUniqueId(), pageNumber);
- }
- /**
- * @param player get the player some open the gui.
- */
- public int getPlayerViwePage(Player player) {
- return this.wiversOfPage.get(player.getUniqueId());
- }
- /**
- * @param player check if the cache are emtey.
- */
- public boolean PlayerViwePageEmpty(Player player) {
- return this.wiversOfPage.contains(player.getUniqueId());
- }
- /**
- * @param player remove the players cache data for page.
- */
- public void removePlayerViwePage(Player player) {
- this.wiversOfPage.remove(player.getUniqueId());
- }
- }
Add Comment
Please, Sign In to add comment