Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class AVoidChestEconomy implements IVoidEconomy {
- private final PluginDescriptionFile desc;
- protected AVoidChestEconomy(final PluginDescriptionFile desc) {
- this.desc = desc;
- }
- @Override
- public double getProfit(ItemStack item) {
- return getProfit(item, 1);
- }
- @Override
- public double getProfit(VoidStorage voidChest, ItemStack item, int amount) {
- return getProfit(item, amount);
- }
- @Override
- public void initiateSell(final String voidChestType) {
- PlayerDataManager.getInstance().getPlayerDatas().values()
- .forEach(data -> this.initiateSell(data, voidChestType));
- }
- @Override
- public void initiateSell(final PlayerData data, final String voidChestType) {
- final List<VoidStorage> loop = data.getVoidStorages();
- if (loop.isEmpty()) {
- return;
- }
- /* Only runs if the user actually has at least 1 voidchest. */
- double totalMoneyGained = 0;
- double totalBankMoneyGained = 0;
- int totalItemsSold = 0;
- int totalItemsPurged = 0;
- final OfflinePlayer off = data.getOwner();
- final boolean isOnline = off.isOnline();
- final FileConfiguration opt = FileManager.getInstance().getOptions().getFileConfiguration();
- final boolean generalEnableWhenOffline = opt.getBoolean("Player.voidchest.enable-when-owner-offline", false);
- boolean shouldUpdateData = false;
- boolean proceed = false;
- for (VoidStorage voidStorage : loop) {
- if (voidChestType != null && !voidStorage.getName().equals(voidChestType)) {
- continue;
- }
- final VoidStorageFile cachedFile = VoidStorageFileCacher.getInstance().getCachedStorage(voidStorage);
- if (cachedFile == null) {
- throw new VoidFileDoesNotExistException(
- "The voidchest file: " + voidChestType
- + " was not found, seems like it was not cached properly.",
- "Please contact Shin1gamiX.");
- }
- final FileConfiguration voidFile = cachedFile.getFileConfiguration();
- if (!isOnline && !voidFile.getBoolean("Mechanics.enable-when-owner-offline", generalEnableWhenOffline)) {
- continue;
- }
- /* Is charge enabled and doesn't it have any "fuel"? */
- VoidStorageCharge charge = voidStorage.getVoidStorageCharge();
- if (charge.isEnabled() && !charge.hasFuel()) {
- continue;
- }
- /* Is the chunk the voidchest located at loaded? */
- /* If not, let's not bother dealing with this. */
- final Location loc = voidStorage.getLocation();
- if (!Utils.isChunkLoaded(loc)) {
- continue;
- }
- /* Is the inventory for whatever the reason null? */
- final Inventory inv = voidStorage.getBlockInventory();
- if (inv == null) {
- continue;
- }
- /* Let's start the sell attempt. */
- VoidSellChestEvent chestSellEvent = new VoidSellChestEvent(voidStorage);
- Bukkit.getPluginManager().callEvent(chestSellEvent);
- if (chestSellEvent.isCancelled()) {
- continue;
- }
- double moneyLogged = 0;
- proceed = true;
- boolean shouldUpdateMenu = false;
- /*
- *
- * Inventory handling.
- *
- */
- for (int i = 0; i < inv.getSize(); i++) {
- final ItemStack item = inv.getItem(i);
- if (item == null || item.getType() == Material.AIR) {
- continue;
- }
- double eventProfit = this.getProfit(voidStorage, item, item.getAmount()) * voidStorage.getBooster()
- * data.getPlayerBoost().getBooster();
- VoidSellItemEvent sellEvent = new VoidSellItemEvent(voidStorage, item, inv, eventProfit, i);
- Bukkit.getPluginManager().callEvent(sellEvent);
- if (sellEvent.isCancelled()) {
- continue;
- }
- int itemAmount = item.getAmount();
- if (item.getType() == Material.TNT) {
- final BankTNTManager bankInstance = BankTNTManager.getInstance();
- final IVoidChestBankTNT bank = bankInstance.getBank();
- if (bank != null) {
- if (bank.depositToBank(itemAmount, data.getUuid())) {
- inv.clear(i);
- continue;
- }
- } else {
- Utils.debug(true,
- "The bank instance is null, something went wrong? Please contact Shin1gamiX!");
- }
- }
- final double profit = sellEvent.getPrice();
- /* If it is planned for purging. */
- if (profit <= 0d) {
- if (VoidChestOption.PURGE.isEnabled(voidStorage)
- && voidStorage.getVoidStorageAbilities().isPurgeInvalidItems()) {
- totalItemsPurged += itemAmount;
- voidStorage.getStats().setItemsPurged(voidStorage.getStats().getItemsPurged() + itemAmount);
- inv.clear(i);
- if (!shouldUpdateMenu)
- shouldUpdateMenu = true;
- }
- continue;
- }
- /* It must be planned for selling then. */
- if (VoidChestOption.AUTOSELL.isEnabled(voidStorage)
- && voidStorage.getVoidStorageAbilities().isAutoSell()) {
- totalItemsSold += itemAmount;
- if (voidStorage.getVoidStorageAbilities().isBank() && VoidChestOption.BANK.isEnabled(voidStorage)) {
- totalBankMoneyGained += profit;
- } else {
- totalMoneyGained += profit;
- }
- moneyLogged += profit;
- voidStorage.getStats().setItemsSold(voidStorage.getStats().getItemsSold() + itemAmount);
- voidStorage.getStats().setMoney(voidStorage.getStats().getMoney() + profit);
- inv.clear(i);
- if (!shouldUpdateMenu)
- shouldUpdateMenu = true;
- }
- }
- /*
- *
- * Chunk collector handling.
- *
- */
- if (VoidChestOption.CHUNK_COLLECTOR.isEnabled(voidStorage)
- && voidStorage.getVoidStorageAbilities().isChunkCollector()) {
- for (final Item itemEntity : Arrays.stream(loc.getChunk().getEntities())
- .filter(entity -> entity instanceof Item).map(Item.class::cast).collect(Collectors.toList())) {
- if (itemEntity.isDead()) {
- continue;
- }
- final ItemStack stack = itemEntity.getItemStack();
- if (VoidStorageIgnoreList.getInstance().getChunkIgnoreList().contains(Utils.resetItemMeta(stack))) {
- continue;
- }
- if (!opt.getBoolean("chunk-collector.ignore-item-meta", false) && stack.hasItemMeta()) {
- continue;
- }
- final int amount = StackerManager.getInstance().getStacker().getActualAmount(itemEntity);
- double eventProfit = this.getProfit(voidStorage, stack, amount) * voidStorage.getBooster()
- * data.getPlayerBoost().getBooster();
- VoidSellChunkItemEvent sellEvent = new VoidSellChunkItemEvent(voidStorage, itemEntity, amount,
- eventProfit);
- Bukkit.getPluginManager().callEvent(sellEvent);
- if (sellEvent.isCancelled()) {
- continue;
- }
- final double profit = sellEvent.getPrice();
- final int itemAmount = sellEvent.getItemAmount();
- final IVoidChestBankTNT bank = BankTNTManager.getInstance().getBank();
- if (stack.getType() == Material.TNT
- && bank != null && bank.depositToBank(itemAmount, data.getUuid())) {
- itemEntity.remove();
- continue;
- }
- /* If it is planned for purging. */
- if (profit <= 0d && VoidChestOption.PURGE.isEnabled(voidStorage)
- && voidStorage.getVoidStorageAbilities().isPurgeInvalidItems()) {
- totalItemsPurged += itemAmount;
- voidStorage.getStats().setItemsPurged(voidStorage.getStats().getItemsPurged() + itemAmount);
- itemEntity.remove();
- if (!shouldUpdateMenu)
- shouldUpdateMenu = true;
- } else if (profit > 0d && VoidChestOption.AUTOSELL.isEnabled(voidStorage)
- && voidStorage.getVoidStorageAbilities().isAutoSell() && voidFile.getBoolean("chunk-collector.sell-items", true)) {
- totalItemsSold += itemAmount;
- if (voidStorage.getVoidStorageAbilities().isBank()
- && VoidChestOption.BANK.isEnabled(voidStorage)) {
- totalBankMoneyGained += profit;
- } else {
- totalMoneyGained += profit;
- }
- moneyLogged += profit;
- voidStorage.getStats().setItemsSold(voidStorage.getStats().getItemsSold() + itemAmount);
- voidStorage.getStats().setMoney(voidStorage.getStats().getMoney() + profit);
- itemEntity.remove();
- if (!shouldUpdateMenu)
- shouldUpdateMenu = true;
- } else {
- if (!opt.getBoolean("chunk-collector.transfer-non-sellables", true)) {
- continue;
- }
- /* Dropped items, every single one of them together. */
- final ItemStack[] toTransferItems = Utils.getItems(stack, itemAmount);
- /*
- * Attempt to add all items in chest. Whatever is left, it means the dropped
- * item needs to be updated.
- */
- final Map<Integer, ItemStack> leftOvers = inv.addItem(toTransferItems);
- /* No leftovers, means that all items were added, clear the entity. */
- if (leftOvers.isEmpty()) {
- itemEntity.remove();
- continue;
- }
- /* Let's count how many items are left. */
- int countLeft = 0;
- for (final ItemStack loopLeft : leftOvers.values()) {
- final int leftAmount = loopLeft.getAmount();
- countLeft += leftAmount;
- }
- /* No room at all, oh well, let's continue. */
- if (countLeft == itemAmount) {
- continue;
- }
- /* Update the actual item accordingly. */
- StackerManager.getInstance().getStacker().updateItem(itemEntity, countLeft);
- }
- }
- }
- if (shouldUpdateMenu) {
- voidStorage.getInventoryHandler().updateMenuItems(true);
- if (!shouldUpdateData) {
- shouldUpdateData = true;
- }
- }
- if (opt.getBoolean("Mechanics.money-logging.enabled", false) && moneyLogged > opt.getDouble("Mechanics.money-logging.minimum-amount", 0)) {
- final Map<String, String> replace = Maps.newHashMap();
- replace.put("%location_world%", loc.getWorld().getName());
- replace.put("%location_x%", String.valueOf(loc.getBlockX()));
- replace.put("%location_y%", String.valueOf(loc.getBlockY()));
- replace.put("%location_z%", String.valueOf(loc.getBlockZ()));
- replace.put("%player%", voidStorage.getLeaderName());
- replace.put("%amount%", Utils.formatNumber(moneyLogged));
- MessagesUtil.VOIDCHEST_MONEY_LOGGING.msg(Bukkit.getConsoleSender(), replace, false);
- }
- }
- if (!proceed) {
- return;
- }
- final PlayerDataSellStats dataSell = data.getPlayerDataSellStats();
- dataSell.addItemsPurgedStored(totalItemsPurged);
- dataSell.addItemsSoldStored(totalItemsSold);
- dataSell.addMoneyStored(totalMoneyGained + totalBankMoneyGained);
- if (totalItemsSold <= 0 && totalItemsPurged <= 0) {
- return;
- }
- if (totalItemsSold >= 1) {
- if (!BankManager.getInstance().getBank().depositToBank(totalBankMoneyGained, data)) {
- totalMoneyGained += totalBankMoneyGained;
- final IEconomy econ = EconomyManager.getInstance().getEconomy();
- if (econ != null && !econ.deposit(off, totalMoneyGained)) {
- Utils.debug(true, "Something went wrong with the payment of: " + off.getName() + " with uuid:"
- + off.getUniqueId() + " for an amount of:" + totalMoneyGained);
- }
- }
- }
- if (shouldUpdateData) {
- data.loadStatsToFile(false);
- }
- // this.informUser(data, totalMoneyGained, totalItemsSold, totalItemsPurged,
- // isOnline);
- }
- @Override
- public void initiateSell(final PlayerData data) {
- this.initiateSell(data, null);
- }
- @Override
- public void initiateSell() {
- this.initiateSell((String) null);
- }
- @Override
- public String getName() {
- final String simpleName = this.getClass().getSimpleName();
- return simpleName + "{name=" + desc.getName() + ", version=" + desc.getVersion() + '}';
- }
- @Override
- public boolean isVaultDependent() {
- return true;
- }
- }
Add Comment
Please, Sign In to add comment