Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import net.kyori.adventure.text.Component;
- import org.broken.cheststorage.Guimenus.GuiUpgradePages;
- import org.broken.cheststorage.api.containerholders.Inventoryholders;
- import org.broken.cheststorage.data.ChestRegistry;
- import org.broken.cheststorage.util.ItemUtily;
- import org.broken.cheststorage.util.MetadataEnumsForGui;
- import org.broken.cheststorage.util.YamlSettingsContainers;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.entity.Player;
- import org.bukkit.event.block.BlockBreakEvent;
- import org.bukkit.event.inventory.InventoryClickEvent;
- import org.bukkit.event.inventory.InventoryType;
- 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.StrictMap;
- import org.mineacademy.fo.remain.CompMetadata;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.UUID;
- /**
- * Parent class for Inventory management.
- */
- public abstract class InventoryHolder implements Inventoryholders {
- static StrictMap<Location, ArrayList<Inventory>> cachedInventories = new StrictMap<>();
- static StrictMap<UUID, Integer> wiversOfPage = new StrictMap<>();
- protected Location location;
- protected Player player;
- protected int guiSize;
- protected int numberOfPages;
- public boolean ifChestFull;
- private static int counter = 0;
- public InventoryHolder(Location location, Player player) {
- ChestRegistry registry = ChestRegistry.getInstance();
- this.location = location;
- this.player = player;
- this.numberOfPages = registry.getAmountOfPages(location);
- this.guiSize = YamlSettingsContainers.getGuiSize(registry, location, registry.getCurrentUpdate(location));
- }
- /**
- * Get size some are set inside yml file.
- *
- * @return return int value of invetory size.
- */
- public int getguiSize() {
- return this.guiSize;
- }
- /**
- * Create inventory if not exist and add items if
- * the gui has items before.
- *
- * @return the gui player open.
- */
- @Override
- public abstract Inventory findAndLoadInventory();
- /**
- * @return return inventory or null if it allredy created.
- */
- @Override
- public abstract Inventory setInventoryHopper();
- /**
- * Set new page if it not exist.
- *
- * @param type set type of inventory or null if
- * you want chest gui with size.
- * @param size size of the inventory.
- * @param title The description of the gui
- * @return return array of invetorys, on specific cordinats.
- */
- public Inventory setPage(InventoryType type, int size, String title) {
- ArrayList<Inventory> inventorys = cachedInventories.get(this.location);
- ArrayList<Inventory> inv = new ArrayList<>();
- Inventory newInventory = createInventory(null, title, null);
- if (inventorys == null)
- for (int i = 0; i < numberOfPages; i++) {
- inv.add(createInventory(null, title, null));
- }
- else {
- inv.addAll(inventorys);
- inv.add(newInventory);
- }
- cachedInventories.override(this.location, inv);
- return inv.get(0);
- }
- /**
- * Create an inventory for containers.
- *
- * @param inventoryType Set type of container like chest or hopper
- * set it to null to generate chest gui with size.
- * @param title set the title on the gui some be created.
- * @param inventorySize set inventory size, as defult it will take settings from containers yml files.
- * @return A inventory with your settings.
- */
- @Override
- public Inventory createInventory(InventoryType inventoryType, String title, Integer inventorySize) {
- if (inventoryType != null)
- return Bukkit.createInventory(null, inventoryType, title);
- return Bukkit.createInventory(null, inventorySize == null || inventorySize < 9 ? this.guiSize : inventorySize, title);
- }
- /**
- * Create an inventory for containers.
- *
- * @param inventoryType Set type of container like chest or hopper
- * set it to null to generate chest gui with size.
- * @param title set the title on the gui some be created.
- * @param inventorySize set inventory size, as defult it will take settings from containers yml files.
- * @return A inventory with your settings.
- */
- @Override
- public Inventory createInventory(InventoryType inventoryType, Component title, Integer inventorySize) {
- /*
- if (inventoryType != null)
- return Bukkit.createInventory(null, inventoryType, title);
- return Bukkit.createInventory(null, inventorySize == null || inventorySize < 9 ? this.guiSize : inventorySize, title);*/
- return null;
- }
- /**
- * Get the page some has items, if one page
- * are emty it will check next page for items
- * or the page befor if it has items.
- * <p>
- * <p>
- * If inventory not exist, it will create new one automatic
- * with right amount of pages.
- *
- * @param registry get data from chache
- * @return inventory with items.
- */
- @Override
- public Inventory getEmptyPages(ChestRegistry registry) {
- Inventory inventory = null;
- YamlSettingsContainers settings = new YamlSettingsContainers();
- if (registry.getAmountOfPages(this.location) > 0) {
- ArrayList<Inventory> inventoriesList = getInventory(this.location);
- if (inventoriesList == null)
- setInventoryHopper();
- else {
- inventory = inventoriesList.get(0);
- }
- if (inventoriesList != null)
- for (int i = 0; i < inventoriesList.size(); i++)
- if (inventory.isEmpty() && i < settings.getMaxamountOfPages(registry.getContainerFileName(this.location), registry.getCurrentUpdate(this.location))) {
- if (i < registry.getAmountOfPages(location))
- if (inventoriesList.size() > i) {
- inventory = inventoriesList.get(i);
- } else
- setInventoryHopper();
- }
- }
- return inventory;
- }
- /**
- * Handle inventory clickevent. For ether when player add/remove items
- * or change page.
- * See to see how it is set up for Storage Unit and rest of the chest use the defult one in See also.
- *
- * @param registry get data from chache.
- * @param upgradePages gui you open to upgrade number of pages.
- * @param location location of the container.
- * @param event the event.
- * @param player player some interact with the chest.
- * @return return true if player click inside inventory or false if he not clickinside right inventory.
- * @see org.broken.cheststorage.inventoryholders.InventoryHolderStorageUnit#onClickingInsideGui(ChestRegistry, GuiUpgradePages, Location, InventoryClickEvent, Player)
- * @see org.broken.cheststorage.inventoryholders.InventoryHolderDefultchest#onClickingInsideGui(ChestRegistry, GuiUpgradePages, Location, InventoryClickEvent, Player)
- */
- @Override
- public abstract boolean onClickingInsideGui(ChestRegistry registry, GuiUpgradePages upgradePages, Location location, InventoryClickEvent event, Player player);
- public void removeInventory(Location location) {
- if (cachedInventories.get(location) != null) {
- cachedInventories.remove(location);
- }
- }
- /**
- * @return count number of itemstacks and return
- * a number of stacks some you put in.
- */
- @Override
- public int countItemStacks(ItemStack[] itemStacks) {
- if (itemStacks == null)
- return 0;
- return itemStacks.length;
- }
- /**
- * Set items in the chest on this cords,
- * if you have more an one page, it will
- * set items on this too (if a slot not has items
- * it will return air or null (slots will be empty)).
- *
- * @param location where the container are placed.
- */
- @Override
- public abstract void setContents(Location location);
- /**
- * Get the page you want to open.
- *
- * @param page get the page of this container.
- * @return inventory on specific page
- */
- @Override
- public Inventory getPage(Location location, int page) {
- ArrayList<Inventory> inventories = cachedInventories.get(location);
- if (inventories == null)
- return null;
- return page < 0 || page >= inventories.size() ? null : inventories.get(page);
- }
- /**
- * Get number of the gui player has curent
- * open.
- *
- * @param inventory inventory you looking for
- * @return the page of the inventory you current has open.
- */
- @Override
- public int getPageIndex(Inventory inventory) {
- ArrayList<Inventory> inventories = cachedInventories.get(this.location);
- if (inventories != null)
- for (int i = 0; i < inventories.size(); i++) {
- if (inventories.get(i).equals(inventory))
- return i;
- }
- return 0;
- }
- /**
- * Get the contents in the chest.
- *
- * @param location location for the inventory you has close
- * @return items from curent gui you close, will save all pages.
- */
- @Override
- public abstract ItemStack[] getContents(Location location);
- /**
- * Add items to the chest and change page if first is full.
- * If chest are full it will return the items some not fit.
- *
- * @param itemStacks items hopper try to add
- * @return items some not fit in the gui.
- */
- @Override
- public Map<Integer, ItemStack> addItems(ItemStack... itemStacks) {
- Map<Integer, ItemStack> additionalItems = new HashMap<>();
- Map<Integer, ItemStack> itemAdditionalItems = new HashMap<>();
- for (ItemStack itemStack : itemStacks) {
- if (itemStack != null) {
- int currentInventory = 0;
- do {
- Inventory inventory = getPage(this.location, currentInventory);
- if (inventory != null) {
- itemAdditionalItems = inventory.addItem(itemStack);
- }
- currentInventory++;
- } while (!itemAdditionalItems.isEmpty() && currentInventory < getPagesAmount(this.location));
- additionalItems.putAll(itemAdditionalItems);
- }
- }
- return additionalItems;
- }
- /**
- * Check if chest are full.
- *
- * @param itemStack items some you add to the chest
- * @return true if chest are not full.
- */
- @Override
- public boolean checkIfChestFull(ItemStack itemStack, Inventory from) {
- boolean checkitems = addItems(itemStack).isEmpty();
- if (!checkitems) {
- ifChestFull = true;
- from.addItem(itemStack);
- return false;
- }
- ifChestFull = false;
- return true;
- }
- /**
- * Get the number of pages for one container.
- *
- * @return get number of pages the inventory has.
- */
- @Override
- public int getPagesAmount(Location location) {
- if (cachedInventories.get(location) != null)
- return cachedInventories.get(location).size();
- return 0;
- }
- /**
- * Get a Inventory for a container.
- *
- * @param location location of the inventor want to find.
- * @return return inventoy on location you has requested
- * or null if it not find the gui.
- */
- @Override
- public ArrayList<Inventory> getInventory(Location location) {
- return cachedInventories.get(location);
- }
- /**
- * Save the items to cache.
- *
- * @param player get the player some close the gui.
- */
- @Override
- public void handleInventoryClose(Player player) {
- MetadataEnumsForGui locations = MetadataEnumsForGui.CHEST_METADATA;
- ChestRegistry registry = ChestRegistry.getInstance();
- Location chestLocation = SerializeUtil.deserializeLocation(CompMetadata.getMetadata(player, locations.toString()));
- registry.saveChestItemsFromHopper(chestLocation, getContents(chestLocation), registry.getPlayerUUID(chestLocation));
- }
- /**
- * When you break container it collect all pages of items
- * and drop it on the ground.
- *
- * @param event the event some get trigged when you break a container.
- * @return return true after it has finished.
- */
- @Override
- public boolean dropItemsOnBlockBreak(BlockBreakEvent event) {
- Location loc = event.getBlock().getLocation();
- for (int page = 0; page < getPagesAmount(loc); page++) {
- //if (getPage(page, loc) != null) {
- Inventory inventory = getPage(loc, page);
- if (inventory != null) {
- for (ItemStack itemStack : inventory.getContents())
- if (itemStack != null && itemStack.getType() != Material.AIR) {
- event.getBlock().getWorld().dropItemNaturally(loc, itemStack);
- }
- inventory.clear();
- }
- Common.runLater(10, () -> removeInventory(loc));
- //ChestRegistry.getInstance().removeContainer(loc);
- }
- return true;
- }
- /**
- * Drop items 1 stack at the time, if you try drop 128 items
- * they become invisible (but can be picked up). so to avoid
- * this it will drop a maximum of 64 items at the same tick.
- *
- * @param itemStack add itemstacks some get converted to 64 items at max
- * or the items max stack size.
- */
- @Override
- public void dropItemsOnGround(ItemStack itemStack) {
- }
- /**
- * When you break the continer it check what type of container
- * and will return back right type of container and remove from
- * cache both the inventory (if it exist) and the database.
- *
- * @param event the event some get trigged when you break the chest.
- */
- @Override
- public void onContainerBreak(BlockBreakEvent event) {
- ChestRegistry register = ChestRegistry.getInstance();
- Location location = event.getBlock().getLocation();
- Material blocktype = event.getBlock().getType();
- YamlSettingsContainers settings = new YamlSettingsContainers();
- if (register.isRegistered(location) && !event.isCancelled())
- if (event.getBlock().getType() == Material.CHEST || event.getBlock().getType() == Material.BARREL || event.getBlock().getType() == Material.HOPPER) {
- ItemUtily itemUtily = new ItemUtily();
- Map<String, String> metadata = new HashMap<>();
- event.setDropItems(false);
- metadata.put("CHEST", String.valueOf(register.getTypeofContainer(location)));
- metadata.put("FILETYPE", register.getContainerFileName(location));
- metadata.put("TYPEOFLEVEL", register.getCurrentUpdate(location));
- ItemStack itemStack = itemUtily.createItemStack(blocktype, settings.getDisplayName(register.getContainerFileName(location), register.getCurrentUpdate(location)),
- settings.translateColorcodesLore(register.getContainerFileName(location), register.getCurrentUpdate(location)), metadata);
- if (this.player != null) {
- HashMap<Integer, ItemStack> ItemIfdropOrNot = this.player.getInventory().addItem(itemStack);
- if (ItemIfdropOrNot.size() > 0) {
- event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), itemStack);
- }
- } else
- event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), itemStack);
- if (register.getTypeofContainer(location) != null)
- switch (register.getTypeofContainer(location)) {
- case LINKEDCONTAINER:
- register.removeContainer(location);
- break;
- case PAGEDCONTAINER:
- case CRAFTINGCONTAINER:
- case SELLCHEST:
- InventoryHolderDefultchest blockbreak = new InventoryHolderDefultchest(location, this.player);
- blockbreak.dropItemsOnBlockBreak(event);
- register.removeContainer(location);
- break;
- case STORAGEUNIT:
- InventoryHolderStorageUnit blockbreaks = new InventoryHolderStorageUnit(location, this.player);
- blockbreaks.dropItemsOnBlockBreak(event);
- register.removeContainer(location);
- MetadataEnumsForGui metadatachest = MetadataEnumsForGui.CHEST_METADATA;
- if (this.player != null && this.player.hasMetadata(metadatachest.toString()))
- this.player.removeMetadata(metadatachest.toString(), metadatachest.getPlugin());
- break;
- default:
- System.out.println("This should never happen, what has you done with my plugin??!!!!!");
- }
- }
- }
- /**
- * Set the page player will open.
- *
- * @param player set the player.
- * @param pageNumber set pagenumber player shall open.
- */
- @Override
- public void setPlayerViwePage(Player player, int pageNumber) {
- wiversOfPage.override(player.getUniqueId(), pageNumber);
- }
- /**
- * Get what page a player curently has open
- * (will not say if he has the inventory open or not).
- *
- * @param player get the player some open the gui.
- */
- @Override
- public int getPlayerViwePage(Player player) {
- return wiversOfPage.get(player.getUniqueId());
- }
- /**
- * Method to see if cache are empty.
- *
- * @param player check if the cache are empty.
- * @return true if player exist.
- */
- @Override
- public boolean PlayerViwePageEmpty(Player player) {
- return wiversOfPage.contains(player.getUniqueId());
- }
- /**
- * Remove player from cache.
- *
- * @param player remove the players cache data for page.
- */
- @Override
- public void removePlayerViwePage(Player player) {
- wiversOfPage.remove(player.getUniqueId());
- }
- /**
- * Update the inventory title for container.
- *
- * @param amounts amount of items inside the chest.
- * @param cursor items player add to the chest.
- */
- @Override
- public void updateInventoryTitle(BigInteger amounts, ItemStack cursor) {
- }
- public void setPlayerLocation(Location location, Player player) {
- ChestRegistry registry = ChestRegistry.getInstance();
- if (location != null)
- this.location = location;
- if (player != null)
- this.player = player;
- this.numberOfPages = registry.getAmountOfPages(location);
- this.guiSize = new YamlSettingsContainers().getGuiSize(registry, location, registry.getCurrentUpdate(location));
- }
- protected Location getLocation() {
- return this.location;
- }
- protected Player getPlayer() {
- return this.player;
- }
- }
Add Comment
Please, Sign In to add comment