Shin1gamiX

VoidChest Sell Logic Class

Jul 14th, 2020 (edited)
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.75 KB | None | 0 0
  1. public abstract class AVoidChestEconomy implements IVoidEconomy {
  2.  
  3.     private final PluginDescriptionFile desc;
  4.  
  5.     protected AVoidChestEconomy(final PluginDescriptionFile desc) {
  6.         this.desc = desc;
  7.  
  8.     }
  9.  
  10.     @Override
  11.     public double getProfit(ItemStack item) {
  12.         return getProfit(item, 1);
  13.     }
  14.  
  15.     @Override
  16.     public double getProfit(VoidStorage voidChest, ItemStack item, int amount) {
  17.         return getProfit(item, amount);
  18.     }
  19.  
  20.     @Override
  21.     public void initiateSell(final String voidChestType) {
  22.  
  23.         PlayerDataManager.getInstance().getPlayerDatas().values()
  24.                 .forEach(data -> this.initiateSell(data, voidChestType));
  25.  
  26.     }
  27.  
  28.     @Override
  29.     public void initiateSell(final PlayerData data, final String voidChestType) {
  30.  
  31.         final List<VoidStorage> loop = data.getVoidStorages();
  32.         if (loop.isEmpty()) {
  33.             return;
  34.         }
  35.  
  36.         /* Only runs if the user actually has at least 1 voidchest. */
  37.         double totalMoneyGained = 0;
  38.         double totalBankMoneyGained = 0;
  39.         int totalItemsSold = 0;
  40.         int totalItemsPurged = 0;
  41.  
  42.         final OfflinePlayer off = data.getOwner();
  43.         final boolean isOnline = off.isOnline();
  44.  
  45.         final FileConfiguration opt = FileManager.getInstance().getOptions().getFileConfiguration();
  46.  
  47.         final boolean generalEnableWhenOffline = opt.getBoolean("Player.voidchest.enable-when-owner-offline", false);
  48.  
  49.         boolean shouldUpdateData = false;
  50.  
  51.         boolean proceed = false;
  52.  
  53.         for (VoidStorage voidStorage : loop) {
  54.  
  55.             if (voidChestType != null && !voidStorage.getName().equals(voidChestType)) {
  56.                 continue;
  57.             }
  58.  
  59.             final VoidStorageFile cachedFile = VoidStorageFileCacher.getInstance().getCachedStorage(voidStorage);
  60.             if (cachedFile == null) {
  61.                 throw new VoidFileDoesNotExistException(
  62.                         "The voidchest file: " + voidChestType
  63.                                 + " was not found, seems like it was not cached properly.",
  64.                         "Please contact Shin1gamiX.");
  65.             }
  66.             final FileConfiguration voidFile = cachedFile.getFileConfiguration();
  67.  
  68.             if (!isOnline && !voidFile.getBoolean("Mechanics.enable-when-owner-offline", generalEnableWhenOffline)) {
  69.                 continue;
  70.             }
  71.  
  72.             /* Is charge enabled and doesn't it have any "fuel"? */
  73.             VoidStorageCharge charge = voidStorage.getVoidStorageCharge();
  74.             if (charge.isEnabled() && !charge.hasFuel()) {
  75.                 continue;
  76.             }
  77.  
  78.             /* Is the chunk the voidchest located at loaded? */
  79.             /* If not, let's not bother dealing with this. */
  80.             final Location loc = voidStorage.getLocation();
  81.             if (!Utils.isChunkLoaded(loc)) {
  82.                 continue;
  83.             }
  84.  
  85.             /* Is the inventory for whatever the reason null? */
  86.             final Inventory inv = voidStorage.getBlockInventory();
  87.             if (inv == null) {
  88.                 continue;
  89.             }
  90.  
  91.             /* Let's start the sell attempt. */
  92.             VoidSellChestEvent chestSellEvent = new VoidSellChestEvent(voidStorage);
  93.             Bukkit.getPluginManager().callEvent(chestSellEvent);
  94.             if (chestSellEvent.isCancelled()) {
  95.                 continue;
  96.             }
  97.  
  98.  
  99.             double moneyLogged = 0;
  100.  
  101.             proceed = true;
  102.             boolean shouldUpdateMenu = false;
  103.  
  104.             /*
  105.              *
  106.              * Inventory handling.
  107.              *
  108.              */
  109.  
  110.             for (int i = 0; i < inv.getSize(); i++) {
  111.  
  112.                 final ItemStack item = inv.getItem(i);
  113.                 if (item == null || item.getType() == Material.AIR) {
  114.                     continue;
  115.                 }
  116.  
  117.                 double eventProfit = this.getProfit(voidStorage, item, item.getAmount()) * voidStorage.getBooster()
  118.                         * data.getPlayerBoost().getBooster();
  119.  
  120.                 VoidSellItemEvent sellEvent = new VoidSellItemEvent(voidStorage, item, inv, eventProfit, i);
  121.                 Bukkit.getPluginManager().callEvent(sellEvent);
  122.                 if (sellEvent.isCancelled()) {
  123.                     continue;
  124.                 }
  125.  
  126.                 int itemAmount = item.getAmount();
  127.  
  128.                 if (item.getType() == Material.TNT) {
  129.                     final BankTNTManager bankInstance = BankTNTManager.getInstance();
  130.                     final IVoidChestBankTNT bank = bankInstance.getBank();
  131.                     if (bank != null) {
  132.                         if (bank.depositToBank(itemAmount, data.getUuid())) {
  133.                             inv.clear(i);
  134.                             continue;
  135.                         }
  136.                     } else {
  137.                         Utils.debug(true,
  138.                                 "The bank instance is null, something went wrong? Please contact Shin1gamiX!");
  139.                     }
  140.                 }
  141.  
  142.                 final double profit = sellEvent.getPrice();
  143.  
  144.                 /* If it is planned for purging. */
  145.                 if (profit <= 0d) {
  146.  
  147.                     if (VoidChestOption.PURGE.isEnabled(voidStorage)
  148.                             && voidStorage.getVoidStorageAbilities().isPurgeInvalidItems()) {
  149.                         totalItemsPurged += itemAmount;
  150.                         voidStorage.getStats().setItemsPurged(voidStorage.getStats().getItemsPurged() + itemAmount);
  151.                         inv.clear(i);
  152.                         if (!shouldUpdateMenu)
  153.                             shouldUpdateMenu = true;
  154.                     }
  155.                     continue;
  156.  
  157.                 }
  158.  
  159.                 /* It must be planned for selling then. */
  160.                 if (VoidChestOption.AUTOSELL.isEnabled(voidStorage)
  161.                         && voidStorage.getVoidStorageAbilities().isAutoSell()) {
  162.                     totalItemsSold += itemAmount;
  163.  
  164.                     if (voidStorage.getVoidStorageAbilities().isBank() && VoidChestOption.BANK.isEnabled(voidStorage)) {
  165.                         totalBankMoneyGained += profit;
  166.                     } else {
  167.                         totalMoneyGained += profit;
  168.                     }
  169.  
  170.                     moneyLogged += profit;
  171.                     voidStorage.getStats().setItemsSold(voidStorage.getStats().getItemsSold() + itemAmount);
  172.                     voidStorage.getStats().setMoney(voidStorage.getStats().getMoney() + profit);
  173.                     inv.clear(i);
  174.                     if (!shouldUpdateMenu)
  175.                         shouldUpdateMenu = true;
  176.                 }
  177.             }
  178.  
  179.             /*
  180.              *
  181.              * Chunk collector handling.
  182.              *
  183.              */
  184.  
  185.             if (VoidChestOption.CHUNK_COLLECTOR.isEnabled(voidStorage)
  186.                     && voidStorage.getVoidStorageAbilities().isChunkCollector()) {
  187.                 for (final Item itemEntity : Arrays.stream(loc.getChunk().getEntities())
  188.                         .filter(entity -> entity instanceof Item).map(Item.class::cast).collect(Collectors.toList())) {
  189.  
  190.                     if (itemEntity.isDead()) {
  191.                         continue;
  192.                     }
  193.  
  194.                     final ItemStack stack = itemEntity.getItemStack();
  195.  
  196.  
  197.                     if (VoidStorageIgnoreList.getInstance().getChunkIgnoreList().contains(Utils.resetItemMeta(stack))) {
  198.                         continue;
  199.                     }
  200.  
  201.                     if (!opt.getBoolean("chunk-collector.ignore-item-meta", false) && stack.hasItemMeta()) {
  202.                         continue;
  203.                     }
  204.  
  205.                     final int amount = StackerManager.getInstance().getStacker().getActualAmount(itemEntity);
  206.  
  207.                     double eventProfit = this.getProfit(voidStorage, stack, amount) * voidStorage.getBooster()
  208.                             * data.getPlayerBoost().getBooster();
  209.  
  210.                     VoidSellChunkItemEvent sellEvent = new VoidSellChunkItemEvent(voidStorage, itemEntity, amount,
  211.                             eventProfit);
  212.                     Bukkit.getPluginManager().callEvent(sellEvent);
  213.                     if (sellEvent.isCancelled()) {
  214.                         continue;
  215.                     }
  216.  
  217.                     final double profit = sellEvent.getPrice();
  218.                     final int itemAmount = sellEvent.getItemAmount();
  219.  
  220.  
  221.                     final IVoidChestBankTNT bank = BankTNTManager.getInstance().getBank();
  222.                     if (stack.getType() == Material.TNT
  223.                             && bank != null && bank.depositToBank(itemAmount, data.getUuid())) {
  224.                         itemEntity.remove();
  225.                         continue;
  226.                     }
  227.  
  228.  
  229.  
  230.                     /* If it is planned for purging. */
  231.                     if (profit <= 0d && VoidChestOption.PURGE.isEnabled(voidStorage)
  232.                             && voidStorage.getVoidStorageAbilities().isPurgeInvalidItems()) {
  233.                         totalItemsPurged += itemAmount;
  234.                         voidStorage.getStats().setItemsPurged(voidStorage.getStats().getItemsPurged() + itemAmount);
  235.                         itemEntity.remove();
  236.                         if (!shouldUpdateMenu)
  237.                             shouldUpdateMenu = true;
  238.  
  239.  
  240.                     } else if (profit > 0d && VoidChestOption.AUTOSELL.isEnabled(voidStorage)
  241.                             && voidStorage.getVoidStorageAbilities().isAutoSell() && voidFile.getBoolean("chunk-collector.sell-items", true)) {
  242.                         totalItemsSold += itemAmount;
  243.                         if (voidStorage.getVoidStorageAbilities().isBank()
  244.                                 && VoidChestOption.BANK.isEnabled(voidStorage)) {
  245.                             totalBankMoneyGained += profit;
  246.                         } else {
  247.                             totalMoneyGained += profit;
  248.                         }
  249.  
  250.                         moneyLogged += profit;
  251.  
  252.                         voidStorage.getStats().setItemsSold(voidStorage.getStats().getItemsSold() + itemAmount);
  253.                         voidStorage.getStats().setMoney(voidStorage.getStats().getMoney() + profit);
  254.                         itemEntity.remove();
  255.                         if (!shouldUpdateMenu)
  256.                             shouldUpdateMenu = true;
  257.                     } else {
  258.                         if (!opt.getBoolean("chunk-collector.transfer-non-sellables", true)) {
  259.                             continue;
  260.                         }
  261.  
  262.                         /* Dropped items, every single one of them together. */
  263.                         final ItemStack[] toTransferItems = Utils.getItems(stack, itemAmount);
  264.  
  265.                         /*
  266.                          * Attempt to add all items in chest. Whatever is left, it means the dropped
  267.                          * item needs to be updated.
  268.                          */
  269.                         final Map<Integer, ItemStack> leftOvers = inv.addItem(toTransferItems);
  270.  
  271.                         /* No leftovers, means that all items were added, clear the entity. */
  272.                         if (leftOvers.isEmpty()) {
  273.                             itemEntity.remove();
  274.                             continue;
  275.                         }
  276.  
  277.                         /* Let's count how many items are left. */
  278.                         int countLeft = 0;
  279.                         for (final ItemStack loopLeft : leftOvers.values()) {
  280.                             final int leftAmount = loopLeft.getAmount();
  281.                             countLeft += leftAmount;
  282.                         }
  283.  
  284.                         /* No room at all, oh well, let's continue. */
  285.                         if (countLeft == itemAmount) {
  286.                             continue;
  287.                         }
  288.  
  289.                         /* Update the actual item accordingly. */
  290.                         StackerManager.getInstance().getStacker().updateItem(itemEntity, countLeft);
  291.                     }
  292.                 }
  293.             }
  294.  
  295.             if (shouldUpdateMenu) {
  296.                 voidStorage.getInventoryHandler().updateMenuItems(true);
  297.                 if (!shouldUpdateData) {
  298.                     shouldUpdateData = true;
  299.                 }
  300.             }
  301.  
  302.             if (opt.getBoolean("Mechanics.money-logging.enabled", false) && moneyLogged > opt.getDouble("Mechanics.money-logging.minimum-amount", 0)) {
  303.                 final Map<String, String> replace = Maps.newHashMap();
  304.                 replace.put("%location_world%", loc.getWorld().getName());
  305.                 replace.put("%location_x%", String.valueOf(loc.getBlockX()));
  306.                 replace.put("%location_y%", String.valueOf(loc.getBlockY()));
  307.                 replace.put("%location_z%", String.valueOf(loc.getBlockZ()));
  308.                 replace.put("%player%", voidStorage.getLeaderName());
  309.                 replace.put("%amount%", Utils.formatNumber(moneyLogged));
  310.                 MessagesUtil.VOIDCHEST_MONEY_LOGGING.msg(Bukkit.getConsoleSender(), replace, false);
  311.             }
  312.  
  313.         }
  314.  
  315.         if (!proceed) {
  316.             return;
  317.         }
  318.  
  319.         final PlayerDataSellStats dataSell = data.getPlayerDataSellStats();
  320.         dataSell.addItemsPurgedStored(totalItemsPurged);
  321.         dataSell.addItemsSoldStored(totalItemsSold);
  322.         dataSell.addMoneyStored(totalMoneyGained + totalBankMoneyGained);
  323.  
  324.         if (totalItemsSold <= 0 && totalItemsPurged <= 0) {
  325.             return;
  326.         }
  327.  
  328.         if (totalItemsSold >= 1) {
  329.  
  330.             if (!BankManager.getInstance().getBank().depositToBank(totalBankMoneyGained, data)) {
  331.                 totalMoneyGained += totalBankMoneyGained;
  332.                 final IEconomy econ = EconomyManager.getInstance().getEconomy();
  333.                 if (econ != null && !econ.deposit(off, totalMoneyGained)) {
  334.                     Utils.debug(true, "Something went wrong with the payment of: " + off.getName() + " with uuid:"
  335.                             + off.getUniqueId() + " for an amount of:" + totalMoneyGained);
  336.                 }
  337.  
  338.             }
  339.  
  340.         }
  341.  
  342.         if (shouldUpdateData) {
  343.             data.loadStatsToFile(false);
  344.         }
  345.  
  346.         // this.informUser(data, totalMoneyGained, totalItemsSold, totalItemsPurged,
  347.         // isOnline);
  348.  
  349.     }
  350.  
  351.     @Override
  352.     public void initiateSell(final PlayerData data) {
  353.         this.initiateSell(data, null);
  354.     }
  355.  
  356.     @Override
  357.     public void initiateSell() {
  358.         this.initiateSell((String) null);
  359.     }
  360.  
  361.     @Override
  362.     public String getName() {
  363.         final String simpleName = this.getClass().getSimpleName();
  364.         return simpleName + "{name=" + desc.getName() + ", version=" + desc.getVersion() + '}';
  365.     }
  366.  
  367.     @Override
  368.     public boolean isVaultDependent() {
  369.         return true;
  370.     }
  371.  
  372. }
Add Comment
Please, Sign In to add comment