From fae5d65a88170b9f5dc97e4052942f7dcdd8f161 Mon Sep 17 00:00:00 2001 From: Zrips Date: Thu, 19 Sep 2019 14:01:24 +0300 Subject: [PATCH] Lets upgrade BufferedPayment --- src/main/java/com/gamingmesh/jobs/Jobs.java | 44 +++++++----- .../jobs/economy/BufferedEconomy.java | 64 +++++++++-------- .../jobs/economy/BufferedPayment.java | 69 ++++++++++++++----- .../jobs/tasks/BufferedPaymentTask.java | 11 +-- 4 files changed, 122 insertions(+), 66 deletions(-) diff --git a/src/main/java/com/gamingmesh/jobs/Jobs.java b/src/main/java/com/gamingmesh/jobs/Jobs.java index 57b88bab..4b263cc2 100644 --- a/src/main/java/com/gamingmesh/jobs/Jobs.java +++ b/src/main/java/com/gamingmesh/jobs/Jobs.java @@ -1023,7 +1023,7 @@ public class Jobs extends JavaPlugin { Boost boost = getPlayerManager().getFinalBonus(jPlayer, noneJob); JobsPrePaymentEvent JobsPrePaymentEvent = new JobsPrePaymentEvent(jPlayer.getPlayer(), noneJob, income, - pointAmount, block, ent, victim, info); + pointAmount, block, ent, victim, info); Bukkit.getServer().getPluginManager().callEvent(JobsPrePaymentEvent); // If event is canceled, don't do anything if (JobsPrePaymentEvent.isCancelled()) { @@ -1082,7 +1082,13 @@ public class Jobs extends JavaPlugin { if (pointAmount != 0D) jPlayer.setSaved(false); - economy.pay(jPlayer, income, pointAmount, 0.0); + HashMap payments = new HashMap(); + if (income != 0D) + payments.put(CurrencyType.MONEY, income); + if (pointAmount != 0D) + payments.put(CurrencyType.POINTS, pointAmount); + + economy.pay(jPlayer, payments); if (GconfigManager.LoggingUse) { HashMap amounts = new HashMap<>(); @@ -1226,10 +1232,18 @@ public class Jobs extends JavaPlugin { consoleMsg("&c[Jobs] Some issues with boss bar feature accured, try disabling it to avoid it."); } - FastPayment.put(jPlayer.getPlayerUUID(), new FastPayment(jPlayer, info, new BufferedPayment(jPlayer.getPlayer(), income, pointAmount, expAmount), prog + HashMap payments = new HashMap(); + if (income != 0D) + payments.put(CurrencyType.MONEY, income); + if (pointAmount != 0D) + payments.put(CurrencyType.POINTS, pointAmount); + if (expAmount != 0D) + payments.put(CurrencyType.EXP, expAmount); + + FastPayment.put(jPlayer.getPlayerUUID(), new FastPayment(jPlayer, info, new BufferedPayment(jPlayer.getPlayer(), payments), prog .getJob())); - economy.pay(jPlayer, income, pointAmount, expAmount); + economy.pay(jPlayer, payments); int oldLevel = prog.getLevel(); if (GconfigManager.LoggingUse) { @@ -1365,34 +1379,28 @@ public class Jobs extends JavaPlugin { public static void perform(JobsPlayer jPlayer, ActionInfo info, BufferedPayment payment, Job job) { // JobsPayment event - JobsExpGainEvent JobsExpGainEvent = new JobsExpGainEvent(payment.getOfflinePlayer(), job, payment.getExp()); + JobsExpGainEvent JobsExpGainEvent = new JobsExpGainEvent(payment.getOfflinePlayer(), job, payment.get(CurrencyType.EXP)); Bukkit.getServer().getPluginManager().callEvent(JobsExpGainEvent); // If event is canceled, don't do anything if (JobsExpGainEvent.isCancelled()) return; - if (!jPlayer.isUnderLimit(CurrencyType.MONEY, payment.getAmount())) - return; - if (!jPlayer.isUnderLimit(CurrencyType.EXP, payment.getExp())) - return; - if (!jPlayer.isUnderLimit(CurrencyType.POINTS, payment.getPoints())) - return; + for (CurrencyType one : CurrencyType.values()) { + if (!jPlayer.isUnderLimit(one, payment.get(one))) + return; + } - economy.pay(jPlayer, payment.getAmount(), payment.getPoints(), payment.getExp()); + economy.pay(jPlayer, payment.getPayment()); JobProgression prog = jPlayer.getJobProgression(job); int oldLevel = prog.getLevel(); if (GconfigManager.LoggingUse) { - HashMap amounts = new HashMap<>(); - amounts.put(CurrencyType.MONEY, payment.getAmount()); - amounts.put(CurrencyType.EXP, payment.getExp()); - amounts.put(CurrencyType.POINTS, payment.getPoints()); - loging.recordToLog(jPlayer, info, amounts); + loging.recordToLog(jPlayer, info, payment.getPayment()); } - if (prog.addExperience(payment.getExp())) + if (prog.addExperience(payment.get(CurrencyType.EXP))) getPlayerManager().performLevelUp(jPlayer, prog.getJob(), oldLevel); } diff --git a/src/main/java/com/gamingmesh/jobs/economy/BufferedEconomy.java b/src/main/java/com/gamingmesh/jobs/economy/BufferedEconomy.java index f120d521..69544eb6 100644 --- a/src/main/java/com/gamingmesh/jobs/economy/BufferedEconomy.java +++ b/src/main/java/com/gamingmesh/jobs/economy/BufferedEconomy.java @@ -21,6 +21,7 @@ package com.gamingmesh.jobs.economy; import com.gamingmesh.jobs.CMILib.VersionChecker.Version; import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.api.JobsPaymentEvent; +import com.gamingmesh.jobs.container.CurrencyType; import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.stuff.ToggleBarHandling; import com.gamingmesh.jobs.tasks.BufferedPaymentTask; @@ -53,10 +54,20 @@ public class BufferedEconomy { * @param player - player to be paid * @param amount - amount to be paid */ - public void pay(JobsPlayer player, double amount, double points, double exp) { - pay(new BufferedPayment(player.getPlayer(), amount, points, exp)); + public void pay(JobsPlayer player, HashMap payments) { + pay(new BufferedPayment(player.getPlayer(), payments)); } +// /** +// * Add payment to player's payment buffer +// * @param player - player to be paid +// * @param amount - amount to be paid +// */ +// @Deprecated +// public void pay(JobsPlayer player, double amount, double points, double exp) { +// pay(new BufferedPayment(player.getPlayer(), amount, points, exp)); +// } + /** * Add payment to player's payment buffer * @param payment - payment to be paid @@ -89,12 +100,12 @@ public class BufferedEconomy { // combine all payments using paymentCache while (!payments.isEmpty()) { BufferedPayment payment = payments.remove(); - TotalAmount += payment.getAmount(); - TotalPoints += payment.getPoints(); + TotalAmount += payment.get(CurrencyType.MONEY); + TotalPoints += payment.get(CurrencyType.POINTS); if (Jobs.getGCManager().UseTaxes) { - TaxesAmount += payment.getAmount() * (Jobs.getGCManager().TaxesAmount / 100.0); - TaxesPoints += payment.getPoints() * (Jobs.getGCManager().TaxesAmount / 100.0); + TaxesAmount += payment.get(CurrencyType.MONEY) * (Jobs.getGCManager().TaxesAmount / 100.0); + TaxesPoints += payment.get(CurrencyType.POINTS) * (Jobs.getGCManager().TaxesAmount / 100.0); } if (payment.getOfflinePlayer() == null) @@ -104,26 +115,26 @@ public class BufferedEconomy { if (paymentCache.containsKey(uuid)) { BufferedPayment existing = paymentCache.get(uuid); - double money = payment.getAmount(); - double points = payment.getPoints(); - double exp = payment.getExp(); + double money = payment.get(CurrencyType.MONEY); + double points = payment.get(CurrencyType.POINTS); + double exp = payment.get(CurrencyType.EXP); if (Jobs.getGCManager().TakeFromPlayersPayment && Jobs.getGCManager().UseTaxes) { money = money - (money * (Jobs.getGCManager().TaxesAmount / 100.0)); points = points - (points * (Jobs.getGCManager().TaxesAmount / 100.0)); } - existing.setAmount(existing.getAmount() + money); - existing.setPoints(existing.getPoints() + points); - existing.setExp(existing.getExp() + exp); + existing.set(CurrencyType.MONEY, existing.get(CurrencyType.MONEY) + money); + existing.set(CurrencyType.POINTS, existing.get(CurrencyType.POINTS) + points); + existing.set(CurrencyType.EXP, existing.get(CurrencyType.EXP) + exp); } else { - double money = payment.getAmount(); - double points = payment.getPoints(); + double money = payment.get(CurrencyType.MONEY); + double points = payment.get(CurrencyType.POINTS); 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))); + payment.set(CurrencyType.MONEY, money - (money * (Jobs.getGCManager().TaxesAmount / 100.0))); + payment.set(CurrencyType.POINTS, points - (points * (Jobs.getGCManager().TaxesAmount / 100.0))); } paymentCache.put(uuid, payment); @@ -168,14 +179,14 @@ public class BufferedEconomy { continue; // JobsPayment event - JobsPaymentEvent JobsPaymentEvent = new JobsPaymentEvent(payment.getOfflinePlayer(), payment.getAmount(), payment.getPoints()); + JobsPaymentEvent JobsPaymentEvent = new JobsPaymentEvent(payment.getOfflinePlayer(), payment.get(CurrencyType.MONEY), payment.get(CurrencyType.POINTS)); Bukkit.getServer().getPluginManager().callEvent(JobsPaymentEvent); // If event is canceled, dont do anything if (JobsPaymentEvent.isCancelled()) continue; - payment.setAmount(JobsPaymentEvent.getAmount()); - payment.setPoints(JobsPaymentEvent.getPoints()); + payment.set(CurrencyType.MONEY, JobsPaymentEvent.getAmount()); + payment.set(CurrencyType.POINTS, JobsPaymentEvent.getPoints()); if (Jobs.getGCManager().UseServerAccount) { if (!hasMoney) { @@ -220,25 +231,24 @@ public class BufferedEconomy { if (!ToggleBarHandling.getActionBarToggle().containsKey(playername)) return; - if (payment.getAmount() == 0.0D && payment.getPoints() == 0.0D && payment.getExp() == 0.0D) + if (!payment.containsPayment()) return; Boolean show = ToggleBarHandling.getActionBarToggle().get(playername); Player abp = Bukkit.getPlayer(payment.getOfflinePlayer().getUniqueId()); if ((abp != null) && (show.booleanValue())) { String Message = Jobs.getLanguage().getMessage("command.toggle.output.paid.main"); - if (payment.getAmount() != 0.0D) { + if (payment.get(CurrencyType.MONEY) != 0D) { Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.money", new Object[] { "[amount]", String.format(Jobs.getGCManager().getDecimalPlacesMoney(), - new Object[] { Double.valueOf(payment - .getAmount()) }) }); + new Object[] { Double.valueOf(payment.get(CurrencyType.MONEY)) }) }); } - if (payment.getPoints() != 0.0D) { + if (payment.get(CurrencyType.POINTS) != 0D) { Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.points", new Object[] { "[points]", String.format(Jobs.getGCManager().getDecimalPlacesPoints(), - new Object[] { Double.valueOf(payment.getPoints()) }) }); + new Object[] { Double.valueOf(payment.get(CurrencyType.POINTS)) }) }); } - if (payment.getExp() != 0.0D) { + if (payment.get(CurrencyType.EXP) != 0D) { Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.exp", new Object[] { "[exp]", String.format(Jobs.getGCManager().getDecimalPlacesExp(), new Object[] { - Double.valueOf(payment.getExp()) }) }); + Double.valueOf(payment.get(CurrencyType.EXP)) }) }); } Jobs.getActionBar().send(abp, Message); } diff --git a/src/main/java/com/gamingmesh/jobs/economy/BufferedPayment.java b/src/main/java/com/gamingmesh/jobs/economy/BufferedPayment.java index 9a314e7a..bdcf9034 100644 --- a/src/main/java/com/gamingmesh/jobs/economy/BufferedPayment.java +++ b/src/main/java/com/gamingmesh/jobs/economy/BufferedPayment.java @@ -18,46 +18,83 @@ package com.gamingmesh.jobs.economy; +import java.util.HashMap; +import java.util.Map.Entry; + import org.bukkit.OfflinePlayer; +import com.gamingmesh.jobs.container.CurrencyType; + public class BufferedPayment { private OfflinePlayer offlinePlayer; - private double amount = 0.0; - private double points = 0.0; - private double exp = 0.0; + private HashMap payments = new HashMap(); + @Deprecated public BufferedPayment(OfflinePlayer offlinePlayer, double amount, double points, double exp) { this.offlinePlayer = offlinePlayer; - this.amount = amount; - this.points = points; - this.exp = exp; + this.payments.put(CurrencyType.MONEY, amount); + this.payments.put(CurrencyType.EXP, exp); + this.payments.put(CurrencyType.POINTS, points); + } + + public BufferedPayment(OfflinePlayer offlinePlayer, HashMap payments) { + this.offlinePlayer = offlinePlayer; + // This can contain only one value instead of all posible ones + this.payments.putAll(payments); } public OfflinePlayer getOfflinePlayer() { return offlinePlayer; } - public double getAmount() { - return amount; + @Deprecated + public Double getAmount() { + return this.payments.get(CurrencyType.MONEY); } - + + @Deprecated public double getPoints() { - return points; + return this.payments.get(CurrencyType.POINTS); } + @Deprecated public double getExp() { - return exp; + return this.payments.get(CurrencyType.EXP); } + @Deprecated public void setAmount(double amount) { - this.amount = amount; + this.payments.put(CurrencyType.MONEY, amount); } - + + @Deprecated public void setPoints(double points) { - this.points = points; + this.payments.put(CurrencyType.POINTS, points); } - + + @Deprecated public void setExp(double exp) { - this.exp = exp; + this.payments.put(CurrencyType.EXP, exp); + } + + public Double get(CurrencyType type) { + Double amount = this.payments.get(type); + return amount == null ? 0 : amount; + } + + public Double set(CurrencyType type, double amount) { + return this.payments.put(type, amount); + } + + public boolean containsPayment() { + for (Entry one : payments.entrySet()) { + if (one.getValue() != 0D) + return true; + } + return false; + } + + public HashMap getPayment() { + return payments; } } diff --git a/src/main/java/com/gamingmesh/jobs/tasks/BufferedPaymentTask.java b/src/main/java/com/gamingmesh/jobs/tasks/BufferedPaymentTask.java index 062bebe3..d5b38fa6 100644 --- a/src/main/java/com/gamingmesh/jobs/tasks/BufferedPaymentTask.java +++ b/src/main/java/com/gamingmesh/jobs/tasks/BufferedPaymentTask.java @@ -21,6 +21,7 @@ package com.gamingmesh.jobs.tasks; import org.bukkit.Bukkit; import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.container.CurrencyType; import com.gamingmesh.jobs.economy.BufferedEconomy; import com.gamingmesh.jobs.economy.BufferedPayment; import com.gamingmesh.jobs.economy.Economy; @@ -38,14 +39,14 @@ public class BufferedPaymentTask implements Runnable { @Override public void run() { - if (payment.getAmount() > 0) - Bukkit.getScheduler().runTask(Jobs.getInstance(), () -> economy.depositPlayer(payment.getOfflinePlayer(), payment.getAmount())); + if (payment.get(CurrencyType.MONEY) > 0) + Bukkit.getScheduler().runTask(Jobs.getInstance(), () -> economy.depositPlayer(payment.getOfflinePlayer(), payment.get(CurrencyType.MONEY))); else { - if (!economy.withdrawPlayer(payment.getOfflinePlayer(), -payment.getAmount())) + if (!economy.withdrawPlayer(payment.getOfflinePlayer(), -payment.get(CurrencyType.MONEY))) bufferedEconomy.pay(payment); } - if (payment.getPoints() != 0D) - Jobs.getPointsData().addPoints(payment.getOfflinePlayer().getUniqueId(), payment.getPoints()); + if (payment.get(CurrencyType.POINTS) != 0D) + Jobs.getPointsData().addPoints(payment.getOfflinePlayer().getUniqueId(), payment.get(CurrencyType.POINTS)); } } \ No newline at end of file