broken-arrow

Untitled

May 16th, 2021
687
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public abstract class WChest extends DatabaseObject implements Chest {
  2.  
  3.     protected static final WildChestsPlugin plugin = WildChestsPlugin.getPlugin();
  4.     public static final Map<UUID, Chest> viewers = Maps.newHashMap();
  5.     public static Inventory guiConfirm;
  6.     public static String guiConfirmTitle;
  7.  
  8.     protected final UUID placer;
  9.     protected final Location location;
  10.     protected final ChestData chestData;
  11.  
  12.     protected TileEntityContainer tileEntityContainer;
  13.     protected boolean removed = false;
  14.  
  15.     public WChest(UUID placer, Location location, ChestData chestData) {
  16.         this.placer = placer;
  17.         this.location = location.clone();
  18.         this.chestData = chestData;
  19.     }
  20.  
  21.     /* CHEST RELATED METHODS */
  22.  
  23.     @Override
  24.     public UUID getPlacer() {
  25.         return placer;
  26.     }
  27.  
  28.     @Override
  29.     public Location getLocation() {
  30.         return location.clone();
  31.     }
  32.  
  33.     @Override
  34.     public ChestData getData() {
  35.         return chestData;
  36.     }
  37.  
  38.     @Override
  39.     public ChestType getChestType() {
  40.         return getData().getChestType();
  41.     }
  42.  
  43.     @Override
  44.     public void remove(){
  45.         plugin.getChestsManager().removeChest(this);
  46.     }
  47.  
  48.     public boolean isRemoved(){
  49.         return removed;
  50.     }
  51.  
  52.     public void markAsRemoved(){
  53.         removed = true;
  54.     }
  55.  
  56.     /* INVENTORIES / PAGES RELATED METHODS */
  57.  
  58.     @Override
  59.     public ItemStack[] getContents(){
  60.         WildItemStack<?, ?>[] originalContents = getWildContents();
  61.         ItemStack[] contents = new ItemStack[originalContents.length];
  62.  
  63.         for(int i = 0; i < contents.length; i++)
  64.             contents[i] = originalContents[i].getCraftItemStack();
  65.  
  66.         return contents;
  67.     }
  68.  
  69.     public abstract WildItemStack<?, ?>[] getWildContents();
  70.  
  71.     @Override
  72.     public Map<Integer, ItemStack> addItems(ItemStack... itemStacks) {
  73.         Map<Integer, ItemStack> additionalItems = new HashMap<>();
  74.         Map<Integer, ItemStack> itemAdditionalItems = new HashMap<>();
  75.  
  76.         for(ItemStack itemStack : itemStacks) {
  77.             if (itemStack != null) {
  78.                 int currentInventory = 0;
  79.  
  80.                 do {
  81.                     Inventory inventory = getPage(currentInventory);
  82.                     if(inventory != null) {
  83.                         itemAdditionalItems = inventory.addItem(itemStack);
  84.                     }
  85.                     currentInventory++;
  86.                 } while (!itemAdditionalItems.isEmpty() && currentInventory < getPagesAmount());
  87.  
  88.                 additionalItems.putAll(itemAdditionalItems);
  89.             }
  90.         }
  91.  
  92.         return additionalItems;
  93.     }
  94.  
  95.     @Override
  96.     public Map<Integer, ItemStack> addRawItems(ItemStack... itemStacks) {
  97.         return addItems(itemStacks);
  98.     }
  99.  
  100.     @Override
  101.     public void removeItem(int amountToRemove, ItemStack itemStack) {
  102.         Inventory[] pages = getPages();
  103.  
  104.         int itemsRemoved = 0;
  105.  
  106.         for(int i = 0; i < pages.length && itemsRemoved < amountToRemove; i++){
  107.             Inventory page = pages[i];
  108.             int toRemove = Math.min(amountToRemove - itemsRemoved, ItemUtils.countItems(itemStack, page));
  109.             ItemStack cloned = itemStack.clone();
  110.             cloned.setAmount(toRemove);
  111.             page.removeItem(cloned);
  112.         }
  113.     }
  114.  
  115.     @Override
  116.     public ItemStack getItem(int i){
  117.         return getWildItem(i).getCraftItemStack();
  118.     }
  119.  
  120.     public abstract WildItemStack<?, ?> getWildItem(int i);
  121.  
  122.     @Override
  123.     public void setItem(int i, ItemStack itemStack){
  124.         setItem(i, WildItemStack.of(itemStack));
  125.     }
  126.  
  127.     public abstract void setItem(int i, WildItemStack<?, ?> itemStack);
  128.  
  129.     @Override
  130.     public abstract Inventory getPage(int page);
  131.  
  132.     @Override
  133.     public abstract Inventory[] getPages();
  134.  
  135.     @Override
  136.     public void setPage(int page, Inventory inventory) {
  137.         setPage(page, new InventoryHolder(null, inventory.getContents()));
  138.     }
  139.  
  140.     @Override
  141.     public abstract Inventory setPage(int page, int size, String title);
  142.  
  143.     public void setPage(int page, InventoryHolder inventoryHolder){
  144.         Inventory inventory = setPage(page, inventoryHolder.getSize(), inventoryHolder.getTitle());
  145.         inventory.setContents(inventoryHolder.getContents());
  146.     }
  147.  
  148.     @Override
  149.     public void openPage(Player player, int page) {
  150.         viewers.put(player.getUniqueId(), this);
  151.         plugin.getNMSInventory().openPage(player, (CraftWildInventory) getPage(page));
  152.     }
  153.  
  154.     @Override
  155.     public void closePage(Player player) {
  156.         viewers.remove(player.getUniqueId());
  157.     }
  158.  
  159.     @Override
  160.     public abstract int getPagesAmount();
  161.  
  162.     @Override
  163.     public abstract int getPageIndex(Inventory inventory);
  164.  
  165.     /* BLOCK-ACTIONS RELATED METHODS */
  166.  
  167.     @Override
  168.     public boolean onBreak(BlockBreakEvent event){
  169.         Location loc = getLocation();
  170.         for(int page = 0; page < getPagesAmount(); page++){
  171.             Inventory inventory = getPage(page);
  172.             for(ItemStack itemStack : inventory.getContents())
  173.                 if (itemStack != null && itemStack.getType() != Material.AIR) {
  174.                     ItemUtils.dropOrCollect(event.getPlayer(), itemStack, getData().isAutoCollect(), loc);
  175.                 }
  176.             inventory.clear();
  177.         }
  178.  
  179.         viewers.entrySet().removeIf(entry -> entry.getValue().equals(this));
  180.  
  181.         return true;
  182.     }
  183.  
  184.     @Override
  185.     public boolean onOpen(PlayerInteractEvent event){
  186.         viewers.put(event.getPlayer().getUniqueId(), this);
  187.         tileEntityContainer.openContainer(event.getPlayer());
  188.         return true;
  189.     }
  190.  
  191.     @Override
  192.     public boolean onClose(InventoryCloseEvent event) {
  193.         //Checking if player is buying new page
  194.         if(InventoryListener.buyNewPage.containsKey(event.getPlayer().getUniqueId()))
  195.             return false;
  196.  
  197.         viewers.remove(event.getPlayer().getUniqueId());
  198.         tileEntityContainer.closeContainer(event.getPlayer());
  199.  
  200.         //Checking that it's the last player that views the inventory
  201.         return getViewersAmount(event.getViewers()) <= 1;
  202.     }
  203.  
  204.     @Override
  205.     public boolean onInteract(InventoryClickEvent event) {
  206.         if(event.getSlotType() != InventoryType.SlotType.OUTSIDE)
  207.             return false;
  208.  
  209.         ChestData chestData = getData();
  210.         int index = getPageIndex(event.getWhoClicked().getOpenInventory().getTopInventory());
  211.  
  212.         if(event.getClick() == ClickType.LEFT){
  213.             //Making sure he's not in the first page
  214.             if(index != 0){
  215.                 //movingBetweenPages.add(event.getWhoClicked().getUniqueId());
  216.                 openPage((Player) event.getWhoClicked(), index - 1);
  217.                 //movingBetweenPages.remove(event.getWhoClicked().getUniqueId());
  218.             }
  219.         }
  220.  
  221.         else if(event.getClick() == ClickType.RIGHT){
  222.             //Making sure it's not the last page
  223.             if(index + 1 < getPagesAmount()){
  224.                 //movingBetweenPages.add(event.getWhoClicked().getUniqueId());
  225.                 openPage((Player) event.getWhoClicked(), index + 1);
  226.             }
  227.  
  228.             //Making sure next page is purchasble
  229.             else if(chestData.getPagesData().containsKey(++index + 1)){
  230.                 InventoryData inventoryData = chestData.getPagesData().get(index + 1);
  231.                 InventoryListener.buyNewPage.put(event.getWhoClicked().getUniqueId(), inventoryData);
  232.  
  233.                 if(plugin.getSettings().confirmGUI){
  234.                     event.getWhoClicked().openInventory(guiConfirm);
  235.                 }else {
  236.                     Locale.EXPAND_CHEST.send(event.getWhoClicked(), inventoryData.getPrice());
  237.                     event.getWhoClicked().closeInventory();
  238.                 }
  239.             }
  240.         }
  241.  
  242.         return true;
  243.     }
  244.  
  245.     @Override
  246.     public boolean onPlace(BlockPlaceEvent event) {
  247.         throw new UnsupportedOperationException("onPlace for chests is not supported anymore.");
  248.     }
  249.  
  250.     @Override
  251.     public boolean onHopperMove(InventoryMoveItemEvent event) {
  252.         throw new UnsupportedOperationException("onHopperMove for chests is not supported anymore.");
  253.     }
  254.  
  255.     @Override
  256.     public boolean onHopperMove(ItemStack itemStack, Hopper hopper) {
  257.         throw new UnsupportedOperationException("onHopperMove for chests is not supported anymore.");
  258.     }
  259.  
  260.     @Override
  261.     public boolean onHopperItemTake(Inventory hopperInventory) {
  262.         throw new UnsupportedOperationException("onHopperItemTake for chests is not supported anymore.");
  263.     }
  264.  
  265.     @Override
  266.     public boolean canTakeItemThroughFace(int slot, ItemStack itemStack) {
  267.         ChestData chestData = getData();
  268.         return !chestData.isAutoCrafter() || !chestData.isHopperFilter() ||
  269.                 chestData.containsRecipe(itemStack);
  270.     }
  271.  
  272.     @Override
  273.     public boolean canPlaceItemThroughFace(ItemStack itemStack) {
  274.         return true;
  275.     }
  276.  
  277.     @Override
  278.     public int[] getSlotsForFace() {
  279.         return IntStream.range(0, getPage(0).getSize() * getPagesAmount()).toArray();
  280.     }
  281.  
  282.     /* CONTAINER RELATED METHODS */
  283.  
  284.     public TileEntityContainer getTileEntityContainer() {
  285.         return tileEntityContainer;
  286.     }
  287.  
  288.     public void setTileEntityContainer(TileEntityContainer tileEntityContainer) {
  289.         this.tileEntityContainer = tileEntityContainer;
  290.     }
  291.  
  292.     /* DATA RELATED METHODS */
  293.  
  294.     public void loadFromData(ResultSet resultSet) throws SQLException {
  295.         String serialized = resultSet.getString("inventories");
  296.         if(!serialized.isEmpty()) {
  297.             InventoryHolder[] inventories = plugin.getNMSAdapter().deserialze(serialized);
  298.             for (int i = 0; i < inventories.length; i++)
  299.                 setPage(i, inventories[i]);
  300.         }
  301.     }
  302.  
  303.     public void loadFromFile(YamlConfiguration cfg){
  304.         if (cfg.contains("inventory")) {
  305.             ChestData chestData = getData();
  306.             for (String inventoryIndex : cfg.getConfigurationSection("inventory").getKeys(false)) {
  307.                 Inventory inventory = setPage(Integer.parseInt(inventoryIndex), chestData.getDefaultSize(), chestData.getTitle(Integer.parseInt(inventoryIndex) + 1));
  308.                 if(cfg.isConfigurationSection("inventory." + inventoryIndex)){
  309.                     for (String slot : cfg.getConfigurationSection("inventory." + inventoryIndex).getKeys(false)) {
  310.                         try {
  311.                             inventory.setItem(Integer.parseInt(slot), cfg.getItemStack("inventory." + inventoryIndex + "." + slot));
  312.                         } catch (Exception ex) {
  313.                             break;
  314.                         }
  315.                     }
  316.                 }
  317.             }
  318.         }
  319.     }
  320.  
  321.  
  322.     @Override
  323.     public boolean equals(Object o) {
  324.         if (this == o) return true;
  325.         if (o == null || getClass() != o.getClass()) return false;
  326.         WChest wChest = (WChest) o;
  327.         return location.equals(wChest.location);
  328.     }
  329.  
  330.     @Override
  331.     public int hashCode() {
  332.         return Objects.hash(location);
  333.     }
  334.  
  335.     private int getViewersAmount(List<HumanEntity> viewersList){
  336.         int viewers = 0;
  337.  
  338.         for(HumanEntity viewer : viewersList){
  339.             if(viewer.getGameMode() != GameMode.SPECTATOR)
  340.                 viewers++;
  341.         }
  342.  
  343.         return viewers;
  344.     }
  345.  
  346. }
  347.  
RAW Paste Data