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.entity.Player;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.ItemStack;
- import org.mineacademy.fo.Common;
- import org.mineacademy.fo.SerializeUtil;
- import org.mineacademy.fo.collection.SerializedMap;
- import org.mineacademy.fo.collection.StrictMap;
- import org.mineacademy.fo.constants.FoConstants;
- import org.mineacademy.fo.menu.model.InventoryDrawer;
- import org.mineacademy.fo.remain.CompMetadata;
- import org.mineacademy.fo.settings.YamlConfig;
- import java.util.*;
- import java.util.Map.Entry;
- 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
- private final StrictMap<UUID, Integer> wiversOfPage = new StrictMap<>();
- private final StrictMap<Location, ArrayList<Inventory>> openedInventoriesTest = 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();
- }
- 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;
- }
- public boolean AddItemsToChest(int i, Location location, ItemStack itemStack, Inventory from) {
- Inventory firstPage = getPage(0, location);
- /*List<ItemStack> itemsList = Arrays.asList(itemStack);
- Collections.shuffle(itemsList);
- ItemStack[] contents = itemsList.toArray(new ItemStack[0]);*/
- if (firstPage == null)
- return false;
- int pageSize = firstPage.getSize();
- int page = i / pageSize;
- int slot = i % pageSize;
- int pages = 0;
- int inventorySize = 0;
- if (firstPage.firstEmpty() == -1 || firstPage.first(itemStack) == itemStack.getMaxStackSize())
- for (ItemStack itemStack1 : firstPage) {
- inventorySize++;
- if (itemStack1 != null && itemStack1.getAmount() == itemStack1.getMaxStackSize() && inventorySize == pageSize)
- pages++;
- }
- System.out.println("pages " + pages);
- Inventory actualPage = getPage(pages, location);
- if (actualPage == null)
- return false;
- HashMap<Integer, ItemStack> leftOvers = actualPage.addItem(itemStack);
- pages = 0;
- if (getPageIndex >= (pages + 1)) {
- for (ItemStack leftOver : leftOvers.values()) {
- from.addItem(leftOver);
- System.out.println("Items leftOver " + leftOver);
- if (itemStack.equals(leftOver))
- return false;
- }
- }
- return true;
- }
- public void AddItemsToChestold(Location location, ItemStack[] itemStack) {
- this.savedChests.override(location, itemStack);
- this.save();
- }
- public boolean isRegistered(Location location) {
- return this.savedChests.contains(location);
- }
- public ItemStack[] isRegistereds(Location location) {
- return this.savedChests.get(location);
- }
- public void playerViwePage(Player player, int pageNumber) {
- this.wiversOfPage.override(player.getUniqueId(), pageNumber);
- }
- public int getPlayerViwePage(Player player) {
- return this.wiversOfPage.get(player.getUniqueId());
- }
- public boolean PlayerViwePageEmpty(Player player) {
- return this.wiversOfPage.contains(player.getUniqueId());
- }
- public void removePlayerViwePage(Player player) {
- this.wiversOfPage.remove(player.getUniqueId());
- }
- public Inventory findAndLoadInventor(Location location, Player player) {
- ArrayList<Inventory> inv = new ArrayList<>();
- ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
- int inputNumber = wiversOfPage.get(player.getUniqueId());
- InventoryDrawer drawer = InventoryDrawer.of(9 * 4, "");
- Inventory inventory = null;
- if (inventories == null || inventories.size() < getPageIndex) {
- for (int i = 0; i < getPageIndex; i++) {
- inventory = drawer.build();
- inv.add(inventory);
- }
- this.openedInventoriesTest.put(location, inv);
- if (this.openedInventoriesTest != null)
- inventory = inv.get(inputNumber);
- }
- if (inventories != null && inventories.size() > inputNumber) {
- System.out.println("inventory test " + inputNumber);
- inventory = inventories.get(inputNumber);
- }
- drawer.setContent(this.savedChests.get(location));
- return inventory;
- }
- public Inventory findAndLoadInventoryHopper(Location location) {
- ArrayList<Inventory> inv = new ArrayList<>();
- ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
- InventoryDrawer drawer = InventoryDrawer.of(9 * 4, "");
- Inventory inventory = null;
- if (inventories == null || inventories.size() < getPageIndex) {
- for (int i = 0; i < getPageIndex; i++) {
- inventory = drawer.build();
- inv.add(inventory);
- }
- this.openedInventoriesTest.override(location, inv);
- }
- return inventory;
- }
- public Inventory getPage(int page, Location location) {
- ArrayList<Inventory> inventories = this.openedInventoriesTest.get(location);
- return page < 0 || page >= inventories.size() ? null : inventories.get(page);
- }
- 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;
- }
- public ArrayList<Inventory> findAndLoadInventory(Location location) {
- return this.openedInventoriesTest.get(location);
- }
- public void handleInventoryClose(Player player) {
- 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) {
- this.savedChests.override(chestLocation, getContents(chestLocation));
- //this.savedChests.override(chestLocation, inventory.getContents());
- this.save();
- }
- }
- public void handleBlockBreak(Block block) {
- Location location = block.getLocation();
- this.openedInventoriesTest.removeWeak(location);
- this.savedChests.removeWeak(location);
- this.save();
- }
- public ItemStack[] handleHopperMove(Location location) {
- return this.savedChests.get(location);
- }
- }
Add Comment
Please, Sign In to add comment