2015-08-21 15:10:08 +02:00
|
|
|
/**
|
|
|
|
* Jobs Plugin for Bukkit
|
|
|
|
* Copyright (C) 2011 Zak Ford <zak.j.ford@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.gamingmesh.jobs.economy;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.OfflinePlayer;
|
|
|
|
import com.gamingmesh.jobs.Jobs;
|
|
|
|
import com.gamingmesh.jobs.api.JobsPaymentEvent;
|
|
|
|
import com.gamingmesh.jobs.container.JobsPlayer;
|
|
|
|
import com.gamingmesh.jobs.stuff.ChatColor;
|
|
|
|
import com.gamingmesh.jobs.tasks.BufferedPaymentTask;
|
|
|
|
|
|
|
|
public class BufferedEconomy {
|
2016-08-08 16:36:56 +02:00
|
|
|
private Jobs plugin;
|
2015-09-10 12:41:19 +02:00
|
|
|
private Economy economy;
|
|
|
|
private LinkedBlockingQueue<BufferedPayment> payments = new LinkedBlockingQueue<BufferedPayment>();
|
|
|
|
private final Map<UUID, BufferedPayment> paymentCache = Collections.synchronizedMap(new HashMap<UUID, BufferedPayment>());
|
|
|
|
|
|
|
|
private OfflinePlayer ServerAccount = null;
|
|
|
|
private OfflinePlayer ServerTaxesAccount = null;
|
|
|
|
|
|
|
|
PaymentData PaymentData = new PaymentData();
|
|
|
|
|
2016-08-08 16:36:56 +02:00
|
|
|
public BufferedEconomy(Jobs plugin, Economy economy) {
|
2015-09-10 12:41:19 +02:00
|
|
|
this.plugin = plugin;
|
|
|
|
this.economy = economy;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add payment to player's payment buffer
|
|
|
|
* @param player - player to be paid
|
|
|
|
* @param amount - amount to be paid
|
|
|
|
*/
|
2016-03-30 15:42:36 +02:00
|
|
|
public void pay(JobsPlayer player, double amount, double points, double exp) {
|
|
|
|
pay(new BufferedPayment(player.getPlayer(), amount, points, exp));
|
2015-09-10 12:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add payment to player's payment buffer
|
|
|
|
* @param payment - payment to be paid
|
|
|
|
*/
|
|
|
|
public void pay(BufferedPayment payment) {
|
|
|
|
payments.add(payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String format(double money) {
|
|
|
|
return economy.format(money);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Payout all players the amount they are going to be paid
|
|
|
|
*/
|
2016-03-30 15:42:36 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
2015-09-10 12:41:19 +02:00
|
|
|
public void payAll() {
|
|
|
|
if (payments.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
synchronized (paymentCache) {
|
|
|
|
|
|
|
|
Double TotalAmount = 0.0;
|
2016-03-30 15:42:36 +02:00
|
|
|
Double TotalPoints = 0.0;
|
2015-09-10 12:41:19 +02:00
|
|
|
Double TaxesAmount = 0.0;
|
2016-03-30 15:42:36 +02:00
|
|
|
Double TaxesPoints = 0.0;
|
2015-09-10 12:41:19 +02:00
|
|
|
|
|
|
|
// combine all payments using paymentCache
|
|
|
|
while (!payments.isEmpty()) {
|
|
|
|
BufferedPayment payment = payments.remove();
|
|
|
|
TotalAmount += payment.getAmount();
|
2016-03-30 15:42:36 +02:00
|
|
|
TotalPoints += payment.getPoints();
|
2015-09-10 12:41:19 +02:00
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
if (Jobs.getGCManager().UseTaxes) {
|
|
|
|
TaxesAmount += payment.getAmount() * (Jobs.getGCManager().TaxesAmount / 100.0);
|
|
|
|
TaxesPoints += payment.getPoints() * (Jobs.getGCManager().TaxesAmount / 100.0);
|
2015-09-10 12:41:19 +02:00
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2016-06-25 09:13:06 +02:00
|
|
|
if (payment.getOfflinePlayer() == null)
|
|
|
|
continue;
|
|
|
|
|
2015-09-10 12:41:19 +02:00
|
|
|
UUID uuid = payment.getOfflinePlayer().getUniqueId();
|
|
|
|
if (paymentCache.containsKey(uuid)) {
|
|
|
|
BufferedPayment existing = paymentCache.get(uuid);
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2015-09-10 12:41:19 +02:00
|
|
|
double money = payment.getAmount();
|
2016-03-30 15:42:36 +02:00
|
|
|
double points = payment.getPoints();
|
2015-09-10 12:41:19 +02:00
|
|
|
double exp = payment.getExp();
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
if (Jobs.getGCManager().TakeFromPlayersPayment && Jobs.getGCManager().UseTaxes) {
|
|
|
|
money = money - (money * (Jobs.getGCManager().TaxesAmount / 100.0));
|
|
|
|
points = points - (points * (Jobs.getGCManager().TaxesAmount / 100.0));
|
2015-09-10 12:41:19 +02:00
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2015-09-10 12:41:19 +02:00
|
|
|
existing.setAmount(existing.getAmount() + money);
|
2016-03-30 15:42:36 +02:00
|
|
|
existing.setPoints(existing.getPoints() + points);
|
2015-09-10 12:41:19 +02:00
|
|
|
existing.setExp(existing.getExp() + exp);
|
|
|
|
} else {
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2015-09-10 12:41:19 +02:00
|
|
|
double money = payment.getAmount();
|
2016-03-30 15:42:36 +02:00
|
|
|
double points = payment.getPoints();
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
if (Jobs.getGCManager().TakeFromPlayersPayment && Jobs.getGCManager().UseTaxes) {
|
|
|
|
payment.setAmount(money - (money * (Jobs.getGCManager().TaxesAmount / 100.0)));
|
|
|
|
payment.setPoints(points - (points * (Jobs.getGCManager().TaxesAmount / 100.0)));
|
2015-09-10 12:41:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
paymentCache.put(uuid, payment);
|
|
|
|
}
|
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2015-09-10 12:41:19 +02:00
|
|
|
boolean hasMoney = false;
|
2016-03-30 15:42:36 +02:00
|
|
|
String ServerAccountname = Jobs.getGCManager().ServerAcountName;
|
|
|
|
String ServerTaxesAccountname = Jobs.getGCManager().ServertaxesAcountName;
|
2015-09-10 12:41:19 +02:00
|
|
|
if (this.ServerAccount == null)
|
2016-03-30 15:42:36 +02:00
|
|
|
this.ServerAccount = Bukkit.getOfflinePlayer(ServerAccountname);
|
2015-09-10 12:41:19 +02:00
|
|
|
|
|
|
|
if (this.ServerTaxesAccount == null)
|
2016-06-09 17:35:24 +02:00
|
|
|
this.ServerTaxesAccount = Bukkit.getOfflinePlayer(ServerTaxesAccountname);
|
2015-09-10 12:41:19 +02:00
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
if (Jobs.getGCManager().UseTaxes && Jobs.getGCManager().TransferToServerAccount && ServerTaxesAccount != null) {
|
2015-09-10 12:41:19 +02:00
|
|
|
economy.depositPlayer(ServerTaxesAccount, TaxesAmount);
|
|
|
|
|
|
|
|
if (ServerTaxesAccount.isOnline()) {
|
2016-03-30 15:42:36 +02:00
|
|
|
if (!Jobs.getActionbarToggleList().containsKey(ServerTaxesAccountname) && Jobs.getGCManager().ActionBarsMessageByDefault)
|
|
|
|
Jobs.getActionbarToggleList().put(ServerTaxesAccountname, true);
|
|
|
|
if (Jobs.getActionbarToggleList().containsKey(ServerTaxesAccountname) && Jobs.getActionbarToggleList().get(ServerTaxesAccountname)) {
|
|
|
|
Jobs.getActionBar().send(Bukkit.getPlayer(ServerAccountname), Jobs.getLanguage().getMessage("message.taxes", "[amount]", (int) (TotalAmount * 100)
|
|
|
|
/ 100.0));
|
2015-09-10 12:41:19 +02:00
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
}
|
2015-09-10 12:41:19 +02:00
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
if (Jobs.getGCManager().UseServerAccount) {
|
2015-09-10 12:41:19 +02:00
|
|
|
if (economy.hasMoney(ServerAccountname, TotalAmount)) {
|
|
|
|
hasMoney = true;
|
|
|
|
economy.withdrawPlayer(ServerAccountname, TotalAmount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule all payments
|
|
|
|
int i = 0;
|
|
|
|
for (BufferedPayment payment : paymentCache.values()) {
|
|
|
|
i++;
|
|
|
|
|
2016-06-25 09:13:06 +02:00
|
|
|
if (payment.getOfflinePlayer() == null)
|
|
|
|
continue;
|
2016-06-25 15:27:01 +02:00
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
// JobsPayment event
|
|
|
|
JobsPaymentEvent JobsPaymentEvent = new JobsPaymentEvent(payment.getOfflinePlayer(), payment.getAmount(), payment.getPoints());
|
2015-09-10 12:41:19 +02:00
|
|
|
Bukkit.getServer().getPluginManager().callEvent(JobsPaymentEvent);
|
|
|
|
// If event is canceled, dont do anything
|
|
|
|
if (JobsPaymentEvent.isCancelled())
|
|
|
|
continue;
|
2016-06-09 17:35:24 +02:00
|
|
|
|
2016-06-12 14:42:33 +02:00
|
|
|
payment.setAmount(JobsPaymentEvent.getAmount());
|
|
|
|
payment.setPoints(JobsPaymentEvent.getPoints());
|
2015-09-10 12:41:19 +02:00
|
|
|
|
2016-03-30 15:42:36 +02:00
|
|
|
if (Jobs.getGCManager().UseServerAccount) {
|
|
|
|
if (!hasMoney) {
|
|
|
|
Jobs.getActionBar().send(payment.getOfflinePlayer().getPlayer(), ChatColor.RED + Jobs.getLanguage().getMessage("economy.error.nomoney"));
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-25 15:27:01 +02:00
|
|
|
if (Jobs.getGCManager().isEconomyAsync())
|
|
|
|
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, new BufferedPaymentTask(this, economy, payment), i);
|
|
|
|
else
|
|
|
|
Bukkit.getScheduler().runTaskLater(plugin, new BufferedPaymentTask(this, economy, payment), i);
|
2016-03-30 15:42:36 +02:00
|
|
|
} else {
|
|
|
|
if (Jobs.getGCManager().isEconomyAsync())
|
2015-09-10 12:41:19 +02:00
|
|
|
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, new BufferedPaymentTask(this, economy, payment), i);
|
2016-03-30 15:42:36 +02:00
|
|
|
else
|
2015-09-10 12:41:19 +02:00
|
|
|
Bukkit.getScheduler().runTaskLater(plugin, new BufferedPaymentTask(this, economy, payment), i);
|
2016-03-30 15:42:36 +02:00
|
|
|
}
|
2016-04-03 16:22:44 +02:00
|
|
|
try {
|
|
|
|
// Action bar stuff
|
|
|
|
Jobs.getActionBar().ShowActionBar(payment);
|
|
|
|
if (payment.getOfflinePlayer().isOnline()) {
|
|
|
|
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(payment.getOfflinePlayer().getName());
|
|
|
|
Jobs.getBBManager().ShowJobProgression(jPlayer);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2015-09-10 12:41:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// empty payment cache
|
|
|
|
paymentCache.clear();
|
2015-08-21 15:10:08 +02:00
|
|
|
}
|
2015-09-10 12:41:19 +02:00
|
|
|
|
|
|
|
}
|
2015-08-21 15:10:08 +02:00
|
|
|
}
|