broken-arrow

Untitled

May 24th, 2021 (edited)
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.96 KB | None | 0 0
  1. import lombok.Getter;
  2. import org.bukkit.Location;
  3. import org.bukkit.Material;
  4. import org.bukkit.block.Block;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.inventory.Inventory;
  7. import org.bukkit.inventory.ItemStack;
  8. import org.mineacademy.fo.Common;
  9. import org.mineacademy.fo.SerializeUtil;
  10. import org.mineacademy.fo.collection.SerializedMap;
  11. import org.mineacademy.fo.collection.StrictMap;
  12. import org.mineacademy.fo.constants.FoConstants;
  13. import org.mineacademy.fo.menu.model.InventoryDrawer;
  14. import org.mineacademy.fo.remain.CompMetadata;
  15. import org.mineacademy.fo.settings.YamlConfig;
  16.  
  17. import java.util.*;
  18. import java.util.Map.Entry;
  19.  
  20. public final class InventoryRegistry extends YamlConfig {
  21.  
  22.     @Getter
  23.     private static final InventoryRegistry instance = new InventoryRegistry();
  24.  
  25.     // Saved to disk
  26.     private StrictMap<Location, ItemStack[]> savedChests = new StrictMap<>();
  27.  
  28.     // Stored locally
  29.     private final StrictMap<UUID, Integer> wiversOfPage = new StrictMap<>();
  30.     private final StrictMap<Location, ArrayList<Inventory>> openedInventoriesTest = new StrictMap<>();
  31.  
  32.     public int getPageIndex = 2;
  33.  
  34.     private InventoryRegistry() {
  35.         this.loadConfiguration(NO_DEFAULT, FoConstants.File.DATA);
  36.  
  37.     }
  38.  
  39.     @Override
  40.     protected void onLoadFinish() {
  41.         this.savedChests = new StrictMap<>(this.getMap("Chests", Location.class, ItemStack[].class));
  42.    
  43.         // Check for already broken chests and remove
  44.         for (Iterator<Entry<Location, ItemStack[]>> it = this.savedChests.entrySet().iterator(); it.hasNext(); ) {
  45.             Entry<Location, ItemStack[]> entry = it.next();
  46.             Location location = entry.getKey();
  47.  
  48.             System.out.println("entry " + entry);
  49.             System.out.println("chestdata" + Arrays.toString(this.savedChests.get(location)));
  50.             if (location.getBlock().getType() != Material.CHEST) {
  51.                 it.remove();
  52.  
  53.                 System.out.println("Removed broken chest at " + Common.shortLocation(location));
  54.  
  55.             } else
  56.                 System.out.println("Loaded chest at " + Common.shortLocation(location));
  57.             System.out.println("chestdata" + Arrays.toString(this.savedChests.get(location)));
  58.         }
  59.         //save();
  60.     }
  61.  
  62.  
  63.     @Override
  64.     protected SerializedMap serialize() {
  65.         return SerializedMap.of("Chests", this.savedChests);
  66.     }
  67.  
  68.     public void registerChest(Location location, Inventory inventory) {
  69.         this.savedChests.override(location, inventory.getContents());
  70.  
  71.         this.save();
  72.     }
  73.  
  74.     public ItemStack[] getContents(Location location) {
  75.         ItemStack[] contents = new ItemStack[0];
  76.         int pagesAmount = getPageIndex;
  77.  
  78.         if (pagesAmount == 0)
  79.             return contents;
  80.  
  81.         for (int page = 0; page < pagesAmount; page++) {
  82.             Inventory inventory = getPage(page, location);
  83.  
  84.             if (inventory == null)
  85.                 return null;
  86.  
  87.             int oldLength = contents.length;
  88.             contents = Arrays.copyOf(contents, contents.length + inventory.getSize());
  89.             System.arraycopy(inventory.getContents(), 0, contents, oldLength, inventory.getSize());
  90.         }
  91.  
  92.         return contents;
  93.     }
  94.  
  95.     public boolean AddItemsToChest(int i, Location location, ItemStack itemStack, Inventory from) {
  96.         Inventory firstPage = getPage(0, location);
  97.         /*List<ItemStack> itemsList = Arrays.asList(itemStack);
  98.         Collections.shuffle(itemsList);
  99.         ItemStack[] contents = itemsList.toArray(new ItemStack[0]);*/
  100.         if (firstPage == null)
  101.             return false;
  102.  
  103.         int pageSize = firstPage.getSize();
  104.         int page = i / pageSize;
  105.         int slot = i % pageSize;
  106.         int pages = 0;
  107.         int inventorySize = 0;
  108.  
  109.         if (firstPage.firstEmpty() == -1 || firstPage.first(itemStack) == itemStack.getMaxStackSize())
  110.             for (ItemStack itemStack1 : firstPage) {
  111.                 inventorySize++;
  112.                 if (itemStack1 != null && itemStack1.getAmount() == itemStack1.getMaxStackSize() && inventorySize == pageSize)
  113.                     pages++;
  114.             }
  115.         System.out.println("pages " + pages);
  116.         Inventory actualPage = getPage(pages, location);
  117.         if (actualPage == null)
  118.             return false;
  119.         HashMap<Integer, ItemStack> leftOvers = actualPage.addItem(itemStack);
  120.         pages = 0;
  121.         if (getPageIndex >= (pages + 1)) {
  122.             for (ItemStack leftOver : leftOvers.values()) {
  123.                 from.addItem(leftOver);
  124.                 System.out.println("Items leftOver " + leftOver);
  125.                 if (itemStack.equals(leftOver))
  126.                     return false;
  127.  
  128.             }
  129.         }
  130.         return true;
  131.     }
  132.  
  133.  
  134.     public void AddItemsToChestold(Location location, ItemStack[] itemStack) {
  135.         this.savedChests.override(location, itemStack);
  136.  
  137.  
  138.         this.save();
  139.     }
  140.  
  141.     public boolean isRegistered(Location location) {
  142.         return this.savedChests.contains(location);
  143.     }
  144.  
  145.     public ItemStack[] isRegistereds(Location location) {
  146.         return this.savedChests.get(location);
  147.     }
  148.  
  149.  
  150.     public void playerViwePage(Player player, int pageNumber) {
  151.         this.wiversOfPage.override(player.getUniqueId(), pageNumber);
  152.     }
  153.  
  154.     public int getPlayerViwePage(Player player) {
  155.         return this.wiversOfPage.get(player.getUniqueId());
  156.     }
  157.  
  158.     public boolean PlayerViwePageEmpty(Player player) {
  159.         return this.wiversOfPage.contains(player.getUniqueId());
  160.     }
  161.  
  162.     public void removePlayerViwePage(Player player) {
  163.         this.wiversOfPage.remove(player.getUniqueId());
  164.     }
  165.  
  166.     public Inventory findAndLoadInventor(Location location, Player player) {
  167.  
  168.         ArrayList<Inventory> inv = new ArrayList<>();
  169.         ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
  170.         int inputNumber = wiversOfPage.get(player.getUniqueId());
  171.  
  172.         InventoryDrawer drawer = InventoryDrawer.of(9 * 4, "");
  173.         Inventory inventory = null;
  174.  
  175.         if (inventories == null || inventories.size() < getPageIndex) {
  176.             for (int i = 0; i < getPageIndex; i++) {
  177.                 inventory = drawer.build();
  178.                 inv.add(inventory);
  179.             }
  180.             this.openedInventoriesTest.put(location, inv);
  181.             if (this.openedInventoriesTest != null)
  182.                 inventory = inv.get(inputNumber);
  183.         }
  184.         if (inventories != null && inventories.size() > inputNumber) {
  185.             System.out.println("inventory test " + inputNumber);
  186.             inventory = inventories.get(inputNumber);
  187.  
  188.  
  189.         }
  190.  
  191.         drawer.setContent(this.savedChests.get(location));
  192.         return inventory;
  193.     }
  194.  
  195.     public Inventory findAndLoadInventoryHopper(Location location) {
  196.  
  197.         ArrayList<Inventory> inv = new ArrayList<>();
  198.         ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
  199.  
  200.  
  201.         InventoryDrawer drawer = InventoryDrawer.of(9 * 4, "");
  202.         Inventory inventory = null;
  203.  
  204.         if (inventories == null || inventories.size() < getPageIndex) {
  205.             for (int i = 0; i < getPageIndex; i++) {
  206.                 inventory = drawer.build();
  207.                 inv.add(inventory);
  208.             }
  209.             this.openedInventoriesTest.override(location, inv);
  210.         }
  211.         return inventory;
  212.     }
  213.  
  214.  
  215.     public Inventory getPage(int page, Location location) {
  216.         ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
  217.         return page < 0 || page >= inventories.size() ? null : inventories.get(page);
  218.     }
  219.  
  220.     public int getPageIndex(Inventory inventory, Location location) {
  221.         ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
  222.         for (int i = 0; i < inventories.size(); i++) {
  223.             if (inventories.get(i).equals(inventory))
  224.                 return i;
  225.         }
  226.  
  227.         return 0;
  228.     }
  229.  
  230.  
  231.     public ArrayList<Inventory> findAndLoadInventory(Location location) {
  232.         return this.openedInventoriesTest.get(location);
  233.     }
  234.  
  235.     public void handleInventoryClose(Player player) {
  236.         Location chestLocation = SerializeUtil.deserializeLocation(CompMetadata.getMetadata(player, "SharedInventory"));
  237.         ArrayList<Inventory> inventorys = this.openedInventoriesTest.get(chestLocation);
  238.  
  239.         int inputNumber = wiversOfPage.get(player.getUniqueId());
  240.  
  241.         Inventory inventory = inventorys.get(inputNumber);
  242.  
  243.         if (inventory != null) {
  244.             this.savedChests.override(chestLocation, getContents(chestLocation));
  245.             //this.savedChests.override(chestLocation, inventory.getContents());
  246.             this.save();
  247.         }
  248.     }
  249.  
  250.  
  251.     public void handleBlockBreak(Block block) {
  252.         Location location = block.getLocation();
  253.  
  254.         this.openedInventoriesTest.removeWeak(location);
  255.         this.savedChests.removeWeak(location);
  256.  
  257.         this.save();
  258.     }
  259.  
  260.  
  261.     public ItemStack[] handleHopperMove(Location location) {
  262.         return this.savedChests.get(location);
  263.     }
  264. }
  265.  
Add Comment
Please, Sign In to add comment