broken-arrow

Untitled

May 27th, 2021 (edited)
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 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.inventory.Inventory;
  6. import org.bukkit.inventory.ItemStack;
  7. import org.mineacademy.fo.Common;
  8. import org.mineacademy.fo.collection.SerializedMap;
  9. import org.mineacademy.fo.collection.StrictMap;
  10. import org.mineacademy.fo.constants.FoConstants;
  11. import org.mineacademy.fo.settings.YamlConfig;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.Iterator;
  16. import java.util.Map.Entry;
  17. import java.util.UUID;
  18.  
  19. public final class InventoryRegistry extends YamlConfig {
  20.  
  21.     @Getter
  22.     private static final InventoryRegistry instance = new InventoryRegistry();
  23.  
  24.     // Saved to disk
  25.     private StrictMap<Location, ItemStack[]> savedChests = new StrictMap<>();
  26.  
  27.     // Stored locally
  28.     /*StrictMap<Location, ArrayList<Inventory>> openedInventoriesTest = new StrictMap<>();
  29.     StrictMap<UUID, Integer> wiversOfPage = new StrictMap<>();*/
  30.  
  31.     public int getPageIndex = 2;
  32.  
  33.     private InventoryRegistry() {
  34.         this.loadConfiguration(NO_DEFAULT, FoConstants.File.DATA);
  35.  
  36.     }
  37.  
  38.     @Override
  39.     protected void onLoadFinish() {
  40.         this.savedChests = new StrictMap<>(this.getMap("Chests", Location.class, ItemStack[].class));
  41.  
  42.         // Check for already broken chests and remove
  43.         for (Iterator<Entry<Location, ItemStack[]>> it = this.savedChests.entrySet().iterator(); it.hasNext(); ) {
  44.             Entry<Location, ItemStack[]> entry = it.next();
  45.             Location location = entry.getKey();
  46.  
  47.             System.out.println("entry " + entry);
  48.             System.out.println("chestdata" + Arrays.toString(this.savedChests.get(location)));
  49.             if (location.getBlock().getType() != Material.CHEST) {
  50.                 it.remove();
  51.  
  52.                 System.out.println("Removed broken chest at " + Common.shortLocation(location));
  53.  
  54.             } else
  55.                 System.out.println("Loaded chest at " + Common.shortLocation(location));
  56.             System.out.println("chestdata" + Arrays.toString(this.savedChests.get(location)));
  57.         }
  58.         save();
  59.     }
  60.  
  61.  
  62.     @Override
  63.     protected SerializedMap serialize() {
  64.         return SerializedMap.of("Chests", this.savedChests);
  65.     }
  66.  
  67.     public void registerChest(Location location, Inventory inventory) {
  68.         this.savedChests.override(location, inventory.getContents());
  69.         this.save();
  70.         System.out.println("test");
  71.     }
  72.  
  73.  
  74.     public void addItemsToChestData(Location location, ItemStack[] itemStack) {
  75.         this.savedChests.override(location, itemStack);
  76.         this.save();
  77.     }
  78.  
  79.     public boolean isRegistered(Location location) {
  80.         return this.savedChests.contains(location);
  81.     }
  82.  
  83.     public ItemStack[] getContentsLocation(Location location) {
  84.         return this.savedChests.get(location);
  85.     }
  86.  
  87.     public ArrayList<Location> getChestlocations() {
  88.         for (Entry<Location, ItemStack[]> entry : this.savedChests.entrySet()) {
  89.             ArrayList<Location> locationList = new ArrayList<>();
  90.             //System.out.println("entry.getKey() " + entry.getKey());
  91.             locationList.add(entry.getKey());
  92.             return locationList;
  93.         }
  94.         return null;
  95.     }
  96.  
  97.  
  98.     public void handleBlockBreak(Block block) {
  99.         Location location = block.getLocation();
  100.  
  101.         this.openedInventoriesTest.removeWeak(location);
  102.         this.savedChests.removeWeak(location);
  103.  
  104.         this.save();
  105.     }
  106.  
  107.  
  108. }
Add Comment
Please, Sign In to add comment