Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import lombok.Getter;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.block.Block;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.mineacademy.fo.Common;
- import org.mineacademy.fo.collection.SerializedMap;
- import org.mineacademy.fo.collection.StrictMap;
- import org.mineacademy.fo.constants.FoConstants;
- import org.mineacademy.fo.settings.YamlConfig;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Iterator;
- import java.util.Map.Entry;
- import java.util.UUID;
- public final class InventoryRegistry extends YamlConfig {
- @Getter
- private static final InventoryRegistry instance = new InventoryRegistry();
- // Saved to disk
- private StrictMap<Location, ItemStack[]> savedChests = new StrictMap<>();
- // Stored locally
- /*StrictMap<Location, ArrayList<Inventory>> openedInventoriesTest = new StrictMap<>();
- StrictMap<UUID, Integer> wiversOfPage = new StrictMap<>();*/
- public int getPageIndex = 2;
- private InventoryRegistry() {
- this.loadConfiguration(NO_DEFAULT, FoConstants.File.DATA);
- }
- @Override
- protected void onLoadFinish() {
- this.savedChests = new StrictMap<>(this.getMap("Chests", Location.class, ItemStack[].class));
- // Check for already broken chests and remove
- for (Iterator<Entry<Location, ItemStack[]>> it = this.savedChests.entrySet().iterator(); it.hasNext(); ) {
- Entry<Location, ItemStack[]> entry = it.next();
- Location location = entry.getKey();
- System.out.println("entry " + entry);
- System.out.println("chestdata" + Arrays.toString(this.savedChests.get(location)));
- if (location.getBlock().getType() != Material.CHEST) {
- it.remove();
- System.out.println("Removed broken chest at " + Common.shortLocation(location));
- } else
- System.out.println("Loaded chest at " + Common.shortLocation(location));
- System.out.println("chestdata" + Arrays.toString(this.savedChests.get(location)));
- }
- save();
- }
- @Override
- protected SerializedMap serialize() {
- return SerializedMap.of("Chests", this.savedChests);
- }
- public void registerChest(Location location, Inventory inventory) {
- this.savedChests.override(location, inventory.getContents());
- this.save();
- System.out.println("test");
- }
- public void addItemsToChestData(Location location, ItemStack[] itemStack) {
- this.savedChests.override(location, itemStack);
- this.save();
- }
- public boolean isRegistered(Location location) {
- return this.savedChests.contains(location);
- }
- public ItemStack[] getContentsLocation(Location location) {
- return this.savedChests.get(location);
- }
- public ArrayList<Location> getChestlocations() {
- for (Entry<Location, ItemStack[]> entry : this.savedChests.entrySet()) {
- ArrayList<Location> locationList = new ArrayList<>();
- //System.out.println("entry.getKey() " + entry.getKey());
- locationList.add(entry.getKey());
- return locationList;
- }
- return null;
- }
- public void handleBlockBreak(Block block) {
- Location location = block.getLocation();
- this.openedInventoriesTest.removeWeak(location);
- this.savedChests.removeWeak(location);
- this.save();
- }
- }
Add Comment
Please, Sign In to add comment