broken-arrow

Untitled

Jul 24th, 2021 (edited)
832
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import net.kyori.adventure.text.Component;
  2. import org.broken.cheststorage.Guimenus.GuiUpgradePages;
  3. import org.broken.cheststorage.api.containerholders.Inventoryholders;
  4. import org.broken.cheststorage.data.ChestRegistry;
  5. import org.broken.cheststorage.util.ItemUtily;
  6. import org.broken.cheststorage.util.MetadataEnumsForGui;
  7. import org.broken.cheststorage.util.YamlSettingsContainers;
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.Location;
  10. import org.bukkit.Material;
  11. import org.bukkit.entity.Player;
  12. import org.bukkit.event.block.BlockBreakEvent;
  13. import org.bukkit.event.inventory.InventoryClickEvent;
  14. import org.bukkit.event.inventory.InventoryType;
  15. import org.bukkit.inventory.Inventory;
  16. import org.bukkit.inventory.ItemStack;
  17. import org.mineacademy.fo.Common;
  18. import org.mineacademy.fo.SerializeUtil;
  19. import org.mineacademy.fo.collection.StrictMap;
  20. import org.mineacademy.fo.remain.CompMetadata;
  21.  
  22. import java.math.BigInteger;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.UUID;
  27.  
  28. /**
  29.  * Parent class for Inventory management.
  30.  */
  31.  
  32. public abstract class InventoryHolder implements Inventoryholders {
  33.  
  34.     static StrictMap<Location, ArrayList<Inventory>> cachedInventories = new StrictMap<>();
  35.     static StrictMap<UUID, Integer> wiversOfPage = new StrictMap<>();
  36.  
  37.     protected Location location;
  38.     protected Player player;
  39.     protected int guiSize;
  40.     protected int numberOfPages;
  41.     public boolean ifChestFull;
  42.  
  43.     private static int counter = 0;
  44.  
  45.     public InventoryHolder(Location location, Player player) {
  46.         ChestRegistry registry = ChestRegistry.getInstance();
  47.         this.location = location;
  48.         this.player = player;
  49.         this.numberOfPages = registry.getAmountOfPages(location);
  50.         this.guiSize = YamlSettingsContainers.getGuiSize(registry, location, registry.getCurrentUpdate(location));
  51.        
  52.     }
  53.  
  54.  
  55.     /**
  56.      * Get size some are set inside yml file.
  57.      *
  58.      * @return return int value of invetory size.
  59.      */
  60.  
  61.     public int getguiSize() {
  62.         return this.guiSize;
  63.     }
  64.  
  65.  
  66.     /**
  67.      * Create inventory if not exist and add items if
  68.      * the gui has items before.
  69.      *
  70.      * @return the gui player open.
  71.      */
  72.  
  73.  
  74.     @Override
  75.     public abstract Inventory findAndLoadInventory();
  76.  
  77.     /**
  78.      * @return return inventory or null if it allredy created.
  79.      */
  80.     @Override
  81.     public abstract Inventory setInventoryHopper();
  82.  
  83.     /**
  84.      * Set new page if it not exist.
  85.      *
  86.      * @param type  set type of inventory or null if
  87.      *              you want chest gui with size.
  88.      * @param size  size of the inventory.
  89.      * @param title The description of the gui
  90.      * @return return array of invetorys, on specific cordinats.
  91.      */
  92.  
  93.     public Inventory setPage(InventoryType type, int size, String title) {
  94.  
  95.         ArrayList<Inventory> inventorys = cachedInventories.get(this.location);
  96.         ArrayList<Inventory> inv = new ArrayList<>();
  97.         Inventory newInventory = createInventory(null, title, null);
  98.  
  99.         if (inventorys == null)
  100.             for (int i = 0; i < numberOfPages; i++) {
  101.                 inv.add(createInventory(null, title, null));
  102.             }
  103.         else {
  104.             inv.addAll(inventorys);
  105.             inv.add(newInventory);
  106.         }
  107.         cachedInventories.override(this.location, inv);
  108.  
  109.         return inv.get(0);
  110.     }
  111.  
  112.     /**
  113.      * Create an inventory for containers.
  114.      *
  115.      * @param inventoryType Set type of container like chest or hopper
  116.      *                      set it to null to generate chest gui with size.
  117.      * @param title         set the title on the gui some be created.
  118.      * @param inventorySize set inventory size, as defult it will take settings from containers yml files.
  119.      * @return A inventory with your settings.
  120.      */
  121.     @Override
  122.     public Inventory createInventory(InventoryType inventoryType, String title, Integer inventorySize) {
  123.         if (inventoryType != null)
  124.             return Bukkit.createInventory(null, inventoryType, title);
  125.  
  126.         return Bukkit.createInventory(null, inventorySize == null || inventorySize < 9 ? this.guiSize : inventorySize, title);
  127.     }
  128.  
  129.     /**
  130.      * Create an inventory for containers.
  131.      *
  132.      * @param inventoryType Set type of container like chest or hopper
  133.      *                      set it to null to generate chest gui with size.
  134.      * @param title         set the title on the gui some be created.
  135.      * @param inventorySize set inventory size, as defult it will take settings from containers yml files.
  136.      * @return A inventory with your settings.
  137.      */
  138.  
  139.     @Override
  140.     public Inventory createInventory(InventoryType inventoryType, Component title, Integer inventorySize) {
  141. /*
  142.         if (inventoryType != null)
  143.             return Bukkit.createInventory(null, inventoryType, title);
  144.         return Bukkit.createInventory(null, inventorySize == null || inventorySize < 9 ? this.guiSize : inventorySize, title);*/
  145.         return null;
  146.     }
  147.  
  148.     /**
  149.      * Get the page some has items, if one page
  150.      * are emty it will check next page for items
  151.      * or the page befor if it has items.
  152.      * <p>
  153.      * <p>
  154.      * If inventory not exist, it will create new one automatic
  155.      * with right amount of pages.
  156.      *
  157.      * @param registry get data from chache
  158.      * @return inventory with items.
  159.      */
  160.  
  161.     @Override
  162.     public Inventory getEmptyPages(ChestRegistry registry) {
  163.         Inventory inventory = null;
  164.         YamlSettingsContainers settings = new YamlSettingsContainers();
  165.  
  166.         if (registry.getAmountOfPages(this.location) > 0) {
  167.             ArrayList<Inventory> inventoriesList = getInventory(this.location);
  168.  
  169.             if (inventoriesList == null)
  170.                 setInventoryHopper();
  171.             else {
  172.                 inventory = inventoriesList.get(0);
  173.             }
  174.             if (inventoriesList != null)
  175.                 for (int i = 0; i < inventoriesList.size(); i++)
  176.  
  177.                     if (inventory.isEmpty() && i < settings.getMaxamountOfPages(registry.getContainerFileName(this.location), registry.getCurrentUpdate(this.location))) {
  178.  
  179.                         if (i < registry.getAmountOfPages(location))
  180.                             if (inventoriesList.size() > i) {
  181.                                 inventory = inventoriesList.get(i);
  182.                             } else
  183.                                 setInventoryHopper();
  184.                     }
  185.         }
  186.  
  187.         return inventory;
  188.     }
  189.  
  190.  
  191.     /**
  192.      * Handle inventory clickevent. For ether when player add/remove items
  193.      * or change page.
  194.      * See to see how it is set up for Storage Unit and rest of the chest use the defult one in See also.
  195.      *
  196.      * @param registry     get data from chache.
  197.      * @param upgradePages gui you open to upgrade number of pages.
  198.      * @param location     location of the container.
  199.      * @param event        the event.
  200.      * @param player       player some interact with the chest.
  201.      * @return return true if player click inside inventory or false if he not clickinside right inventory.
  202.      * @see org.broken.cheststorage.inventoryholders.InventoryHolderStorageUnit#onClickingInsideGui(ChestRegistry, GuiUpgradePages, Location, InventoryClickEvent, Player)
  203.      * @see org.broken.cheststorage.inventoryholders.InventoryHolderDefultchest#onClickingInsideGui(ChestRegistry, GuiUpgradePages, Location, InventoryClickEvent, Player)
  204.      */
  205.  
  206.     @Override
  207.     public abstract boolean onClickingInsideGui(ChestRegistry registry, GuiUpgradePages upgradePages, Location location, InventoryClickEvent event, Player player);
  208.  
  209.  
  210.     public void removeInventory(Location location) {
  211.         if (cachedInventories.get(location) != null) {
  212.             cachedInventories.remove(location);
  213.         }
  214.     }
  215.  
  216.     /**
  217.      * @return count number of itemstacks and return
  218.      * a number of stacks some you put in.
  219.      */
  220.  
  221.  
  222.     @Override
  223.     public int countItemStacks(ItemStack[] itemStacks) {
  224.         if (itemStacks == null)
  225.             return 0;
  226.         return itemStacks.length;
  227.     }
  228.  
  229.     /**
  230.      * Set items in the chest on this cords,
  231.      * if you have more an one page, it will
  232.      * set items on this too (if a slot not has items
  233.      * it will return air or null (slots will be empty)).
  234.      *
  235.      * @param location where the container are placed.
  236.      */
  237.     @Override
  238.     public abstract void setContents(Location location);
  239.  
  240.  
  241.     /**
  242.      * Get the page you want to open.
  243.      *
  244.      * @param page get the page of this container.
  245.      * @return inventory on specific page
  246.      */
  247.  
  248.     @Override
  249.     public Inventory getPage(Location location, int page) {
  250.  
  251.         ArrayList<Inventory> inventories = cachedInventories.get(location);
  252.         if (inventories == null)
  253.             return null;
  254.         return page < 0 || page >= inventories.size() ? null : inventories.get(page);
  255.     }
  256.  
  257.     /**
  258.      * Get number of the gui player has curent
  259.      * open.
  260.      *
  261.      * @param inventory inventory you looking for
  262.      * @return the page of the inventory you current has open.
  263.      */
  264.     @Override
  265.     public int getPageIndex(Inventory inventory) {
  266.         ArrayList<Inventory> inventories = cachedInventories.get(this.location);
  267.         if (inventories != null)
  268.             for (int i = 0; i < inventories.size(); i++) {
  269.                 if (inventories.get(i).equals(inventory))
  270.                     return i;
  271.             }
  272.  
  273.         return 0;
  274.     }
  275.  
  276.     /**
  277.      * Get the contents in the chest.
  278.      *
  279.      * @param location location for the inventory you has close
  280.      * @return items from curent gui you close, will save all pages.
  281.      */
  282.  
  283.     @Override
  284.     public abstract ItemStack[] getContents(Location location);
  285.  
  286.     /**
  287.      * Add items to the chest and change page if first is full.
  288.      * If chest are full it will return the items some not fit.
  289.      *
  290.      * @param itemStacks items hopper try to add
  291.      * @return items some not fit in the gui.
  292.      */
  293.  
  294.     @Override
  295.     public Map<Integer, ItemStack> addItems(ItemStack... itemStacks) {
  296.         Map<Integer, ItemStack> additionalItems = new HashMap<>();
  297.         Map<Integer, ItemStack> itemAdditionalItems = new HashMap<>();
  298.  
  299.         for (ItemStack itemStack : itemStacks) {
  300.  
  301.             if (itemStack != null) {
  302.                 int currentInventory = 0;
  303.  
  304.                 do {
  305.                     Inventory inventory = getPage(this.location, currentInventory);
  306.                     if (inventory != null) {
  307.                         itemAdditionalItems = inventory.addItem(itemStack);
  308.                     }
  309.                     currentInventory++;
  310.                 } while (!itemAdditionalItems.isEmpty() && currentInventory < getPagesAmount(this.location));
  311.  
  312.                 additionalItems.putAll(itemAdditionalItems);
  313.             }
  314.         }
  315.  
  316.         return additionalItems;
  317.     }
  318.  
  319.     /**
  320.      * Check if chest are full.
  321.      *
  322.      * @param itemStack items some you add to the chest
  323.      * @return true if chest are not full.
  324.      */
  325.  
  326.     @Override
  327.     public boolean checkIfChestFull(ItemStack itemStack, Inventory from) {
  328.         boolean checkitems = addItems(itemStack).isEmpty();
  329.  
  330.         if (!checkitems) {
  331.             ifChestFull = true;
  332.             from.addItem(itemStack);
  333.             return false;
  334.         }
  335.         ifChestFull = false;
  336.         return true;
  337.     }
  338.  
  339.     /**
  340.      * Get the number of pages for one container.
  341.      *
  342.      * @return get number of pages the inventory has.
  343.      */
  344.     @Override
  345.     public int getPagesAmount(Location location) {
  346.         if (cachedInventories.get(location) != null)
  347.             return cachedInventories.get(location).size();
  348.         return 0;
  349.     }
  350.  
  351.     /**
  352.      * Get a Inventory for a container.
  353.      *
  354.      * @param location location of the inventor want to find.
  355.      * @return return inventoy on location you has requested
  356.      * or null if it not find the gui.
  357.      */
  358.     @Override
  359.     public ArrayList<Inventory> getInventory(Location location) {
  360.         return cachedInventories.get(location);
  361.     }
  362.  
  363.     /**
  364.      * Save the items to cache.
  365.      *
  366.      * @param player get the player some close the gui.
  367.      */
  368.     @Override
  369.     public void handleInventoryClose(Player player) {
  370.         MetadataEnumsForGui locations = MetadataEnumsForGui.CHEST_METADATA;
  371.         ChestRegistry registry = ChestRegistry.getInstance();
  372.         Location chestLocation = SerializeUtil.deserializeLocation(CompMetadata.getMetadata(player, locations.toString()));
  373.  
  374.         registry.saveChestItemsFromHopper(chestLocation, getContents(chestLocation), registry.getPlayerUUID(chestLocation));
  375.  
  376.     }
  377.  
  378.     /**
  379.      * When you break container it collect all pages of items
  380.      * and drop it on the ground.
  381.      *
  382.      * @param event the event some get trigged when you break a container.
  383.      * @return return true after it has finished.
  384.      */
  385.  
  386.     @Override
  387.     public boolean dropItemsOnBlockBreak(BlockBreakEvent event) {
  388.         Location loc = event.getBlock().getLocation();
  389.         for (int page = 0; page < getPagesAmount(loc); page++) {
  390.             //if (getPage(page, loc) != null) {
  391.  
  392.             Inventory inventory = getPage(loc, page);
  393.             if (inventory != null) {
  394.                 for (ItemStack itemStack : inventory.getContents())
  395.                     if (itemStack != null && itemStack.getType() != Material.AIR) {
  396.                         event.getBlock().getWorld().dropItemNaturally(loc, itemStack);
  397.                     }
  398.                 inventory.clear();
  399.             }
  400.             Common.runLater(10, () -> removeInventory(loc));
  401.             //ChestRegistry.getInstance().removeContainer(loc);
  402.         }
  403.         return true;
  404.     }
  405.  
  406.     /**
  407.      * Drop items 1 stack at the time, if you try drop 128 items
  408.      * they become invisible (but can be picked up). so to avoid
  409.      * this it will drop a maximum of 64 items at the same tick.
  410.      *
  411.      * @param itemStack add itemstacks some get converted to 64 items at max
  412.      *                  or the items max stack size.
  413.      */
  414.  
  415.     @Override
  416.     public void dropItemsOnGround(ItemStack itemStack) {
  417.  
  418.     }
  419.  
  420.     /**
  421.      * When you break the continer it check what type of container
  422.      * and will return back right type of container and remove from
  423.      * cache both the inventory (if it exist) and the database.
  424.      *
  425.      * @param event the event some get trigged when you break the chest.
  426.      */
  427.  
  428.     @Override
  429.     public void onContainerBreak(BlockBreakEvent event) {
  430.         ChestRegistry register = ChestRegistry.getInstance();
  431.         Location location = event.getBlock().getLocation();
  432.         Material blocktype = event.getBlock().getType();
  433.         YamlSettingsContainers settings = new YamlSettingsContainers();
  434.  
  435.  
  436.         if (register.isRegistered(location) && !event.isCancelled())
  437.             if (event.getBlock().getType() == Material.CHEST || event.getBlock().getType() == Material.BARREL || event.getBlock().getType() == Material.HOPPER) {
  438.                 ItemUtily itemUtily = new ItemUtily();
  439.                 Map<String, String> metadata = new HashMap<>();
  440.                 event.setDropItems(false);
  441.  
  442.                 metadata.put("CHEST", String.valueOf(register.getTypeofContainer(location)));
  443.                 metadata.put("FILETYPE", register.getContainerFileName(location));
  444.                 metadata.put("TYPEOFLEVEL", register.getCurrentUpdate(location));
  445.                 ItemStack itemStack = itemUtily.createItemStack(blocktype, settings.getDisplayName(register.getContainerFileName(location), register.getCurrentUpdate(location)),
  446.                         settings.translateColorcodesLore(register.getContainerFileName(location), register.getCurrentUpdate(location)), metadata);
  447.  
  448.                 if (this.player != null) {
  449.                     HashMap<Integer, ItemStack> ItemIfdropOrNot = this.player.getInventory().addItem(itemStack);
  450.                     if (ItemIfdropOrNot.size() > 0) {
  451.                         event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), itemStack);
  452.                     }
  453.                 } else
  454.                     event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), itemStack);
  455.                 if (register.getTypeofContainer(location) != null)
  456.                     switch (register.getTypeofContainer(location)) {
  457.                         case LINKEDCONTAINER:
  458.                             register.removeContainer(location);
  459.                             break;
  460.                         case PAGEDCONTAINER:
  461.                         case CRAFTINGCONTAINER:
  462.                         case SELLCHEST:
  463.                             InventoryHolderDefultchest blockbreak = new InventoryHolderDefultchest(location, this.player);
  464.                             blockbreak.dropItemsOnBlockBreak(event);
  465.                             register.removeContainer(location);
  466.                             break;
  467.                         case STORAGEUNIT:
  468.                             InventoryHolderStorageUnit blockbreaks = new InventoryHolderStorageUnit(location, this.player);
  469.                             blockbreaks.dropItemsOnBlockBreak(event);
  470.                             register.removeContainer(location);
  471.                             MetadataEnumsForGui metadatachest = MetadataEnumsForGui.CHEST_METADATA;
  472.                             if (this.player != null && this.player.hasMetadata(metadatachest.toString()))
  473.                                 this.player.removeMetadata(metadatachest.toString(), metadatachest.getPlugin());
  474.                             break;
  475.                         default:
  476.                             System.out.println("This should never happen, what has you done with my plugin??!!!!!");
  477.                     }
  478.             }
  479.  
  480.     }
  481.  
  482.     /**
  483.      * Set the page player will open.
  484.      *
  485.      * @param player     set the player.
  486.      * @param pageNumber set pagenumber player shall open.
  487.      */
  488.  
  489.     @Override
  490.     public void setPlayerViwePage(Player player, int pageNumber) {
  491.         wiversOfPage.override(player.getUniqueId(), pageNumber);
  492.     }
  493.  
  494.     /**
  495.      * Get what page a player curently has open
  496.      * (will not say if he has the inventory open or not).
  497.      *
  498.      * @param player get the player some open the gui.
  499.      */
  500.  
  501.     @Override
  502.     public int getPlayerViwePage(Player player) {
  503.         return wiversOfPage.get(player.getUniqueId());
  504.     }
  505.  
  506.     /**
  507.      * Method to see if cache are empty.
  508.      *
  509.      * @param player check if the cache are empty.
  510.      * @return true if player exist.
  511.      */
  512.     @Override
  513.     public boolean PlayerViwePageEmpty(Player player) {
  514.         return wiversOfPage.contains(player.getUniqueId());
  515.     }
  516.  
  517.     /**
  518.      * Remove player from cache.
  519.      *
  520.      * @param player remove the players cache data for page.
  521.      */
  522.     @Override
  523.     public void removePlayerViwePage(Player player) {
  524.         wiversOfPage.remove(player.getUniqueId());
  525.     }
  526.  
  527.     /**
  528.      * Update the inventory title for container.
  529.      *
  530.      * @param amounts amount of items inside the chest.
  531.      * @param cursor  items player add to the chest.
  532.      */
  533.  
  534.     @Override
  535.     public void updateInventoryTitle(BigInteger amounts, ItemStack cursor) {
  536.  
  537.     }
  538.  
  539.  
  540.     public void setPlayerLocation(Location location, Player player) {
  541.         ChestRegistry registry = ChestRegistry.getInstance();
  542.         if (location != null)
  543.             this.location = location;
  544.         if (player != null)
  545.             this.player = player;
  546.         this.numberOfPages = registry.getAmountOfPages(location);
  547.         this.guiSize = new YamlSettingsContainers().getGuiSize(registry, location, registry.getCurrentUpdate(location));
  548.  
  549.     }
  550.  
  551.     protected Location getLocation() {
  552.         return this.location;
  553.     }
  554.  
  555.     protected Player getPlayer() {
  556.         return this.player;
  557.     }
  558. }
RAW Paste Data